comparison etc/periodic/daily/720.local-triggered-action @ 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 7dcf2ae34350
comparison
equal deleted inserted replaced
393:d83f877d7849 394:7c597ee2c8d9
13 then 13 then
14 . /etc/defaults/periodic.conf 14 . /etc/defaults/periodic.conf
15 source_periodic_confs 15 source_periodic_confs
16 fi 16 fi
17 17
18 # Set it to "YES" to enable this script
18 : ${daily_local_triggered_action_enable:=NO} 19 : ${daily_local_triggered_action_enable:=NO}
20 # The readability of any of the given files triggers the action
19 : ${daily_local_triggered_action_files=} 21 : ${daily_local_triggered_action_files=}
20 # e.g. "service nginx onestatus" 22 #
23 # A condition to check also before executing an action.
24 #
25 # May be a Shell pipeline.
26 # E.g. "service nginx onestatus || service apache2 onestatus"
27 #
21 : ${daily_local_triggered_action_condition=} 28 : ${daily_local_triggered_action_condition=}
22 # e.g. "service nginx reload" 29 #
30 # The action to execute.
31 #
32 # May be a Shell pipeline.
33 # E.g. "{ service nginx onereload && service apache2 onereload ; } || true"
34 #
23 : ${daily_local_triggered_action_action=} 35 : ${daily_local_triggered_action_action=}
36 #
37 # By default all files triggering the action are removed. Set to "NO" if
38 # the files should remain.
39 #
24 : ${daily_local_triggered_action_files_remove:=YES} 40 : ${daily_local_triggered_action_files_remove:=YES}
41 #
42 # If profiles are defined this script is re-executed once for for every
43 # profile with the profile as parameter.
44 # No global options above besides "daily_local_triggered_action_enable" are
45 # used.
46 # Instead profile level configurations are named
47 # daily_local_triggered_action_<profile>_<option> .
48 #
49 : ${daily_local_triggered_action_profiles=}
50
51
52 #:
53 #: Check whether a given profile is defined in the configuration
54 #:
55 #: Args:
56 #: $1: The profile to check for
57 #:
58 #: Returns:
59 #: 0: If the profile is found
60 #: 1: If the profile is not found
61 #:
62 _is_profile() {
63 local prof
64
65 for prof in ${daily_local_triggered_action_profiles}; do
66 if [ "${prof}" = "$1" ]; then
67 return 0
68 fi
69 done
70 return 1
71 }
72
73
74 rc=0
75
76 if [ -n "${daily_local_triggered_action_profiles}" ]; then
77 if [ $# -eq 1 ]; then
78 profile="$1"
79 if ! _is_profile "${profile}"; then
80 echo "ERROR: no such profile: ${profile}" 1>&2
81 exit 1
82 fi
83 eval daily_local_triggered_action_files="\${daily_local_triggered_action_${profile}_files-}"
84 eval daily_local_triggered_action_condition="\${daily_local_triggered_action_${profile}_condition-}"
85 eval daily_local_triggered_action_action="\${daily_local_triggered_action_${profile}_action-}"
86 eval daily_local_triggered_action_files_remove="\${daily_local_triggered_action_${profile}_files_remove:-YES}"
87
88 elif [ $# -gt 1 ]; then
89 echo "ERROR: usage" 1>&2
90 exit 1
91 else
92 for _p in ${daily_local_triggered_action_profiles} ; do
93 # Re-execute with profile
94 $0 "${_p}"
95 _tmprc=$?
96 if [ ${_tmprc} -ne 0 ]; then
97 rc=${_tmprc}
98 fi
99 done
100 exit ${rc}
101 fi
102 fi
25 103
26 case "${daily_local_triggered_action_enable}" in 104 case "${daily_local_triggered_action_enable}" in
27 [Yy][Ee][Ss]) 105 [Yy][Ee][Ss])
106 if [ -z "${profile}" ]; then
107 profilestr=
108 else
109 profilestr=" (${profile})"
110 fi
111
28 echo 112 echo
29 echo "Testing for newly renewed Let's Encrypt certificates" 113 echo "Testing for newly triggered action${profilestr}"
30
31 rc=0
32 114
33 _do_action="" 115 _do_action=""
34 _remove_files="" 116 _remove_files=""
35 117
36 for _f in ${daily_local_triggered_action_files}; do 118 for _f in ${daily_local_triggered_action_files}; do
44 fi 126 fi
45 done 127 done
46 128
47 if [ "${_do_action}" = "yes" ]; then 129 if [ "${_do_action}" = "yes" ]; then
48 if [ -z "${daily_local_triggered_action_action}" ]; then 130 if [ -z "${daily_local_triggered_action_action}" ]; then
49 echo "ERROR: no deploy action defined" 1>&2 131 echo "ERROR: no action defined${profilestr}" 1>&2
50 exit 2 132 exit 2
51 fi 133 fi
52 134
53 echo "Deploying newly renewed Let's Encrypt certificates" 135 echo "Executing action because valid trigger found${profilestr}"
54 if [ -n "${daily_local_triggered_action_condition}" ]; then 136 if [ -n "${daily_local_triggered_action_condition}" ]; then
55 if ${daily_local_triggered_action_condition} ; then 137 eval ${daily_local_triggered_action_condition}
56 ${daily_local_triggered_action_action} 138 _tmprc=$?
139 if [ ${_tmprc} -eq 0 ]; then
140 eval ${daily_local_triggered_action_action}
57 rc=$? 141 rc=$?
142 if [ ${rc} -ne 0 ] ; then
143 echo "ERROR: Action failed${profilestr}" 1>&2
144 fi
58 else 145 else
59 rc=1 146 rc=1
60 fi 147 fi
61 else 148 else
62 ${daily_local_triggered_action_action} 149 eval ${daily_local_triggered_action_action}
63 rc=$? 150 rc=$?
64 fi 151 fi
65 152
66 # Remove trigger files if configured to do so 153 # Remove trigger files if configured to do so
67 if [ ${rc} -eq 0 ]; then 154 if [ ${rc} -eq 0 ]; then
68 echo "Removing trigger files..." 155 if [ -n "${_remove_files}" ]; then
69 for _rf in ${_remove_files}; do 156 echo "Removing trigger files${profilestr} ..."
70 rm -fv "${_rf}" 157 for _rf in ${_remove_files}; do
71 done 158 rm -fv "${_rf}"
159 done
160 fi
72 fi 161 fi
73 else 162 else
74 echo "No newly renewed Let's Encrypt certificates found" 163 echo "No action triggers found${profilestr}"
75 fi 164 fi
76 ;; 165 ;;
77
78 *) 166 *)
79 rc=0 167 rc=0
80 ;; 168 ;;
81 esac 169 esac
82 170