#368 Add docker-compose support
Merged 11 months ago by lholecek. Opened 11 months ago by lholecek.
lholecek/waiverdb docker-compose  into  master

Add docker-compose support
Lukas Holecek • 11 months ago  
file modified
+1
@@ -14,3 +14,4 @@ 

  dist

  .ropeproject

  .pytest_cache/

+ docker/home/

file added
+99
@@ -0,0 +1,99 @@ 

+ # Use podman-compose by default if available.

+ ifeq (, $(shell which podman-compose))

+     COMPOSE := docker-compose

+     PODMAN := docker

+ else

+     COMPOSE := podman-compose

+     PODMAN := podman

+ endif

+ 

+ BROWSER := xdg-open

+ SERVICE := dev

+ TEST_REQUIREMENTS := test-requirements.txt

+ 

+ PYTHON := python3

+ PIP := $(PYTHON) -m pip

+ PYTEST := $(PYTHON) -m pytest --color=yes

+ FLAKE8 := $(PYTHON) -m flake8

+ PYLINT := $(PYTHON) -m pylint

+ 

+ PYTEST_ARGS := tests functional-tests

+ 

+ all: help

+ 

+ help:

+ 	@echo 'Usage:'

+ 	@echo

+ 	@echo '  make up - starts containers in docker-compose environment'

+ 	@echo

+ 	@echo '  make down - stops containers in docker-compose environment'

+ 	@echo

+ 	@echo '  make build - builds container images for docker-compose environment'

+ 	@echo

+ 	@echo '  make recreate - recreates containers for docker-compose environment'

+ 	@echo

+ 	@echo '  make exec [CMD=".."] - executes command in dev container'

+ 	@echo

+ 	@echo '  make sudo [CMD=".."] - executes command in dev container under root user'

+ 	@echo

+ 	@echo '  make pytest [ARGS=".."] - executes pytest with given arguments in dev container'

+ 	@echo

+ 	@echo '  make flake8 - executes flake8 in dev container'

+ 	@echo

+ 	@echo '  make pylint - executes pylint in dev container'

+ 	@echo

+ 	@echo '  make test - alias for "make pytest flake8 pylint"'

+ 	@echo

+ 	@echo '  make coverage [ARGS=".."] - generates and shows test code coverage'

+ 	@echo

+ 	@echo 'Variables:'

+ 	@echo

+ 	@echo '  COMPOSE=docker-compose|podman-compose'

+ 	@echo '    - docker-compose or podman-compose command'

+ 	@echo '      (default is "podman-compose" if available)'

+ 	@echo

+ 	@echo '  PODMAN=docker|podman'

+ 	@echo '    - docker or podman command'

+ 	@echo '      (default is "podman" if "podman-compose" is available)'

+ 	@echo

+ 	@echo '  SERVICE={dev|waiverdb-db}'

+ 	@echo '    - service for which to run `make exec` and similar (default is "dev")'

+ 	@echo '      Example: make exec SERVICE=waiverdb-db CMD=flake8'

+ 

+ up:

+ 	$(COMPOSE) up -d

+ 

+ down:

+ 	$(COMPOSE) down

+ 

+ build:

+ 	$(COMPOSE) build

+ 

+ recreate:

+ 	$(COMPOSE) up -d --force-recreate

+ 

+ exec:

+ 	$(PODMAN) exec waiverdb_$(SERVICE)_1 bash -c '$(CMD)'

+ 

+ sudo:

+ 	$(PODMAN) exec -u root waiverdb_$(SERVICE)_1 bash -c '$(CMD)'

+ 

+ test: test_requirements pytest flake8 pylint

+ 

+ test_requirements:

+ 	$(MAKE) exec CMD="$(PIP) install --user -r $(TEST_REQUIREMENTS)"

+ 

+ pytest:

+ 	$(MAKE) exec \

+ 	    CMD="COVERAGE_FILE=/home/dev/.coverage-$(SERVICE) $(PYTEST) $(if $(ARGS),$(ARGS),$(PYTEST_ARGS))"

+ 

+ flake8:

+ 	$(MAKE) exec CMD="$(PIP) install --user flake8 && $(FLAKE8) waiverdb"

+ 

+ pylint:

+ 	$(MAKE) exec CMD="$(PIP) install --user pylint && $(PYLINT) waiverdb"

+ 

+ coverage:

+ 	$(MAKE) exec CMD="$(PIP) install --user pytest-cov"

