diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9db96de..16898a7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -46,10 +46,17 @@ deploy: url: http://web.sik.party only: - develop + before_script: + - # Install ssh-agent if not already installed, it is required by Docker. + - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' + - eval $(ssh-agent -s) + - ssh-add <(echo "$SSH_PRIVATE_KEY") + - mkdir -p ~/.ssh + - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' script: - python -V - - pip install -r requirements.txt - - cp sikweb/settings-sample.py sikweb/default_settings.py - - cp sikweb/.ci-settings.py sikweb/settings.py - - python manage.py migrate --noinput - - python manage.py createdefaultadmin + - docker-compose build + - docker-compose push + - scp docker-compose.yaml $SSH_USER@web.sik.party:~/deployment/ + - ssh $SSH_USER@web.sik.party 'cd deployment && docker-compose pull web20:latest && docker-compose up -d' + diff --git a/.wait-for-it.sh b/.wait-for-it.sh new file mode 100755 index 0000000..bbe4043 --- /dev/null +++ b/.wait-for-it.sh @@ -0,0 +1,177 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +cmdname=$(basename $0) + +echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't 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 1 +} + +wait_for() +{ + if [[ $TIMEOUT -gt 0 ]]; then + echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" + else + echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" + fi + start_ts=$(date +%s) + while : + do + if [[ $ISBUSY -eq 1 ]]; then + nc -z $HOST $PORT + result=$? + else + (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1 + result=$? + fi + if [[ $result -eq 0 ]]; then + end_ts=$(date +%s) + echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" + break + fi + sleep 1 + done + return $result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $QUIET -eq 1 ]]; then + timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & + else + timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & + fi + PID=$! + trap "kill -INT -$PID" INT + wait $PID + RESULT=$? + if [[ $RESULT -ne 0 ]]; then + echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" + fi + return $RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + hostport=(${1//:/ }) + HOST=${hostport[0]} + PORT=${hostport[1]} + shift 1 + ;; + --child) + CHILD=1 + shift 1 + ;; + -q | --quiet) + QUIET=1 + shift 1 + ;; + -s | --strict) + STRICT=1 + shift 1 + ;; + -h) + HOST="$2" + if [[ $HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + HOST="${1#*=}" + shift 1 + ;; + -p) + PORT="$2" + if [[ $PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + PORT="${1#*=}" + shift 1 + ;; + -t) + TIMEOUT="$2" + if [[ $TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$HOST" == "" || "$PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +TIMEOUT=${TIMEOUT:-15} +STRICT=${STRICT:-0} +CHILD=${CHILD:-0} +QUIET=${QUIET:-0} + +# check to see if timeout is from busybox? +# check to see if timeout is from busybox? +TIMEOUT_PATH=$(realpath $(which timeout)) +if [[ $TIMEOUT_PATH =~ "busybox" ]]; then + ISBUSY=1 + BUSYTIMEFLAG="-t" +else + ISBUSY=0 + BUSYTIMEFLAG="" +fi + +if [[ $CHILD -gt 0 ]]; then + wait_for + RESULT=$? + exit $RESULT +else + if [[ $TIMEOUT -gt 0 ]]; then + wait_for_wrapper + RESULT=$? + else + wait_for + RESULT=$? + fi +fi + +if [[ $CLI != "" ]]; then + if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then + echoerr "$cmdname: strict mode, refusing to execute subprocess" + exit $RESULT + fi + exec "${CLI[@]}" +else + exit $RESULT +fi diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6f87337 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3 +ENV PYTHONUNBUFFERED 1 +ENV IS_DOCKER 1 +RUN mkdir /code +WORKDIR /code +ADD requirements.txt /code/ +RUN env +RUN pip install -r requirements.txt +ADD . /code/ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cf9ed4c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +version: '3' + +services: + db: + image: postgres + web: + build: . + image: 86.50.143.82:5000/web20 + command: ["bash", "-c", "./.wait-for-it.sh db:5432 -- bash setup.py --no-input"] + volumes: + - .:/code + ports: + - "8080:8080" + depends_on: + - db diff --git a/setup.sh b/setup.sh index 15aba08..1e295e3 100755 --- a/setup.sh +++ b/setup.sh @@ -5,7 +5,13 @@ echo "This script will set up the environment for this project." echo "=========================================================" echo "Dependencies: postgresql>9.5, python>3.5" -read -p "Are these programs installed? [y/n]" -n 1 -r +INTERACTIVE="true" +if [[ $* == *--no-input* ]] +then + INTERACTIVE="false" +fi + +$INTERACTIVE && read -p "Are these programs installed? [y/n]" -n 1 -r || REPLY="y" echo "" if [[ ! $REPLY =~ ^[Yy]$ ]] @@ -14,7 +20,7 @@ then exit 0 fi -read -p "Create user 'sik' in postgres (needs sudo) [y/n]?" -n 1 -r +$INTERACTIVE && read -p "Create user 'sik' in postgres (needs sudo) [y/n]?" -n 1 -r || REPLY="n" echo "" if [[ $REPLY =~ ^[Yy]$ ]] @@ -22,7 +28,7 @@ then sudo -u postgres psql < "$PWD/scripts/db/init.sql" fi -read -p "Is virtualenv activated? [y/n]" -n 1 -r +$INTERACTIVE && read -p "Is virtualenv activated? [y/n]" -n 1 -r || REPLY="y" echo "" if [[ ! $REPLY =~ ^[Yy]$ ]] @@ -31,7 +37,7 @@ then exit 0 fi -read -p "Copy settings from template? [y/n]" -n 1 -r +$INTERACTIVE && read -p "Copy settings from template? [y/n]" -n 1 -r || REPLY="y" echo "" if [[ $REPLY =~ ^[Yy]$ ]] @@ -40,7 +46,7 @@ then fi -read -p "Start setup? [y/n]" -n 1 -r +$INTERACTIVE && read -p "Start setup? [y/n]" -n 1 -r || REPLY="y" echo "" if [[ ! $REPLY =~ ^[Yy]$ ]] @@ -49,10 +55,12 @@ then exit 0 fi +set -e (set -x; pip install -r requirements.txt) (set -x; npm install) (set -x; python manage.py migrate) (set -x; python manage.py createdefaultadmin) +set +e echo "Done." echo "Run 'python manage.py runserver 0.0.0.0:8000' to start the development server!"