forked from SimplicityGuy/docker-crontab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·276 lines (231 loc) · 8.6 KB
/
entrypoint.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
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
#!/bin/bash
set -e
DOCKER_SOCK=/var/run/docker.sock
CRONTAB_FILE=/etc/crontabs/docker
LOG_DIR=/var/log/crontab
if [ -z "${HOME_DIR}" ] && [ -n "${TEST_MODE}" ]; then
HOME_DIR=/tmp/crontab-docker-testing
CRONTAB_FILE=${HOME_DIR}/test
LOG_DIR=${HOME_DIR}/crontab
elif [ -z "${HOME_DIR}" ]; then
echo "HOME_DIR not set."
exit 1
fi
# Ensure dir exist - in case of volume mapping.
mkdir -p "${HOME_DIR}"/jobs
if [ -z "${DOCKER_HOST}" ] && [ -a "${DOCKER_PORT_2375_TCP}" ]; then
export DOCKER_HOST="tcp://docker:2375"
fi
if [ "${LOG_FILE}" == "" ]; then
LOG_FILE=${LOG_DIR}/jobs.log
mkdir -p "${LOG_DIR}"
touch "${LOG_FILE}"
fi
normalize_config() {
JSON_CONFIG={}
if [ -n "${CRONTAB_CONFIG_JSON}" ]; then
JSON_CONFIG=${CRONTAB_CONFIG_JSON}
elif [ -n "${CRONTAB_CONFIG_JSON_BASE64}" ]; then
JSON_CONFIG="$(echo -n "${CRONTAB_CONFIG_JSON_BASE64}" | base64 -d)"
elif [ -f "${HOME_DIR}/config.json" ]; then
JSON_CONFIG="$(cat "${HOME_DIR}"/config.json)"
elif [ -f "${HOME_DIR}/config.toml" ]; then
JSON_CONFIG="$(rq -t <<< "$(cat "${HOME_DIR}"/config.toml)")"
elif [ -f "${HOME_DIR}/config.yml" ]; then
JSON_CONFIG="$(rq -y <<< "$(cat "${HOME_DIR}"/config.yml)")"
elif [ -f "${HOME_DIR}/config.yaml" ]; then
JSON_CONFIG="$(rq -y <<< "$(cat "${HOME_DIR}"/config.yaml)")"
fi
jq 'map(.)' <<< "${JSON_CONFIG}" > "${HOME_DIR}"/config.working.json
# jq -S -r '."~~shared-settings" as $shared | del(."~~shared-settings") | to_entries | map_values(.value + { name: .key } + $shared)' <<< "${JSON_CONFIG}" > "${HOME_DIR}"/config.working.json
}
ensure_docker_socket_accessible() {
if ! grep -q "^docker:" /etc/group; then
# Ensure 'docker' user has permissions for docker socket (without changing permissions).
DOCKER_GID=$(stat -c '%g' ${DOCKER_SOCK})
if [ "${DOCKER_GID}" != "0" ]; then
if ! grep -qE "^[^:]+:[^:]+:${DOCKER_GID}:" /etc/group; then
# No group with such gid exists - create group 'docker'.
addgroup -g "${DOCKER_GID}" docker
adduser docker docker
else
# Group with such gid exists - add user 'docker' to this group.
DOCKER_GROUP_NAME=$(getent group "${DOCKER_GID}" | awk -F':' '{{ print $1 }}')
adduser docker "${DOCKER_GROUP_NAME}"
fi
else
# Docker socket belongs to 'root' group - add user 'docker' to this group.
adduser docker root
fi
fi
}
slugify() {
echo "${@}" | iconv -t ascii | sed -r s/[~^]+//g | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr '[:upper:]' '[:lower:]'
}
make_image_cmd() {
DOCKERARGS=$(echo "${1}" | jq -r .dockerargs)
ENVIRONMENT=$(echo "${1}" | jq -r 'select(.environment != null) | .environment | map("--env " + .) | join(" ")')
EXPOSE=$(echo "${1}" | jq -r 'select(.expose != null) | .expose | map("--expose " + .) | join(" ")' )
NAME=$(echo "${1}" | jq -r 'select(.name != null) | .name')
NETWORKS=$(echo "${1}" | jq -r 'select(.networks != null) | .networks | map("--network " + .) | join(" ")')
PORTS=$(echo "${1}" | jq -r 'select(.ports != null) | .ports | map("--publish " + .) | join(" ")')
VOLUMES=$(echo "${1}" | jq -r 'select(.volumes != null) | .volumes | map("--volume " + .) | join(" ")')
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
DOCKERARGS+=" "
if [ -n "${ENVIRONMENT}" ]; then DOCKERARGS+="${ENVIRONMENT} "; fi
if [ -n "${EXPOSE}" ]; then DOCKERARGS+="${EXPOSE} "; fi
if [ -n "${NAME}" ]; then DOCKERARGS+="--name ${NAME} "; fi
if [ -n "${NETWORKS}" ]; then DOCKERARGS+="${NETWORKS} "; fi
if [ -n "${PORTS}" ]; then DOCKERARGS+="${PORTS} "; fi
if [ -n "${VOLUMES}" ]; then DOCKERARGS+="${VOLUMES} "; fi
IMAGE=$(echo "${1}" | jq -r .image | envsubst)
if [ "${IMAGE}" == "null" ]; then return; fi
COMMAND=$(echo "${1}" | jq -r .command)
echo "docker run ${DOCKERARGS} ${IMAGE} ${COMMAND}"
}
make_container_cmd() {
DOCKERARGS=$(echo "${1}" | jq -r .dockerargs)
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
CONTAINER=$(echo "${1}" | jq -r .container | envsubst)
if [ "${CONTAINER}" == "null" ]; then return; fi
COMMAND=$(echo "${1}" | jq -r .command )
if [ "${COMMAND}" == "null" ]; then return; fi
echo "docker exec ${DOCKERARGS} ${CONTAINER} ${COMMAND}"
}
make_cmd() {
if [ "$(echo "${1}" | jq -r .image)" != "null" ]; then
make_image_cmd "${1}"
elif [ "$(echo "${1}" | jq -r .container)" != "null" ]; then
make_container_cmd "${1}"
else
echo "${1}" | jq -r .command
fi
}
parse_schedule() {
IFS=" "
read -a params <<< "$@"
case ${params[0]} in
"@yearly" | "@annually")
echo "0 0 1 1 *"
;;
"@monthly")
echo "0 0 1 * *"
;;
"@weekly")
echo "0 0 * * 0"
;;
"@daily")
echo "0 0 * * *"
;;
"@midnight")
echo "0 0 * * *"
;;
"@hourly")
echo "0 * * * *"
;;
"@random")
M="*"
H="*"
D="*"
for when in "${params[@]:1}"
do
case $when in
"@m")
M=$(shuf -i 0-59 -n 1)
;;
"@h")
H=$(shuf -i 0-23 -n 1)
;;
"@d")
D=$(shuf -i 0-6 -n 1)
;;
esac
done
echo "${M} ${H} * * ${D}"
;;
*)
echo "${params[@]}"
;;
esac
}
function build_crontab() {
rm -rf "${CRONTAB_FILE}"
ONSTART=()
while read -r i ; do
KEY=$(jq -r .["$i"] "${CONFIG}")
SCHEDULE=$(echo "${KEY}" | jq -r '.schedule' | sed 's/\*/\\*/g')
if [ "${SCHEDULE}" == "null" ]; then
echo "'schedule' missing: '${KEY}"
continue
fi
SCHEDULE=$(parse_schedule "${SCHEDULE}" | sed 's/\\//g')
COMMAND=$(echo "${KEY}" | jq -r '.command')
if [ "${COMMAND}" == "null" ]; then
echo "'command' missing: '${KEY}'"
continue
fi
COMMENT=$(echo "${KEY}" | jq -r '.comment')
SCRIPT_NAME=$(echo "${KEY}" | jq -r '.name')
SCRIPT_NAME=$(slugify "${SCRIPT_NAME}")
if [ "${SCRIPT_NAME}" == "null" ]; then
SCRIPT_NAME=$(cat /proc/sys/kernel/random/uuid)
fi
CRON_COMMAND=$(make_cmd "${KEY}")
SCRIPT_PATH="${HOME_DIR}/jobs/${SCRIPT_NAME}.sh"
touch "${SCRIPT_PATH}"
chmod +x "${SCRIPT_PATH}"
{
echo "#\!/usr/bin/env bash"
echo "set -e"
echo ""
echo "echo \"start cron job __${SCRIPT_NAME}__\""
echo "${CRON_COMMAND}"
} > "${SCRIPT_PATH}"
TRIGGER=$(echo "${KEY}" | jq -r '.trigger')
if [ "${TRIGGER}" != "null" ]; then
while read -r j ; do
TRIGGER_KEY=$(echo "${KEY}" | jq -r .trigger["$j"])
TRIGGER_COMMAND=$(echo "${TRIGGER_KEY}" | jq -r '.command')
if [ "${TRIGGER_COMMAND}" == "null" ]; then
continue
fi
make_cmd "${TRIGGER_KEY}" >> "${SCRIPT_PATH}"
done < <(echo "${KEY}" | jq -r '.trigger | keys[]')
fi
echo "echo \"end cron job __${SCRIPT_NAME}__\"" >> "${SCRIPT_PATH}"
if [ "${COMMENT}" != "null" ]; then
echo "# ${COMMENT}" >> "${CRONTAB_FILE}"
fi
echo "${SCHEDULE} ${SCRIPT_PATH}" >> "${CRONTAB_FILE}"
ONSTART_COMMAND=$(echo "${KEY}" | jq -r '.onstart')
if [ "${ONSTART_COMMAND}" == "true" ]; then
ONSTART+=("${SCRIPT_PATH}")
fi
done < <(jq -r '. | keys[]' "${CONFIG}")
printf "##### crontab generated #####\n"
cat "${CRONTAB_FILE}"
printf "##### run commands with onstart #####\n"
for ONSTART_COMMAND in "${ONSTART[@]}"; do
printf "%s\n" "${ONSTART_COMMAND}"
${ONSTART_COMMAND} &
done
printf "##### cron running #####\n"
}
start_app() {
normalize_config
export CONFIG=${HOME_DIR}/config.working.json
if [ ! -f "${CONFIG}" ]; then
printf "missing generated %s. exiting.\n" "${CONFIG}"
exit 1
fi
if [ "${1}" == "crond" ]; then
build_crontab
fi
printf "%s\n" "${@}"
exec "${@}"
}
if [ -z "${TEST_MODE}" ]; then
ensure_docker_socket_accessible
fi
printf "✨ starting crontab container ✨\n"
start_app "${@}"