forked from EUDAT-B2STAGE/http-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdo
More file actions
executable file
·383 lines (326 loc) · 10.1 KB
/
do
File metadata and controls
executable file
·383 lines (326 loc) · 10.1 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
docker_volumes_prefix="httpapitemplate"
echo "# ############################################ #"
echo -e "\t\tHTTP API development"
echo "# ############################################ #"
echo ""
if [ "$1" == "help" -o -z "$1" ]; then
echo "Available commands:"
echo ""
echo -e "init:\t\tStartup your repository code, containers and volumes"
echo -e "training:\tLaunch the environment for training on GraphDB"
# echo -e "addiuser:\tAdd a new certificated user to irods"
echo ""
echo -e "check:\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 "irestart:\tRestart the main iRODS iCAT service instance"
echo -e "irods_shell:\tOpen a shell inside the iRODS iCAT server container"
echo -e "server_shell:\tOpen a shell inside the Flask server container"
echo -e "client_shell:\tOpen a shell to test API endpoints"
echo -e "api_test:\tRun tests with nose (+ coverage)"
echo ""
echo -e "push:\tPush code to github"
echo -e "update:\tPull updated code and images"
echo ""
echo -e "***Modes***:"
echo -e "DEBUG:\tREST API server should be launched using container shell"
echo -e "DEVELOPMENT:\tREST API server with Flask WSGI and Debug"
echo -e "PRODUCTION:\tREST API server with Gunicorn behind nginx proxy"
echo ""
echo -e "[Mode] restart:\t(Re)Launch the Docker stack"
echo -e "logs:\tAttach to all container logs"
exit 0
fi
#####################
# Confs
subdir="backend"
submodule_tracking="submodules.current.commit"
irodscontainer="icat"
restcontainer="rest"
proxycontainer="proxy"
clientcontainer="apitests"
vcom="docker volume"
compose_base="docker-compose -f docker-compose.yml"
# Check the prefix
if [ "$docker_volumes_prefix" == "httpapitemplate" ]; then
echo '$docker_volumes_prefix: "httpapitemplate"'
echo ""
echo "Please consider changing the main docker volume prefix"
echo "(Line 3 of the './do' file)"
echo ""
exit 1
else
export VOLUMES_PREFIX="$docker_volumes_prefix"
fi
# Init mode
if [ "$1" == "init" ]; then
compose_run="$compose_base -f composers/init.yml"
# Production mode
elif [ "$1" == "PRODUCTION" ]; then
compose_run="$compose_base -f composers/production.yml"
# Development mode
elif [ "$1" == "DEVELOPMENT" ]; then
compose_run="$compose_base -f composers/development.yml"
# Training mode
elif [ "$1" == "training" ]; then
compose_run="$compose_base -f composers/training.yml"
# Normal / debug mode
else
compose_run="$compose_base -f composers/debug.yml"
fi
make_tests="$compose_run exec rest ./tests.sh"
#####################
# Check prerequisites
coms="docker $compose"
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
if [ "$(ls -A $subdir)" ]; then
echo "Submodule already exists" > /dev/null
else
echo "Inizialitazion for the http-api-base submodule"
git clone https://github.com/EUDAT-B2STAGE/http-api-base.git $subdir
# git submodule init
# git submodule update --remote
cd $subdir
git checkout master
cd ..
fi
# Update the remote github repos
if [ "$1" == "push" ]; then
check_container=`$compose_run ps rest | grep -i exit`
if [ "$check_container" != "" ]; then
echo "Please make sure that Flask container server is running"
echo "You may try with the command:"
echo "$0 DEBUG"
echo ""
exit 1
fi
if [ "$2" != "force" ]; then
testlogs="/tmp/tests.log"
echo "Running tests before pushing..."
$make_tests > $testlogs
if [ "$?" == "0" ]; then
echo "Test are fine!"
else
echo "Failed, to test... (see $testlogs file)"
echo "Fix errors before pushing, or run again with:"
echo "$0 $1 force"
exit 1
fi
fi
echo "Pushing submodule"
cd $subdir
git push
cd ..
# Save a snapshot of current submodule
echo "Save submodule status"
echo -e \
$(cd $subdir && git log -n 1 --oneline --no-color)"\n"$(cd $subdir && git branch --no-color) \
> $submodule_tracking
echo "Pushing main repo"
git add $submodule_tracking
git commit
git push
echo "Completed"
exit 0
fi
# Update your code
if [ "$1" == "update" ]; then
echo "Updating docker images to latest release"
$compose_run pull
echo "Pulling main repo"
git pull
echo "Pulling submodule"
cd $subdir
git pull
echo "Done"
exit 0
fi
#######################################
## // TO FIX: make this parametric:
# https://github.com/pdonorio/restapi-template/issues/1
# # Check if init has been executed
# volumes=`$vcom ls | awk '{print $NF}' | grep "^${docker_volumes_prefix}_"`
# #echo -e "VOLUMES are\n*$volumes*"
# if [ "$volumes" == "" ]; then
# if [ "$1" != "init" ]; then
# echo ""
# echo "Docker volumes are missing."
# echo "You must *init* this project:"
# echo ""
# echo "\$ $0 init"
# echo ""
# exit 1
# fi
# fi
################################
# EXECUTE OPTIONS
# Init your stack
if [ "$1" == "init" ]; then
echo "WARNING: Removing old containers/volumes if any"
echo "(Sleeping some seconds to let you stop in case you made a mistake)"
sleep 7
echo "Containers stopping"
$compose_run stop
echo "Containers deletion"
$compose_run rm -f
if [ "$volumes" != "" ]; then
echo "Destroy volumes:"
docker volume rm $volumes
fi
echo "READY TO INIT"
$compose_run up icat rest
if [ "$?" == "0" ]; then
echo ""
echo "Your project is ready to be used."
echo "Everytime you need to start just run:"
echo "\$ $0 DEBUG"
echo ""
fi
exit 0
# training
elif [ "$1" == "training" ]; then
container="training"
$compose_run rm -f $container
$compose_run up -d $container
$compose_run exec --user root $container chown -R root /opt/certificates
echo ""
echo "Please edit the python file with path'./training/custom.py',"
echo "then execute your code within the container:"
echo "$ ./training.py"
echo ""
$compose_run exec --user root $container bash
exit 0
# Verify the status
elif [ "$1" == "check" ]; then
echo "Stack status:"
$compose_run ps
exit 0
# Freeze containers
elif [ "$1" == "stop" ]; then
echo "Freezing the stack"
$compose_run stop
exit 0
# Remove all containers
elif [ "$1" == "remove" ]; then
echo "REMOVE CONTAINERS"
$compose_run stop
$compose_run rm -f
exit 0
# Destroy everything: containers and data saved so far
elif [ "$1" == "clean" ]; then
echo "REMOVE DATA"
echo "are you really sure?"
sleep 5
# From docker-compose man:
# > "down": Stop and remove containers, networks, images, and volumes
$compose_run down
# $compose_run stop
# $compose_run rm -f
# for volume in $volumes;
# do
# echo "Remove $volume volume"
# $vcom rm $volume
# sleep 1
# done
exit 0
elif [ "$1" == "addiuser" ]; then
echo "Adding a new certificated iRODS user:"
$compose_run exec $irodscontainer /addusercert $2
exit 0
elif [ "$1" == "irestart" ]; then
$compose_run exec $irodscontainer /bin/bash /irestart
exit 0
elif [ "$1" == "irods_shell" ]; then
$compose_run exec $irodscontainer bash
exit 0
elif [ "$1" == "server_shell" ]; then
$compose_run exec $restcontainer bash
exit 0
elif [ "$1" == "api_test" ]; then
echo "Opening a shell for nose2 tests"
$make_tests
exit 0
elif [ "$1" == "client_shell" ]; then
echo "Opening a client shell"
# $compose_run up --no-deps -d $clientcontainer
$compose_run exec $clientcontainer ash
exit 0
# Handle the right logs
elif [ "$1" == "logs" ]; then
$compose_run logs -f -t --tail="10"
exit 0
fi
# Boot up
if [ "$1" == "DEBUG" -o "$1" == "DEVELOPMENT" -o "$1" == "PRODUCTION" ];
then
echo "Docker stack: booting"
if [ "$2" == "restart" ]; then
echo "Clean previous containers"
$compose_run stop
$compose_run rm -f
fi
case $2 in
''|*[!0-9]*) ;;
*)
service="worker"
echo "Setting $2 $service(s)"
# Make sure we bring up the containers and all of its links
$compose_run up -d $service
# Scale to the number of requested workers
$compose_run scale $service=$2
;;
esac
# Check certificates
if [ "$1" == "PRODUCTION" ]; then
if [ ! -f "./certs/nginx-selfsigned.key" -o ! -f "./certs/nginx-selfsigned.crt" ];
then
echo "Missing certificates."
echo "To create self_signed files you may use:"
echo "./confs/create_self_signed_ssl.sh"
exit 1
fi
fi
# The client container always has the best link to access the server
$compose_run up -d $clientcontainer
status="$?"
echo "Stack processes:"
$compose_run ps
if [ "$status" == "0" ]; then
$compose_run exec --user root rest update-ca-certificates
echo ""
echo "To access the flask api container:"
echo "$0 server_shell"
echo ""
echo "To query the api server (if running) use the client container:"
echo "$0 client_shell"
path="/api/status"
if [ "$1" == "PRODUCTION" ]; then
echo "/ # http --follow --verify /tmp/cert.crt awesome.docker$path"
elif [ "$1" == "DEVELOPMENT" ]; then
echo "/ # http GET http://apiserver$path"
else
echo "/ # http GET apiserver:5000$path"
fi
echo ""
fi
echo "Boot completed"
exit 0
fi
echo "Unknown operation '$1'!"
echo "Use \"$0 help\" to see available commands "
exit 1