-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrun.sh
executable file
·115 lines (104 loc) · 3.24 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
usage() {
echo "Usage: $0 [ -r RAILS_ENV_FILE ] [-i (NO ARG, INTERACTIVE SESSION)] [-p (NO ARG, SET PLATFORM TO AMD64 FOR M1/M2 CHIPS)] [-d (NO ARG, DEVELOP MODE)]" 1>&2
echo "Example: $0 -r PATH_TO/RAILS_ENV_FILE"
}
exit_abnormal() {
usage
exit 1
}
# If there is a running container, bash into it.
# The output of this docker command is:
# CONTAINER_ID container-discovery:TAG
# We only need the first information to bash into.
running_test=$(docker inspect --format="{{.Id}} {{.Config.Image}}" $(docker ps -q) | grep container-discovery:)
for term in $running_test
do
docker exec -it "$term" bash
exit
done
# https://www.baeldung.com/linux/bash-expand-relative-path
resolve_relative_path() (
# If the path is a directory, we just need to 'cd' into it and print the new path.
if [ -d "$1" ]; then
cd "$1" || return 1
pwd
# If the path points to anything else, like a file or FIFO
elif [ -e "$1" ]; then
# Strip '/file' from '/dir/file'
# We only change the directory if the name doesn't match for the cases where
# we were passed something like 'file' without './'
if [ ! "${1%/*}" = "$1" ]; then
cd "${1%/*}" || return 1
fi
# Strip all leading slashes upto the filename
echo "$(pwd)/${1##*/}"
else
return 1 # Failure, neither file nor directory exists.
fi
)
aws_creds=""
compose="docker-compose.yaml"
rails_env="production"
rails_env_file=""
image_name="container-discovery"
interactive=""
platform=""
target="container-discovery"
port="9292:9292"
feature=""
development=""
run_cmd="up"
# run_cron="false"
# while getopts "cdhipt:a:r:" options; do
while getopts "dhipt:a:m:r:" options; do
case "${options}" in
a) aws_creds=$(resolve_relative_path "${OPTARG}") ;; # -a aws_creds
# aws_creds="-v ${abs_path}:/custom_mnt/credentials:ro" ;;
# c) run_cron="true" ;; # -c cron
d) rails_env="development"
development="-f docker-compose-development.yaml" ;;
m) image_name="${OPTARG}" ;; # -m image_name
r) rails_env_file=$(resolve_relative_path "${OPTARG}") ;;
i) run_cmd="run --entrypoint=bash webapp" ;;
p) platform="--platform=linux/amd64" ;;
h) usage
exit 0 ;;
*) exit_abnormal ;;
esac
done
# if [ "${aws_creds}" == "" ] && [ "${rails_env}" == "" ]
if [ "${aws_creds}" == "" ] && [ "${rails_env_file}" == "" ]
then
echo "-r flag is required."
exit_abnormal
fi
if [ "${aws_creds}" != "" ]
then
export AWS_CREDENTIALS=${aws_creds}
compose="docker-compose-cred.yaml"
if [ "${development}" != "" ]
then
# currently not supported
development=""
fi
fi
export IMAGE_NAME=${image_name}
export RAILS_ENV=${rails_env}
export RAILS_ENV_FILE=${rails_env_file}
# if [ "${run_cron}" == "true" ]
# then
# export RUN_CRON="true"
# fi
# img_id=$(git rev-parse head)
# echo "Running ${target}:${img_id} ${feature}"
echo "docker compose -f ${compose} down --remove-orphans"
docker compose -f ${compose} down --remove-orphans
# docker system prune -f
echo "docker compose -f ${compose} ${development} ${run_cmd}"
docker compose -f ${compose} ${development} ${run_cmd}
unset AWS_CREDENTIALS
unset IMAGE_NAME
unset RAILS_ENV
unset RAILS_ENV_FILE
# unset RUN_CRON