-
Notifications
You must be signed in to change notification settings - Fork 0
/
only-once
executable file
·59 lines (43 loc) · 2.33 KB
/
only-once
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
#!/bin/bash
set -euo pipefail
. /.functions
[ -v WRAPPED_STATE ] || fail "Must set the WRAPPED_STATE environment variable to point to the location where to track if the initializer script has already been run"
require_dir_readwrite "${WRAPPED_STATE}"
[ -v WRAPPED_SCRIPT ] || fail "Must set the WRAPPED_SCRIPT environment variable to point to the script to be wrapped"
require_file_executable "${WRAPPED_SCRIPT}"
# In case the arguments haven't been defined
[ -v WRAPPED_SCRIPT_ARGS ] || WRAPPED_SCRIPT_ARGS=""
# If we're not given an explicit name to track state with, we deduce it from the script's
# filename and add a dot in front to hide the file
set_or_default WRAPPED_STATE_NAME ".${WRAPPED_SCRIPT##*/}"
set_or_default WRAPPED_SCRIPT_STATE_MARKER
# Remove leading and trailing spaces from the marker value
WRAPPED_SCRIPT_STATE_MARKER="$(echo -n "${WRAPPED_SCRIPT_STATE_MARKER}" | sed -e 's;^\s*;;g' -e 's;\s*$;;g')"
MARKER_FILE="${WRAPPED_STATE}/${WRAPPED_STATE_NAME}"
if [ -e "${MARKER_FILE}" ] ; then
QUIT="true"
if [ -n "${WRAPPED_SCRIPT_STATE_MARKER}" ] ; then
# Is the WRAPPED_SCRIPT_STATE_MARKER in the state file?
grep -Eqi "[[:space:]]${WRAPPED_SCRIPT_STATE_MARKER}$" "${MARKER_FILE}" || QUIT="false"
# The marker isn't there... we must run the initialization
err "The run marker value [${WRAPPED_SCRIPT_STATE_MARKER}] is not present in the state tracker file at [${MARKER_FILE}]"
fi
as_boolean "${QUIT}" && quit "The state has already been initialized"
fi
init_ssl
running "Calling the wrapped script"
(
# This should help mitigate exploitation
eval CMD=( "${WRAPPED_SCRIPT@Q}" ${WRAPPED_SCRIPT_ARGS} ) || fail "Failed to define the wrapped command using script [${WRAPPED_SCRIPT}] and args [${WRAPPED_SCRIPT_ARGS}]"
execute "${CMD[@]}"
) || fail "Failed to execute the wrapped script (rc=${?})"
TS="$(timestamp)"
WRAPPED_SCRIPT_STATE_MARKER_MSG=""
if [ -n "${WRAPPED_SCRIPT_STATE_MARKER}" ] ; then
WRAPPED_SCRIPT_STATE_MARKER_MSG=", with the marker value [${WRAPPED_SCRIPT_STATE_MARKER}]"
# For when it's written out
WRAPPED_SCRIPT_STATE_MARKER="\t${WRAPPED_SCRIPT_STATE_MARKER}"
fi
ok "Wrapped script succeeded, marking the state to [${MARKER_FILE}] (timestamp = ${TS}${WRAPPED_SCRIPT_STATE_MARKER_MSG})"
echo -e "${TS}${WRAPPED_SCRIPT_STATE_MARKER}" >> "${MARKER_FILE}" || fail "Failed to create the state tracking marker"
exit 0