changeset 394:7c597ee2c8d9

Implement profile support for the daily script 720.local-triggered-action
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 24 Feb 2023 01:07:19 +0100
parents d83f877d7849
children 64d73ad6e37d
files etc/periodic/daily/720.local-triggered-action
diffstat 1 files changed, 104 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/etc/periodic/daily/720.local-triggered-action	Thu Feb 23 22:00:40 2023 +0100
+++ b/etc/periodic/daily/720.local-triggered-action	Fri Feb 24 01:07:19 2023 +0100
@@ -15,20 +15,102 @@
     source_periodic_confs
 fi
 
+# Set it to "YES" to enable this script
 : ${daily_local_triggered_action_enable:=NO}
+# The readability of any of the given files triggers the action
 : ${daily_local_triggered_action_files=}
-# e.g. "service nginx onestatus"
+#
+# A condition to check also before executing an action.
+#
+# May be a Shell pipeline.
+# E.g. "service nginx onestatus || service apache2 onestatus"
+#
 : ${daily_local_triggered_action_condition=}
-# e.g. "service nginx reload"
+#
+# The action to execute.
+#
+# May be a Shell pipeline.
+# E.g. "{ service nginx onereload && service apache2 onereload ; } || true"
+#
 : ${daily_local_triggered_action_action=}
+#
+# By default all files triggering the action are removed. Set to "NO" if
+# the files should remain.
+#
 : ${daily_local_triggered_action_files_remove:=YES}
+#
+# If profiles are defined this script is re-executed once for for every
+# profile with the profile as parameter.
+# No global options above besides "daily_local_triggered_action_enable" are
+# used.
+# Instead profile level configurations are named
+# daily_local_triggered_action_<profile>_<option> .
+#
+: ${daily_local_triggered_action_profiles=}
+
+
+#:
+#: Check whether a given profile is defined in the configuration
+#:
+#: Args:
+#:  $1: The profile to check for
+#:
+#: Returns:
+#:   0: If the profile is found
+#:   1: If the profile is not found
+#:
+_is_profile() {
+    local prof
+
+    for prof in ${daily_local_triggered_action_profiles}; do
+	if [ "${prof}" = "$1" ]; then
+	    return 0
+	fi
+    done
+    return 1
+}
+
+
+rc=0
+
+if [ -n "${daily_local_triggered_action_profiles}" ]; then
+    if [ $# -eq 1 ]; then
+        profile="$1"
+        if ! _is_profile "${profile}"; then
+            echo "ERROR: no such profile: ${profile}" 1>&2
+            exit 1
+        fi
+        eval daily_local_triggered_action_files="\${daily_local_triggered_action_${profile}_files-}"
+        eval daily_local_triggered_action_condition="\${daily_local_triggered_action_${profile}_condition-}"
+        eval daily_local_triggered_action_action="\${daily_local_triggered_action_${profile}_action-}"
+        eval daily_local_triggered_action_files_remove="\${daily_local_triggered_action_${profile}_files_remove:-YES}"
+
+    elif [ $# -gt 1 ]; then
+        echo "ERROR: usage" 1>&2
+        exit 1
+    else
+        for _p in ${daily_local_triggered_action_profiles} ; do
+            # Re-execute with profile
+            $0 "${_p}"
+            _tmprc=$?
+            if [ ${_tmprc} -ne 0 ]; then
+                rc=${_tmprc}
+            fi
+        done
+        exit ${rc}
+    fi
+fi
 
 case "${daily_local_triggered_action_enable}" in
     [Yy][Ee][Ss])
+        if [ -z "${profile}" ]; then
+            profilestr=
+        else
+            profilestr=" (${profile})"
+        fi
+
         echo
-        echo "Testing for newly renewed Let's Encrypt certificates"
-
-        rc=0
+        echo "Testing for newly triggered action${profilestr}"
 
         _do_action=""
         _remove_files=""
@@ -46,35 +128,41 @@
 
         if [ "${_do_action}" = "yes" ]; then
             if [ -z "${daily_local_triggered_action_action}" ]; then
-                echo "ERROR: no deploy action defined" 1>&2
+                echo "ERROR: no action defined${profilestr}" 1>&2
                 exit 2
             fi
 
-            echo "Deploying newly renewed Let's Encrypt certificates"
+            echo "Executing action because valid trigger found${profilestr}"
             if [ -n "${daily_local_triggered_action_condition}" ]; then
-                if ${daily_local_triggered_action_condition} ; then
-                    ${daily_local_triggered_action_action}
+                eval ${daily_local_triggered_action_condition}
+                _tmprc=$?
+                if [ ${_tmprc} -eq 0 ]; then
+                    eval ${daily_local_triggered_action_action}
                     rc=$?
+                    if [ ${rc} -ne 0 ] ; then
+                        echo "ERROR: Action failed${profilestr}" 1>&2
+                    fi
                 else
                     rc=1
                 fi
             else
-                ${daily_local_triggered_action_action}
+                eval ${daily_local_triggered_action_action}
                 rc=$?
             fi
 
             # Remove trigger files if configured to do so
             if [ ${rc} -eq 0 ]; then
-                echo "Removing trigger files..."
-                for _rf in ${_remove_files}; do
-                    rm -fv "${_rf}"
-                done
+                if [ -n "${_remove_files}" ]; then
+                    echo "Removing trigger files${profilestr} ..."
+                    for _rf in ${_remove_files}; do
+                        rm -fv "${_rf}"
+                    done
+                fi
             fi
         else
-            echo "No newly renewed Let's Encrypt certificates found"
+            echo "No action triggers found${profilestr}"
         fi
         ;;
-
     *)
         rc=0
         ;;