From 9712ec4e9fbfabb508bdc8880ce8332186f1b4d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Jakse?= Date: Mon, 13 Apr 2020 10:32:51 +0200 Subject: [PATCH] Add scripts to deploy to production --- bin/disable-const-let.sh | 17 +++++++++++ bin/setup_prod.sh | 3 ++ bin/upgrade-prod.sh | 63 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 bin/disable-const-let.sh create mode 100644 bin/setup_prod.sh create mode 100644 bin/upgrade-prod.sh diff --git a/bin/disable-const-let.sh b/bin/disable-const-let.sh new file mode 100644 index 0000000..1b9b510 --- /dev/null +++ b/bin/disable-const-let.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env sh + +# This is needed for compatibility with Safari 9, used in the iPad 2. +# This browser understands the code of Trivabble, except const and let. +# Let's sadly edit them out of javascript files on production to enable +# more people to play. + +WORKING_DIRECTORY="$(dirname "$(dirname "$0")")" + +if [ -d "$WORKING_DIRECTORY/.git" ]; then + if [ -z "$(git status --porcelain | grep '.js$')" ]; then + sed -i -E 's/(^|\(|\s)(let|const)(\s)/\1var\3/g' "$WORKING_DIRECTORY"/*.js + else + >&2 echo "You have untracked changes in JS files. I'm not going to mess up with your working copy." + exit 1 + fi +fi diff --git a/bin/setup_prod.sh b/bin/setup_prod.sh new file mode 100644 index 0000000..4a1578f --- /dev/null +++ b/bin/setup_prod.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +exec "$(dirname $0)/disable-const-let.sh" diff --git a/bin/upgrade-prod.sh b/bin/upgrade-prod.sh new file mode 100644 index 0000000..c52de1b --- /dev/null +++ b/bin/upgrade-prod.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env sh + +set -euf + +error() { + >&2 echo "$1" + exit 1 +} + +if [ -z "${1+x}" ]; then + error "Please provide the production directory in parameter." +fi + +if [ "$(id -u)" = "0" ]; then + error "Not running as root. Please run me as your webserver's user for instance, using something like sudo -H -u www-data $0 $1" +fi + +PROD_DIR="$1" + +if ! [ -d "$PROD_DIR" ]; then + error "The production directory $PROD_DIR does not exist. I give up." +fi + +if [ -d "$PROD_DIR/.git" ]; then + error "The production folder should not be a git repository." +fi + +WORKING_DIRECTORY="$(dirname "$(dirname "$0")")" + +if ! [ -d "$WORKING_DIRECTORY/.git" ]; then + error "Not a git repository. Cannot update." +fi + +if [ -f "$WORKING_DIRECTORY/games.backup.json" ]; then + error "Your working copy has a 'games.backup.json' file. I give up." +fi + +if [ -z "$(git status --porcelain)" ]; then + error "Your git repository is not clean. I am not doing anything." +fi + +# Thx https://stackoverflow.com/questions/17414104/git-checkout-latest-tag + +echo "Fetching updates from git" +git fetch +new_version="$(git tag --list '[prod]*' --sort=v:refname | tail -1)" + +echo "Checking out ${new_version}..." +git checkout "$new_version" + +echo "Setting up production mode..." +"$(dirname $0)"/setup_prod.sh + +BACKUP_DIR="$WORKING_DIRECTORY/../trivabble-prod.bak" + +echo "Backing up production to $BACKUP_DIR ..." +rsync -avp --delete "$PROD_DIR/" "$BACKUP_DIR/" + +echo "Sending to production..." +rsync -avp --delete --exclude 'games.backup.json' --exclude '/.git' "$WORKING_DIRECTORY/" "$PROD_DIR/" + +echo echo "Resetting the directory..." +git reset --hard "$new_version" -- 2.30.2