From b0859f0ceec0d856c04f54da341a634adc6c1128 Mon Sep 17 00:00:00 2001 From: Ivan Ogasawara Date: Wed, 25 May 2022 17:23:07 -0300 Subject: [PATCH] chore(docker): add postgresql service properly --- .env.tpl | 1 + Makefile | 16 +++++++++++---- docker/compose-dev.yaml | 24 ---------------------- docker/compose-prod.yaml | 7 ------- docker/{compose-base.yaml => compose.yaml} | 17 +++++++++++++++ docker/postgresql/dump.sh | 19 +++++++++++++++++ docker/postgresql/restore.sh | 0 7 files changed, 49 insertions(+), 35 deletions(-) delete mode 100644 docker/compose-dev.yaml delete mode 100644 docker/compose-prod.yaml rename docker/{compose-base.yaml => compose.yaml} (89%) create mode 100644 docker/postgresql/dump.sh create mode 100644 docker/postgresql/restore.sh diff --git a/.env.tpl b/.env.tpl index 14dcfc2e..431b859d 100644 --- a/.env.tpl +++ b/.env.tpl @@ -2,6 +2,7 @@ ANSIBLE_VAULT_KEY=${ANSIBLE_VAULT_KEY} ENV=${ENV} FLOWER_PORT=${FLOWER_PORT} FLOWER_PASSWORD=${FLOWER_PASSWORD} +HOST_POSTGRES_DUMP=${HOST_POSTGRES_DUMP} MAPBOX_API_KEY=${MAPBOX_API_KEY} POSTGRES_HOST=${POSTGRES_HOST} POSTGRES_PORT=${POSTGRES_PORT} diff --git a/Makefile b/Makefile index d0a34688..0cabf567 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,7 @@ CONSOLE:=bash DOCKER=docker-compose \ --env-file .env \ --project-name eph-$(ENV) \ - --file docker/compose-base.yaml \ - --file docker/compose-$(ENV).yaml + --file docker/compose.yaml # DOCKER @@ -21,7 +20,7 @@ docker-build: .PHONY:docker-start docker-start: - $(DOCKER) up -d ${SERVICES} + $(DOCKER) up --remove-orphans -d ${SERVICES} .PHONY:docker-stop @@ -29,14 +28,23 @@ docker-stop: $(DOCKER) stop ${SERVICES} +.PHONY:docker-down +docker-down: + $(DOCKER) down --volumes + + .PHONY:docker-restart docker-restart: docker-stop docker-start echo "[II] Docker services restarted!" +.PHONY:docker-logs-follow +docker-logs-follow: + $(DOCKER) logs --follow --tail 100 ${SERVICES} + .PHONY:docker-logs docker-logs: - $(DOCKER) logs --follow --tail 100 ${SERVICES} + $(DOCKER) logs --tail 200 ${SERVICES} .PHONY: docker-wait docker-wait: diff --git a/docker/compose-dev.yaml b/docker/compose-dev.yaml deleted file mode 100644 index 9308974c..00000000 --- a/docker/compose-dev.yaml +++ /dev/null @@ -1,24 +0,0 @@ -version: '3.4' -services: - - epigraphhub-superset: - depends_on: - - epigraphhub-db - - epigraphhub-celery - - epigraphhub-redis - - epigraphhub-flower - - epigraphhub-db: - environment: - # overwrite .env default variables - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres - - POSTGRES_DB=postgres - ports: - - ${POSTGRES_PORT}:25432 - healthcheck: - test: ["CMD-SHELL", "pg_isready -p ${POSTGRES_PORT} -U postgres"] - interval: 10s - timeout: 5s - retries: 10 - command: -p 25432 diff --git a/docker/compose-prod.yaml b/docker/compose-prod.yaml deleted file mode 100644 index 2ca72095..00000000 --- a/docker/compose-prod.yaml +++ /dev/null @@ -1,7 +0,0 @@ -version: '3.4' -services: - - epigraphhub-superset: - depends_on: - - epigraphhub-db - - epigraphhub-celery diff --git a/docker/compose-base.yaml b/docker/compose.yaml similarity index 89% rename from docker/compose-base.yaml rename to docker/compose.yaml index a236e22f..7efee62f 100644 --- a/docker/compose-base.yaml +++ b/docker/compose.yaml @@ -41,6 +41,7 @@ services: ports: - ${SUPERSET_PORT}:8088 depends_on: + - epigraphhub-db - epigraphhub-redis - epigraphhub-celery - epigraphhub-celery-beat @@ -136,6 +137,21 @@ services: timeout: 10s retries: 5 start_period: 40s # requires docker-compose 3.4 + volumes: + - pgdata:/var/lib/postgresql/data/pgdata + - ${HOST_POSTGRES_DUMP}:/tmp/dump + ports: + - ${POSTGRES_PORT}:5432 + environment: + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_USER: ${POSTGRES_USER} + PGDATA: /var/lib/postgresql/data/pgdata + healthcheck: + test: ["CMD-SHELL", "pg_isready -p ${POSTGRES_PORT} -U postgres"] + interval: 10s + timeout: 5s + retries: 10 + command: -p ${POSTGRES_PORT} epigraphhub-flower: image: mher/flower:0.9.7 @@ -161,3 +177,4 @@ services: volumes: redis: + pgdata: diff --git a/docker/postgresql/dump.sh b/docker/postgresql/dump.sh new file mode 100644 index 00000000..677e97a0 --- /dev/null +++ b/docker/postgresql/dump.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +PROJECT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd ../.. && pwd )" + +if [ -f ${PROJECT_DIR}/.env ]; then + # Load Environment Variables + export $(cat ${PROJECT_DIR}/.env | grep -v '#' | sed 's/\r$//' | awk '/=/ {print $1}' ) +fi + +HOST_POSTGRES_DUMP=${HOST_POSTGRES_DUMP:-/tmp/dump} +mkdir -p ${HOST_POSTGRES_DUMP} + +set -ex + +PGPASSWORD=${POSTGRES_PASSWORD} pg_dumpall \ + --host ${POSTGRES_HOST} \ + --port ${POSTGRES_PORT} \ + --user ${POSTGRES_USER} \ + > ${HOST_POSTGRES_DUMP}/$(date --iso-8601).dump diff --git a/docker/postgresql/restore.sh b/docker/postgresql/restore.sh new file mode 100644 index 00000000..e69de29b