-
Notifications
You must be signed in to change notification settings - Fork 2
/
mm
executable file
·407 lines (366 loc) · 9.96 KB
/
mm
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
404
405
406
407
#! /bin/bash
canvas_dir=$(pwd)/canvas-lms
use_local_image=false
mm_docker() {
echo "$@"
docker "$@"
}
mm_help_build() {
cat <<EOF
Usage: mm build IMAGE
IMAGE = web|db|cache|ha_proxy
EOF
}
mm_build() {
case $1 in
web)
mm_docker build -t mmooc/canvas:local mmooc-docker-canvas
;;
db)
mm_docker build -t mmooc/db:local mmooc-docker-postgresql
;;
cache)
mm_docker build -t mmooc/cache:local mmooc-docker-redis
;;
dev)
mm_docker build -t mmooc/dev:local mmooc-docker-dev
;;
haproxy)
mm_docker build -t mmooc/haproxy:local mmooc-docker-haproxy
;;
*)
mm_help build
;;
esac
}
mm_url() {
echo "Finding network ports for the container named 'web'"
port_http=$(docker port web 80 | cut -d: -f2)
port_https=$(docker port web 443 | cut -d: -f2)
if which docker-machine ip >/dev/null 2>&1
then
echo "Found \"docker-machine\", guessing you're not on Linux. Getting ip address from VM."
addr=$(docker-machine ip)
else
echo "No \"docker-machine\" found, guessing you're on Linux. Using localhost."
addr="127.0.0.1"
fi
echo ""
echo "Canvas addresses:"
echo " http://${addr}:${port_http}/"
echo " https://${addr}:${port_https}/"
}
mm_image_name() {
if [ "$use_local_image" = true ]; then
echo "$1:local"
else
echo $1
fi
}
mm_container_name() {
if [ "$use_local_image" = true ]; then
echo "l$1"
else
echo $1
fi
}
docker_run() {
local container=$(mm_container_name $2)
local image=$(mm_image_name $1)
local options=$3
echo "docker_run image : $image"
echo "docker_run container: $container"
echo "docker_run options : $options"
if ! container_exists $container ; then
# local command="docker run --env-file=env -d -P $options --name=$container $image"
local command="docker run --env-file=env -d -P $options --name=$container $image"
echo $command >> mmdos.bat
$command || true
else
echo "docker_run container exists. Starting $container"
docker start $container
fi
}
docker_run_db() {
docker_run mmooc/db db "--volumes-from=$(mm_container_name db-data)"
}
docker_run_cache() {
docker_run mmooc/cache cache
}
docker_run_web() {
docker_run mmooc/canvas web "--volumes-from=$(mm_container_name web-data) --link $(mm_container_name db):db --link $(mm_container_name cache):cache"
}
docker_run_jobs() {
local image=$(mm_image_name mmooc/canvas)
local command="docker run --env-file=env -d -P --volumes-from=$(mm_container_name web-data) --link=$(mm_container_name db):db --link=$(mm_container_name cache):cache --name=jobs $image /opt/canvas-lms/script/canvas_init run"
echo $command >> mmdos.bat
$command || true
}
docker_run_haproxy() {
# docker run -d --name canvas-haproxy --link web:web1 -p 443:443 -p 80:80 canvas-haproxy
docker_run mmooc/haproxy haproxy "--link web:web1 -p 443:443 -p 80:80"
}
container_exists() {
echo "docker inspect $1"
docker inspect "$1" > /dev/null 2>&1
}
mm_start_data() {
echo "mm_start_data"
if ! container_exists $(mm_container_name web-data) ; then
local command="docker run -v /var/log/apache2 -v /opt/canvas-lms/log -v /opt/canvas-lms/tmp/files --name=$(mm_container_name web-data) ubuntu:16.04"
echo $command >> mmdos.bat
$command
else
echo "web-data existed."
fi
if ! container_exists $(mm_container_name db-data) ; then
echo "db-data does not exist. Start it."
local container=$(mm_container_name "db-data")
echo "container name: $container"
local command="docker run -v /var/lib/postgresql/9.5/main --name=$container ubuntu:16.04"
echo $command >> mmdos.bat
$command
else
echo "db-data existed."
fi
}
mm_help_start() {
cat <<EOF
Usage: mm start COMMAND
Commands
all Start all service containers
cache Start the Redis container
data Start data volume containers
db Start the PostgreSQL container
jobs FIXME
web Start the Canvas container
haproxy Start the HAProxy container
EOF
}
mm_start() {
echo "mm_start $1"
case $1 in
all)
docker_run_db
docker_run_cache
docker_run_web
docker_run_haproxy
docker_run_jobs
;;
data)
mm_start_data
;;
db)
docker_run_db
;;
cache)
docker_run_cache
;;
jobs)
docker_run_jobs
;;
web)
docker_run_web
;;
haproxy)
docker_run_haproxy
;;
*)
mm_help start
;;
esac
}
mm_init_schema() {
local image=$(mm_image_name mmooc/canvas)
echo "Schema setup is bugged and needs to run twice."
echo "Setting up schema for the first time. Output in init_schema.1.txt"
local command="docker run --rm --env-file=env -w /opt/canvas-lms --link=$(mm_container_name db):db --link=$(mm_container_name cache):cache $image bundle exec rake db:initial_setup >&2 2> init_schema.1.txt"
echo $command >> mmdos.bat
$command
echo "Second time's the charm. Output in init_schema.2.txt"
local command="docker run --rm --env-file=env -w /opt/canvas-lms --link=$(mm_container_name db):db --link=$(mm_container_name cache):cache $image bundle exec rake db:initial_setup >&2 2> init_schema.2.txt"
echo $command >> mmdos.bat
$command
}
mm_initdb() {
echo "mm_initdb begin"
local image=$(mm_image_name mmooc/db)
echo "image: $image"
local command="docker run --rm -t -i --env-file=env --user=root --volumes-from=$(mm_container_name db-data) $image /bin/bash /root/initdb"
echo $command >> mmdos.bat
$command
echo "mm_initdb end"
}
mm_boot() {
mm_start_data
mm_initdb
mm_start db
mm_start cache
mm_init_schema
mm_start web
mm_start haproxy
mm_url
}
mm_stop() {
case $1 in
services)
for X in web cache db; do
echo "Stopping & removing $X..."
docker stop $X
docker rm $X
done
;;
data)
for X in db-data web-data; do
echo "Stopping & removing $X..."
docker rm $X
done
;;
*)
mm_help stop
;;
esac
}
mm_help_main() {
cat <<EOF
Usage: mm [OPTIONS] COMMAND
Utilities FIXME
Options
-l, --local Use images with tag :local
Commads:
boot First time use. Takes up the whole system
build Build local docker images
help Display help information
initdb Create the postgres cluster, roles and databases
init-schema Initialize the database schema and insert initial data
pull Pull new versions of docker images
rails FIXME
rake FIXME
reboot Stop & remove web, cache, db services, pull new images, restart.
reset Stop & remove web, cache, db services; remove web-data and db-data, pull new images, restart.
start Start one or all docker containers
url Print the url from which you can access canvas
EOF
}
mm_help_fixme() {
echo "FIXME To be documented ..."
}
mm_help() {
case $1 in
build)
mm_help_build
;;
boot)
mm_help_fixme
;;
help)
mm_help_fixme
;;
initdb)
mm_help_fixme
;;
init-schema)
mm_help_fixme
;;
pull)
mm_help_fixme
;;
rails)
mm_help_fixme
;;
rails-dev)
mm_help_fixme
;;
rake)
mm_help_fixme
;;
start)
mm_help_start
;;
stop)
mm_help_fixme
;;
*)
mm_help_main
;;
esac
}
if [ "$1" = "--local" -o "$1" = "-l" ]; then
use_local_image=true
shift
fi
command=$1
shift || true
case $command in
build)
mm_build "$@"
;;
boot)
mm_boot
;;
help)
mm_help "$@"
;;
startdata)
mm_start_data
;;
initdb)
mm_initdb
;;
init-schema)
mm_init_schema
;;
pull)
for X in db canvas cache haproxy tools dev; do
docker pull mmooc/$X
done
;;
rails)
image=$(mm_image_name mmooc/canvas)
local command='docker run --rm -t -i -P --env-file=env --link db:db -w /opt/canvas-lms $image bundle exec rails "$@"'
echo $command >> mmdos.bat
$command
;;
rails-dev)
local command='docker run --rm -t -i -p 3000:3000 --env-file=env -e RAILS_ENV=development --link db:db --link cache:cache -w /opt/canvas-lms mmooc/canvas bundle exec rails "$@"'
echo $command >> mmdos.bat
$command
;;
dev)
local command='docker run -t -i -p 3001:3000 --name=$(mm_container_name dev) --env-file=env -e RAILS_ENV=development --link db:db --link cache:cache -w /opt/canvas-lms mmooc/dev'
echo $command >> mmdos.bat
$command
;;
rake)
local command='docker run --rm -t -i -P --env-file=env -e RAILS_ENV=development -v $canvas_dir:/canvas-lms --link db:db -w /canvas-lms mmooc/canvas bundle exec rake "$@"'
echo $command >> mmdos.bat
$command
;;
reboot)
mm_stop services
for X in db canvas cache; do
docker pull mmooc/$X
done
mm_boot
;;
reset)
mm_stop services
mm_stop data
for X in db canvas cache; do
docker pull mmooc/$X
done
mm_boot
;;
start)
mm_start "$@"
;;
stop)
mm_stop "$@"
;;
url)
mm_url
;;
*)
mm_help
;;
esac