Skip to content

Commit 4a0d85f

Browse files
author
Omar Ahmad
committed
Adding python conversion for TOML and YAML; slightly more verbose syntax for dockerargs
1 parent 21e58d0 commit 4a0d85f

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM library/docker:stable
22

33
ENV HOME_DIR=/opt/crontab
4-
RUN apk add --no-cache --virtual .run-deps gettext bash jq \
4+
RUN apk add --no-cache --virtual .run-deps gettext bash py3-toml py3-yaml python3 jq \
55
&& mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects \
66
&& adduser -S docker -D
77

config.toml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
["cron with triggered commands"]
2+
comment = "cron with triggered commands"
3+
schedule = "* * * * *"
4+
command = "echo hello"
5+
project = "crontab"
6+
container = "myapp"
7+
[["cron with triggered commands".trigger]]
8+
command = "echo world"
9+
container = "crontab_myapp_1"
10+
11+
["map a volume"]
12+
comment = "map a volume"
13+
schedule = "* * * * *"
14+
dockerargs = "-d -v /tmp:/tmp"
15+
command = "echo new"
16+
image = "alpine:3.5"
17+
18+
["use an ENV from inside a container"]
19+
comment = "use an ENV from inside a container"
20+
schedule = "@hourly"
21+
dockerargs = "-d -e FOO=BAR"
22+
command = "sh -c 'echo hourly ${FOO}'"
23+
image = "alpine:3.5"
24+
25+
["trigger every 2 min"]
26+
comment = "trigger every 2 min"
27+
schedule = "@every 2m"
28+
command = "echo 2 minute"
29+
image = "alpine:3.5"
30+
[["trigger every 2 min".trigger]]
31+
command = "echo world"
32+
container = "crontab_myapp_1"
33+
34+
["? /usr/sbin/logrotate /etc/logrotate.conf*/5 * * * *"]
35+
schedule = "*/5 * * * *"
36+
command = "/usr/sbin/logrotate /etc/logrotate.conf"
37+
38+
["Regenerate Certificate then reload nginx"]
39+
comment = "Regenerate Certificate then reload nginx"
40+
schedule = "43 6,18 * * *"
41+
command = "sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns'"
42+
dockerargs = "--env-file /opt/crontab/env/letsencrypt.env -v ${PWD}:/etc/ssl -v webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge"
43+
image = "willfarrell/letsencrypt"
44+
onstart = true
45+
[["Regenerate Certificate then reload nginx".trigger]]
46+
command = "sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'"
47+
project = "conduit"
48+
container = "nginx"
49+

config.yml

Whitespace-only changes.

docker-entrypoint

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@ if [ "${LOG_FILE}" == "" ]; then
1515
touch ${LOG_FILE}
1616
fi
1717

18-
CONFIG=${HOME_DIR}/config.json
18+
19+
if [ -f "${HOME_DIR}/config.toml" ]; then
20+
python3 -c "with open('${HOME_DIR}/config.toml') as ct, open('${HOME_DIR}/config.json', 'w') as cj: import toml; import json; json.dump(list(toml.load(ct).values()), cj)"
21+
elif [ -f "${HOME_DIR}/config.yml" ]; then
22+
python3 -c "with open('${HOME_DIR}/config.yml') as cy, open('${HOME_DIR}/config.json', 'w') as cj: import yaml; import json; json.dump(list(yaml.safe_load(cy).values()), cj)"
23+
elif [ -f "${HOME_DIR}/config.yaml" ]; then
24+
python3 -c "with open('${HOME_DIR}/config.yaml') as cy, open('${HOME_DIR}/config.json', 'w') as cj: import yaml; import json; json.dump(list(yaml.safe_load(cy).values()), cj)"
25+
fi
26+
27+
if [ -f "${HOME_DIR}/config.json" ]; then
28+
CONFIG=${HOME_DIR}/config.json
29+
else
30+
echo "NO CONFIG FILE FOUND"
31+
fi
32+
1933
DOCKER_SOCK=/var/run/docker.sock
2034
CRONTAB_FILE=/etc/crontabs/docker
2135

@@ -49,7 +63,22 @@ slugify() {
4963

5064
make_image_cmd() {
5165
DOCKERARGS=$(echo ${1} | jq -r .dockerargs)
66+
VOLUMES=$(echo ${1} | jq -r '.volumes | map(" -v " + .) | join("")')
67+
PORTS=$(echo ${1} | jq -r '.ports | map(" -p " + .) | join("")')
68+
EXPOSE=$(echo ${1} | jq -r '.expose | map(" --expose " + .) | join("")')
69+
# We'll add name in, if it exists
70+
NAME=$(echo ${1} | jq -r 'select(.name != null) | .name')
71+
NETWORK=$(echo ${1} | jq -r 'select(.network != null) | .network')
72+
ENVIRONMENT=$(echo ${1} | jq -r '.environment | map(" -e " + .) | join("")')
73+
# echo ${1} | jq -r '.environment | join("\n")' > ${PWD}/${NAME}.env
74+
# ENVIRONMENT=" --env-file ${PWD}/${NAME}.env"
5275
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
76+
if [ ! -z "${NAME}" ]; then DOCKERARGS="${DOCKERARGS} --rm --name ${NAME} "; fi
77+
if [ ! -z "${NETWORK}" ]; then DOCKERARGS="${DOCKERARGS} --network ${NETWORK} "; fi
78+
if [ ! -z "${VOLUMES}" ]; then DOCKERARGS="${DOCKERARGS}${VOLUMES}"; fi
79+
if [ ! -z "${ENVIRONMENT}" ]; then DOCKERARGS="${DOCKERARGS}${ENVIRONMENT}"; fi
80+
if [ ! -z "${PORTS}" ]; then DOCKERARGS="${DOCKERARGS}${PORTS}"; fi
81+
if [ ! -z "${EXPOSE}" ]; then DOCKERARGS="${DOCKERARGS}${EXPOSE}"; fi
5382
IMAGE=$(echo ${1} | jq -r .image | envsubst)
5483
TMP_COMMAND=$(echo ${1} | jq -r .command)
5584
echo "docker run ${DOCKERARGS} ${IMAGE} ${TMP_COMMAND}"
@@ -80,6 +109,7 @@ for CONTAINER_NAME in \$CONTAINERS; do
80109
done
81110
EOF
82111
echo "/bin/bash ${HOME_DIR}/projects/${SCRIPT_NAME}.sh"
112+
# cat "/bin/bash ${HOME_DIR}/projects/${SCRIPT_NAME}.sh"
83113
else
84114
echo "docker exec ${DOCKERARGS} ${CONTAINER} ${TMP_COMMAND}"
85115
fi
@@ -236,7 +266,7 @@ if [ "$1" = "crond" ]; then
236266
if [ -f ${CONFIG} ]; then
237267
build_crontab
238268
else
239-
echo "Unable to find ${HOME_DIR}/config.json"
269+
echo "Unable to find ${CONFIG}"
240270
fi
241271
fi
242272

0 commit comments

Comments
 (0)