diff --git a/.gitignore b/.gitignore index e1382ad..80e1bbf 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ tests/* !tests/Debian/Vagrantfile !tests/run-tests.sh !tests/README.md +logs/* diff --git a/sample.xo-install.cfg b/sample.xo-install.cfg index 808d20e..32d5588 100644 --- a/sample.xo-install.cfg +++ b/sample.xo-install.cfg @@ -14,8 +14,8 @@ CONFIGUPDATE=true # Also, you can set this to "release" to use the latest tagged branch. BRANCH="master" -# Log path for possible errors -LOGFILE="$(dirname $0)/xo-install.log" +# Installation log path +LOGPATH="$(dirname $0)/logs" # Only one PLUGIN variable can be used at a time. Comment out the other one if you change these below. Comment out both if you don't want any plugins to be installed. diff --git a/xo-install.sh b/xo-install.sh index 2a27860..a258a60 100755 --- a/xo-install.sh +++ b/xo-install.sh @@ -21,7 +21,8 @@ source $CONFIG_FILE PORT=${PORT:-80} INSTALLDIR=${INSTALLDIR:-"/opt/xo"} BRANCH=${BRANCH:-"master"} -LOGFILE=${LOGFILE:-"$(dirname $0)/xo-install.log"} +LOGPATH="${LOGPATH:-$(dirname $0)/logs}" +LOGFILE="${LOGPATH}/xo-install.log-$(date +%Y%d%m%H%M)" AUTOUPDATE=${AUTOUPDATE:-"true"} PRESERVE=${PRESERVE:-"3"} XOUSER=${XOUSER:-"root"} @@ -43,17 +44,17 @@ PROGRESS="[${COLOR_BLUE}..${COLOR_N}]" # Protocol to use for webserver. If both of the X.509 certificate files exist, # then assume that we want to enable HTTPS for the server. -if [[ $PATH_TO_HTTPS_CERT ]] || [[ $PATH_TO_HTTPS_KEY ]]; then - if [[ -s $PATH_TO_HTTPS_CERT ]] && [[ -s $PATH_TO_HTTPS_KEY ]]; then - HTTPS=true - else - HTTPS=false - HTTPSFAIL="- certificate or Key doesn't exist or file is empty" - fi +if [[ -s $PATH_TO_HTTPS_CERT ]] && [[ -s $PATH_TO_HTTPS_KEY ]]; then + HTTPS=true else HTTPS=false fi +# create logpath if doesn't exist +if [[ ! -d $LOGPATH ]]; then + mkdir -p $LOGPATH +fi + function CheckUser { # Make sure the script is ran as root @@ -65,13 +66,19 @@ function CheckUser { } +function cmdlog { + echo "=== CMD ===: $@" >> $LOGFILE + echo >> $LOGFILE +} + function ErrorHandling { echo -e "${FAIL} Something went wrong, exiting. Check $LOGFILE for more details and use rollback feature if needed" if [[ -d $INSTALLDIR/xo-builds/xen-orchestra-$TIME ]]; then echo -e "${FAIL} Removing $INSTALLDIR/xo-builds/xen-orchestra-$TIME because of failed installation." - rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME + cmdlog "rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME" + rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME >> $LOGFILE 2>&1 fi } @@ -83,70 +90,84 @@ function InstallDependenciesCentOS { # Install necessary dependencies for XO build - #determine which python package is needed. CentOS 7 requires python, 8 is python3 - if [[ $OSVERSION == "8" ]]; then - PYTHON="python3" - else - PYTHON="python" - fi + #determine which python package is needed. CentOS 7 requires python, 8 is python3 + if [[ $OSVERSION == "8" ]]; then + PYTHON="python3" + else + PYTHON="python" + fi - # install packages - echo - echo -ne "${PROGRESS} Installing build dependencies, redis server, python, git, nfs-utils, cifs-utils" - yum -y install gcc gcc-c++ make openssl-devel redis libpng-devel $PYTHON git nfs-utils cifs-utils lvm2 >/dev/null - echo -e "\r${OK} Installing build dependencies, redis server, python, git, nfs-utils, cifs-utils" + # install packages + echo + echo -ne "${PROGRESS} Installing build dependencies, redis server, python, git, nfs-utils, cifs-utils" + cmdlog "yum -y install gcc gcc-c++ make openssl-devel redis libpng-devel $PYTHON git nfs-utils cifs-utils lvm2" + yum -y install gcc gcc-c++ make openssl-devel redis libpng-devel $PYTHON git nfs-utils cifs-utils lvm2 >>$LOGFILE 2>&1 + echo -e "\r${OK} Installing build dependencies, redis server, python, git, nfs-utils, cifs-utils" # only run automated node install if executable not found - if [[ -z $(which node) ]]; then + cmdlog "which node" + if [[ -z $(which node 2>>$LOGFILE) ]]; then echo echo -ne "${PROGRESS} Installing node.js" - curl -s -L https://rpm.nodesource.com/setup_12.x | bash - >/dev/null + cmdlog "curl -s -L https://rpm.nodesource.com/setup_12.x | bash -" + curl -s -L https://rpm.nodesource.com/setup_12.x | bash - >>$LOGFILE 2>&1 echo -e "\r${OK} Installing node.js" fi # only install yarn repo and package if not found - if [[ -z $(which yarn) ]] ; then + cmdlog "which yarn" + if [[ -z $(which yarn 2>>$LOGFILE) ]] ; then echo echo -ne "${PROGRESS} Installing yarn" - curl -s -o /etc/yum.repos.d/yarn.repo https://dl.yarnpkg.com/rpm/yarn.repo >/dev/null && \ - yum -y install yarn >/dev/null + cmdlog "curl -s -o /etc/yum.repos.d/yarn.repo https://dl.yarnpkg.com/rpm/yarn.repo && yum -y install yarn" + curl -s -o /etc/yum.repos.d/yarn.repo https://dl.yarnpkg.com/rpm/yarn.repo >>$LOGFILE 2>&1 && \ + yum -y install yarn >>$LOGFILE 2>&1 echo -e "\r${OK} Installing yarn" fi # only install epel-release if doesn't exist - if [[ -z $(rpm -q epel-release) ]] ; then + cmdlog "rpm -q epel-release" + if [[ -z $(rpm -q epel-release 2>>$LOGFILE) ]] ; then echo echo -ne "${PROGRESS} Installing epel-repo" - yum -y install epel-release >/dev/null + cmdlog "yum -y install epel-release" + yum -y install epel-release >>$LOGFILE 2>&1 echo -e "\r${OK} Installing epel-repo" fi # only install libvhdi-tools if vhdimount is not present - if [[ -z $(which vhdimount) ]] ; then + cmdlog "which vhdimount" + if [[ -z $(which vhdimount 2>>$LOGFILE) ]] ; then echo echo -ne "${PROGRESS} Installing libvhdi-tools from forensics repository" if [[ $OSVERSION == "7" ]]; then - rpm -ivh https://forensics.cert.org/cert-forensics-tools-release-el7.rpm >/dev/null + cmdlog "rpm -ivh https://forensics.cert.org/cert-forensics-tools-release-el7.rpm" + rpm -ivh https://forensics.cert.org/cert-forensics-tools-release-el7.rpm >>$LOGFILE 2>&1 fi if [[ $OSVERSION == "8" ]]; then - rpm -ivh https://forensics.cert.org/cert-forensics-tools-release-el8.rpm >/dev/null + cmdlog "rpm -ivh https://forensics.cert.org/cert-forensics-tools-release-el8.rpm" + rpm -ivh https://forensics.cert.org/cert-forensics-tools-release-el8.rpm >>$LOGFILE 2>&1 fi - sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/cert-forensics-tools.repo - yum --enablerepo=forensics install -y libvhdi-tools >/dev/null + cmdlog "sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/cert-forensics-tools.repo" + sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/cert-forensics-tools.repo >>$LOGFILE 2>&1 + cmdlog "yum --enablerepo=forensics install -y libvhdi-tools" + yum --enablerepo=forensics install -y libvhdi-tools >>$LOGFILE 2>&1 echo -e "\r${OK} Installing libvhdi-tools from forensics repository" fi echo echo -ne "${PROGRESS} Enabling and starting redis service" - /bin/systemctl enable redis >/dev/null && /bin/systemctl start redis >/dev/null + cmdlog "/bin/systemctl enable redis && /bin/systemctl start redis" + /bin/systemctl enable redis >>$LOGFILE 2>&1 && /bin/systemctl start redis >>$LOGFILE 2>&1 echo -e "\r${OK} Enabling and starting redis service" echo echo -ne "${PROGRESS} Enabling and starting rpcbind service" - /bin/systemctl enable rpcbind >/dev/null && /bin/systemctl start rpcbind >/dev/null + cmdlog "/bin/systemctl enable rpcbind && /bin/systemctl start rpcbind" + /bin/systemctl enable rpcbind >>$LOGFILE 2>&1 && /bin/systemctl start rpcbind >>$LOGFILE 2>&1 echo -e "\r${OK} Enabling and starting rpcbind service" -} 2>$LOGFILE +} function InstallDependenciesDebian { @@ -158,68 +179,84 @@ function InstallDependenciesDebian { if [[ $OSVERSION =~ (16|18|20) ]]; then echo -ne "${PROGRESS} OS Ubuntu so making sure universe repository is enabled" - add-apt-repository universe >/dev/null + cmdlog "add-apt-repository universe" + add-apt-repository universe >>$LOGFILE 2>&1 echo -e "\r${OK} OS Ubuntu so making sure universe repository is enabled" echo fi echo echo -ne "${PROGRESS} Running apt-get update" - apt-get update >/dev/null + cmdlog "apt-get update" + apt-get update >>$LOGFILE 2>&1 echo -e "\r${OK} Running apt-get update" - #determine which python package is needed. Ubuntu 20 requires python2-minimal, 16 and 18 are python-minimal - if [[ $OSVERSION == "20" ]]; then - PYTHON="python2-minimal" - else - PYTHON="python-minimal" - fi + #determine which python package is needed. Ubuntu 20 requires python2-minimal, 16 and 18 are python-minimal + if [[ $OSVERSION == "20" ]]; then + PYTHON="python2-minimal" + else + PYTHON="python-minimal" + fi - # install packages - echo - echo -ne "${PROGRESS} Installing build dependencies, redis server, git, libvhdi-utils, python-minimal, lvm2, nfs-common, cifs-utils, curl" - apt-get install -y build-essential redis-server libpng-dev git libvhdi-utils $PYTHON lvm2 nfs-common cifs-utils curl >/dev/null - echo -e "\r${OK} Installing build dependencies, redis server, python, git, libvhdi-utils, lvm2, nfs-common, cifs-utils, curl" + # install packages + echo + echo -ne "${PROGRESS} Installing build dependencies, redis server, git, libvhdi-utils, python-minimal, lvm2, nfs-common, cifs-utils, curl" + cmdlog "apt-get install -y build-essential redis-server libpng-dev git libvhdi-utils $PYTHON lvm2 nfs-common cifs-utils curl" + apt-get install -y build-essential redis-server libpng-dev git libvhdi-utils $PYTHON lvm2 nfs-common cifs-utils curl >>$LOGFILE 2>&1 + echo -e "\r${OK} Installing build dependencies, redis server, python, git, libvhdi-utils, lvm2, nfs-common, cifs-utils, curl" # Install apt-transport-https and ca-certificates because of yarn https repo url echo echo -ne "${PROGRESS} Installing apt-transport-https and ca-certificates packages to support https repos" - apt-get install -y apt-transport-https ca-certificates >/dev/null + cmdlog "apt-get install -y apt-transport-https ca-certificates" + apt-get install -y apt-transport-https ca-certificates >>$LOGFILE 2>&1 echo -e "\r${OK} Installing apt-transport-https and ca-certificates packages to support https repos" if [[ $OSVERSION == "10" ]]; then echo echo -ne "${PROGRESS} Debian 10, so installing gnupg also" - apt-get install gnupg -y >/dev/null + cmdlog "apt-get install gnupg -y" + apt-get install gnupg -y >>$LOGFILE 2>&1 echo -e "\r${OK} Debian 10, so installing gnupg also" fi # install setcap for non-root port binding if missing - if [[ -z $(which setcap) ]]; then + cmdlog "which setcap" + if [[ -z $(which setcap 2>>$LOGFILE) ]]; then echo echo -ne "${PROGRESS} Installing setcap" - apt-get install -y libcap2-bin >/dev/null + cmdlog "apt-get install -y libcap2-bin" + apt-get install -y libcap2-bin >>$LOGFILE 2>&1 echo -e "\r${OK} Installing setcap" fi # only install yarn repo and package if not found - if [[ -z $(which yarn) ]]; then + cmdlog "which yarn" + if [[ -z $(which yarn 2>>$LOGFILE) ]]; then echo echo -ne "${PROGRESS} Installing yarn" - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - >/dev/null - echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list >/dev/null - apt-get update >/dev/null - apt-get install -y yarn >/dev/null + cmdlog "curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -" + curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - >>$LOGFILE 2>&1 + cmdlog "echo \"deb https://dl.yarnpkg.com/debian/ stable main\" | tee /etc/apt/sources.list.d/yarn.list" + echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list >>$LOGFILE 2>&1 + cmdlog "apt-get update" + apt-get update >>$LOGFILE 2>&1 + cmdlog "apt-get install -y yarn" + apt-get install -y yarn >>$LOGFILE 2>&1 echo -e "\r${OK} Installing yarn" fi # only run automated node install if executable not found - if [[ -z $(which node) ]] || [[ -z $(which npm) ]]; then + cmdlog "which node" + cmdlog "which npm" + if [[ -z $(which node 2>>$LOGFILE) ]] || [[ -z $(which npm 2>>$LOGFILE) ]]; then echo echo -ne "${PROGRESS} Installing node.js" - curl -sL https://deb.nodesource.com/setup_12.x | bash - >/dev/null - apt-get install -y nodejs >/dev/null + cmdlog "curl -sL https://deb.nodesource.com/setup_12.x | bash -" + curl -sL https://deb.nodesource.com/setup_12.x | bash - >>$LOGFILE 2>&1 + cmdlog "apt-get install -y nodejs" + apt-get install -y nodejs >>$LOGFILE 2>&1 echo -e "\r${OK} Installing node.js" fi @@ -229,23 +266,27 @@ function InstallDependenciesDebian { if [[ -n $NODEV ]] && [[ $NODEV < 12 ]]; then echo echo -ne "${PROGRESS} Installing node.js" - curl -sL https://deb.nodesource.com/setup_12.x | bash - >/dev/null - apt-get install -y nodejs >/dev/null + cmdlog "curl -sL https://deb.nodesource.com/setup_12.x | bash -" + curl -sL https://deb.nodesource.com/setup_12.x | bash - >>$LOGFILE 2>&1 + cmdlog "apt-get install -y nodejs" + apt-get install -y nodejs >>$LOGFILE 2>&1 echo -e "\r${OK} Installing node.js" fi fi echo echo -ne "${PROGRESS} Enabling and starting redis service" - /bin/systemctl enable redis-server >/dev/null && /bin/systemctl start redis-server >/dev/null + cmdlog "/bin/systemctl enable redis-server && /bin/systemctl start redis-server" + /bin/systemctl enable redis-server >>$LOGFILE 2>&1 && /bin/systemctl start redis-server >>$LOGFILE 2>&1 echo -e "\r${OK} Enabling and starting redis service" echo echo -ne "${PROGRESS} Enabling and starting rpcbind service" - /bin/systemctl enable rpcbind >/dev/null && /bin/systemctl start rpcbind >/dev/null + cmdlog "/bin/systemctl enable rpcbind && /bin/systemctl start rpcbind" + /bin/systemctl enable rpcbind >>$LOGFILE 2>&1 && /bin/systemctl start rpcbind >>$LOGFILE 2>&1 echo -e "\r${OK} Enabling and starting rpcbind service" -} 2>$LOGFILE +} function UpdateNodeYarn { @@ -254,17 +295,19 @@ function UpdateNodeYarn { if [ $OSNAME == "CentOS" ]; then echo echo -ne "${PROGRESS} Checking updates for nodejs and yarn" - yum update -y nodejs yarn > /dev/null + cmdlog "yum update -y nodejs yarn" + yum update -y nodejs yarn >>$LOGFILE 2>&1 echo -e "\r${OK} Checking updates for nodejs and yarn" else echo echo -ne "${PROGRESS} Checking updates for nodejs and yarn" - apt-get install -y --only-upgrade nodejs yarn > /dev/null + cmdlog "apt-get install -y --only-upgrade nodejs yarn" + apt-get install -y --only-upgrade nodejs yarn >>$LOGFILE 2>&1 echo -e "\r${OK} Checking updates for nodejs and yarn" fi fi -} 2>$LOGFILE +} function InstallXOPlugins { @@ -277,6 +320,7 @@ function InstallXOPlugins { if [[ "$PLUGINS" == "all" ]]; then echo echo -ne "${PROGRESS} Installing plugins" + cmdlog "find \"$INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/\" -maxdepth 1 -mindepth 1 -not -name \"xo-server\" -not -name \"xo-web\" -not -name \"xo-server-cloud\" -exec ln -sn {} \"$INSTALLDIR/xo-builds/xen-orchestra-$TIME/\"" find "$INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/" -maxdepth 1 -mindepth 1 -not -name "xo-server" -not -name "xo-web" -not -name "xo-server-cloud" -exec ln -sn {} "$INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/node_modules/" \; else echo @@ -284,19 +328,21 @@ function InstallXOPlugins { local PLUGINSARRAY=($(echo "$PLUGINS" | tr ',' ' ')) for x in "${PLUGINSARRAY[@]}"; do if [[ $(find $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages -type d -name "$x") ]]; then - ln -sn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/$x $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/node_modules/ + cmdlog "ln -sn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/$x $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/node_modules/" + ln -sn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/$x $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/node_modules/ >>$LOGFILE 2>&1 fi done fi - cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME && yarn >/dev/null && yarn build >/dev/null + cmdlog "cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME && yarn >/dev/null && yarn build" + cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME && yarn >/dev/null && yarn build >>$LOGFILE 2>&1 echo -e "\r${OK} Installing plugins" else echo echo -e "${INFO} No plugins to install" fi -} 2>$LOGFILE +} function InstallXO { @@ -312,7 +358,8 @@ function InstallXO { if [[ -z $(getent passwd $XOUSER) ]]; then echo echo -ne "${PROGRESS} Creating missing $XOUSER user" - useradd -s /sbin/nologin $XOUSER + cmdlog "useradd -s /sbin/nologin $XOUSER" + useradd -s /sbin/nologin $XOUSER >>$LOGFILE 2>&1 echo -e "\r${OK} Creating missing $XOUSER user" sleep 2 fi @@ -321,6 +368,7 @@ function InstallXO { # Create installation directory if doesn't exist already if [[ ! -d "$INSTALLDIR" ]] ; then echo -ne "${PROGRESS} Creating missing basedir to $INSTALLDIR" + cmdlog "mkdir -p \"$INSTALLDIR\"" mkdir -p "$INSTALLDIR" echo -e "\r${OK} Creating missing basedir to $INSTALLDIR" fi @@ -328,6 +376,7 @@ function InstallXO { # Create missing xo-builds directory if doesn't exist already if [[ ! -d "$INSTALLDIR/xo-builds" ]]; then echo -ne "${PROGRESS} Creating missing xo-builds directory to $INSTALLDIR/xo-builds" + cmdlog "mkdir \"$INSTALLDIR/xo-builds\"" mkdir "$INSTALLDIR/xo-builds" echo -e "\r${OK} Creating missing xo-builds directory to $INSTALLDIR/xo-builds" fi @@ -336,54 +385,75 @@ function InstallXO { echo -e "${INFO} Fetching Xen Orchestra source code" echo if [[ ! -d "$XO_SRC_DIR" ]]; then + cmdlog "mkdir -p \"$XO_SRC_DIR\"" mkdir -p "$XO_SRC_DIR" - git clone https://github.com/vatesfr/xen-orchestra "$XO_SRC_DIR" + cmdlog "git clone https://github.com/vatesfr/xen-orchestra \"$XO_SRC_DIR\"" + git clone https://github.com/vatesfr/xen-orchestra "$XO_SRC_DIR" >>$LOGFILE 2>&1 else - cd "$XO_SRC_DIR" - git pull - cd $(dirname $0) + cmdlog "cd \"$XO_SRC_DIR\"" + cd "$XO_SRC_DIR" >>$LOGFILE 2>&1 + cmdlog "git pull" + git pull >>$LOGFILE 2>&1 + cd $(dirname $0) >>$LOGFILE 2>&1 + cmdlog "cd $(dirname $0)" fi # Deploy the latest xen-orchestra source to the new install directory. echo echo -ne "${INFO} Creating install directory: $INSTALLDIR/xo-builds/xen-orchestra-$TIME" - rm -rf "$INSTALLDIR/xo-builds/xen-orchestra-$TIME" - cp -r "$XO_SRC_DIR" "$INSTALLDIR/xo-builds/xen-orchestra-$TIME" + cmdlog "rm -rf \"$INSTALLDIR/xo-builds/xen-orchestra-$TIME\"" + rm -rf "$INSTALLDIR/xo-builds/xen-orchestra-$TIME" >>$LOGFILE 2>&1 + cmdlog "cp -r \"$XO_SRC_DIR" "$INSTALLDIR/xo-builds/xen-orchestra-$TIME\"" + cp -r "$XO_SRC_DIR" "$INSTALLDIR/xo-builds/xen-orchestra-$TIME" >>$LOGFILE 2>&1 if [[ "$BRANCH" == "release" ]]; then + cmdlog "cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME" cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) echo echo -e "${INFO} Checking out latest tagged release '$TAG'" - git checkout $TAG 2> /dev/null # Suppress the detached-head message. + cmdlog "git checkout $TAG" + git checkout $TAG >>$LOGFILE 2>&1 + cmdlog "cd $(dirname $0)" cd $(dirname $0) elif [[ "$BRANCH" != "master" ]]; then echo echo -e "${INFO} Checking out source code from branch '$BRANCH'" - cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME - git checkout $BRANCH - cd $(dirname $0) + cmdlog "cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME" + cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME >>$LOGFILE 2>&1 + cmdlog "git checkout $BRANCH" + git checkout $BRANCH >>$LOGFILE 2>&1 + cmdlog "cd $(dirname $0)" + cd $(dirname $0) >>$LOGFILE 2>&1 fi # Check if the new repo is any different from the currently-installed # one. If not, then skip the build and delete the repo we just cloned. # Get the commit ID of the to-be-installed xen-orchestra. + cmdlog "cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME" cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME - NEW_REPO_HASH=$(git rev-parse HEAD) - NEW_REPO_HASH_SHORT=$(git rev-parse --short HEAD) - cd $(dirname $0) + cmdlog "git rev-parse HEAD" + NEW_REPO_HASH=$(git rev-parse HEAD 2>>$LOGFILE) + cmdlog "git rev-parse --short HEAD" + NEW_REPO_HASH_SHORT=$(git rev-parse --short HEAD 2>>$LOGFILE) + cmdlog "cd $(dirname $0)" + cd $(dirname $0) >>$LOGFILE 2>&1 # Get the commit ID of the currently-installed xen-orchestra (if one # exists). if [[ -L $INSTALLDIR/xo-server ]] && [[ -n $(readlink -e $INSTALLDIR/xo-server) ]]; then - cd $INSTALLDIR/xo-server - OLD_REPO_HASH=$(git rev-parse HEAD) - OLD_REPO_HASH_SHORT=$(git rev-parse --short HEAD) - cd $(dirname $0) + cmdlog "cd $INSTALLDIR/xo-server" + cd $INSTALLDIR/xo-server >>$LOGFILE 2>&1 + cmdlog "git rev-parse HEAD" + OLD_REPO_HASH=$(git rev-parse HEAD 2>>$LOGFILE) + cmdlog "git rev-parse --short HEAD" + OLD_REPO_HASH_SHORT=$(git rev-parse --short HEAD 2>>$LOGFILE) + cmdlog "cd $(dirname $0)" + cd $(dirname $0) >>$LOGFILE 2>&1 else # If there's no existing installation, then we definitely want # to proceed with the bulid. @@ -397,7 +467,8 @@ function InstallXO { echo echo -e "${INFO} No changes to xen-orchestra since previous install. Skipping xo-server and xo-web build." echo -e "${INFO} Cleaning up install directory: $INSTALLDIR/xo-builds/xen-orchestra-$TIME" - rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME + cmdlog "rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME" + rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME >>$LOGFILE 2>&1 return 0 fi @@ -406,6 +477,7 @@ function InstallXO { if [[ $(pgrep -f xo-server) ]]; then echo echo -ne "${PROGRESS} Shutting down xo-server" + cmdlog "/bin/systemctl stop xo-server" /bin/systemctl stop xo-server || { echo -e "${FAIL} failed to stop service, exiting..." ; exit 1; } echo -e "\r${OK} Shutting down xo-server" fi @@ -420,7 +492,8 @@ function InstallXO { echo -e "${INFO} xo-server and xo-web build quite a while. Grab a cup of coffee and lay back" echo echo -ne "${PROGRESS} Running installation" - cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME && yarn >/dev/null && yarn build >/dev/null + cmdlog "cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME && yarn && yarn build" + cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME >>$LOGFILE 2>&1 && yarn >>$LOGFILE 2>&1 && yarn build >>$LOGFILE 2>&1 echo -e "\r${OK} Running installation" # Install plugins @@ -428,14 +501,17 @@ function InstallXO { echo echo -e "${INFO} Fixing binary path in systemd service configuration file" + cmdlog "sed -i \"s#ExecStart=.*#ExecStart=$INSTALLDIR\/xo-server\/bin\/xo-server#\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service" sed -i "s#ExecStart=.*#ExecStart=$INSTALLDIR\/xo-server\/bin\/xo-server#" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service echo echo -e "${INFO} Adding WorkingDirectory parameter to systemd service configuration file" - sed -i "/ExecStart=.*/a WorkingDirectory=$INSTALLDIR/xo-server" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service + cmdlog "sed -i \"/ExecStart=.*/a WorkingDirectory=$INSTALLDIR/xo-server\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service" + sed -i "/ExecStart=.*/a WorkingDirectory=$INSTALLDIR/xo-server" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service >>$LOGFILE 2>&1 if [[ "$XOUSER" != "root" ]]; then echo -e "${INFO} Adding user to systemd config" - sed -i "/SyslogIdentifier=.*/a User=$XOUSER" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service + cmdlog "sed -i \"/SyslogIdentifier=.*/a User=$XOUSER\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service" + sed -i "/SyslogIdentifier=.*/a User=$XOUSER" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service >>$LOGFILE 2>&1 if [ "$PORT" -le "1024" ]; then NODEBINARY="$(which node)" @@ -445,7 +521,8 @@ function InstallXO { if [[ -n $NODEBINARY ]]; then echo -ne "${PROGRESS} Attempting to set cap_net_bind_service permission for $NODEBINARY" - setcap 'cap_net_bind_service=+ep' $NODEBINARY >/dev/null \ + cmdlog "setcap 'cap_net_bind_service=+ep' $NODEBINARY" + setcap 'cap_net_bind_service=+ep' $NODEBINARY >>$LOGFILE 2>&1 \ && echo -e "\r${OK} Attempting to set cap_net_bind_service permission for $NODEBINARY" || { echo -e "\r${FAIL} Attempting to set cap_net_bind_service permission for $NODEBINARY" ; echo " Non-privileged user might not be able to bind to <1024 port. xo-server won't start most likely" ; } else echo -e "${FAIL} Can't find node executable, or it's a symlink to non existing file. Not trying to setcap. xo-server won't start most likely" @@ -458,25 +535,32 @@ function InstallXO { echo -e ${INFO} "Fixing relative path to xo-web installation in xo-server configuration file" INSTALLDIRESC=$(echo $INSTALLDIR | sed 's/\//\\\//g') - sed -i "s/#'\/any\/url' = '\/path\/to\/directory'/'\/' = '$INSTALLDIRESC\/xo-web\/dist\/'/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml + cmdlog "sed -i \"s/#'\/any\/url' = '\/path\/to\/directory'/'\/' = '$INSTALLDIRESC\/xo-web\/dist\/'/\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml" + sed -i "s/#'\/any\/url' = '\/path\/to\/directory'/'\/' = '$INSTALLDIRESC\/xo-web\/dist\/'/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml >>$LOGFILE 2>&1 sleep 2 if [[ $PORT != "80" ]]; then echo -e "${INFO} Changing port in xo-server configuration file" - sed -i "s/port = 80/port = $PORT/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml + cmdlog "sed -i \"s/port = 80/port = $PORT/\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml" + sed -i "s/port = 80/port = $PORT/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml >>$LOGFILE 2>&1 sleep 2 fi - if $HTTPS ; then - echo -e "${INFO} Enabling HTTPS in xo-server configuration file" - sed -i "s%# cert = '.\/certificate.pem'%cert = '$PATH_TO_HTTPS_CERT'%" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml - sed -i "s%# key = '.\/key.pem'%key = '$PATH_TO_HTTPS_KEY'%" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml - sed -i "s/# redirectToHttps/redirectToHttps/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml - sleep 2 - fi + if [[ "$HTTPS" == "true" ]] ; then + echo -e "${INFO} Enabling HTTPS in xo-server configuration file" + cmdlog "sed -i \"s%# cert = '.\/certificate.pem'%cert = '$PATH_TO_HTTPS_CERT'%\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml" + sed -i "s%# cert = '.\/certificate.pem'%cert = '$PATH_TO_HTTPS_CERT'%" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml >>$LOGFILE 2>&1 + cmdlog \"sed -i "s%# key = '.\/key.pem'%key = '$PATH_TO_HTTPS_KEY'%\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml" + sed -i "s%# key = '.\/key.pem'%key = '$PATH_TO_HTTPS_KEY'%" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml >>$LOGFILE 2>&1 + cmdlog "sed -i \"s/# redirectToHttps/redirectToHttps/\" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml" + sed -i "s/# redirectToHttps/redirectToHttps/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml >>$LOGFILE 2>&1 + sleep 2 + fi echo -e "${INFO} Activating modified configuration file" + cmdlog "mkdir -p $CONFIGPATH/.config/xo-server" mkdir -p $CONFIGPATH/.config/xo-server + cmdlog "mv -f $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml $CONFIGPATH/.config/xo-server/config.toml" mv -f $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml $CONFIGPATH/.config/xo-server/config.toml @@ -484,41 +568,51 @@ function InstallXO { echo echo -e "${INFO} Symlinking fresh xo-server install/update to $INSTALLDIR/xo-server" - ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server $INSTALLDIR/xo-server + cmdlog "ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server $INSTALLDIR/xo-server" + ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server $INSTALLDIR/xo-server >>$LOGFILE 2>&1 sleep 2 echo -e "${INFO} Symlinking fresh xo-web install/update to $INSTALLDIR/xo-web" - ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-web $INSTALLDIR/xo-web + cmdlog "ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-web $INSTALLDIR/xo-web" + ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-web $INSTALLDIR/xo-web >>$LOGFILE 2>&1 if [[ "$XOUSER" != "root" ]]; then - chown -R $XOUSER:$XOUSER $INSTALLDIR/xo-builds/xen-orchestra-$TIME + cmdlog "chown -R $XOUSER:$XOUSER $INSTALLDIR/xo-builds/xen-orchestra-$TIME" + chown -R $XOUSER:$XOUSER $INSTALLDIR/xo-builds/xen-orchestra-$TIME >>$LOGFILE 2>&1 if [ ! -d /var/lib/xo-server ]; then - mkdir /var/lib/xo-server 2>/dev/null + cmdlog "mkdir /var/lib/xo-server" + mkdir /var/lib/xo-server >>$LOGFILE 2>&1 fi - chown -R $XOUSER:$XOUSER /var/lib/xo-server + cmdlog "chown -R $XOUSER:$XOUSER /var/lib/xo-server" + chown -R $XOUSER:$XOUSER /var/lib/xo-server >>$LOGFILE 2>&1 - chown -R $XOUSER:$XOUSER $CONFIGPATH/.config/xo-server + ccmdlog "chown -R $XOUSER:$XOUSER $CONFIGPATH/.config/xo-server" + chown -R $XOUSER:$XOUSER $CONFIGPATH/.config/xo-server >>$LOGFILE 2>&1 fi # fix to prevent older installations to not update because systemd service is not symlinked anymore if [[ $(find /etc/systemd/system -maxdepth 1 -type l -name "xo-server.service") ]]; then - rm -f /etc/systemd/system/xo-server.service + cmdlog "rm -f /etc/systemd/system/xo-server.service" + rm -f /etc/systemd/system/xo-server.service >>$LOGFILE 2>&1 fi echo echo -e "${INFO} Replacing systemd service configuration file" - /bin/cp -f $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service + cmdlog "/bin/cp -f $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service" + /bin/cp -f $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service >>$LOGFILE 2>&1 sleep 2 echo -e "${INFO} Reloading systemd configuration" echo - /bin/systemctl daemon-reload + cmdlog "/bin/systemctl daemon-reload" + /bin/systemctl daemon-reload >>$LOGFILE 2>&1 sleep 2 echo echo -e "${INFO} Starting xo-server..." - /bin/systemctl start xo-server >/dev/null + cmdlog "/bin/systemctl start xo-server" + /bin/systemctl start xo-server >>$LOGFILE 2>&1 # no need to exit/trap on errors anymore set +e @@ -537,7 +631,8 @@ function InstallXO { echo -e " ${COLOR_GREEN}Default username: admin@admin.net password: admin${COLOR_N}" echo echo -e "${INFO} Installation successful. Enabling xo-server to start on reboot" - /bin/systemctl enable xo-server > /dev/null + cmdlog "/bin/systemctl enable xo-server" + /bin/systemctl enable xo-server >>$LOGFILE 2>&1 else echo echo -e "${FAIL} Looks like there was a problem when starting xo-server/reading journalctl. Please see logs for more details" @@ -545,7 +640,7 @@ function InstallXO { exit 1 fi -} 2>$LOGFILE +} function UpdateXO { @@ -557,13 +652,14 @@ function UpdateXO { # remove old builds. leave as many as defined in PRESERVE variable echo echo -ne "${PROGRESS} Removing old installations. Leaving $PRESERVE latest" - find $INSTALLDIR/xo-builds/ -maxdepth 1 -type d -name "xen-orchestra*" -printf "%T@ %p\n" | sort -n | cut -d' ' -f2- | head -n -$PRESERVE | xargs -r rm -r + cmdlog "find $INSTALLDIR/xo-builds/ -maxdepth 1 -type d -name \"xen-orchestra*\" -printf \"%T@ %p\\n\" | sort -n | cut -d' ' -f2- | head -n -$PRESERVE | xargs -r rm -r" + find $INSTALLDIR/xo-builds/ -maxdepth 1 -type d -name "xen-orchestra*" -printf "%T@ %p\n" | sort -n | cut -d' ' -f2- | head -n -$PRESERVE | xargs -r rm -r >>$LOGFILE 2>&1 echo -e "\r${OK} Removing old installations. Leaving $PRESERVE latest" else echo -e "${INFO} PRESERVE variable is set to 0. This needs to be at least 1. Not doing a cleanup" fi -} 2>$LOGFILE +} function HandleArgs { @@ -615,16 +711,21 @@ function RollBackInstallation { *xen-orchestra*) echo echo -e "${INFO} Setting $INSTALLDIR/xo-server symlink to $INSTALLATION/packages/xo-server" - ln -sfn $INSTALLATION/packages/xo-server $INSTALLDIR/xo-server + cmdlog "ln -sfn $INSTALLATION/packages/xo-server $INSTALLDIR/xo-server" + ln -sfn $INSTALLATION/packages/xo-server $INSTALLDIR/xo-server >>$LOGFILE 2>&1 echo -e "${INFO} Setting $INSTALLDIR/xo-web symlink to $INSTALLATION/packages/xo-web" - ln -sfn $INSTALLATION/packages/xo-web $INSTALLDIR/xo-web + cmdlog "ln -sfn $INSTALLATION/packages/xo-web $INSTALLDIR/xo-web" + ln -sfn $INSTALLATION/packages/xo-web $INSTALLDIR/xo-web >>$LOGFILE 2>&1 echo echo -e "${INFO} Replacing xo.server.service systemd configuration file" - /bin/cp -f $INSTALLATION/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service - /bin/systemctl daemon-reload + cmdlog "/bin/cp -f $INSTALLATION/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service" + /bin/cp -f $INSTALLATION/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service >>$LOGFILE 2>&1 + cmdlog "/bin/systemctl daemon-reload" + /bin/systemctl daemon-reload >>$LOGFILE 2>&1 echo echo -e "${INFO} Restarting xo-server..." - /bin/systemctl restart xo-server + cmdlog "/bin/systemctl restart xo-server" + /bin/systemctl restart xo-server >>$LOGFILE 2>&1 echo break ;; @@ -660,7 +761,7 @@ function CheckOS { exit 0 fi -} 2>$LOGFILE +} function CheckSystemd { @@ -682,7 +783,6 @@ function CheckDocker { function CheckCertificate { if [[ "$HTTPS" == "true" ]]; then - local CERT="$(openssl x509 -modulus -noout -in "$PATH_TO_HTTPS_CERT" | openssl md5)" local KEY="$(openssl rsa -modulus -noout -in "$PATH_TO_HTTPS_KEY" | openssl md5)" if [[ "$CERT" != "$KEY" ]]; then @@ -697,7 +797,7 @@ function CheckCertificate { fi fi -} 2>$LOGFILE +} function CheckMemory { SYSMEM=$(grep MemTotal /proc/meminfo | awk '{print $2}') @@ -722,7 +822,7 @@ function PullDockerImage { echo -e "${INFO} If you want to persist xen-orchestra and redis data, use volume flags like:" echo " docker run -itd -p 80:80 -v /path/to/data/xo-server:/var/lib/xo-server -v /path/to/data/redis:/var/lib/redis ronivay/xen-orchestra" -} 2>$LOGFILE +} function StartUpScreen { @@ -742,7 +842,6 @@ else fi echo -e "Port: ${COLOR_WHITE}$PORT${COLOR_N}" -echo -e "HTTPS: ${COLOR_WHITE}${HTTPS}${COLOR_N} ${COLOR_RED}${HTTPSFAIL}${COLOR_N}" echo -e "Git Branch for source: ${COLOR_WHITE}$BRANCH${COLOR_N}" echo -e "Following plugins will be installed: ${COLOR_WHITE}"$PLUGINS"${COLOR_N}" echo -e "Number of previous installations to preserve: ${COLOR_WHITE}$PRESERVE${COLOR_N}" @@ -769,7 +868,8 @@ read -p ": " option case $answer in y) echo "Stopping xo-server..." - /bin/systemctl stop xo-server || { echo -e "${FAIL} failed to stop service, exiting..." ; exit 1; } + cmdlog "/bin/systemctl stop xo-server" + /bin/systemctl stop xo-server >>$LOGFILE 2>&1 || { echo -e "${FAIL} failed to stop service, exiting..." ; exit 1; } ;; n) exit 0