JBAS-415 EG server updater script
authorBill Erickson <berickxx@gmail.com>
Wed, 21 Jan 2015 20:33:52 +0000 (15:33 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Script for applying and deploying Evergreen updates to an Evergreen
server.  Supports various ways to get the code from git (pulling,
brancheds, tags) and options for how to install the code (web files
only, full rebuild, install only).  See -h for usage.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/misc-scripts/eg-updater.sh [new file with mode: 0755]

diff --git a/KCLS/misc-scripts/eg-updater.sh b/KCLS/misc-scripts/eg-updater.sh
new file mode 100755 (executable)
index 0000000..64accc5
--- /dev/null
@@ -0,0 +1,325 @@
+#!/bin/bash
+set -euo pipefail
+# -------------------------------------------------------------------------
+# Author: Bill Erickson
+# Date: 2015-01-21
+#
+# Updates and deploys Evergreen code on an Ubuntu-based KCLS Evergreen server.
+# -------------------------------------------------------------------------
+
+# constants
+EVERGREEN_BASE="/home/opensrf/Evergreen"
+PING_FILE="/openils/var/web/ping.txt"
+DETACH_SLEEP=30
+OSRF="sudo -u opensrf"
+
+# run git commands from an opensrf login shell (-i) and use git -C to
+# set the Evergreen checkout directory, since -i drops you into $HOME.
+GIT="$OSRF -i git -C $EVERGREEN_BASE"
+
+# command line arguments
+GIT_TAG=""
+GIT_BRANCH=""
+GIT_PULL=""
+CLIENT_BUILD_ID=""
+GIT_REMOTE="kcls"
+SKIP_DETACH=""
+REATTACH=""
+SKIP_APACHE=""
+WEB_ONLY=""
+SKIP_REBUILD=""
+
+# if the script exits early, SUCCESS will be left as 0, 
+# which is our sign to warn the user.
+SUCCESS=0
+
+function usage {
+
+    cat <<USAGE
+
+        Synopsis:
+
+            Deploy the Git tag 'kcls-2.4.001', using a staff client build
+            ID of 'kcls-2.4.001'.
+
+            $0 -t kcls-2.4.001
+
+            Check out and update the git branch 'dev/berick/abc-123-foobar', 
+            then deploy with a staff client build ID of 'test-123'.
+
+            $0 -b dev/berick/abc-123-foobar -s test-123
+
+            Pull changes to the currently checked out branch, avoid 
+            detaching the brick, then copy only the web files into place.
+
+            $0  -d -w -p
+
+        Options:
+
+            -p 
+                'git pull' the currently checked out branch.
+
+            -t <tag-name>
+                Check out a copy of the specified git tag.
+
+            -b <branch-name>
+                Check out (or use an existing checkout) of the specified
+                git branch.  Beware this action is destructive.  It discards 
+                all local modifications to files in the checked out branch.
+
+            -s <staff-build-id>
+                Staff client build ID.  If -t is used and no -s is specified,
+                the value for -t will also be used as the build ID.
+
+            -r <git-remote>
+                Defaults to "kcls".
+
+            -d
+                Do NOT detach the server as one normally does for a brick.
+                This is useful for development servers.  The default is
+                to always detach.
+
+            -a
+                Automatically reattach the brick after the update.  This is 
+                useful if there is no desire to manually test the brick
+                after the update.  The default is to leave detached.
+                This is ignored if -d is specified.
+
+            -n
+                Do not stop/start Apache.  This is useful for non-brick
+                servers, like SIP, etc. servers.
+
+            -w 
+                Update web files only.  Instead of running the full make /
+                install dance, directly copy web files into place.  This
+                is ideal for rapid UI development.
+
+            -c
+                Skip the autoreconf, configure, and 'make clean' steps 
+                when building Evergreen.  These are generally only needed
+                for major upgrades.  This option is ignored when a branch
+                (-b) is used, since checking out a branch results in a 
+                'git reset --hard', forcing configure, etc. to run anyway.
+USAGE
+trap - EXIT
+exit;
+}
+
+# -- utility functions --
+
+function announce {
+    echo "$(date +'%F %T') $(hostname): $1"
+}
+
+# called when the script exits for any reason. 
+# if the script exits early, warn the user.
+function on_exit {
+    if [ $SUCCESS -ne 1 ]; then
+        announce "** $0 exited unexpectedly! **"
+    else
+        announce "$0 completed successfully"
+    fi
+}
+
+function inspect_params {
+
+    # parameter sanity checks
+    if [ -z "$GIT_TAG" -a -z "$GIT_BRANCH" -a -z "$GIT_PULL" ]; then
+        announce "Tag, branch, or pull must be specified"
+        usage;
+    fi;
+
+    # default to using the git tag if no client build ID is provided
+    [ -z "$CLIENT_BUILD_ID" -a -n "$GIT_TAG" ] && CLIENT_BUILD_ID="$GIT_TAG";
+
+    # more parameter sanity checks
+    if [ -z "$CLIENT_BUILD_ID" -a -z "$WEB_ONLY" ]; then
+        announce "Staff client build ID (-s) required if no tag is specified"
+        usage;
+    fi;
+
+    # checking out a branch causes a 'git reset --hard', which
+    # means that the full build has to be done regardless,
+    # so skip this option when GIT_BRANCH is set.
+    if [ -n "$GIT_BRANCH" -a -n "$SKIP_REBUILD" ]; then 
+        announce "-c param is ignored when a branch is specified"
+        SKIP_REBUILD=""; 
+    fi
+}
+
+function checkout_code {
+    LOCAL=$1  # local git target name
+    REMOTE=$2 # remote git target name
+    CURRENT=$($GIT rev-parse --abbrev-ref HEAD);
+
+    if [ "$CURRENT" == "heads/$LOCAL" ]; then
+        # git rev-parse shows tags as 'heads/$tagname'
+        CURRENT=$LOCAL
+    fi
+
+    set +e # temporarily disable exit-on-error setting
+    $GIT show-ref --verify --quiet "refs/heads/$LOCAL"
+    stat=$?
+    set -e
+
+    if [ $stat -ne 0 ]; then
+
+        # we have no local checkout for the requested target
+        announce "Checking out a new copy of $LOCAL"
+        $GIT checkout -f -b "$LOCAL" "$REMOTE" > /dev/null
+
+    elif [ "$CURRENT" != "$LOCAL" ]; then
+
+        # local checkout is available, but not checked out.
+        announce "Recovering existing checkout for $LOCAL"
+        $GIT checkout "$LOCAL" > /dev/null
+
+    else
+        announce "$LOCAL is already checked out"
+    fi;
+}
+
+function fetch_code {
+
+    # update local refs
+    $GIT fetch $GIT_REMOTE > /dev/null
+
+    if [ -n "$GIT_PULL" ]; then
+        announce "Pulling updates for $($GIT rev-parse --abbrev-ref HEAD)";
+        $GIT pull > /dev/null
+
+    elif [ -n "$GIT_TAG" ]; then
+        announce "Checking out tag $GIT_TAG"
+        checkout_code "$GIT_TAG" "$GIT_TAG"
+
+    elif [ -n "$GIT_BRANCH" ]; then
+        announce "Checking out branch $GIT_BRANCH"
+        checkout_code "$GIT_BRANCH" "$GIT_REMOTE/$GIT_BRANCH"
+
+        # discard uncommitted local changes and update 
+        # the local checkout to match the remote.
+        $GIT reset --hard "$GIT_REMOTE/$GIT_BRANCH" > /dev/null
+    fi;
+}
+
+# copy web-served files into place
+function deploy_web {
+    announce "Deploying web files"
+
+    $OSRF cp -r Open-ILS/web/* /openils/var/web/
+    $OSRF cp -r Open-ILS/src/templates/* /openils/var/templates/
+    $OSRF cp -r KCLS/openils/var/templates_kcls/* /openils/var/templates_kcls/
+    $OSRF cp -r Open-ILS/xul/staff_client/server/* /openils/var/web/xul/server/
+    $OSRF cp Open-ILS/examples/fm_IDL.xml /openils/conf/
+    $OSRF cp /openils/conf/fm_IDL.xml /openils/var/web/reports/
+}
+
+function deploy_code {
+
+    announce "Building and installing Evergreen"
+
+    if [ -n "$SKIP_REBUILD" ]; then
+        # pass PATH in case 'make' forces ./configure to run.
+        $OSRF PATH=$PATH:/openils/bin make > /dev/null
+    else
+
+        $OSRF autoreconf -i > /dev/null
+        $OSRF PATH=$PATH:/openils/bin ./configure --prefix=/openils \
+            --sysconfdir=/openils/conf \
+            --with-dbi=/usr/lib/x86_64-linux-gnu/dbd > /dev/null
+        $OSRF make clean all > /dev/null
+    fi
+
+    sudo make STAFF_CLIENT_STAMP_ID="$CLIENT_BUILD_ID" install > /dev/null
+    sudo chown -R opensrf:opensrf $EVERGREEN_BASE
+
+    announce "Running post-install steps"
+
+    set +e # chown returns non-zero on R/O NFS shares
+    sudo chown -R opensrf:opensrf /openils > /dev/null 2>&1;
+    set -e
+
+    $OSRF KCLS/misc-scripts/post-install-tasks.sh -b kcls-2.4.001 > /dev/null
+
+    $OSRF PATH=$PATH:/openils/bin \
+        /openils/bin/osrf_control --localhost --start-all > /dev/null
+
+    # give services a chance to start up
+    announce "Giving services a few seconds to start up"
+    sleep 5;
+
+    announce "Running autogen"
+    $OSRF /openils/bin/autogen.sh > /dev/null
+}
+
+# -- script starts here --
+
+trap on_exit EXIT;
+
+while getopts "t:b:r:s:pdanwch" opt; do
+    case $opt in
+        t) GIT_TAG="$OPTARG";;
+        b) GIT_BRANCH="$OPTARG";;
+        r) GIT_REMOTE="$OPTARG";;
+        s) CLIENT_BUILD_ID="$OPTARG";;
+        p) GIT_PULL="YES";;
+        d) SKIP_DETACH="YES";;
+        a) REATTACH="YES";;
+        n) SKIP_APACHE="YES";;
+        w) WEB_ONLY="YES";;
+        c) SKIP_REBUILD="YES";;
+        h|*) usage;
+    esac
+done;
+
+inspect_params;
+
+if [ -z "$SKIP_DETACH" ]; then
+
+    if [ ! -f $PING_FILE ]; then
+        announce "Detach requested, but no ping file exists at $PING_FILE"
+        announce "Try -d to skip the brick detach step."
+        exit;
+    fi;
+
+    announce "Detaching brick and sleeping $DETACH_SLEEP seconds"
+    $OSRF mv $PING_FILE $PING_FILE-
+    sleep $DETACH_SLEEP
+fi;
+
+cd $EVERGREEN_BASE;
+
+if [ -n "$WEB_ONLY" ]; then
+    # web-only updates don't require restarts.
+    fetch_code
+    deploy_web
+else 
+
+    announce "Stopping services"
+    $OSRF /openils/bin/osrf_control --localhost --stop-all > /dev/null
+
+    if [ -z "$SKIP_APACHE" ]; then
+        announce "Stopping Apache"
+        sudo service apache2 stop > /dev/null
+    fi
+
+    fetch_code;
+    deploy_code;
+
+    if [ -z "$SKIP_APACHE" ]; then
+        announce "Starting Apache"
+        sudo service apache2 start > /dev/null
+    fi;
+fi
+
+if [ -n "$REATTACH" -a -z "$SKIP_DETACH" ]; then
+    announce "Reattaching brick (after a short sleep)"
+    sleep 5;
+    $OSRF mv $PING_FILE- $PING_FILE
+fi;
+
+SUCCESS=1
+
+exit;
+
+