-
Notifications
You must be signed in to change notification settings - Fork 2
/
do
executable file
·340 lines (291 loc) · 8.5 KB
/
do
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
#############################
# Defaults
apiconf="vanilla/specs/api_init.json"
jsconf="vanilla/specs/js_init.json"
#############################
# Other variables
project="restangulask"
compose_com="docker-compose"
webbuild="bower"
volume_prefix="restangulask_${1}_"
fronted_container="customfe"
backend_container="custombe"
submodule_repo="backend"
submodule_git="https://github.com/EUDAT-B2STAGE/http-api-base.git"
services="$backend_container $fronted_container"
submodule_tracking="submodules.current.commit"
#############################
echo "# ############################################ #"
echo -e "\t\tRestangulask"
echo "# ############################################ #"
echo ""
#####################
# Check prerequisites
coms="docker $compose_com"
for com in $coms;
do
dcheck=`which $com`
if [ "$dcheck" == "" ]; then
echo "Please, install $com to use this project."
exit 1
fi
dcheck=`$com ps 2>&1 | grep -i "cannot connect"`
if [ "$dcheck" != "" ]; then
echo "Please check if your Docker daemon is running"
exit 1
fi
done
#############################
cd containers
if [ "$1" != "help" ]; then
if [ -z "$1" ]; then
echo "Please specify one configuration to use."
echo "Available configurations:"
for i in `ls custom`;
do
name=`echo $i | sed "s/\.yml//"`
echo -e "\t$name"
done
exit
elif [ "$1" != "help" ]; then
echo "### SELECTED BLUEPRINT: [$1]"
echo ""
fi
#############################
# Check compose stack existence
file="custom/${1}.yml"
if [ ! -f "$file" ]; then
echo "File 'containers/$file' not found!"
echo "You might start up copying 'demo.yml'."
exit 1
fi
files="-f docker-compose.yml -f $file"
# Remove previous configuration
#echo "Clean configuration files"
if [ -f "../$apiconf" ]; then
rm ../$apiconf
fi
if [ -f "../$jsconf" ]; then
rm ../$jsconf
fi
fi
if [ "$1" == "help" -o -z "$2" ]; then
echo "Usage: $0 BLUEPRINT COMMAND [OPTIONS]"
echo ""
echo "Available list of COMMANDs:"
echo ""
echo -e "init:\t\tStartup your repository code, containers and volumes"
echo -e "graceful:\tTry to bring up only missing containers"
echo -e "restart:\t(Re)Launch the Docker stack"
echo -e "status:\tCheck the stack status"
echo -e "stop:\tFreeze your containers stack"
echo -e "remove:\tRemove all containers"
echo -e "clean:\tRemove containers and volumes (BE CAREFUL!)"
echo ""
echo -e "backend:\tOpen a shell inside the Flask REST API server container"
echo -e "test_backend:\tLaunch python tests with nose2"
echo -e "frontend:\tOpen a shell inside the Flask REST API server container"
echo -e "sql:\tLaunch a sqladminer on port 8888"
echo -e "bower:\tInstall all libraries or only the one specified"
echo -e "karma:\tA shell to test angularjs code"
echo ""
echo -e "push:\tPush code to github"
echo -e "update:\tPull both updated code and docker images"
echo ""
exit 0
fi
#############################
# Check libs
if [ -z "$2" -o "$2" != "init" ]; then
libs="../vanilla/libs/bower_components"
out=`ls $libs 2> /dev/null | wc -m | tr -d ' '`
if [ "$out" == "0" ]; then
echo ""
echo "[ERROR] You are missing JS libraries!"
echo "Please run:"
echo "$0 $1 init"
exit 1
fi
fi
#############################
# DEFAULTS FILES
# Backend
file="$1.json"
check="vanilla/specs/$file"
if [ ! -s "../$check" ]; then
echo "File '$check' not found or empty..."
echo "Please create it to define APIs endpoints."
exit 1
fi
echo "{ \"$1\": \"$file\" }" > ../$apiconf
# Frontend
check="vanilla/jscode/$1"
if [ ! "$(ls -A ../$check 2> /dev/null)" ]; then
echo "Directory '$check' not found or empty..."
echo "Please create it to define AngularJS code."
exit 1
fi
echo "{ \"blueprint\": \"$1\" }" > ../$jsconf
#############################
# Run services
bcom="$compose_com run --rm $webbuild bower install"
# First time install
if [ "$2" == "init" ]; then
echo -e "ACTION: Project init\n"
#git remote add private
#[email protected]:mdantoni/telethon_repo.git
echo "Download docker images"
$compose_com $files pull
cd ..
if [ ! -d "$submodule_repo" ]; then
echo "Clone submodules"
git clone $submodule_git $submodule_repo
fi
cd -
echo "Build bower packages (Javascript libraries)"
$bcom
echo "Completed"
elif [ "$2" == "status" ]; then
echo -e "ACTION: Status check\n"
elif [ "$2" == "graceful" ]; then
echo -e "ACTION: Start\n"
$compose_com $files up -d $services
elif [ "$2" == "restart" ]; then
echo -e "ACTION: Reboot\n"
echo "Cleaning project containers (if any)"
$compose_com $files stop
$compose_com $files rm -f --all
echo "Starting up"
$compose_com $files up -d $services
elif [ "$2" == "stop" ]; then
echo -e "ACTION: Stop\n"
echo "Freezing services"
$compose_com $files stop
elif [ "$2" == "remove" ]; then
echo -e "ACTION: Removal\n"
echo "Destroying services"
$compose_com $files stop
$compose_com $files rm -f --all
elif [ "$2" == "clean" ]; then
echo -e "ACTION: Total removal\n"
echo "Destroying (forever) containers & volumes. Are you really sure?"
sleep 5
$compose_com $files stop
$compose_com $files rm -f --all
for volume in `docker volume ls -q | grep $volume_prefix`;
do
docker volume rm $volume
echo "Removed volume: $volume"
done
echo ""
elif [ "$2" == "backend" ]; then
echo "Opening a shell inside the $2 container"
echo "(If in debug mode, run Flask with './boot devel')"
echo ""
$compose_com $files exec $backend_container bash
exit 0
elif [ "$2" == "frontend" ]; then
echo "Opening a shell inside the $2 container"
echo "(If in debug mode, run Flask with './boot devel')"
echo ""
$compose_com $files exec $fronted_container bash
exit 0
elif [ "$2" == "test_backend" ]; then
echo "Running tests"
$compose_com $files exec $backend_container ./tests.sh
if [ "$?" != "0" ]; then
echo "Tests failed!"
exit 1
fi
echo "Well done"
exit 0
elif [ "$2" == "sql" ]; then
echo "Launch adminer for SQL servers"
$compose_com run --rm --service-ports sqladmin
elif [ "$2" == "push" ]; then
echo "Running tests"
# Add nohup?
$compose_com $files exec $backend_container ./tests.sh
if [ "$?" != "0" ]; then
echo "Tests are failing. Push was stopped"
exit 1
fi
cd ..
echo "Pushing submodule"
cd $submodule_repo
git push
if [ $? != "0" ]; then
echo "Failed to push submodule"
exit 1
fi
cd ..
# Save a snapshot of current submodule
echo "Save submodule status"
echo -e \
$(cd $submodule_repo && git log -n 1 --oneline --no-color)"\n"$(cd $submodule_repo && git branch --no-color) \
> $submodule_tracking
echo "Pushing main repo"
git add $submodule_tracking
git commit && echo "Commit has been done"
# Push to all repos setted
for repo in `git remote`;
do
git push $repo
done
exit 0
# Update repos, packages and images
elif [ "$2" == "update" ]; then
$compose_com $files pull
current=`pwd`
cd ..
echo "Pulling main repo"
git pull
echo "Pulling submodule"
cd $submodule_repo
git pull
cd ..
echo "Updating libs"
cd $current
$bcom
exit 0
# Bower install
elif [ "$2" == "bower" ]; then
if [ "$3" != "" ]; then
bcom="${bcom} $3 --save"
echo "Install package(s): $3"
fi
$bcom
# Angularjs tests
elif [ "$2" == "karma" ]; then
echo "Opening shell for nodejs"
$compose_com run --rm $2 bash
exit 0
else
echo "Unknown command '$2'. Ask for help:"
echo ""
echo "$0 help"
exit 0
fi
# Final check up
if [ "$?" == "0" ]; then
if [ "$2" == "init" ]; then
echo ""
echo "You may launch containers now. Use:"
echo "$0 $1 restart"
echo ""
else
echo "[$1] configuration. Status:"
$compose_com $files ps
echo ""
echo "Available volumes for current configuration:"
docker volume ls | grep $volume_prefix
fi
if [ "$2" == 'restart' -o "$2" == 'graceful' ]; then
echo ""
echo "Access containers with:"
echo "$0 $1 backend"
echo "and/or"
echo "$0 $1 frontend"
fi
fi