-
Notifications
You must be signed in to change notification settings - Fork 19
/
Makefile
520 lines (448 loc) · 22 KB
/
Makefile
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
export COMPOSE_PROJECT_NAME=bims
export COMPOSE_FILE=deployment/docker-compose.yml:deployment/docker-compose.override.yml
export ANSIBLE_PROJECT_SETUP_DIR=deployment/ansible
SHELL := /bin/bash
# ----------------------------------------------------------------------------
# S E T U P C O M M A N D S
# ----------------------------------------------------------------------------
setup:
@echo
@echo "------------------------------------------------------------------"
@echo "Building dist file "
@echo "------------------------------------------------------------------"
@python setup.py sdist
ansible-check:
@echo "Check ansible command"
@ansible -i ${ANSIBLE_PROJECT_SETUP_DIR}/development/hosts all -m ping
@ansible-playbook -i ${ANSIBLE_PROJECT_SETUP_DIR}/development/hosts ${ANSIBLE_PROJECT_SETUP_DIR}/development/site.yml --list-tasks --list-hosts $(ANSIBLE_ARGS)
setup-ansible:
@echo "Setup configurations using ansible"
@ansible-playbook -i ${ANSIBLE_PROJECT_SETUP_DIR}/development/hosts ${ANSIBLE_PROJECT_SETUP_DIR}/development/site.yml $(ANSIBLE_ARGS)
flake8-check:
@echo "Flake8 check"
@flake8 --config .flake8 .
# ----------------------------------------------------------------------------
# P R O D U C T I O N C O M M A N D S
# ----------------------------------------------------------------------------
default: web
run: build permissions web migrate collectstatic
deploy: run
@echo
@echo "------------------------------------------------------------------"
@echo "Bringing up fresh instance "
@echo "You can access it on http://localhost:63200"
@echo "------------------------------------------------------------------"
build:
@echo
@echo "------------------------------------------------------------------"
@echo "Building in production mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} build uwsgi
dev:
@echo
@echo "------------------------------------------------------------------"
@echo "Running in dev mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up -d webpack-watcher dev
web:
@echo
@echo "------------------------------------------------------------------"
@echo "Running in production mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up -d web
@# Dont confuse this with the dbbackup make command below
@# This one runs the postgis-backup cron container
@# We add --no-recreate so that it does not destroy & recreate the db container
@docker compose ${ARGS} up --no-recreate --no-deps -d dbbackups
up-travis:
@echo
@echo "------------------------------------------------------------------"
@echo "Running in production mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up -d web
@docker compose ${ARGS} up -d firefox
@# Dont confuse this with the dbbackup make command below
@# This one runs the postgis-backup cron container
@# We add --no-recreate so that it does not destroy & recreate the db container
@docker compose ${ARGS} up --no-recreate --no-deps -d dbbackups
up: web
# Synonymous with web
permissions:
# Probably we want something more granular here....
# Your sudo password will be needed to set the file permissions
# on logs, media, static and pg dirs
@if [ ! -d "logs" ]; then mkdir logs; fi
@if [ ! -d "media" ]; then mkdir media; fi
@if [ ! -d "static" ]; then mkdir static; fi
@if [ ! -d "backups" ]; then mkdir backups; fi
@if [ -d "logs" ]; then sudo chmod -R a+rwx logs; fi
@if [ -d "media" ]; then sudo chmod -R a+rwx media; fi
@if [ -d "static" ]; then sudo chmod -R a+rwx static; fi
@if [ -d "pg" ]; then sudo chmod -R a+rwx pg; fi
@if [ -d "backups" ]; then sudo chmod -R a+rwx backups; fi
db:
@echo
@echo "------------------------------------------------------------------"
@echo "Running db in production mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up -d db
wait-db:
@docker compose ${ARGS} exec -T db su - postgres -c "until pg_isready; do sleep 5; done"
create-test-db:
@docker compose ${ARGS} exec -T db su - postgres -c "psql -c 'create database test_db;'"
@docker compose ${ARGS} exec -T db su - postgres -c "psql -d test_db -c 'create extension postgis;'"
nginx:
@echo
@echo "------------------------------------------------------------------"
@echo "Running nginx in production mode"
@echo "Normally you should use this only for testing"
@echo "In a production environment you will typically use nginx running"
@echo "on the host rather if you have a multi-site host."
@echo "------------------------------------------------------------------"
@docker compose up -d nginx
@echo "Site should now be available at http://localhost"
migrate:
@echo
@echo "------------------------------------------------------------------"
@echo "Running migrate static in production mode"
@echo "------------------------------------------------------------------"
@docker compose exec uwsgi python manage.py migrate
update-migrations:
@echo
@echo "------------------------------------------------------------------"
@echo "Running update migrations in production mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} exec uwsgi python manage.py makemigrations
collectstatic:
@echo
@echo "------------------------------------------------------------------"
@echo "Collecting static in production mode"
@echo "------------------------------------------------------------------"
#@docker compose run --rm uwsgi python manage.py collectstatic --noinputshel
#We need to run collect static in the same context as the running
# uwsgi container it seems so I use docker exec here
# no -it flag so we can run over remote shell
@docker compose ${ARGS} exec uwsgi python manage.py collectstatic --noinput ${CMD_ARGS} -i geoexplorer
reload:
@echo
@echo "------------------------------------------------------------------"
@echo "Reload django project in production mode"
@echo "------------------------------------------------------------------"
# no -it flag so we can run over remote shell
@docker compose exec uwsgi uwsgi --reload /tmp/django.pid
rebuildindex:
@echo
@echo "------------------------------------------------------------------"
@echo "Running rebuild_index in production mode"
@echo "------------------------------------------------------------------"
@docker compose run --rm uwsgi python manage.py rebuild_index
updateindex:
@echo
@echo "------------------------------------------------------------------"
@echo "Running update_index in production mode"
@echo "------------------------------------------------------------------"
@docker compose run --rm uwsgi python manage.py update_index
kill:
@echo
@echo "------------------------------------------------------------------"
@echo "Killing in production mode"
@echo "------------------------------------------------------------------"
@docker compose kill
rm: dbbackup rm-only
down:
# Synonymous with kill rm
@docker compose down
rm-only: kill
@echo
@echo "------------------------------------------------------------------"
@echo "Removing production instance!!! "
@echo "------------------------------------------------------------------"
@docker compose rm
logs:
@echo
@echo "------------------------------------------------------------------"
@echo "Showing uwsgi logs in production mode"
@echo "------------------------------------------------------------------"
@docker compose logs -f --tail=30 uwsgi
dblogs:
@echo
@echo "------------------------------------------------------------------"
@echo "Showing db logs in production mode"
@echo "------------------------------------------------------------------"
@docker compose logs -f --tail=30 db
nginxlogs:
@echo
@echo "------------------------------------------------------------------"
@echo "Showing nginx logs in production mode"
@echo "------------------------------------------------------------------"
@docker compose logs -f --tail=30 web
shell:
@echo
@echo "------------------------------------------------------------------"
@echo "Shelling in in production mode"
@echo "------------------------------------------------------------------"
@docker compose exec uwsgi /bin/bash
superuser:
@echo
@echo "------------------------------------------------------------------"
@echo "Creating a superuser in production mode"
@echo "------------------------------------------------------------------"
@docker compose exec uwsgi python manage.py createsuperuser
dbbash:
@echo
@echo "------------------------------------------------------------------"
@echo "Bashing in to production database"
@echo "------------------------------------------------------------------"
@docker compose exec db /bin/bash
dbsnapshot:
@echo
@echo "------------------------------------------------------------------"
@echo "Grab a quick snapshot of the database and place in the host filesystem"
@echo "------------------------------------------------------------------"
@docker compose exec db /bin/bash -c "PGPASSWORD=docker pg_dump -Fc -h localhost -U docker -f /tmp/$(COMPOSE_PROJECT_NAME)-snapshot.dmp gis"
@docker cp $(COMPOSE_PROJECT_NAME)-db:/tmp/$(COMPOSE_PROJECT_NAME)-snapshot.dmp .
@docker compose exec db /bin/bash -c "rm /tmp/$(COMPOSE_PROJECT_NAME)-snapshot.dmp"
@ls -lahtr *.dmp
dbschema:
@echo
@echo "------------------------------------------------------------------"
@echo "Print the database schema to stdio"
@echo "------------------------------------------------------------------"
@docker compose exec db /bin/bash -c "PGPASSWORD=docker pg_dump -s -h localhost -U docker gis"
dbshell:
@echo
@echo "------------------------------------------------------------------"
@echo "Shelling in in production database"
@echo "------------------------------------------------------------------"
@docker compose exec db psql -U docker -h localhost gis
dbrestore:
@echo
@echo "------------------------------------------------------------------"
@echo "Restore dump from backups/latest.dmp in production mode"
@echo "------------------------------------------------------------------"
@# - prefix causes command to continue even if it fails
-@docker compose exec db su - postgres -c "dropdb gis"
@docker compose exec db su - postgres -c "createdb -O docker -T template_postgis gis"
@docker compose exec db bash -c 'pg_restore /backups/latest.dmp | su - postgres -c "psql gis"'
db-fresh-restore:
@echo
@echo "------------------------------------------------------------------"
@echo "Restore dump from backups/latest.dmp in production mode"
@echo "------------------------------------------------------------------"
-@docker compose exec db su - postgres -c "dropdb gis"
@docker compose exec db su - postgres -c "createdb -O docker -T template_postgis gis"
@docker compose exec db su - postgres -c "psql gis -f /sql/bims-old.sql"
@docker compose exec db su - postgres -c "psql gis -f /sql/migration.sql"
dbbackup:
@echo
@echo "------------------------------------------------------------------"
@echo "Create `date +%d-%B-%Y`.dmp in production mode"
@echo "Warning: backups/latest.dmp will be replaced with a symlink to "
@echo "the new backup."
@echo "------------------------------------------------------------------"
@# - prefix causes command to continue even if it fails
@# Explicitly don't use -it so we can call this make target over a remote ssh session
@docker compose exec dbbackups /backups.sh
@docker compose exec dbbackups cat /var/log/cron.log | tail -2 | head -1 | awk '{print $4}'
-@if [ -f "backups/latest.dmp" ]; then rm backups/latest.dmp; fi
# backups is intentionally missing from front of first clause below otherwise symlink comes
# out with wrong path...
@ln -s `date +%Y`/`date +%B`/PG_$(COMPOSE_PROJECT_NAME)_gis.`date +%d-%B-%Y`.dmp deployment/backups/latest.dmp
@echo "Backup should be at: backups/`date +%Y`/`date +%B`/PG_$(COMPOSE_PROJECT_NAME)_gis.`date +%d-%B-%Y`.dmp"
sentry:
@echo
@echo "--------------------------"
@echo "Running sentry production mode"
@echo "--------------------------"
@docker compose up -d sentry
create-machine:
@echo
@echo "------------------------------------------------------------------"
@echo "Creating a docker machine."
@echo "------------------------------------------------------------------"
@docker-machine create -d virtualbox $(COMPOSE_PROJECT_NAME)
enable-machine:
@echo
@echo "------------------------------------------------------------------"
@echo "Enabling docker machine."
@echo "------------------------------------------------------------------"
@echo "eval \"$(docker-machine env freshwater)\""
sync: up
@docker compose ${ARGS} exec uwsgi python manage.py makemigrations --noinput --merge
@docker compose ${ARGS} exec uwsgi paver sync
sync-geonode:
@docker compose ${ARGS} exec uwsgi paver sync
sync-roles:
@echo
@echo "------------------------------------------------------------------"
@echo "Running update migrations in production mode"
@echo "------------------------------------------------------------------"
@docker compose run --rm uwsgi python manage.py sync_roles
gruntserver:
@echo
@echo "------------------------------------------------------------------"
@echo "Run grunt"
@echo "------------------------------------------------------------------"
@docker compose exec uwsgi bash -c "python manage.py gruntserver"
generate-boundaries:
@echo
@echo "------------------------------------------------------------------"
@echo "Generate boundaries"
@echo "------------------------------------------------------------------"
@docker compose exec uwsgi bash -c "python manage.py update_countries_boundary"
@docker compose exec uwsgi bash -c "python manage.py update_provinces_boundary"
@docker compose exec uwsgi bash -c "python manage.py update_districts_boundary"
@docker compose exec uwsgi bash -c "python manage.py update_municipals_boundary"
@docker compose exec uwsgi bash -c "python manage.py generate_boundary_geojson"
# ----------------------------------------------------------------------------
# DEVELOPMENT C O M M A N D S
# --no-deps will attach to prod deps if running
# after running you will have ssh and web ports open (see dockerfile for no's)
# and you can set your pycharm to use the python in the container
# Note that pycharm will copy in resources to the /root/ user folder
# for pydevd etc. If they dont get copied, restart pycharm...
# ----------------------------------------------------------------------------
devweb: db
@echo
@echo "------------------------------------------------------------------"
@echo "Running in DEVELOPMENT mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up --no-recreate --no-deps -d dev
sleep:
@echo
@echo "------------------------------------------------------------------"
@echo "Sleep for 50 seconds"
@echo "------------------------------------------------------------------"
@sleep 50
@echo "Done"
devweb-test:
@echo
@echo "------------------------------------------------------------------"
@echo "Running in DEVELOPMENT mode"
@echo "------------------------------------------------------------------"
@docker compose exec -T dev python manage.py test --keepdb --noinput
build-devweb: db
@echo
@echo "------------------------------------------------------------------"
@echo "Building devweb"
@echo "------------------------------------------------------------------"
@docker compose build dev
build-worker: db
@echo
@echo "------------------------------------------------------------------"
@echo "Building worker"
@echo "------------------------------------------------------------------"
@docker compose -f deployment/docker-compose.yml -p $(PROJECT_ID) up -d worker
reset-search-results:
@echo
@echo "------------------------------------------------------------------"
@echo "Reset search results"
@echo "------------------------------------------------------------------"
@docker compose exec uwsgi python /home/web/django_project/manage.py clear_search_results
@docker compose ${ARGS} restart searchworker
@docker compose ${ARGS} restart worker
@docker compose ${ARGS} restart geocontextworker
@docker compose ${ARGS} restart cache
# Run pep8 style checking
#http://pypi.python.org/pypi/pep8
pep8:
@echo
@echo "-----------"
@echo "PEP8 issues"
@echo "-----------"
@pep8 --version
@pep8 --repeat --ignore=E203,E121,E122,E123,E124,E125,E126,E127,E128,E402 --exclude='../django_project/.pycharm_helpers','../django_project/*/migrations/','../django_project/*/urls.py','../django_project/core/settings/secret.py' ../django_project || true
status:
@echo
@echo "--------------------------"
@echo "Show status of all containers"
@echo "--------------------------"
@docker compose ps
django-test:
@docker compose exec uwsgi python manage.py test --noinput --verbosity 3 bims
coverage-django-test:
@docker compose exec uwsgi coverage run -p --branch --source='.' manage.py test --noinput ${CMD_ARGS} bims
coverage-django-test-selenium:
@docker compose exec uwsgi coverage run -p --branch --source='.' manage.py test --noinput --keepdb ${CMD_ARGS} bims
update-taxa:
@echo
@echo "--------------------------"
@echo "Updating tax"
@echo "--------------------------"
@docker compose exec uwsgi python manage.py update_taxa
add-default-location-site-view:
@echo
@echo "---------------------------------------"
@echo "Add db view for default location site"
@echo "---------------------------------------"
@docker compose exec uwsgi python manage.py add_default_location_site_view
update-location-context-documents:
@echo
@echo "--------------------------"
@echo "Updating ALL location context documents"
@echo "--------------------------"
@docker compose exec uwsgi python manage.py update_location_context_documents
clear-location-context-documents:
@echo
@echo "--------------------------"
@echo "Clearing ALL location context documents"
@echo "--------------------------"
@docker compose exec uwsgi python manage.py clear_location_context_documents
add-location-context-group:
@echo
@echo "--------------------------"
@echo "Adding group to the location context documents"
@echo "--------------------------"
@docker compose exec uwsgi python manage.py add_location_context_group
update-ecological-data:
@echo
@echo "--------------------------"
@echo "Update ecological category and condition data from csv"
@echo "--------------------------"
@docker compose exec uwsgi python manage.py update_ecological_data
show-commit-and-branch:
@echo "--------------------------"
@echo "Update and show commit ID and branch name of this deployment on display"
@echo "--------------------------"
@git rev-parse HEAD > .commit.txt
@git rev-parse --abbrev-ref HEAD > .branch.txt
@git describe --tags --abbrev=0 > .tag.txt
# --------------- help --------------------------------
help:
@echo "* **build** - builds all required containers."
@echo "* **build-devweb** - build the development container. See [development notes](README-dev.md)."
@echo "* **collectstatic** - run the django collectstatic command."
@echo "* **create-machine** ."
@echo "* **db** - build and run the db container."
@echo "* **dbbackup** - make a snapshot of the database, saving it to deployments/backups/YYYY/MM/project-DDMMYYYY.dmp. It also creates a symlink to backups/latest.dmp for the latest backup."
@echo "* **dbbash** - open a bash shell inside the database container."
@echo "* **dblogs** - view the database logs."
@echo "* **dbrestore** - restore deployment/backups/latest.dmp over the active database. Will delete any existing data in your database and replace with the restore, so **use with caution**."
@echo "* **dbschema** - dump the current db schema (without data) to stdio. Useful if you want to compare changes between instances."
@echo "* **dbshell** - get a psql prompt into the db container. "
@echo "* **dbsnapshot** - as above but makes the backup as deployment/snapshot.smp - replacing any pre-existing snapshot."
@echo "* **dbsync** - use this from a development or offsite machine. It will rsync all database backups from deployment/backups to your offsite machine."
@echo "* **default** ."
@echo "* **deploy** ."
@echo "* **devweb** - create an ssh container derived from uwsgi that can be used as a remote interpreter for PyCharm. See [development notes](README-dev.md)."
@echo "* **enable-machine** - "
@echo "* **kill** - kills all running containers. Does not remove them."
@echo "* **logs** - view the logs of all running containers. Note that you can also view individual logs in the deployment/logs directory."
@echo "* **mailerrorlogs** - View the error logs from the mail server."
@echo "* **maillogs** - view the transaction logs from the mail server."
@echo "* **mediasync** - use this from a development or offsite machine. It will rsync all media backups from deployment/media to your offsite machine."
@echo "* **migrate** - run any pending migrations. "
@echo "* **nginx** - builds and runs the nginx container."
@echo "* **nginxlogs** - view just the nginx activity logs."
@echo "* **permissions** - Update the permissions of shared volumes. Note this will destroy any existing permissions you have in place."
@echo "* **reload** - reload the uwsgi process. Useful when you need django to pick up any changes you may have deployed."
@echo "* **rm** - remove all containers."
@echo "* **rm-only** - remove any containers without trying to kill them first. "
@echo "* **run** - builds and runs the complete orchestrated set of containers."
@echo "* **sentry** - **currently not working I think.** The idea is to spin up a sentry instance together with your app for fault reporting."
@echo "* **shell** - open a bash shell in the uwsgi (where django runs) container."
@echo "* **superuser** - create a django superuser account."
@echo "* **update-migrations** - freshen all migration definitions to match the current code base."
@echo "* **web** - same as **run** - runs the production site."
@echo "* **pep8** - Run Python PEP8 check."
@echo "* **status** - show status for all containers."