-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappctl.sh
More file actions
executable file
·323 lines (278 loc) · 7.49 KB
/
appctl.sh
File metadata and controls
executable file
·323 lines (278 loc) · 7.49 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
source .infra/global/libs/functions.sh
usage() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
Usage:
appctl.sh build app1 [app2 app3 ...]
start app1 [app2 app3 ...]
stop app1 [app2 app3 ...]
refresh-db app1 [app2 app3 ...]
logs app1 [app2 app3 ...]
validate app1 [app2 app3 ...]
attach app1
list
EOF
}
usage_refresh_db() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
This command recreate and reload database of specified apps from source.
Usage:
appctl.sh refresh-db app1 [app2 app3 ...]
EOF
}
usage_list() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
List available applications managed by virtual environment.
Usage:
appctl.sh list
EOF
}
usage_attach() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
Attach to running app and get a login shell.
Usage:
appctl.sh attach app
EOF
}
usage_build() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
This command builds container image of specified apps from source.
Usage:
appctl.sh build app1 [app2 app3 ...]
EOF
}
usage_start() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
This command start container of specified apps.
Usage:
appctl.sh starts app1 [app2 app3 ...]
EOF
}
usage_stop() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
This command stops container of specified apps.
Usage:
appctl.sh stop app1 [app2 app3 ...]
EOF
}
usage_validate() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
This command validate specified apps.
Usage:
appctl.sh validate app1 [app2 app3 ...]
EOF
}
usage_logs() {
cat <<EOF
Infrastructure control tool for Virtual development environment.
Crafted by Justin Zhang <[email protected]>
This command continuously shows logs from specified apps.
Usage:
appctl.sh logs app1 [app2 app3 ...]
EOF
}
build() {
ARG=$1
if [[ -z $ARG ]]; then
usage_build
exit 1
fi
ID_FILE=""
if [[ -f ~/.ssh/id_ed25519 ]]; then
ID_FILE=id_ed25519
elif [[ -f ~/.ssh/id_ecdsa ]]; then
ID_FILE=id_ecdsa
elif [[ -f ~/.ssh/id_rsa ]]; then
ID_FILE=id_rsa
else
echo "Please setup ssh key to access git properly!"
exit 2
fi
for APP in $@; do
if [[ ! -d ./.apps/$APP ]]; then
echo "Project '$APP' does not exist under .apps!"
exit 3
fi
done
all_compose_files=""
for file in *-*.yml; do
all_compose_files="$all_compose_files -f $file"
done
for APP in $@; do
TMP_PRIV_DIR="./.apps/$APP/.ssh"
TMP_PRIV_FILE="$TMP_PRIV_DIR/$ID_FILE"
mkdir -p $TMP_PRIV_DIR
cp ~/.ssh/$ID_FILE $TMP_PRIV_FILE
TMP_MVN_DIR="./.apps/$APP/.m2"
if [[ -f ~/.m2/settings.xml ]]; then
mkdir -p $TMP_MVN_DIR
TMP_MVN_SETTINGS="./.apps/$APP/.m2/settings.xml"
cp ~/.m2/settings.xml $TMP_MVN_SETTINGS
fi
podman-compose $all_compose_files build --build-arg ID_FILE=$ID_FILE $APP
rm -fr $TMP_PRIV_DIR
rm -fr $TMP_MVN_DIR
done
}
attach() {
ARG=$1
if [[ -z $ARG ]]; then
usage_attach
exit 1
fi
podman-compose -f "app-${ARG}.yml" exec $ARG sh
}
list() {
for file in app-*; do
if [[ $file =~ ^app-(.+).yml$ ]]; then
echo ${BASH_REMATCH[1]}
fi
done;
}
start() {
if [[ -z $1 ]]; then
usage_start
exit 1
fi
all_compose_files=""
for file in *-*.yml; do
all_compose_files="$all_compose_files -f $file"
done
all_apps=""
for app in $@; do
all_apps="$all_apps $app"
done
podman-compose $all_compose_files up -d $all_apps
# do app-specific post setup
for app in $@; do
if [[ -f provision/apps/$app/post/setup.sh ]]; then
echo "Run post setup script for $app..."
sh provision/apps/$app/post/setup.sh
fi
done
}
stop() {
if [[ -z $1 ]]; then
usage_stop
exit 1
fi
all_compose_files=""
for file in *-*.yml; do
all_compose_files="$all_compose_files -f $file"
done
all_apps=""
for app in $@; do
all_apps="$all_apps $app"
done
podman-compose $all_compose_files stop $all_apps
}
logs() {
if [[ -z $1 ]]; then
usage_logs
exit 1
fi
all_compose_files=""
for file in *-*.yml; do
all_compose_files="$all_compose_files -f $file"
done
all_apps=""
for app in $@; do
all_apps=" $app"
done
podman-compose $all_compose_files logs -f $all_apps
}
validate() {
if [[ -z $1 ]]; then
usage_validate
exit 1
fi
basedir=$(pwd)
for app in $@; do
echo "Validating $app..."
PWD=$(pwd)
app_dir="$basedir/provision/$app"
cd $app_dir
if [[ -f $app_dir/validate.sh ]]; then
echo "Validating running status for project: $app"
sh $app_dir/validate.sh
fi
cd $PWD
done
}
refresh_db() {
# make state directories exist
if [[ ! -d .state ]]; then
mkdir .state
fi
# provision databases for backend service
databaseReady=0
dbContainer=$(podman ps -f label=database=true -q)
if [[ -z $dbContainer ]]; then
echo "Database is not ready..."
exit 1
fi
dbType=$(podman inspect -f {{.Config.Labels.dbtype}} $dbContainer)
printf "Checking $dbType readiness"
for attempt in {1..20}; do
printf "."
stat=$(getDatabaseStatus $dbContainer $dbType)
if [[ $stat == *"running"* ]]; then
echo ""
echo "$dbType is ready!"
databaseReady=1
break;
fi
sleep 1
done
basedir=$(pwd)
if [ $databaseReady -eq 1 ]; then
for app in $basedir/.apps/*/; do
if [[ $# == 0 || $* =~ $(basename $app) ]]; then
PWD=$(pwd)
cd $app
if [ -f project_root/schema/schema.sql ]; then
db=$(head -3 project_root/schema/schema.sql | grep -i USE | head -1 | cut -d' ' -f2 | sed 's/;//')
echo "Prepare database ${db} for project $(basename $app)..."
podman exec -it ${dbContainer} /bin/sh /setup/create-database.sh $db localenv
echo "Loading schema and data using podman for project $(basename $app)..."
podman exec -it ${dbContainer} /bin/sh /setup/load-schema-and-data.sh $(basename $app) localenv $db .apps
fi
cd $PWD
fi
done;
else
echo "$dbtype is not working, try to setup database later!!!"
fi
}
cmd=$1
if [[ -z $cmd ]]; then
usage
exit 1
fi
shift
case "${cmd}" in
build) build $@;;
start) start $@;;
stop) stop $@;;
logs) logs $@;;
list) list $@;;
attach) attach $@;;
validate) validate $@;;
refresh-db) refresh_db $@;;
*) usage && exit 1;;
esac