From 0f72add04a3244b39843a7afb29eb12135eec947 Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Jan 03 2020 12:11:29 +0000 Subject: Add docker-compose support Signed-off-by: Lukas Holecek --- diff --git a/.gitignore b/.gitignore index 6d83716..11ba7d4 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ docs/_build dist .ropeproject .pytest_cache/ +docker/home/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c90f076 --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 5425a8d..130df92 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,14 @@ against test results. ## 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 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7d8457a --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/docker/client_secrets.json b/docker/client_secrets.json new file mode 100644 index 0000000..1b0dd62 --- /dev/null +++ b/docker/client_secrets.json @@ -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" + } +} diff --git a/docker/dev-command.sh b/docker/dev-command.sh new file mode 100755 index 0000000..e4bda54 --- /dev/null +++ b/docker/dev-command.sh @@ -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 diff --git a/docker/dev.env b/docker/dev.env new file mode 100644 index 0000000..bd3f546 --- /dev/null +++ b/docker/dev.env @@ -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 diff --git a/docker/home/README.md b/docker/home/README.md new file mode 100644 index 0000000..dc51045 --- /dev/null +++ b/docker/home/README.md @@ -0,0 +1 @@ +This directory is used as home for dev user with docker-compose. diff --git a/docker/settings.py b/docker/settings.py new file mode 100644 index 0000000..4c62d5d --- /dev/null +++ b/docker/settings.py @@ -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' diff --git a/docker/waiverdb-db.env b/docker/waiverdb-db.env new file mode 100644 index 0000000..1ea5a31 --- /dev/null +++ b/docker/waiverdb-db.env @@ -0,0 +1,5 @@ +POSTGRES_USER=waiverdb +POSTGRES_PASSWORD=waiverdb +POSTGRES_DB=waiverdb +PGPORT=5433 +POSTGRES_INITDB_ARGS=--auth=ident --auth=trust