This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdhis2-init.sh
executable file
·184 lines (137 loc) · 5.1 KB
/
dhis2-init.sh
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
#!/usr/bin/env bash
# SC2155: Declare and assign separately to avoid masking return values.
# shellcheck disable=SC2155
set -Eeo pipefail
################################################################################
SELF="$( basename "$0" )"
echo "[INFO] $SELF: started..."
# Exit immediately if any required scripts or programs are not found
ITEMS=(
basename
dirname
flock
head
mkdir
rm
seq
shuf
sleep
tee
)
for ITEM in "${ITEMS[@]}"; do
if ! command -V "$ITEM" &>/dev/null ; then
echo "[ERROR] $SELF: Unable to find $ITEM, exiting..." >&2
exit 1
fi
done
################################################################################
if [[ -d /dhis2-init.progress/ ]]; then
LOCK_FILE="/dhis2-init.progress/${SELF%.sh}.lock"
# Random sleep from 0.01 to 5 seconds to prevent possible race condition with acquiring the file lock
sleep "$( seq 0 .01 5 | shuf | head -n1 )s"
# Ensure lock file parent directory exists
if [[ ! -d "$( dirname "$LOCK_FILE" )" ]]; then
mkdir -p "$( dirname "$LOCK_FILE" )"
fi
# Acquire lock as named file
# NOTE: the named file descriptor cannot be set with a variable and it cannot contain a hyphen
# http://mywiki.wooledge.org/BashFAQ/045 via http://stackoverflow.com/a/17515965/534275
# https://github.com/pkrumins/bash-redirections-cheat-sheet/blob/92dac40/bash-redirections-cheat-sheet.txt#L118-L121
exec {dhis2init}> "$LOCK_FILE"
# Wait until lock is available before proceeding, exit with error if timeout is reached
if ! timeout 3600s bash -c "until flock -n ${dhis2init} ; do echo \"[INFO] $SELF: Waiting 10s for lock $LOCK_FILE to be released...\"; sleep 10s; done" ; then
echo "[WARNING] $SELF: script lock was not released in time, exiting..." >&2
exit 1
fi
fi
################################################################################
if [[ -d /dhis2-init.progress/ ]]; then
STATUS_FILE="/dhis2-init.progress/${SELF%.sh}_status.txt"
# Ensure status file parent directory exists
if [[ ! -d "$( dirname "$STATUS_FILE" )" ]]; then
mkdir -p "$( dirname "$STATUS_FILE" )"
fi
if [[ "${DHIS2_INIT_FORCE:-0}" == "1" ]]; then
echo "[DEBUG] $SELF: DHIS2_INIT_FORCE=1; delete \"$STATUS_FILE\"..." >&2
rm -v -f "$STATUS_FILE"
fi
fi
################################################################################
# If PGPASSWORD is empty or null, set it to the contents of PGPASSWORD_FILE
if [[ -z "${PGPASSWORD:-}" ]] && [[ -r "${PGPASSWORD_FILE:-}" ]]; then
export PGPASSWORD="$(<"${PGPASSWORD_FILE}")"
fi
# If PGHOST is empty or null, set it to DHIS2_DATABASE_HOST
if [[ -z "${PGHOST:-}" ]] && [[ -n "${DHIS2_DATABASE_HOST:-}" ]]; then
export PGHOST="${DHIS2_DATABASE_HOST:-}"
fi
# If PGPORT is empty or null, set it to DHIS2_DATABASE_PORT
if [[ -z "${PGPORT:-}" ]] && [[ -n "${DHIS2_DATABASE_PORT:-}" ]]; then
export PGPORT="${DHIS2_DATABASE_PORT:-}"
fi
# Set default values if not provided in the environment
if [[ -z "${PGUSER:-}" ]]; then
export PGUSER='postgres'
fi
if [[ -z "${PGDATABASE:-}" ]]; then
export PGDATABASE='postgres'
fi
################################################################################
# Inspired by: https://github.com/docker-library/postgres/blob/ba30220/13/docker-entrypoint.sh
# NOTE: This is to be used as a CMD, not an ENTRYPOINT.
# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
[ "${#FUNCNAME[@]}" -ge 2 ] \
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
&& [ "${FUNCNAME[1]}" = 'source' ]
}
# Source: https://github.com/docker-library/postgres/blob/ba30220/13/docker-entrypoint.sh#L144-L173
# usage: dhis2_process_init_files [file [file [...]]]
# ie: dhis2_process_init_files /usr/local/share/dhis2-init.d/*
# process initializer files, based on file extensions and permissions
dhis2_process_init_files() {
echo
local f
for f; do
# if the basename of the script is in the environ $DHIS_INIT_SKIP (a list separated by commas), then skip it
if [[ "${DHIS2_INIT_SKIP:-}" =~ (^|,)$(basename "${f}")(,|$) ]]; then
echo "[INFO] $SELF: Skipping \"$f\" because \"$(basename "${f}")\" is in DHIS2_INIT_SKIP..."
echo
continue
fi
case "$f" in
*.sh)
# If .sh file is executable, run it
if [ -x "$f" ]; then
echo "[INFO] $SELF: running $f"
"$f"
# If .sh file is not executable, source it
else
echo "[INFO] $SELF: sourcing $f"
# shellcheck disable=SC1090
source "$f"
fi
;;
*)
echo "[INFO] $SELF: ignoring $f"
;;
esac
echo
done
}
_main() {
# Execute the files in the "/usr/local/share/dhis2-init.d" directory.
# Ensure files ending in ".sh" have the execute bit or else they will be sourced.
dhis2_process_init_files /usr/local/share/dhis2-init.d/*
################################################################################
if [[ -d /dhis2-init.progress/ ]]; then
# Record script progess
echo "$SELF: COMPLETED" | tee "$STATUS_FILE"
else
echo "$SELF: COMPLETED"
fi
}
if ! _is_sourced; then
_main "$@"
fi