Skip to content

Commit a3d7529

Browse files
yvessglaand
andauthored
Feat/docker setup improvements (#74)
Setup: * updated README.md for auto linked www.local.conf * check if nginx config www.local.conf symlink/file exits, otherwise create it automatically * set default SECRET_KEY for test or dev setup * remove need for symlink to node_modules, updated docker vue image fixes security issues, updated to docker image gemeindescan-webui-vue:0.3.8 * fixes import order error * needs optionals in docker image, updated docker vue image, vue composition api was also added before * renamed docker-compose.yml to docker-compose.dev.yml * added docker-compose.yml to ignore * added _DEV env vars to docker-compose.dev.yml for dev out of the box settings * Setup symlink for docker compose dev file to default * new alpine openresty image which has arm64 and amd64 images Testing: * added DOCKER_NOTTY option to Makefiles for test runs * added init-test to Makefile for noinput test setup, use bash in Makefile for more features (return true) * run django and vue tests in two separate steps for different reports * Fixed django & vue tests Cleanup: * deleted old not used make command for deployment Co-authored-by: André Glatzl <[email protected]>
1 parent 41e6ee3 commit a3d7529

File tree

22 files changed

+6072
-17403
lines changed

22 files changed

+6072
-17403
lines changed

.github/workflows/tests.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ jobs:
2727
- uses: actions/checkout@v2
2828

2929
- name: Setup
30-
run: touch env.hosts.prod && docker-compose pull && make up && make init
30+
run: ln -s docker-compose.dev.yml docker-compose.yml && docker-compose pull && make up && DOCKER_NOTTY=YES make init-test
3131

32-
# Runs a single command using the runners shell
33-
- name: Run tests
34-
run: make tests
32+
- name: Run django tests
33+
run: cd django && DOCKER_NOTTY=YES make tests
34+
35+
- name: Run vue tests
36+
run: cd vue && DOCKER_NOTTY=YES make tests

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ server.yml
66
docker-prod.yml
77
docker-compose-prod.yml
88
__pycache__/
9+
docker-compose.yml

Makefile

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ SHELL = /bin/bash
22
DOCKER_EXEC_DJANGO=$(shell command -v docker > /dev/null && echo "docker-compose exec django")
33
DOCKER_EXEC_VUE=$(shell command -v docker > /dev/null && echo "docker-compose exec vue")
44
DOCKER_EXEC_WWW=$(shell command -v docker > /dev/null && echo "docker-compose exec www")
5-
65
DOCKER_CRON_VUE=$(shell command -v docker > /dev/null && echo "docker-compose exec -T vue")
76
DOCKER_CRON_DJANGO=$(shell command -v docker > /dev/null && echo "docker-compose exec -T django")
87

@@ -11,7 +10,10 @@ DOCKER_CRON_DJANGO=$(shell command -v docker > /dev/null && echo "docker-compose
1110

1211
init:
1312
cd django && make init
14-
cd vue && make init
13+
14+
init-test:
15+
sleep 10 # wait for db setup
16+
cd django && make init-test
1517

1618
up:
1719
docker-compose up -d
@@ -38,7 +40,7 @@ enter_vue:
3840
$(DOCKER_EXEC_VUE) ash
3941

4042
enter_www:
41-
$(DOCKER_EXEC_WWW) ash
43+
$(DOCKER_EXEC_WWW) bash
4244

4345
start_all: up
4446
$(DOCKER_CRON_VUE) make &
@@ -57,23 +59,6 @@ start_screenshotservice:
5759
reload_www:
5860
$(DOCKER_EXEC_WWW) sh -c 'openresty -t & openresty -s reload'
5961

60-
61-
deploy_prod:
62-
cd vue && make build
63-
source env.hosts.prod && ssh $$DJANGO_PROD_HOST -t "cd $$DJANGO_PROD_PATH && git pull -v"
64-
source env.hosts.prod && ssh $$DJANGO_PROD_HOST -t "cd $$DJANGO_PROD_PATH && COMPOSE_FILE=$$COMPOSE_PROD docker-compose up -d"
65-
source env.hosts.prod && rsync -av --delete vue/dist $$DJANGO_PROD_HOST:$$VUE_PROD_PATH
66-
source env.hosts.prod && ssh $$DJANGO_PROD_HOST -t "cd $$DJANGO_PROD_PATH && COMPOSE_FILE=$$COMPOSE_PROD docker-compose exec django make migrate"
67-
source env.hosts.prod && ssh $$DJANGO_PROD_HOST -t "cd $$DJANGO_PROD_PATH && COMPOSE_FILE=$$COMPOSE_PROD docker-compose exec django killall -TERM gunicorn"
68-
69-
deploy_dev:
70-
cd vue && make build
71-
source env.hosts.prod && ssh $$DJANGO_DEV_HOST -t "cd $$DJANGO_DEV_PATH && git pull -v"
72-
source env.hosts.prod && ssh $$DJANGO_DEV_HOST -t "cd $$DJANGO_DEV_PATH && COMPOSE_FILE=$$COMPOSE_DEV docker-compose up -d"
73-
source env.hosts.prod && rsync -av --delete vue/dist $$DJANGO_DEV_HOST:$$VUE_DEV_PATH
74-
source env.hosts.prod && ssh $$DJANGO_DEV_HOST -t "cd $$DJANGO_DEV_PATH && COMPOSE_FILE=$$COMPOSE_DEV docker-compose exec django make migrate"
75-
source env.hosts.prod && ssh $$DJANGO_DEV_HOST -t "cd $$DJANGO_DEV_PATH && COMPOSE_FILE=$$COMPOSE_DEV docker-compose exec django killall -TERM gunicorn"
76-
7762
deploy_local:
7863
docker-compose up -d
7964
make -f vue/Makefile build-cron

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ For a tour of the app and a testing protocol see [`docs/testing.md`](./docs/test
3333
(1) Prepare local configuration
3434

3535
- Add `www.local` and `django` to the 127.0.0.1 entry in `/etc/hosts` (for screenshot service).
36-
- For local development add a symlink to the nginx configuration file, for production use a dedicated copy:
37-
```bash
38-
ln -s etc/nginx/www.local.dev etc/nginx/www.local.conf
39-
```
40-
- Create an `.env` file with the following environment variables:
36+
- A symlink to the nginx configuration file in the www container will be created automatically (`/etc/nginx/conf.d/www.local.conf`) for production use a dedicated copy
37+
- for production or custom settings add an `.env` file with the following environment variables:
4138
```bash
4239
DJANGO_SECRET=some.R4nd0m_k3y
4340
MAPBOX_TOKEN=a.mapbox.api.token
@@ -64,10 +61,6 @@ python3 manage.py createsuperuser
6461
make import-gemeinden-json
6562
# exit django container
6663
exit
67-
# enter vue container
68-
maker enter_vue
69-
ln -s /node_modules ./
70-
```
7164

7265
For development we use a named docker volume for the `var` folder inside the container, this named
7366
volume will be created automatically. This makes setup under windows easier.

django/Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
DJANGO_VERSION=3.1.4
22
DJANGO_VERSION_PREV=0.3.4
3-
DOCKER_EXEC=$(shell command -v docker > /dev/null && echo "docker-compose exec django")
3+
NOTTY=$(shell command [ "$$DOCKER_NOTTY" = YES ] && echo "-T")
4+
DOCKER_EXEC=$(shell command -v docker > /dev/null && echo "docker-compose exec $(NOTTY) django")
5+
SHELL := /bin/bash
6+
47
all: dev run-tests run-tests-dev
58
.PHONY: all
69
default: dev
@@ -26,11 +29,14 @@ runprod:
2629
adminuser:
2730
$(DOCKER_EXEC) python3 manage.py createsuperuser
2831

32+
adminuser-noinput:
33+
$(DOCKER_EXEC) python3 manage.py createsuperuser --noinput --username admin --email [email protected]
34+
2935
collectstatic:
3036
$(DOCKER_EXEC) python3 manage.py collectstatic --noinput
3137

3238
symlinks:
33-
$(DOCKER_EXEC) sh -c "cd /var/services/django; ln -s media downloads"
39+
$(DOCKER_EXEC) bash -c "mkdir -p /var/services/django/media && cd /var/services/django; [ ! -e /var/services/django/downloads ] && ln -s media downloads" || true
3440

3541
migrate:
3642
$(DOCKER_EXEC) python3 manage.py migrate
@@ -40,7 +46,7 @@ createcachetable:
4046
$(DOCKER_EXEC) python3 manage.py createcachetable
4147

4248
tests:
43-
$(DOCKER_EXEC) pytest --create-db
49+
$(DOCKER_EXEC) pytest --create-db -vvv
4450

4551
tests-dev:
4652
$(DOCKER_EXEC) pytest --reuse-db -vvv --nomigrations
@@ -60,3 +66,10 @@ init:
6066
make createcachetable
6167
make adminuser
6268
make import-gemeinden-json
69+
70+
init-test:
71+
make symlinks
72+
make migrate
73+
make createcachetable
74+
make adminuser-noinput
75+
make import-gemeinden-json

django/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@pytest.fixture(scope='session')
66
def django_db_setup(django_db_setup, django_db_blocker):
77
with django_db_blocker.unblock():
8-
call_command('loaddata', 'fixtures-tests/sites.site.json')
9-
call_command('loaddata', 'fixtures-tests/gsuser.user.json')
10-
call_command('loaddata', 'fixtures-tests/gsmap.municipality.json')
11-
call_command('loaddata', 'fixtures-tests/gsmap.snapshot.json')
8+
call_command('loaddata', '/opt/app/fixtures-tests/sites.site.json', verbosity=2)
9+
call_command('loaddata', '/opt/app/fixtures-tests/gsuser.user.json', verbosity=2)
10+
call_command('loaddata', '/opt/app/fixtures-tests/gsmap.municipality.json', verbosity=2)
11+
call_command('loaddata', '/opt/app/fixtures-tests/gsmap.snapshot.json', verbosity=2)

django/gsmap/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_snapshots_exists():
127127
'pk': 'R4RPGC',
128128
'title': 'test snapshot',
129129
'topic': 'test topic',
130-
'screenshot': ''
130+
'screenshot': None
131131
}
132132
}]
133133
}

