forked from doubtfire-lms/doubtfire-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.sh
More file actions
executable file
·403 lines (368 loc) · 8.84 KB
/
docker.sh
File metadata and controls
executable file
·403 lines (368 loc) · 8.84 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/sh
#
# Doubtfire Docker Script
# =======================
#
# Provides commands to run Doubtfire via Docker without
# actually needing to know Docker commands (hopefully)
#
#
# Resets the color
#
msg_reset () {
RESET='\033[0m'
printf "${RESET}\n"
}
#
# Log an error
#
error () {
RED_FORE='\033[0;31m'
printf "${RED_FORE}ERROR: $1"
msg_reset
}
#
# Log verbose message
#
verbose () {
if [ $VERBOSE_OUTPUT -eq 1 ]; then
return
fi
CYAN_FORE='\033[0;36m'
printf "${CYAN_FORE}INFO: $1"
msg_reset
}
#
# Log message
#
msg () {
printf "$1\n"
}
#
# Try and get the docker machine IP
# Returns the IP or else exits the script
#
get_docker_machine_ip () {
DF_DOCKER_MACHINE_IP=$(docker-machine ip doubtfire)
if [ $? -ne 0 ]; then
verbose "Docker machine is not running. Attempting to start..."
docker-machine start doubtfire
if [ $? -ne 0 ]; then
error "Could not start Doubtfire docker machine!"
msg "Ensure you have created the docker machine first:"
msg " docker-machine create --driver virtualbox doubtfire"
exit 1
fi
fi
verbose "Running Docker daemon..."
eval "$(docker-machine env doubtfire)"
}
#
# Returns whether or not the provided service is running
#
is_service_running () {
verbose "Checking if $1 is running..."
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire ps $1 | grep 'Up' &> /dev/null
if [ $? -ne 0 ]; then
verbose "Service $1 is not running!"
return 1
fi
verbose "Service $1 is running"
return 0
}
#
# Returns whether or not all services are running
#
is_doubtfire_running () {
verbose "Checking if doubtfire is running..."
if is_service_running "db" && is_service_running "api" && is_service_running "db" ; then
verbose "Doubtfire is up and running"
return 0
fi
verbose "Doubtfire is not running!"
return 1
}
#
# Tries and restores a previous instance of the docker
#
restore_doubtfire_services () {
msg "Attempting to restore Doubtfire services..."
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire up -d
if [ $? -ne 0 ]; then
verbose "Failed to restore Doubtfire."
return 1
fi
verbose "Restored Doubtfire"
return 0
}
#
# Tries and restores a previous instance of the docker
#
build_doubtfire_images () {
case $1 in
'api'|'web'|'db')
;;
'')
msg "Building all Doubtfire images. This may take a while..."
;;
*)
msg "Invalid image provided. Please provide one of api, web or db."
return 1
;;
esac
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire build $1
if [ $? -ne 0 ]; then
error "Failed to build Doubtfire. Refer to logs above."
return 1
fi
verbose "Built Doubtfire"
return 0
}
#
# Tries to create Doubtfire services
#
create_doubtfire_services () {
verbose "Attempting to create Doubtfire services..."
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire create
if [ $? -ne 0 ]; then
error "Failed to create services! Refer to docker-compose output above."
return 1
fi
verbose "Created Doubtfire services"
return 0
}
#
# Starts the doubtfire database
#
start_doubtfire_database () {
verbose "Attempting to start Doubtfire database container..."
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire start db
if [ $? -ne 0 ]; then
error "Failed to start Doubtfire database! Refer to docker-compose output above."
return 1
fi
verbose "Started Doubtfire Database"
return 0
}
#
# Tries a direct connect to the DF postgresql database
#
is_postgres_running () {
IS_RUNNING=$(DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire run db bash -c "PGPASSWORD=d872\\\$dh psql -h db -U itig -c \"select 'It is running'\" 2>/dev/null | grep -c \"It is running\"")
if [[ $IS_RUNNING == *"1"* ]]; then
return 0
else
return 1
fi
}
#
# Populates doubtfire database
#
populate_doubtfire_database () {
msg "Ensuring database is running..."
# Check if database is running. If not start it
if ! is_service_running "db" && ! start_doubtfire_database; then
error "Cannot populate as service isn't running"
return 1
fi
# Ensure we can actually connect to the DB before we ask to write to it
while ! is_postgres_running; do
verbose "Postgres not yet running... sleep 1"
sleep 1
done
msg "Populating database with test data..."
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire run api rake db:populate
if [ $? -ne 0 ]; then
error "Failed to populate Doubtfire database! Refer to error above."
return 1
fi
msg "Database populated!"
return 0
}
#
# Shows DF is running message
#
show_running_message () {
msg "Doubtfire is now running at:"
msg " Doubtfire API: http://$DF_DOCKER_MACHINE_IP:3000/api/docs/"
msg " Doubtfire Web: http://$DF_DOCKER_MACHINE_IP:8000/"
msg "It may take several moments for the URLs above to become active"
}
#
# Starts doubtfire
#
start_doubtfire () (
try_build_df_again () {
verbose "Attempting to recover by re-building Doubtfire images..."
if ! build_doubtfire_images; then
error "Cannot build Doubtfire images"
return 1
fi
}
get_docker_machine_ip
msg "Starting doubtfire ..."
if is_doubtfire_running; then
msg "No need to start Doubtfire. It is already running."
show_running_message
return 0
fi
# First, try to see if the services have already been created
# and if not create them
if ! restore_doubtfire_services && ! create_doubtfire_services; then
verbose "Problem creating services. Trying to rebuild images..."
if ! try_build_df_again; then
error "Failed to start Doubtfire - cannot create services"
return 1
fi
fi
# Repopulate database
if ! populate_doubtfire_database; then
verbose "Problem populating. Trying to rebuild images..."
if ! try_build_df_again || ! restore_doubtfire_services; then
error "Failed to start Doubtfire - could not populate data and restore"
return 1
fi
fi
show_running_message
)
#
# Stop doubtfire
#
stop_doubtfire () {
get_docker_machine_ip
if ! is_doubtfire_running; then
msg "No need to stop Doubtfire. It is already stopped."
return 1
fi
msg "Stopping doubtfire ..."
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire down
if [ $? -ne 0 ]; then
error "Failed to stop Doubtfire! Check logs above"
return 1
fi
msg "Doubtfire is no longer running"
}
#
# Restart doubtfire
#
restart_doubtfire () {
get_docker_machine_ip
if ! is_doubtfire_running; then
msg "Cannot restart Doubtfire. It is not running"
return 1
fi
msg "Restarting doubtfire ..."
DF_DOCKER_MACHINE_IP=$DF_DOCKER_MACHINE_IP docker-compose -p doubtfire restart
if [ $? -ne 0 ]; then
error "Failed to restart Doubtfire! Check logs above"
return 1
fi
msg "Doubtfire was restarted."
show_running_message
}
#
# Attach to container
#
attach_to () {
get_docker_machine_ip
case $1 in
'api'|'web'|'db')
;;
*)
msg "Invalid container provided. Please provide one of api, web or db."
return 1
;;
esac
if ! is_doubtfire_running; then
msg "Cannot attach or ssh into to Doubtfire container. Doubtfire is not running"
return 1
fi
msg "Attaching to $1. Use ctrl+c to detach."
docker attach --sig-proxy=false doubtfire-$1
msg "\nDeattached from $1."
}
#
# Show help
#
show_help () {
msg "Run Doubtfire using Docker."
msg
msg "Usage:"
msg "./docker.sh [COMMAND] [ARGS...]"
msg "./docker.sh -h"
msg
msg "Options:"
msg " -v Show more output"
msg " -h Display help"
msg
msg "Commands:"
msg " start Start Doubtfire docker services"
msg " stop Stop Doubtfire docker services"
msg " restart Restart Doubtfire docker services"
msg " build (Re)build Doubtfire docker image(s)"
msg " populate Populate the API with test data"
msg " attach Attach to one of the api, web or db containers"
return 0
}
#
# Handle options
#
handle_options () {
COMMAND=$1
VERBOSE_OUTPUT=1
shift 1
# Not a switch?
if [[ $1 != "-"* ]]; then
ARG_1=$1
shift 1
fi
while getopts ":vh" OPT; do
case $OPT in
v)
VERBOSE_OUTPUT=0
;;
h)
show_help
return $?
;;
\?)
error "Invalid option: -$OPTARG" >&2
;;
esac
done
case $COMMAND in
"start")
start_doubtfire
return $?
;;
"stop")
stop_doubtfire
return $?
;;
"restart")
restart_doubtfire
return $?
;;
"populate")
populate_doubtfire_database
return $?
;;
"build")
build_doubtfire_images $ARG_1
return $?
;;
"attach")
attach_to $ARG_1
return $?
;;
"")
show_help
return $?
;;
*)
msg "Invalid command provided"
return 1
;;
esac
}
handle_options $*