From c5f0f9bbfb951bf536d95e3bc7262c52511973f4 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Fri, 14 May 2021 16:52:42 +0200
Subject: [PATCH] v0.3 add params (list, force, help)

---
 deploy_app.sh | 259 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 165 insertions(+), 94 deletions(-)

diff --git a/deploy_app.sh b/deploy_app.sh
index e732b28..d55088b 100755
--- a/deploy_app.sh
+++ b/deploy_app.sh
@@ -6,6 +6,7 @@
 # ----------------------------------------------------------------------
 # 2021-04-19  v0.1  <axel.hahn@iml.unibe.ch>  initial version
 # 2021-05-09  v0.2  <axel.hahn@iml.unibe.ch>  chown includes dot files
+# 2021-05-14  v0.3  <axel.hahn@iml.unibe.ch>  add params (list, force, help)
 # ======================================================================
 
 
@@ -100,131 +101,201 @@ function run_task(){
     fi
 }
 
-# ----------------------------------------------------------------------
-# MAIN
-# ----------------------------------------------------------------------
 
-cd $( dirname $0 )
+function deploy(){
+    skipmessage="SKIP: no newer download file. You can use parameter -f to fore reinstall."
 
-if [ $# -eq 0 ]; then
-    header "looping over all profiles"
-    getprofiles
-    echo
-    for myprofile in $( getprofiles )
-    do
-        $0 $myprofile    
-    done
-    rc=$?
-    profile=
-    header "loop over projects is finished"
-    echo exiting with statuscode $rc
-    exit $rc
-fi
+    # ----------------------------------------------------------------------
+    header "Set profile [$1]"
+    setprofile $1
 
-echo
-echo
-echo "<<<<<<<<<<##########|  POC :: DEPLOYMENT SCRIPT  |##########>>>>>>>>>>"
-echo
 
+    # ----------------------------------------------------------------------
+    header "Download ${IMLCI_PROJECT}.tgz"
+    typeset -i local isupdate=$defaultupdate
+    ${selfdir}/bin/getfile.sh -f ${IMLCI_PROJECT}.tgz -o ${downloadtmp}
+    if [ $? -ne 0 ]; then
+        echo Repeating request with debug param -d to get the error...
+        ${selfdir}/bin/getfile.sh -d -f ${IMLCI_PROJECT}.tgz -o ${downloadtmp}
+        exit 1
+    fi
 
-# ----------------------------------------------------------------------
-header "Set profile [$1]"
-setprofile $1
 
+    # ----------------------------------------------------------------------
+    header "Detect if download is newer than last download."
+    if [ -f ${downloadfile} ]; then
+        # ls -l "${downloadfile}" "${downloadtmp}" 
+        diff "${downloadfile}" "${downloadtmp}"
+        if [ $? -eq 0 ]; then
+            echo "INFO: the downloaded file is the same like last download."
+            rm -f "${downloadtmp}"
+        else
+            echo "OK: donwload contains an update."
+            isupdate=$isupdate+1
+            mv "${downloadtmp}" "${downloadfile}"
+        fi
+    else
+        echo "INFO: last download not available - first install or a forced update."
+        isupdate=$isupdate+0
+        mv "${downloadtmp}" "${downloadfile}"
+    fi
+    ls -l "${downloadfile}"
 
-# ----------------------------------------------------------------------
-header "Download ${IMLCI_PROJECT}.tgz"
-isupdate=0
-${selfdir}/bin/getfile.sh -f ${IMLCI_PROJECT}.tgz -o ${downloadtmp}
-if [ $? -ne 0 ]; then
-	echo Repeating request with debug param -d to get the error...
-	${selfdir}/bin/getfile.sh -d -f ${IMLCI_PROJECT}.tgz -o ${downloadtmp}
-	exit 1
-fi
 
+    # ----------------------------------------------------------------------
+    header "Switch into install dir ${installdir} ..."
+    test -d "${installdir}" || mkdir -p "${installdir}"
+    cd ${installdir} || exit 2
 
-# ----------------------------------------------------------------------
-header "Detect if download is newer than last download."
-if [ -f ${downloadfile} ]; then
-    # ls -l "${downloadfile}" "${downloadtmp}" 
-    diff "${downloadfile}" "${downloadtmp}"
-    if [ $? -eq 0 ]; then
-        echo "INFO: the downloaded file is the same like last download."
-        rm -f "${downloadtmp}"
+
+    # ----------------------------------------------------------------------
+    header "PRE tasks"
+    # what you could do here:
+    # - enable maintenance flag
+    # - stop service
+    # - cleanup directory ... up to remove all current files
+    test $isupdate -eq 0 && echo $skipmessage
+    test $isupdate -eq 0 || run_task "${profiledir}/tasks_preinstall.sh"
+
+
+    # ----------------------------------------------------------------------
+    header "PRE tasks II - cleanup"
+    if [ $isupdate -eq 0 ]; then
+        echo $skipmessage
     else
-        echo "OK: donwload contains an update."
-        isupdate=1
-        mv "${downloadtmp}" "${downloadfile}"
+        test "$cleanup_preview" -eq "1" || echo "SKIP: preview of cleanup is disabled."
+        test "$cleanup_preview" -eq "1" && "${selfdir}/bin/preinstall_cleanup.sh" "${installdir}" "${downloadfile}"
+
+        test "$cleanup_force" -eq "1"   || echo "SKIP: cleanup files is disabled."
+        test "$cleanup_force" -eq "1"   && "${selfdir}/bin/preinstall_cleanup.sh" "${installdir}" "${downloadfile}" "force"
     fi
-else
-    echo "INFO: last download not available - first install or a forced update."
-    isupdate=1
-    mv "${downloadtmp}" "${downloadfile}"
-fi
-ls -l "${downloadfile}"
 
 
-# ----------------------------------------------------------------------
-header "Switch into install dir ${installdir} ..."
-test -d "${installdir}" || mkdir -p "${installdir}"
-cd ${installdir} || exit 2
+    # ----------------------------------------------------------------------
+    header "Extract ${downloadfile} $( pwd )"
+    test $isupdate -eq 0 && echo $skipmessage
+    test $isupdate -eq 0 || tar -xzf "${downloadfile}" . || exit 3
+    ls -l
 
 
-# ----------------------------------------------------------------------
-header "PRE tasks"
-# what you could do here:
-# - enable maintenance flag
-# - stop service
-# - clenaup directory ... up to remove all current files
-run_task "${profiledir}/tasks_preinstall.sh"
+    # ----------------------------------------------------------------------
+    # header "Remove download archive ${IMLCI_PROJECT}.tgz"
+    # echo rm -f  ${IMLCI_PROJECT}.tgz
 
 
-# ----------------------------------------------------------------------
-header "PRE tasks II - cleanup"
-test "$cleanup_preview" -eq "1" || echo "SKIP: preview of cleanup is disabled."
-test "$cleanup_preview" -eq "1" && "${selfdir}/bin/preinstall_cleanup.sh" "${installdir}" "${downloadfile}"
+    # ----------------------------------------------------------------------
+    header "Update config files"
+    echo "Showing replacements:" ; grep '@replace\[' hooks/templates/*
+    run_task "${profiledir}/tasks_config.sh"
 
-test "$cleanup_force" -eq "1"   || echo "SKIP: cleanup files is disabled."
-test "$cleanup_force" -eq "1"   && "${selfdir}/bin/preinstall_cleanup.sh" "${installdir}" "${downloadfile}" "force"
 
+    # ----------------------------------------------------------------------
+    header "Set file owner [${appowner}]"
+    if [ $isupdate -eq 0 ]; then
+        echo $skipmessage
+    else
+        if [ ! -z "${appowner}" ]; then
+            sudo chown -R $appowner * .* || exit 5
+            ls -la
+        else
+            echo "SKIP: variable appowner was not set"
+        fi
+    fi
 
-# ----------------------------------------------------------------------
-header "Extract ${downloadfile} $( pwd )"
-tar -xzf "${downloadfile}" . || exit 3
-ls -l
 
+    # ----------------------------------------------------------------------
+    header "POST tasks"
+    # what you could do here:
+    # - start current deploy scripts
+    # - apply database updates
+    # - set permissions
+    # - start service
+    # - remove maintenance flag
+    # - send success message as email/ slack/ [another fancy tool]
+    test $isupdate -eq 0 && echo $skipmessage
+    test $isupdate -eq 0 || run_task "${profiledir}/tasks_postinstall.sh"
 
+    cd $( dirname $0 )
+
+}
+# ----------------------------------------------------------------------
+# MAIN
 # ----------------------------------------------------------------------
-# header "Remove download archive ${IMLCI_PROJECT}.tgz"
-# echo rm -f  ${IMLCI_PROJECT}.tgz
 
 
-# ----------------------------------------------------------------------
-header "Update config files"
-echo "Showing replacements:" ; grep '@replace\[' hooks/templates/*
-run_task "${profiledir}/tasks_config.sh"
+cd $( dirname $0 )
 
+action="deploy"
+typeset -i defaultupdate=0
 
-# ----------------------------------------------------------------------
-header "Set file owner [${appowner}]"
-if [ ! -z "${appowner}" ]; then
-    sudo chown -R $appowner * .* || exit 5
-    ls -la
+echo
+echo
+echo "<<<<<<<<<<##########|  IML - DEPLOYMENT SCRIPT  |##########>>>>>>>>>>"
+echo
+
+while getopts 'hfl' arg; do
+  case ${arg} in
+    h) 
+        echo "HELP"
+        echo "Load one or more profiles profile to deploy an application."
+        echo "If the download file is not newer then it does not extract files and does not"
+        echo "run pre and post hooks - it updates the config files only and sets the owner."
+        echo
+        echo "Syntax:"
+        echo "  $( basename $0 ) [OPTION] [PROFILE(S)]"
+        echo
+        echo "Optioms:"
+        echo "  -h | show this help and exit"
+        echo "  -f | force full installation even if the download file is not newer"
+        echo "  -l | list exiting profile names"
+        echo
+        echo "Profile(s):"
+        echo "  Set one or more valid profile names. By default it loops over all profiles."
+        echo "  This prameter limits the execution to the given profiles." 
+        echo "  Use option -l to get a list of profiles." 
+        echo
+        exit 0
+        ;;
+    f)
+      echo "FORCE update"
+      defaultupdate=1
+      shift 1
+      ;;
+    l)
+      echo "LIST of existing profiles:"
+      getprofiles
+      echo
+      exit 0
+      ;;
+    ?)
+      echo "Invalid option: -${OPTARG}."
+      exit 2
+      ;;
+  esac
+done
+
+
+if [ $# -eq 0 ]; then
+    header "looping over all profiles"
+    getprofiles
+    echo
+    allprofiles=$( getprofiles )
 else
-    echo "SKIP: variable appowner was not set"
+    allprofiles="$*"
 fi
 
+for myprofile in $allprofiles
+do
+    deploy $myprofile
+    profile=
+done
+
+rc=$?
+profile=
+header "All done."
+echo exiting with statuscode $rc
+exit $rc
 
-# ----------------------------------------------------------------------
-header "POST tasks"
-# what you could do here:
-# - start current deploy scripts
-# - apply database updates
-# - set permissions
-# - start service
-# - remove maintenance flag
-# - send success message as email/ slack/ [another fancy tool]
-run_task "${profiledir}/tasks_postinstall.sh"
 
 
 # ----------------------------------------------------------------------
-- 
GitLab