Fix the upgrade production mechanism
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Sun, 17 May 2020 07:50:05 +0000 (09:50 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Sun, 17 May 2020 08:36:58 +0000 (10:36 +0200)
README.md
bin/upgrade-prod.sh

index d7218c4ebe40a03af92aae51989481a422cf882a..aed9170e53f1d0a46aee74ee3dfb1d8b28c0eff5 100644 (file)
--- a/README.md
+++ b/README.md
@@ -72,12 +72,13 @@ We will assume www-data is the UNIX user of your Web server. Adapt if necessary.
    export TRIVABBLE_SERVER_PATH=/opt/trivabble-server
    export TRIVABBLE_PUBLIC_PATH=/var/www/html/trivabble
 
-   sudo useradd trivabble
+   sudo groupadd trivabble
+   sudo useradd -G trivabble trivabble
    sudo mkdir -p "$TRIVABBLE_PUBLIC_PATH"
-   sudo chown trivabble:www-data "$TRIVABBLE_PUBLIC_PATH"
+   sudo chown www-data:www-data "$TRIVABBLE_PUBLIC_PATH"
    sudo chmod g+x "$TRIVABBLE_PUBLIC_PATH"
    sudo mkdir -p "$TRIVABBLE_SERVER_PATH"
-   sudo chown trivabble "$TRIVABBLE_PUBLIC_PATH"
+   sudo chown trivabble:trivabble "$TRIVABBLE_PUBLIC_PATH"
    ```
 
 2. Clone the repository somewhere (in your HOME directory for instance):
@@ -102,10 +103,10 @@ We will assume www-data is the UNIX user of your Web server. Adapt if necessary.
 
    Also make sure you still are in the trivabble repository.
 
-5. Run:
+5. Run, as a user having sudo privileges:
 
     ```sh
-    sudo -H -u www-data ./bin/upgrade-prod.sh --prod-public-dir "$TRIVABBLE_PUBLIC_PATH" --prod-server-dir "$TRIVABBLE_SERVER_PATH"
+    ./bin/upgrade-prod.sh --prod-public-dir "$TRIVABBLE_PUBLIC_PATH" --prod-server-dir "$TRIVABBLE_SERVER_PATH" --trivabble-chown trivabble:trivabble --webserver-chown www-data:www-data
     ```
 
     This will:
index b8e60a47c2cb8c80875de43c99cb572ea5727c4c..c5c78f73a8e5cb9e36c180d72314f11461fce577 100755 (executable)
@@ -19,23 +19,44 @@ The server is to be stored somewhere on your system that is not served by your H
 Parameters:
     --prod-public-dir DIR: the path to the public directory, from which your HTTP server will serve the client
     --prod-server-dir DIR: the path to the server directory, not served by your HTTP server.
+    --webserver-chown USER: the user:group chown string to use for the webserver directory
+    --trivabble-chown USER: the user:group chown string to use for the server directory
+    --no-git: do not attempt to fetch the last production revision
+    --allow-root: allow running the script as root. Must be the first parameter.
 EOF
     exit 0
 }
 
 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"
+    if [ "$1" = "--allow-root" ]; then
+        shift
+    else
+        error "Not running as root. Please run me as a normal user with sudo rights, using something like sudo -H -u user $0 $@"
+    fi
 fi
 
+NO_GIT=false
+
 while ! [ -z "${1+x}" ]; do
     if [ "$1" = "--prod-public-dir" ]; then
-        PROD_PUBLIC_DIR=$2
+        export PROD_PUBLIC_DIR=$2
         shift
         shift
     elif [ "$1" = "--prod-server-dir" ]; then
-        PROD_SERVER_DIR=$2
+        export PROD_SERVER_DIR=$2
+        shift
+        shift
+    elif [ "$1" = "--trivabble-chown" ]; then
+        export TRIVABBLE_CHOWN=$2
+        shift
+        shift
+    elif [ "$1" = "--webserver-chown" ]; then
+        export WEBSERVER_CHOWN=$2
         shift
         shift
+    elif [ "$1" = "--no-git" ]; then
+        export NO_GIT=true
+        shift
     elif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
         help
     else
@@ -51,6 +72,14 @@ if [ -z "${PROD_PUBLIC_DIR+x}" ]; then
    error "The public production directory is not set. Type --help for more information."
 fi
 
+if [ -z "${TRIVABBLE_CHOWN+x}" ]; then
+   error "The trivabble chown string is not set. Type --help for more information."
+fi
+
+if [ -z "${WEBSERVER_CHOWN+x}" ]; then
+   error "The webserver chown string is not set. Type --help for more information."
+fi
+
 if ! [ -d "$PROD_SERVER_DIR" ]; then
    error "The server production directory $PROD_SERVER_DIR does not exist. I give up."
 fi
@@ -75,19 +104,25 @@ 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)"
+if ! [ "${NO_GIT}" = "true" ]; then
+    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 "Checking out ${new_version}..."
-git checkout "$new_version"
+    # Re-execute the updated upgrade-prod.sh script
+    # Relies on environment variables being set in the current run
+    exec "$0" --allow-root --no-git
+fi
 
 echo "Setting up production mode..."
 "$(dirname $0)"/setup_prod.sh
 
 echo "Sending to production..."
-rsync -avp --delete --exclude 'config.js' "$WORKING_DIRECTORY/public/" "$PROD_PUBLIC_DIR/"
-rsync -avp --delete --exclude 'config.js' "$WORKING_DIRECTORY/server/" "$PROD_SERVER_DIR/"
+sudo rsync --chown="$WEBSERVER_CHOWN" -O -avp --delete --exclude 'config.js' "$WORKING_DIRECTORY/public/" "$PROD_PUBLIC_DIR/"
+sudo rsync --chown="$TRIVABBLE_CHOWN" -O -avp --delete --exclude 'games.backup.json' "$WORKING_DIRECTORY/server/" "$PROD_SERVER_DIR/"
 
 echo echo "Resetting the directory..."
-git reset --hard "$new_version"
+git reset --hard HEAD