django/main/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

3-
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
3+
# use your own secret_key, default for testing and dev
4+
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY') or os.getenv('DJANGO_SECRET_KEY_DEV')
45
DEBUG = os.getenv('DJANGO_DEBUG') == 'True'
56
USE_HTTPS = os.getenv('DJANGO_HTTPS') == 'True'
67

docker-compose.yml renamed to docker-compose.dev.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ services:
2828
- TERM=xterm-256color
2929
- START_SCREENSHOTSERVICE=NO
3030
- VUE_APP_MAPBOX_ACCESSTOKEN=${MAPBOX_TOKEN}
31+
- VUE_APP_MAPBOX_ACCESSTOKEN_DEV=pk.eyJ1IjoiZ2VtZWluZGVzY2FuIiwiYSI6ImNrNXdrOXV2ZjEwdXcza28wcXNoYXE1ZGMifQ.P-nCvxNRK4AOs7YdRbHCLg
3132
- VUE_APP_MAPBOX_DEFAULT_STYLES=${MAPBOX_STYLE}
33+
- VUE_APP_MAPBOX_DEFAULT_STYLES_DEV=mapbox://styles/gemeindescan/ck6rp249516tg1iqkmt48o4pz
3234
- VUE_APP_GRAPHQL_URI=http://www:8000/graphql/
3335
- VUE_APP_DJANGOBASEURL=http://www:8000
3436
- PUBLIC_URL=http://www:8000
@@ -50,6 +52,7 @@ services:
5052
- TERM=linux
5153
- START_DJANGO=NO
5254
- DJANGO_SECRET_KEY=${DJANGO_SECRET}
55+
- DJANGO_SECRET_KEY_DEV=adddxxccxx4rt0ioywg3yf5kjq64-t5
5356
- DJANGO_DB_NAME=webui
5457
- DJANGO_DB_USER=webui
5558
- DJANGO_DB_PASSWORD=pgpass
@@ -73,11 +76,15 @@ services:
7376
# command: -f /dev/null
7477

7578
www:
76-
image: openresty/openresty:1.15.8.3-alpine
77-
hostname: www
79+
extends:
80+
file: docker-images.yml
81+
service: www
7882
volumes:
7983
- ./etc/nginx:/etc/nginx/conf.d:Z
8084
- var-gemeindescan-webui:/var/services
8185
ports:
8286
- 8000:8000
87+
command: >
88+
sh -c '[ ! -e /etc/nginx/conf.d/www.local.conf ] && ln -s /etc/nginx/conf.d/www.local.dev /etc/nginx/conf.d/www.local.conf;
89+
/usr/local/openresty/bin/openresty -g "daemon off;"'
8390
# command: tail -f /dev/null

docker-images.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ services:
66
hostname: pdb
77

88
vue:
9-
image: smartuse/gemeindescan-webui-vue:0.3.7
9+
image: smartuse/gemeindescan-webui-vue:0.3.8
1010
hostname: vue
1111

1212
django:
1313
image: smartuse/gemeindescan-webui-django:3.1.4
1414
hostname: django
15+
16+
www:
17+
image: openresty/openresty:1.19.3.1-alpine-fat
18+
hostname: www

0 commit comments

Comments
 (0)