diff --git a/Dockerfile b/Dockerfile index e0e3a43..1ca978c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM library/docker:stable ENV HOME_DIR=/opt/crontab -RUN apk add --no-cache --virtual .run-deps gettext bash jq \ +RUN apk add --no-cache --virtual .run-deps gettext bash py3-toml py3-yaml python3 jq \ && mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects \ && adduser -S docker -D diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..ee05c03 --- /dev/null +++ b/config.toml @@ -0,0 +1,49 @@ +["cron with triggered commands"] +comment = "cron with triggered commands" +schedule = "* * * * *" +command = "echo hello" +project = "crontab" +container = "myapp" +[["cron with triggered commands".trigger]] +command = "echo world" +container = "crontab_myapp_1" + +["map a volume"] +comment = "map a volume" +schedule = "* * * * *" +dockerargs = "-d -v /tmp:/tmp" +command = "echo new" +image = "alpine:3.5" + +["use an ENV from inside a container"] +comment = "use an ENV from inside a container" +schedule = "@hourly" +dockerargs = "-d -e FOO=BAR" +command = "sh -c 'echo hourly ${FOO}'" +image = "alpine:3.5" + +["trigger every 2 min"] +comment = "trigger every 2 min" +schedule = "@every 2m" +command = "echo 2 minute" +image = "alpine:3.5" +[["trigger every 2 min".trigger]] +command = "echo world" +container = "crontab_myapp_1" + +["? /usr/sbin/logrotate /etc/logrotate.conf*/5 * * * *"] +schedule = "*/5 * * * *" +command = "/usr/sbin/logrotate /etc/logrotate.conf" + +["Regenerate Certificate then reload nginx"] +comment = "Regenerate Certificate then reload nginx" +schedule = "43 6,18 * * *" +command = "sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns'" +dockerargs = "--env-file /opt/crontab/env/letsencrypt.env -v ${PWD}:/etc/ssl -v webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge" +image = "willfarrell/letsencrypt" +onstart = true +[["Regenerate Certificate then reload nginx".trigger]] +command = "sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'" +project = "conduit" +container = "nginx" + diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..e69de29 diff --git a/docker-entrypoint b/docker-entrypoint index 40bb622..2ce5c01 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -15,7 +15,21 @@ if [ "${LOG_FILE}" == "" ]; then touch ${LOG_FILE} fi -CONFIG=${HOME_DIR}/config.json + +if [ -f "${HOME_DIR}/config.toml" ]; then + 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)" +elif [ -f "${HOME_DIR}/config.yml" ]; then + 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)" +elif [ -f "${HOME_DIR}/config.yaml" ]; then + 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)" +fi + +if [ -f "${HOME_DIR}/config.json" ]; then + CONFIG=${HOME_DIR}/config.json +else + echo "NO CONFIG FILE FOUND" +fi + DOCKER_SOCK=/var/run/docker.sock CRONTAB_FILE=/etc/crontabs/docker @@ -49,7 +63,22 @@ slugify() { make_image_cmd() { DOCKERARGS=$(echo ${1} | jq -r .dockerargs) + VOLUMES=$(echo ${1} | jq -r '.volumes | map(" -v " + .) | join("")') + PORTS=$(echo ${1} | jq -r '.ports | map(" -p " + .) | join("")') + EXPOSE=$(echo ${1} | jq -r '.expose | map(" --expose " + .) | join("")') + # We'll add name in, if it exists + NAME=$(echo ${1} | jq -r 'select(.name != null) | .name') + NETWORK=$(echo ${1} | jq -r 'select(.network != null) | .network') + ENVIRONMENT=$(echo ${1} | jq -r '.environment | map(" -e " + .) | join("")') + # echo ${1} | jq -r '.environment | join("\n")' > ${PWD}/${NAME}.env + # ENVIRONMENT=" --env-file ${PWD}/${NAME}.env" if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi + if [ ! -z "${NAME}" ]; then DOCKERARGS="${DOCKERARGS} --rm --name ${NAME} "; fi + if [ ! -z "${NETWORK}" ]; then DOCKERARGS="${DOCKERARGS} --network ${NETWORK} "; fi + if [ ! -z "${VOLUMES}" ]; then DOCKERARGS="${DOCKERARGS}${VOLUMES}"; fi + if [ ! -z "${ENVIRONMENT}" ]; then DOCKERARGS="${DOCKERARGS}${ENVIRONMENT}"; fi + if [ ! -z "${PORTS}" ]; then DOCKERARGS="${DOCKERARGS}${PORTS}"; fi + if [ ! -z "${EXPOSE}" ]; then DOCKERARGS="${DOCKERARGS}${EXPOSE}"; fi IMAGE=$(echo ${1} | jq -r .image | envsubst) TMP_COMMAND=$(echo ${1} | jq -r .command) echo "docker run ${DOCKERARGS} ${IMAGE} ${TMP_COMMAND}" @@ -80,6 +109,7 @@ for CONTAINER_NAME in \$CONTAINERS; do done EOF echo "/bin/bash ${HOME_DIR}/projects/${SCRIPT_NAME}.sh" + # cat "/bin/bash ${HOME_DIR}/projects/${SCRIPT_NAME}.sh" else echo "docker exec ${DOCKERARGS} ${CONTAINER} ${TMP_COMMAND}" fi @@ -236,7 +266,7 @@ if [ "$1" = "crond" ]; then if [ -f ${CONFIG} ]; then build_crontab else - echo "Unable to find ${HOME_DIR}/config.json" + echo "Unable to find ${CONFIG}" fi fi