diff --git a/.drone.yml b/.drone.yml index 2b8052c0..18eecf3e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -121,16 +121,18 @@ pipeline: commands: - cd docker - cp drone_env/* env_files/ - - cp drone_env/.env . + - for x in drone_env/*; do cat $x >> .env; done; - docker-compose up -d app-drone - - docker-compose logs app-drone - # @todo #517:60m Fill drone-sided app with data. - # Do migrate and fixtures load. - # Currently we faced with issues here: - # https://ci.fidals.com/fidals/shopelectro/721/3 - # Then try to resurrect health check. + - docker-compose exec -T app-drone docker/wait-for.sh postgres:5432 -- python manage.py migrate + - docker-compose exec -T app-drone python manage.py custom_pages + # - docker-compose exec -T app-drone docker/wait-for.sh localhost:8000 -- docker/check-health.sh localhost:8000 + + # @todo #534:60m Fix CI's error for docker-up pipe. + # Any of below commands returns this: "/bin/sh: syntax error: unterminated quoted string" + # docker-compose exec -T app-drone nc -z google.com 80 > /dev/null 2>&1 + # docker-compose exec -T app-drone wget google.com + # docker-compose exec -T app-drone docker/check-health.sh localhost:8000 -# - docker-compose exec -T app-drone bash -c "./docker/check-health.sh http://0.0.0.0:8000/" volumes: - /var/run/docker.sock:/var/run/docker.sock when: @@ -140,9 +142,7 @@ pipeline: image: docker/compose:1.22.0 commands: - cd docker - - cp drone_env/* env_files/ - - cp drone_env/.env . - - docker-compose logs app + - docker-compose logs app-drone - docker-compose rm -fs volumes: - /var/run/docker.sock:/var/run/docker.sock diff --git a/docker/check-health.sh b/docker/check-health.sh index d86bb975..4008b81b 100755 --- a/docker/check-health.sh +++ b/docker/check-health.sh @@ -1,8 +1,11 @@ -#!/usr/bin/env bash - -set -e +#!/usr/bin/env sh URL=$1 -[[ $URL ]] || (echo "Specify an url to be checked as the first argument" && exit 1) + +if [ -z $URL ] +then + echo "Specify an url to be checked as the first argument" + exit 1 +fi wget -O- $URL -q | grep shopelectro.ru > /dev/null && echo "OK" || exit 1 diff --git a/docker/docker-compose-production.yml b/docker/docker-compose-production.yml index de95c791..89baa778 100644 --- a/docker/docker-compose-production.yml +++ b/docker/docker-compose-production.yml @@ -30,7 +30,7 @@ services: networks: - se-backend - se-frontend - command: gunicorn shopelectro.wsgi:application -c /etc/gunicorn.py -b 0.0.0.0:$VIRTUAL_HOST_PORT + command: ./docker/wait-for.sh postgres:5432 -- gunicorn shopelectro.wsgi:application -c /etc/gunicorn.py -b 0.0.0.0:$VIRTUAL_HOST_PORT celery-beat: image: fidals/se:prod diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 4c810a72..6ec52a50 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -18,8 +18,7 @@ services: - $VIRTUAL_HOST_LIVESERVER_PORT networks: - se-backend - - se-frontend - command: python manage.py runserver 0.0.0.0:$VIRTUAL_HOST_PORT + command: ./docker/wait-for.sh postgres:5432 -- python manage.py runserver 0.0.0.0:$VIRTUAL_HOST_PORT app-drone: extends: app-base @@ -66,8 +65,6 @@ services: - env_files/credentials networks: - se-backend - volumes: - - $POSTGRES_DATA_DIR:/var/lib/postgresql/data rabbitmq: image: rabbitmq:management-alpine @@ -142,4 +139,3 @@ services: networks: se-backend: - se-frontend: diff --git a/docker/drone_env/.env b/docker/drone_env/.env deleted file mode 100644 index d40c073c..00000000 --- a/docker/drone_env/.env +++ /dev/null @@ -1,57 +0,0 @@ -# Both .env and env_files/ are needed because of docker-compose realization. -# See good explanation here: -# https://github.com/docker/compose/issues/4223#issuecomment-280077263 -# Docs here: https://docs.docker.com/compose/environment-variables/#/the-env-file - -COMPOSE_PROJECT_NAME=drone_shopelectro -# It may affect on app logic. Valid values: LOCAL | CI | PROD -ENV_TYPE=LOCAL - -# App related settings -DJANGO_SETTINGS_MODULE=shopelectro.settings.local -DJANGO_LOG_LEVEL=INFO -SECRET_KEY=secret-key -STAGE_SECRET_KEY=another-secret-key - -# URL to required services -POSTGRES_URL=postgres -REDIS_URL=redis -REDIS_PORT=6379 -RABBITMQ_URL=rabbitmq -RABBITMQ_PORT=5672 - -SELENIUM_WAIT_SECONDS=60 -SELENIUM_TIMEOUT_SECONDS=30 - -# Credentials for app's services -POSTGRES_USER=postgres -POSTGRES_PASSWORD=pass -POSTGRES_DB=se_prod -POSTGRES_INITDB_XLOGDIR=/var/lib/postgresql/xlog -STAGE_DB_NAME=se_stage -RABBITMQ_DEFAULT_USER=user -RABBITMQ_DEFAULT_PASS=pass -REDIS_PASSWORD=pass - -# Credentials for ftp with files from 1C -FTP_PASS -FTP_USER=1c_exc -FTP_IP=37.18.77.165 - -# Credentials for yandex services -YANDEX_SHOP_PASS=pass -EMAIL_HOST_PASSWORD=pass -# Identify the dependencies folder -DEPS_DIR=/usr/local/lib/python3.7/site-packages -# Identify the source folder -SRC_DIR=/usr/app/src -SRC_DIR_PROD=/home/andre/prog/shopelectro - -REFARM_DIR=../../refarm-site -POSTGRES_DATA_DIR=/var/fidals/se_db -# Exposed ports -VIRTUAL_HOST_PORT=8000 -VIRTUAL_HOST_EXPOSE_PORT=8012 -VIRTUAL_HOST_LIVESERVER_PORT=8020-8030 -VIRTUAL_HOST_STAGE_PORT=8001 -VIRTUAL_HOST_STAGE_EXPOSE_PORT=8011 diff --git a/docker/drone_env/app b/docker/drone_env/app index 24892dc8..e4355e7f 100644 --- a/docker/drone_env/app +++ b/docker/drone_env/app @@ -5,7 +5,7 @@ COMPOSE_PROJECT_NAME=drone_shopelectro ENV_TYPE=LOCAL # App related settings -DJANGO_SETTINGS_MODULE=shopelectro.settings.local +DJANGO_SETTINGS_MODULE=shopelectro.settings.drone DJANGO_LOG_LEVEL=INFO SECRET_KEY=secret-key STAGE_SECRET_KEY=another-secret-key diff --git a/docker/images/python/Dockerfile.dev b/docker/images/python/Dockerfile.dev index 91f86e82..b5e675ee 100644 --- a/docker/images/python/Dockerfile.dev +++ b/docker/images/python/Dockerfile.dev @@ -2,9 +2,8 @@ FROM python:3.7-slim RUN apt-get update \ # wget is needed for working with ftp - # git is needed for pip - && apt-get install --no-install-recommends --no-install-suggests -y wget git make \ - && apt-get remove --purge -y git \ + # netcat is needed for waiting of postgres at `dc up -d app` + && apt-get install --no-install-recommends --no-install-suggests -y wget netcat \ && apt-get -y --purge autoremove \ && rm -rf /var/lib/apt/lists/* diff --git a/docker/wait-for.sh b/docker/wait-for.sh new file mode 100755 index 00000000..401196be --- /dev/null +++ b/docker/wait-for.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env sh + +# The MIT License (MIT) +# Copyright (c) 2016 Giles Hall + +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. + +TIMEOUT=15 +QUIET=0 + +echoerr() { + if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi +} + +usage() { + exitcode="$1" + cat << USAGE >&2 +Usage: + $cmdname host:port [-t timeout] [-- command args] + -q | --quiet Do not output any status messages + -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit "$exitcode" +} + +wait_for() { + for i in `seq $TIMEOUT` ; do + nc -z "$HOST" "$PORT" > /dev/null 2>&1 + + result=$? + if [ $result -eq 0 ] ; then + if [ $# -gt 0 ] ; then + exec "$@" + fi + exit 0 + fi + sleep 1 + done + echo "Operation timed out" >&2 + exit 1 +} + +while [ $# -gt 0 ] +do + case "$1" in + *:* ) + HOST=$(printf "%s\n" "$1"| cut -d : -f 1) + PORT=$(printf "%s\n" "$1"| cut -d : -f 2) + shift 1 + ;; + -q | --quiet) + QUIET=1 + shift 1 + ;; + -t) + TIMEOUT="$2" + if [ "$TIMEOUT" = "" ]; then break; fi + shift 2 + ;; + --timeout=*) + TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + break + ;; + --help) + usage 0 + ;; + *) + echoerr "Unknown argument: $1" + usage 1 + ;; + esac +done + +if [ "$HOST" = "" -o "$PORT" = "" ]; then + echoerr "Error: you need to provide a host and port to test." + usage 2 +fi + +wait_for "$@"