+ 	$(MAKE) pytest ARGS="--cov-config .coveragerc --cov=waiverdb --cov-report html:/home/dev/htmlcov-$(SERVICE) $(ARGS)"

+ 	$(BROWSER) docker/home/htmlcov-$(SERVICE)/index.html

file modified
+8
@@ -10,6 +10,14 @@ 

  

  ## Quick development setup

  

+ The fastest way to setting up development environment is to install

+ docker-compose or podman-compose and use `make up` to provision required

+ containers and use `make test` to run tests or `make coverage` to generate and

+ open coverage report.

+ 

+ As alternative to using containers, below are steps to set up development

+ environment on local machine.

+ 

  Install dependencies:

  

      $ sudo dnf builddep waiverdb.spec

file added
+29
@@ -0,0 +1,29 @@ 

+ version: '2.1'

+ services:

+   dev:

+     build:

+       context: .

+       dockerfile: openshift/containers/waiverdb/Dockerfile

+     working_dir: /code

+     env_file: ["docker/dev.env"]

+     command: ["bash", "-c", "/start.sh"]

+     volumes:

+       - ./:/code:ro,Z

+       - ./docker/home:/home/dev:Z

+       - ./docker/dev-command.sh:/start.sh:ro,z

+       - ./docker/settings.py:/etc/waiverdb/settings.py:ro,z

+       - ./docker/client_secrets.json:/etc/secret/client_secrets.json:ro,z

+     ports:

+      - 5004:5004

+     user: '0'

+     depends_on:

+       - waiverdb-db

+ 

+   waiverdb-db:

+     image: postgres:9.5.2

+     restart: always

+     env_file: ["docker/waiverdb-db.env"]

+ 

+ networks:

+   default:

+     driver: bridge

@@ -0,0 +1,11 @@ 

+ {

+     "web": {

+         "redirect_uris": ["http://localhost:5004/"],

+         "token_uri": "https://iddev.fedorainfracloud.org/openidc/Token",

+         "auth_uri": "https://iddev.fedorainfracloud.org/openidc/Authorization",

+         "client_id": "D-e69a1ac7-30fa-4d18-9001-7468c4f34c3c",

+         "client_secret": "qgz8Bzjg6nO7JWCXoB0o8L49KfI5atLF",

+         "userinfo_uri": "https://iddev.fedorainfracloud.org/openidc/UserInfo",

+         "token_introspection_uri": "https://iddev.fedorainfracloud.org/openidc/TokenInfo"

+     }

+ }

@@ -0,0 +1,10 @@ 

+ #!/bin/bash

+ set -e

+ python3 waiverdb/manage.py wait-for-db

+ python3 waiverdb/manage.py db upgrade

+ exec /usr/bin/gunicorn-3 \

+   --reload \

+   --bind=0.0.0.0:5004 \

+   --access-logfile=- \

+   --enable-stdio-inheritance \

+   waiverdb.wsgi:app

file added
+5
@@ -0,0 +1,5 @@ 

+ DATABASE_PASSWORD=waiverdb

+ SECRET_KEY=waiverdb

+ WAIVERDB_CONFIG=/etc/waiverdb/settings.py

+ WAIVERDB_TEST_URL=http://waiverdb:5004/

+ PYTEST_ADDOPTS=-o cache_dir=/tmp/.pytest_cache

@@ -0,0 +1,1 @@ 

+ This directory is used as home for dev user with docker-compose.

file added
+13
@@ -0,0 +1,13 @@ 

+ import os

+ 

+ DATABASE_URI = 'postgresql+psycopg2://waiverdb:waiverdb@waiverdb-db:5433/waiverdb'

+ 

+ if os.getenv('TEST') == 'true':

+     DATABASE_URI += '_test'

+ 

+ HOST = '0.0.0.0'

+ PORT = 5004

+ AUTH_METHOD = 'dummy'

+ MESSAGE_BUS_PUBLISH = False

+ SUPERUSERS = ['dummy']

+ RESULTSDB_API_URL = 'http://resultsdb:5001/api/v2.0'

@@ -0,0 +1,5 @@ 

+ POSTGRES_USER=waiverdb

+ POSTGRES_PASSWORD=waiverdb

+ POSTGRES_DB=waiverdb

+ PGPORT=5433

+ POSTGRES_INITDB_ARGS=--auth=ident --auth=trust