From 03f5ad9d109d650dc6d07573d818049918c63286 Mon Sep 17 00:00:00 2001 From: ronivay Date: Mon, 23 Aug 2021 16:20:32 +0300 Subject: [PATCH] implement self upgrade to the script --- sample.xo-install.cfg | 3 +++ xo-install.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/sample.xo-install.cfg b/sample.xo-install.cfg index b6b6e12..8c394d8 100644 --- a/sample.xo-install.cfg +++ b/sample.xo-install.cfg @@ -7,6 +7,9 @@ PORT="80" # Base dir for installation and future updates INSTALLDIR="/opt/xo" +# Script will update itself if there's a newer version available. This assumes that script inside a git directory and remote is ronivay/XenOrchestraInstallerUpdater +SELFUPGRADE=true + # Xen Orchestra configuration file is stored in XOUSER's home directory ($HOME/.config/xo-server/config.toml) and by default will be overwritten with every update done by this script. # You may disable this if you edit configuration by hand and don't want an update to overwrite it. Note that some of the options defined here won't be applied even if changed if this is set to false. # options: true/false diff --git a/xo-install.sh b/xo-install.sh index e2445a0..1a27048 100755 --- a/xo-install.sh +++ b/xo-install.sh @@ -21,6 +21,7 @@ fi source "$CONFIG_FILE" # Set some default variables if sourcing config file fails for some reason +SELFUPGRADE=${SELFUPGRADE:-"true"} PORT=${PORT:-80} INSTALLDIR=${INSTALLDIR:-"/opt/xo"} BRANCH=${BRANCH:-"master"} @@ -83,6 +84,31 @@ function CheckUser { } +# script self upgrade +function selfUpgrade { + + set -o pipefail + + if [[ "$SELFUPGRADE" != "true" ]]; then + return 0 + fi + + if [[ -d "$SCRIPT_DIR/.git" ]]; then + local REMOTE="$(runcmd_stdout "cd $SCRIPT_DIR && git config --get remote.origin.url")" + if [[ "$REMOTE" == *"ronivay/XenOrchestraInstallerUpdater"* ]]; then + runcmd "cd $SCRIPT_DIR && git fetch" + if [[ $(runcmd_stdout "cd $SCRIPT_DIR && git diff --name-only @{upstream}| grep xo-install.sh") ]]; then + printinfo "Newer version of script available, attempting to self upgrade" + runcmd "cd $SCRIPT_DIR && git pull --ff-only" && \ + { printok "Self upgrade done" ; exec "$SCRIPT_DIR/xo-install.sh" "$@"; } || \ + printfail "Failed to self upgrade. Check logs for more details" + fi + fi + fi + +} + + # log script version (git commit) and configuration variables to logfile function scriptInfo { @@ -1143,7 +1169,9 @@ if [[ $# == "0" ]]; then INTERACTIVE="true" fi + # these functions check specific requirements and are run everytime +selfUpgrade "$@" scriptInfo CheckUser CheckArch