-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
executable file
·211 lines (183 loc) · 3.97 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# v0.2.0
APP_NAME='mealpedant'
RED='\033[0;31m'
YELLOW='\033[0;33m'
RESET='\033[0m'
error_close() {
echo -e "\n${RED}ERROR - EXITED: ${YELLOW}$1${RESET}\n"
exit 1
}
# $1 any variable name
# $2 variable name
check_variable() {
if [ -z "$1" ]; then
error_close "Missing variable $2"
fi
}
check_variable "$APP_NAME" "\$APP_NAME"
if ! [ -x "$(command -v dialog)" ]; then
error_close "dialog is not installed"
fi
set_base_dir() {
local workspace="/workspaces"
if [[ -d "$workspace" ]]; then
BASE_DIR="${workspace}"
else
BASE_DIR=$HOME
fi
}
set_base_dir
APP_DIR="${BASE_DIR}/${APP_NAME}_api.d"
DOCKER_DIR="${APP_DIR}/docker"
# Options
PRO=production
DEV=dev
# Containers
SERVER_API="${APP_NAME}_api"
BASE_CONTAINERS=("${APP_NAME}_postgres" "${APP_NAME}_redis")
ALL=("${BASE_CONTAINERS[@]}" "${SERVER_API}")
TO_RUN=("${BASE_CONTAINERS[@]}")
make_db_data() {
cd "${BASE_DIR}" || error_close "${BASE_DIR} doesn't exist"
local pg_data="${BASE_DIR}/databases/${APP_NAME}/pg_data"
local redis_data="${BASE_DIR}/databases/${APP_NAME}/redis_data"
for DIRECTORY in $pg_data $redis_data; do
if [[ ! -d "$DIRECTORY" ]]; then
mkdir -p "$DIRECTORY"
fi
done
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
}
make_logs_directories() {
cd "${BASE_DIR}" || error_close "${BASE_DIR} doesn't exist"
local logs_dir="${BASE_DIR}/logs/${APP_NAME}"
if [[ ! -d "$logs_dir" ]]; then
mkdir -p "$logs_dir"
fi
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
}
make_all_directories() {
make_db_data
make_logs_directories
}
dev_up() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
echo "starting containers: ${TO_RUN[*]}"
docker compose -f dev.docker-compose.yml up --force-recreate --build -d "${TO_RUN[@]}"
}
dev_down() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose -f dev.docker-compose.yml down
}
production_up() {
make_all_directories
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose up -d
}
production_down() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose -f docker-compose.yml down
}
production_rebuild() {
make_all_directories
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose up -d --build
}
select_containers() {
cmd=(dialog --separate-output --backtitle "Dev containers selection" --checklist "select: postgres + redis +" 14 80 16)
options=(
1 "${SERVER_API}" off
)
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
exitStatus=$?
clear
if [ $exitStatus -ne 0 ]; then
exit
fi
for choice in $choices; do
case $choice in
0)
exit
;;
1)
TO_RUN=("${TO_RUN[@]}" "${SERVER_API}")
;;
esac
done
dev_up
}
# Checkout the latest main code, and then make new branch based on latest semver tag
git_pull_branch() {
git checkout -- .
git checkout main
git pull origin main
git fetch --tags
latest_tag=$(git tag | sort -V | tail -n 1)
git checkout -b "$latest_tag"
}
pull_branch() {
GIT_CLEAN=$(git status --porcelain)
if [ -n "$GIT_CLEAN" ]; then
echo -e "\n${RED}GIT NOT CLEAN${RESET}\n"
printf "%s\n" "${GIT_CLEAN}"
fi
if [[ -n "$GIT_CLEAN" ]]; then
ask_yn "Happy to clear git state"
if [[ "$(user_input)" =~ ^n$ ]]; then
exit
fi
fi
git_pull_branch
main
}
main() {
cmd=(dialog --backtitle "Start ${APP_NAME} containers" --radiolist "choose environment" 14 80 16)
options=(
1 "${DEV} up" off
2 "${DEV} down" off
3 "${PRO} up" off
4 "${PRO} down" off
5 "${PRO} rebuild" off
6 "pull & branch" off
)
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
exitStatus=$?
clear
if [ $exitStatus -ne 0 ]; then
exit
fi
for choice in $choices; do
case $choice in
0)
exit
;;
1)
select_containers
break
;;
2)
dev_down
break
;;
3)
echo "production up: ${ALL[*]}"
production_up
break
;;
4)
production_down
break
;;
5)
production_rebuild
break
;;
6)
pull_branch
break
;;
esac
done
}
main