From e6d47c2b25df5a68fc1d0397927c903d220d99e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 7 Jan 2019 22:36:54 +0100 Subject: [PATCH] wip -- core.deploy -- Scripts to deploy {www,dev}.cof --- provisioning/bootstrap.sh | 4 +- provisioning/deploy.sh | 140 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 provisioning/deploy.sh diff --git a/provisioning/bootstrap.sh b/provisioning/bootstrap.sh index cb6917a7..5ba96d57 100644 --- a/provisioning/bootstrap.sh +++ b/provisioning/bootstrap.sh @@ -16,8 +16,8 @@ apt-get install -y python3-pip python3-dev python3-venv libpq-dev postgresql \ postgresql-contrib libjpeg-dev nginx git redis-server # Postgresql -sudo -u postgres createdb $DBNAME -sudo -u postgres createuser -SdR $DBUSER +sudo -u postgres createdb $DBNAME || echo "Database creation: skipped" +sudo -u postgres createuser -SdR $DBUSER || echo "Database user creation: skipped" sudo -u postgres psql -c "ALTER USER $DBUSER WITH PASSWORD '$DBPASSWD';" sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;" diff --git a/provisioning/deploy.sh b/provisioning/deploy.sh new file mode 100644 index 00000000..2492958d --- /dev/null +++ b/provisioning/deploy.sh @@ -0,0 +1,140 @@ +#!/bin/bash + +set -eu + +info() { echo "[INFO] $*"; } +warning() { echo "[WARNING] $*"; } +error() { echo "[ERROR] $*"; } +fatal() { echo "[FATAL] $*"; exit 1; } + +# Config +# +# Presets are available, and can be used with the env var "TARGET_NAME": +# - TARGET_NAME=www.cof for www.cof.ens.fr +# - TARGET_NAME=dev.cof for dev.cof.ens.fr + +TARGET_NAME="${TARGET_NAME:-dev.cof}" + +SERVICE_DIR="${SERVICE_DIR:-/etc/system/systemd}" + +if [ "${TARGET_NAME}" = "www.cof" ]; then + DJANGO_SETTINGS_MODULE="cof.settings.prod" + SERVICE_DAPHNE_PATH="${SERVICE_DIR}/gestiocof-daphne.service" + SERVICE_WORKER_PATH="${SERVICE_DIR}/gestiocof-worker.service" + SERVICE_LOCAL_DIR="./provisioning/www.cof/systemd" + VENV="" +elif [ "${TARGET_NAME}" = "dev.cof" ]; then + DJANGO_SETTINGS_MODULE="cof.settings." + SERVICE_DAPHNE_PATH="${SERVICE_DIR}/gestiocof-dev-daphne.service" + SERVICE_WORKER_PATH="${SERVICE_DIR}/gestiocof-dev-worker.service" + SERVICE_LOCAL_DIR="./provisioning/dev.cof/systemd" + VENV="" +fi + +SERVICE_LOCAL_DAPHNE_PATH="${SERVICE_LOCAL_DIR}/daphne.service" +SERVICE_LOCAL_WORKER_PATH="${SERVICE_LOCAL_DIR}/worker.service" +PYTHON="DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE} ${VENV}/bin/python" + +# Default to @{u}, equivalent to "origin/". +GIT_TARGET="${GIT_TARGET:-@{u}}" + +# Services + +service_print_changes() { + # TODO: One could check diff, print it and prompt user to confirm. + # info ">>> Services: diff" + return +} + +service_start() { + info "Starting service ${1}..." + sudo systemctl enable "${2}" + sudo systemctl start "${2}" +} + +service_stop() { + info "Stopping service ${1}... " + if sudo systemctl stop "${2}"; then info OK; else warning SKIPPED; fi +} + +service_upgrade_config() { + info "Upgrading config of service ${1}... " + sudo cp "${2}" "${3}" +} + +service_start_all() { + info ">>> Services: starting all..." + service_start Worker "${SERVICE_WORKER_PATH}" + service_start Daphne "${SERVICE_DAPHNE_PATH}" +} + +service_stop_all() { + info ">>> Services: stopping all..." + service_stop Worker "${SERVICE_WORKER_PATH}" + service_stop Daphne "${SERVICE_DAPHNE_PATH}" +} + +service_upgrade_config_all() { + info ">>> Services: upgrading all..." + service_upgrade Daphne "${SERVICE_LOCAL_DAPHNE_PATH}" "${SERVICE_DAPHNE_PATH}" + service_upgrade Worker "${SERVICE_LOCAL_WORKER_PATH}" "${SERVICE_WORKER_PATH}" + systemctl daemon-reload +} + +# Django + +django_migrate() { + info ">>> Django: applying database migrations..." + ${PYTHON} manage.py migrate +} + +django_collectstatic() { + info ">>> Django: collecting static assets and placing them in configured location..." + ${PYTHON} manage.py collectstatic +} + +# Git + +git_check_clean_workingtree() { + [ -z "$(git status --porcelain)" ] +} + +git_run_checks() { + info ">>> Git: running checks" + + info "- Checking working tree state... " + if ! git_check_clean_workingtree; then + git status -s + warning "The working tree is not cleaned. Some data could be lost." + warning "Stash or commit the changes." + read -rp "Do you want to continue? [y/N] " continue + [ "$continue" = y ] || fatal "Aborting!" + fi + + info "- Current commit is: $(git rev-parse HEAD)" + info "- New commit will be: ${GIT_TARGET}" +} + +git_upgrade() { + info ">>> Git: pulling latest version of remote branch" + # git fetch + # git reset --hard "${GIT_TARGET}" +} + +# Our job + +main() { + git_run_checks + service_stop_all + git_upgrade + # TODO: Handle failure of the following commands + service_print_changes + django_migrate + django_collectstatic + service_upgrade_all + service_start_all +} + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main "$@" +fi