-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·131 lines (118 loc) · 3.59 KB
/
run.sh
File metadata and controls
executable file
·131 lines (118 loc) · 3.59 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/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
}
# 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"
development=""
feature=""
image_name="container-discovery"
integration=0
interactive=""
platform=""
port="9292:9292"
rails_env="production"
rails_env_file=""
run_cmd="up"
run_cron="false"
test_app_version=""
# while getopts "cdhipt:a:r:" options; do
while getopts "cdhitp:a:e:m:r:v:" options; do
case "${options}" in
a) aws_creds=$(resolve_relative_path "${OPTARG}") ;; # -a aws_creds
c) run_cron="true" ;;
d) rails_env="development"
development="-f docker-compose-development.yaml" ;;
e) rails_env="${OPTARG}" ;; # -e rails_env
h) usage
exit 0 ;;
i) run_cmd="run --entrypoint=bash webapp" ;;
m) image_name="${OPTARG}" ;; # -m image_name
p) platform="--platform=linux/amd64" ;;
r) rails_env_file=$(resolve_relative_path "${OPTARG}") ;;
t) integration=1
rails_env="integration" ;;
v) test_app_version="${OPTARG}" ;;
*) exit_abnormal ;;
esac
done
if [ "${integration}" == 1 ] && [ "${image_name}" == "container-discovery" ]
then
image_name="container-discovery-int"
fi
# 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 ${image_name}:)
for term in $running_test
do
docker exec -it "$term" bash
exit
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
if [ "${test_app_version}" != "" ]
then
echo "APP VERSION for testing: ${test_app_version}"
export TEST_APP_VERSION=${test_app_version}
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
unset TEST_APP_VERSION