-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloe_run
executable file
·290 lines (247 loc) · 9.44 KB
/
cloe_run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
SCRIPT_START_TIME=$(date -Iminutes)
ENVDIR_BIN=$(which envdir)
RUNPARTS_BIN=$(which run-parts)
TS_BIN=$(which ts)
trap "TERM_SIGNALED=1 ; catch_term" TERM EXIT
function catch_term {
if [[ ${CLOE_RUN_SLEEP_PID} -gt 0 ]]; then
echo "TERM signal received while sleeping. Killing sleep pid:${CLOE_RUN_SLEEP_PID}." | eval ${PIPECMD}
kill ${CLOE_RUN_SLEEP_PID}
CLOE_RUN_SLEEP_PID=0
fi
}
# cloe_run_parts: Run scripts in a directory using run-parts
function cloe_run_parts {
if [[ -z ${RUNPARTS_BIN} ]]; then
return
fi
if [[ ! -d $1 ]]; then
return
fi
if [[ ! -r $1 ]]; then
echo "cloe_run_parts: Directory ${1} exists but is not readable." | eval ${PIPECMD}
return
fi
export CLOE_RUN_SECONDS=${SECONDS}
${RUNPARTS_BIN} --lsbsysinit --verbose -- $1
}
# envdir_source: Builds an envdir command string for a directory
# and adds it to the ENVDIR_CMD variable
function envdir_source {
if [[ -z ${ENVDIR_BIN} ]]; then
return
fi
if [[ ! -d $1 ]]; then
return
fi
if [[ ! -r $1 ]]; then
echo "envdir_source: Directory ${1} exists but is not readable." | eval ${PIPECMD}
return
fi
echo "Sourcing environment variables from directory '${1}'" | eval ${PIPECMD}
ENVDIR_CMD="${ENVDIR_CMD} ${ENVDIR_BIN} ${1}"
}
function set_screen_title {
printf "\033k%s\033\\" "$*"
}
function set_cloe_status {
local SIGNALS=""
# If running under a 'screen' session, set the window name
if [[ ${TERM_SIGNALED} -gt 0 ]]; then
SIGNALS="${SIGNALS} [SIGTERM] "
fi
if [ -n "$STY" ] ; then
set_screen_title "${SCRIPT_START_TIME} ($(date -Iseconds)) ${CLOE_RUN_CONF_NAME} ${SIGNALS}-- $*"
fi
}
function check_suspend {
local SUSPEND_FILE=${1:-${CLOE_RUN_SUSPEND_FILE:-"${CLOE_RUN_SUSPEND_DIR:-${PWD}}/suspend.cloe_run"}}
local SUSPEND_SLEEP=${2:-${CLOE_RUN_SUSPEND_SLEEP:-"5m"}}
while [ -f ${SUSPEND_FILE} ];
do
if [ -z ${SUSPEND_TIME} ]; then
SUSPEND_TIME=$(date -Iminutes)
fi
set_cloe_status "Suspended since ${SUSPEND_TIME}"
echo -n "Script is suspended since ${SUSPEND_TIME}. Delete '${SUSPEND_FILE}' file to resume it. " | eval ${PIPECMD}
if [[ -z `which inotifywait` ]]; then
echo "Now entering sleep for ${SUSPEND_SLEEP}..." | eval ${PIPECMD}
sleep ${SUSPEND_SLEEP}
else
echo "Waiting indefinitely..." | eval ${PIPECMD}
inotifywait --event delete ${SUSPEND_FILE}
fi
done
SUSPEND_TIME=""
}
##############################################################################
# Build PIPECMD string
#
# All the output will be piped through this command chain. This allows you
# to customize the output behaviour, such as disabling output timestamping.
#
# WARNING: Since no configuration file has been sourced yet at this point,
# configuration values must be set externally before launching cloe_run.
# Allow to disable timestamping.
if [[ ${CLOE_RUN_DISABLE_TIMESTAMP:=0} -eq 0 ]]; then
if [[ -z ${TS_BIN} ]]; then
echo "Program 'ts' is not installed. You can find it in the 'moreutils' package."
exit 1
fi
PIPECMD=" ts |"
fi
# Add optional, user-supplied commands to pipe chain.
# Warning: Some commands can break the chain because they do not forward
# their received input to stdout.
# Example: using 'logger' without the '--stderr' option.
if [[ ! -z ${CLOE_RUN_PIPECMD} ]]; then
PIPECMD="${PIPECMD} ${CLOE_RUN_PIPECMD} |&"
fi
# Close PIPECMD with optional output logging to file.
PIPECMD="${PIPECMD} tee -a ${LOGFILE}"
##############################################################################
##############################################################################
# Configuration values
#
# Settings are read from environment variables.
# You can also set them in specific files.
# Default system-wide settings can be set in CLOE_RUN_SYSTEM_DEFAULTS_CONF_FILE.
# Common local directory settings can be set in CLOE_RUN_COMMON_CONF_FILE.
# You can specify a script-level settings file in the CLOE_RUN_CONF_FILE
# environment variable or passing the configuration file name as the
# first command argument. If it is not set, script-level conf file will
# default to "SCRIPTNAME.conf" file.
CLOE_RUN_SYSTEM_CONF_DIR="/etc/cloe_run"
CLOE_RUN_SYSTEM_DEFAULTS_CONF_FILE="/etc/default/cloe_run"
# Try to get the configuration filename from the command line parameter
# first. This takes precedence over environment variable value.
if [[ ! -z ${1} ]]; then
CLOE_RUN_CONF_NAME="${1}"
fi
CLOE_RUN_COMMON_CONF_FILE="conf/cloe_run.common.conf"
[[ -z ${CLOE_RUN_CONF_NAME} ]] && CLOE_RUN_CONF_NAME="${0}"
CLOE_RUN_CONF_FILE="${CLOE_RUN_CONF_NAME}.conf"
##############################################################################
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_SYSTEM_CONF_DIR}/first-run.d" |& eval ${PIPECMD}
SHUTDOWN_FILE="${0}.shutdown"
while [ ! -r $SHUTDOWN_FILE ];
do
############################################################################
# Check and perform suspension logic
############################################################################
# Check for global "$PWD/suspend.cloe_run" file (default).
check_suspend
# Check for script-level "$PWD/suspend.<conf-name>" file.
check_suspend "${CLOE_RUN_SUSPEND_DIR:-${PWD}}/suspend.$(basename ${CLOE_RUN_CONF_NAME})"
############################################################################
############################################################################
# Source configuration files
if [[ -r ${CLOE_RUN_SYSTEM_DEFAULTS_CONF_FILE} ]]; then
echo "Sourcing system-wide configuration defaults '${CLOE_RUN_SYSTEM_DEFAULTS_CONF_FILE}'." \
| eval ${PIPECMD}
. ${CLOE_RUN_SYSTEM_DEFAULTS_CONF_FILE}
fi
envdir_source "${CLOE_RUN_SYSTEM_CONF_DIR}/envdir.d/"
if [[ -r ${CLOE_RUN_COMMON_CONF_FILE} ]]; then
echo "Sourcing common configuration file '${CLOE_RUN_COMMON_CONF_FILE}'." \
| eval ${PIPECMD}
. ${CLOE_RUN_COMMON_CONF_FILE}
fi
envdir_source "${CLOE_RUN_COMMON_CONF_FILE}.d"
if [[ -r ${CLOE_RUN_CONF_FILE} ]]; then
echo "Sourcing configuration file '${CLOE_RUN_CONF_FILE}'." \
| eval ${PIPECMD}
. ${CLOE_RUN_CONF_FILE}
else
echo "Warning: configuration file '${CLOE_RUN_CONF_FILE}' not found." \
| eval ${PIPECMD}
fi
envdir_source "${CLOE_RUN_CONF_FILE}.d"
if [[ -z ${CLOE_RUN_BIN_DIR} ]]; then
CLOE_RUN_BIN_DIR="${CLOE_RUN_SYSTEM_CONF_DIR}/run.d"
fi
if [[ -z ${CLOE_RUN_BIN} ]]; then
# Look for run-parts executable files at CLOE_RUN_BIN_DIR before complaining
# about otherwise mandatory CLOE_RUN_BIN variable being empty.
if [ ! $(find ${CLOE_RUN_BIN_DIR} -maxdepth 1 -type f -executable 2> /dev/null) ]; then
echo "Variable CLOE_RUN_BIN not set. Exiting." | eval ${PIPECMD}
exit 1
fi
fi
############################################################################
# Execute the PRE_RUN_COMMAND
if [[ ! -z ${PRE_RUN_COMMAND} ]]; then
MSG="Executing PRE_RUN_COMMAND: ${PRE_RUN_COMMAND}"
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
export CLOE_RUN_SECONDS=${SECONDS}
${PRE_RUN_COMMAND} |& eval ${PIPECMD}
fi
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_SYSTEM_CONF_DIR}/pre-run.d" |& eval ${PIPECMD}
############################################################################
export CLOE_RUN_LOOP_COUNT=$(( ++CLOE_RUN_LOOP_COUNT ))
# Build command options
CMD_PARAMS="${CLOE_RUN_PARAMS} ${CLOE_RUN_PARAMS_EXTRA}"
# Execute main command
if [[ ! -z ${CLOE_RUN_BIN} ]]; then
MSG="Executing: ${CLOE_RUN_BIN} ${CMD_PARAMS}"
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
export CLOE_RUN_SECONDS=${SECONDS}
${ENVDIR_CMD} ${CLOE_RUN_BIN} ${CMD_PARAMS} 2>&1 | eval ${PIPECMD}
fi
# Execute run-parts main commands
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_BIN_DIR}" |& eval ${PIPECMD}
# Execute the POST_RUN_COMMAND
if [[ ! -z ${POST_RUN_COMMAND} ]]; then
MSG="Executing POST_RUN_COMMAND: ${POST_RUN_COMMAND}"
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
export CLOE_RUN_SECONDS=${SECONDS}
${POST_RUN_COMMAND} |& eval ${PIPECMD}
fi
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_SYSTEM_CONF_DIR}/post-run.d" |& eval ${PIPECMD}
if [[ ${CLOE_RUN_SINGLE} -gt 0 ]]; then
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_SYSTEM_CONF_DIR}/last-run.d" |& eval ${PIPECMD}
MSG="Finished because of single run requested."
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
exit 0
fi
if [[ ${TERM_SIGNALED} -gt 0 ]]; then
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_SYSTEM_CONF_DIR}/last-run.d" |& eval ${PIPECMD}
MSG="Finished because of TERM signal received."
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
exit 0
fi
if [[ ! -z ${SLEEP} ]]; then
MSG="Sleeping for ${SLEEP}..."
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
sleep ${SLEEP} &
CLOE_RUN_SLEEP_PID=${!}
wait ${CLOE_RUN_SLEEP_PID}
fi
if [[ ${TERM_SIGNALED} -gt 0 ]]; then
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_SYSTEM_CONF_DIR}/last-run.d" |& eval ${PIPECMD}
MSG="Finished because of TERM signal received."
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
exit 0
fi
done
export CLOE_RUN_SECONDS=${SECONDS}
cloe_run_parts "${CLOE_RUN_SYSTEM_CONF_DIR}/last-run.d" |& eval ${PIPECMD}
MSG="File ${SHUTDOWN_FILE} found. Exiting."
echo ${MSG} |& eval ${PIPECMD}
set_cloe_status ${MSG}
rm -v ${SHUTDOWN_FILE}