wip -- core.deploy -- Scripts to deploy {www,dev}.cof

This commit is contained in:
Aurélien Delobelle 2019-01-07 22:36:54 +01:00
parent eb83c58f05
commit e6d47c2b25
2 changed files with 142 additions and 2 deletions

View file

@ -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-contrib libjpeg-dev nginx git redis-server
# Postgresql # Postgresql
sudo -u postgres createdb $DBNAME sudo -u postgres createdb $DBNAME || echo "Database creation: skipped"
sudo -u postgres createuser -SdR $DBUSER 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 "ALTER USER $DBUSER WITH PASSWORD '$DBPASSWD';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;" sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;"

140
provisioning/deploy.sh Normal file
View file

@ -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="<path/to/venv>"
elif [ "${TARGET_NAME}" = "dev.cof" ]; then
DJANGO_SETTINGS_MODULE="cof.settings.<WHICH ONE?>"
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="<path/to/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/<current branch>".
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