feat: Major Updates

- removed support for `projects` as the feature was very incomplete and it served little purpose
- added support for common settings between jobs using `~~shared-settings` as a key in the config
- cleaned up some items that have long bugged me
  - better reuse of code
  - better variable naming
  - improved flow and readability
  - formatting to the logs
This commit is contained in:
Robert Wlodarczyk 2023-02-18 18:42:20 -08:00
parent 76b524fd17
commit 035cd4a906
3 changed files with 128 additions and 106 deletions

View File

@ -24,7 +24,8 @@ RUN apk update && \
jq \ jq \
tini \ tini \
wget && \ wget && \
mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects && \ mkdir -p ${HOME_DIR}/jobs && \
rm -rf /etc/periodic /etc/crontabs/root && \
adduser -S docker -D adduser -S docker -D
COPY --from=rq-build /usr/bin/rq/rq /usr/local/bin COPY --from=rq-build /usr/bin/rq/rq /usr/local/bin

View File

@ -1,6 +1,6 @@
# docker-crontab # crontab
A simple wrapper over `docker` to all complex cron job to be run in other containers. Note, this is a maintained fork of [willfarrell/docker-crontab](https://github.com/willfarrell/docker-crontab). A simple wrapper over `docker` to all complex cron job to be run in other containers.
## Why? ## Why?
Yes, I'm aware of [mcuadros/ofelia](https://github.com/mcuadros/ofelia) (>250MB when this was created), it was the main inspiration for this project. Yes, I'm aware of [mcuadros/ofelia](https://github.com/mcuadros/ofelia) (>250MB when this was created), it was the main inspiration for this project.
@ -11,42 +11,44 @@ A great project, don't get me wrong. It was just missing certain key enterprise
- Allows for comments, cause we all need friendly reminders of what `update_script.sh` actually does. - Allows for comments, cause we all need friendly reminders of what `update_script.sh` actually does.
- Start an image using `image`. - Start an image using `image`.
- Run command in a container using `container`. - Run command in a container using `container`.
- Run command on a instances of a scaled container using `project`.
- Ability to trigger scripts in other containers on completion cron job using `trigger`. - Ability to trigger scripts in other containers on completion cron job using `trigger`.
- Ability to share settings between cron jobs using `~~shared-settings` as a key.
## Config file ## Config file
The config file can be specifed in any of `json`, `toml`, or `yaml`, and can be defined as either an array or mapping (top-level keys will be ignored; can be useful for organizing commands) The config file can be specified in any of `json`, `toml`, or `yaml`, and can be defined as either an array or mapping (top-level keys will be ignored; can be useful for organizing commands)
- `name`: Human readable name that will be used as the job filename. Will be converted into a slug. Optional. - `name`: Human readable name that will be used as the job filename. Will be converted into a slug. Optional.
- `comment`: Comments to be included with crontab entry. Optional. - `comment`: Comments to be included with crontab entry. Optional.
- `schedule`: Crontab schedule syntax as described in https://en.wikipedia.org/wiki/Cron. Ex `@hourly`, `@every 1h30m`, `* * * * *`. Required. - `schedule`: Crontab schedule syntax as described in https://en.wikipedia.org/wiki/Cron. Examples: `@hourly`, `@every 1h30m`, `* * * * *`. Required.
- `command`: Command to be run on in crontab container or docker container/image. Required. - `command`: Command to be run on in crontab container or docker container/image. Required.
- `image`: Docker images name (ex `library/alpine:3.5`). Optional. - `image`: Docker images name (ex `library/alpine:3.5`). Optional.
- `project`: Docker Compose/Swarm project name. Optional, only applies when `contain` is included. - `container`: Full container name. Ignored if `image` is included. Optional.
- `container`: Full container name or container alias if `project` is set. Ignored if `image` is included. Optional.
- `dockerargs`: Command line docker `run`/`exec` arguments for full control. Defaults to ` `. - `dockerargs`: Command line docker `run`/`exec` arguments for full control. Defaults to ` `.
- `trigger`: Array of docker-crontab subset objects. Subset includes: `image`,`project`,`container`,`command`,`dockerargs` - `trigger`: Array of docker-crontab subset objects. Sub-set includes: `image`, `container`, `command`, `dockerargs`
- `onstart`: Run the command on `crontab` container start, set to `true`. Optional, defaults to falsey. - `onstart`: Run the command on `crontab` container start, set to `true`. Optional, defaults to false.
See [`config-samples`](config-samples) for examples. See [`config-samples`](config-samples) for examples.
```json ```json
[{ {
"schedule":"@every 5m", "logrotate": {
"command":"/usr/sbin/logrotate /etc/logrotate.conf" "schedule":"@every 5m",
},{ "command":"/usr/sbin/logrotate /etc/logrotate.conf"
"comment":"Regenerate Certificate then reload nginx", },
"schedule":"43 6,18 * * *", "cert-regen": {
"command":"sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns'", "comment":"Regenerate Certificate then reload nginx",
"dockerargs":"--env-file /opt/crontab/env/letsencrypt.env -v webapp_nginx_tls_cert:/etc/ssl -v webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge", "schedule":"43 6,18 * * *",
"image":"willfarrell/letsencrypt", "command":"sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns'",
"trigger":[{ "dockerargs":"--it --env-file /opt/crontab/env/letsencrypt.env",
"command":"sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'", "volumes":["webapp_nginx_tls_cert:/etc/ssl", "webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge"],
"project":"conduit", "image":"willfarrell/letsencrypt",
"container":"nginx" "trigger":[{
}], "command":"sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'",
"onstart":true "container":"nginx"
}] }],
"onstart":true
}
}
``` ```
## How to use ## How to use
@ -69,5 +71,23 @@ docker run -d \
* otherwise [read the docker-compose docs](https://docs.docker.com/compose/networking/) * otherwise [read the docker-compose docs](https://docs.docker.com/compose/networking/)
2. Add `dockerargs` to your docker-crontab `config.json` 2. Add `dockerargs` to your docker-crontab `config.json`
* use `--network NETWORK_NAME` to connect new container into docker-compose network * use `--network NETWORK_NAME` to connect new container into docker-compose network
* use `--rm --name NAME` to use named container * use `--name NAME` to use named container
* e.g. `"dockerargs": "--network my_dir_default --rm --name my-best-cron-job"` * e.g. `"dockerargs": "--it"`
### Dockerfile
```Dockerfile
FROM registry.gitlab.com/simplicityguy/docker/crontab
COPY config.json ${HOME_DIR}/
```
### Logrotate Dockerfile
```Dockerfile
FROM registry.gitlab.com/simplicityguy/docker/crontab
RUN apk add --no-cache logrotate
RUN echo "*/5 * * * * /usr/sbin/logrotate /etc/logrotate.conf" >> /etc/crontabs/logrotate
COPY logrotate.conf /etc/logrotate.conf
CMD ["crond", "-f"]
```

View File

@ -11,7 +11,7 @@ if [ -z "${HOME_DIR}" ]; then
fi fi
# Ensure dir exist - in case of volume mapping. # Ensure dir exist - in case of volume mapping.
mkdir -p "${HOME_DIR}"/jobs "${HOME_DIR}"/projects mkdir -p "${HOME_DIR}"/jobs
if [ -z "${DOCKER_HOST}" ] && [ -a "${DOCKER_PORT_2375_TCP}" ]; then if [ -z "${DOCKER_HOST}" ] && [ -a "${DOCKER_PORT_2375_TCP}" ]; then
export DOCKER_HOST="tcp://docker:2375" export DOCKER_HOST="tcp://docker:2375"
@ -35,7 +35,8 @@ normalize_config() {
elif [ -f "${HOME_DIR}/config.yaml" ]; then elif [ -f "${HOME_DIR}/config.yaml" ]; then
JSON_CONFIG="$(rq -y <<< "$(cat "${HOME_DIR}"/config.yaml)")" JSON_CONFIG="$(rq -y <<< "$(cat "${HOME_DIR}"/config.yaml)")"
fi fi
jq -r 'to_entries | map_values(.value + { name: .key })' <<< "${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() { ensure_docker_socket_accessible() {
@ -44,16 +45,16 @@ ensure_docker_socket_accessible() {
DOCKER_GID=$(stat -c '%g' ${DOCKER_SOCK}) DOCKER_GID=$(stat -c '%g' ${DOCKER_SOCK})
if [ "${DOCKER_GID}" != "0" ]; then if [ "${DOCKER_GID}" != "0" ]; then
if ! grep -qE "^[^:]+:[^:]+:${DOCKER_GID}:" /etc/group; then if ! grep -qE "^[^:]+:[^:]+:${DOCKER_GID}:" /etc/group; then
# No group with such gid exists - create group docker. # No group with such gid exists - create group 'docker'.
addgroup -g "${DOCKER_GID}" docker addgroup -g "${DOCKER_GID}" docker
adduser docker docker adduser docker docker
else else
# Group with such gid exists - add user "docker" to this group. # Group with such gid exists - add user 'docker' to this group.
DOCKER_GROUP_NAME=$(getent group "${DOCKER_GID}" | awk -F':' '{{ print $1 }}') DOCKER_GROUP_NAME=$(getent group "${DOCKER_GID}" | awk -F':' '{{ print $1 }}')
adduser docker "${DOCKER_GROUP_NAME}" adduser docker "${DOCKER_GROUP_NAME}"
fi fi
else else
# Docker socket belongs to "root" group - add user "docker" to this group. # Docker socket belongs to 'root' group - add user 'docker' to this group.
adduser docker root adduser docker root
fi fi
fi fi
@ -65,51 +66,41 @@ slugify() {
make_image_cmd() { make_image_cmd() {
DOCKERARGS=$(echo "${1}" | jq -r .dockerargs) DOCKERARGS=$(echo "${1}" | jq -r .dockerargs)
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi ENVIRONMENT=$(echo "${1}" | jq -r 'select(.environment != null) | .environment | map("--env " + .) | join(" ")')
VOLUMES=$(echo "${1}" | jq -r 'select(.volumes != null) | .volumes | map(" -v " + .) | join("")') EXPOSE=$(echo "${1}" | jq -r 'select(.expose != null) | .expose | map("--expose " + .) | join(" ")' )
PORTS=$(echo "${1}" | jq -r 'select(.ports != null) | .ports | map(" -p " + .) | join("")')
EXPOSE=$(echo "${1}" | jq -r 'select(.expose != null) | .expose | map(" --expose " + .) | join("")')
NAME=$(echo "${1}" | jq -r 'select(.name != null) | .name') NAME=$(echo "${1}" | jq -r 'select(.name != null) | .name')
NETWORK=$(echo "${1}" | jq -r 'select(.network != null) | .network') NETWORK=$(echo "${1}" | jq -r 'select(.network != null) | .network | map("--network " + .) | join(" ")')
ENVIRONMENT=$(echo "${1}" | jq -r 'select(.environment != null) | .environment | map(" -e " + .) | join("")') PORTS=$(echo "${1}" | jq -r 'select(.ports != null) | .ports | map("--publish " + .) | join(" ")')
if [ -n "${NAME}" ]; then DOCKERARGS+=" --rm --name ${NAME} "; fi VOLUMES=$(echo "${1}" | jq -r 'select(.volumes != null) | .volumes | map("--volume " + .) | join(" ")')
if [ -n "${NETWORK}" ]; then DOCKERARGS+=" --network ${NETWORK} "; fi
if [ -n "${VOLUMES}" ]; then DOCKERARGS+="${VOLUMES}"; fi if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
if [ -n "${ENVIRONMENT}" ]; then DOCKERARGS+="${ENVIRONMENT}"; fi DOCKERARGS+=" "
if [ -n "${PORTS}" ]; then DOCKERARGS+="${PORTS}"; fi if [ -n "${ENVIRONMENT}" ]; then DOCKERARGS+="${ENVIRONMENT} "; fi
if [ -n "${EXPOSE}" ]; then DOCKERARGS+="${EXPOSE}"; fi if [ -n "${EXPOSE}" ]; then DOCKERARGS+="${EXPOSE} "; fi
if [ -n "${NAME}" ]; then DOCKERARGS+="--name ${NAME} "; fi
if [ -n "${NETWORK}" ]; then DOCKERARGS+="${NETWORK} "; fi
if [ -n "${PORTS}" ]; then DOCKERARGS+="${PORTS} "; fi
if [ -n "${VOLUMES}" ]; then DOCKERARGS+="${VOLUMES} "; fi
IMAGE=$(echo "${1}" | jq -r .image | envsubst) IMAGE=$(echo "${1}" | jq -r .image | envsubst)
TMP_COMMAND=$(echo "${1}" | jq -r .command) if [ "${IMAGE}" == "null" ]; then return; fi
echo "docker run ${DOCKERARGS} ${IMAGE} ${TMP_COMMAND}"
COMMAND=$(echo "${1}" | jq -r .command)
echo "docker run ${DOCKERARGS} ${IMAGE} ${COMMAND}"
} }
make_container_cmd() { make_container_cmd() {
DOCKERARGS=$(echo "${1}" | jq -r .dockerargs) DOCKERARGS=$(echo "${1}" | jq -r .dockerargs)
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
SCRIPT_NAME=$(echo "${1}" | jq -r .name)
SCRIPT_NAME=$(slugify "${SCRIPT_NAME}")
PROJECT=$(echo "${1}" | jq -r .project)
CONTAINER=$(echo "${1}" | jq -r .container | envsubst) CONTAINER=$(echo "${1}" | jq -r .container | envsubst)
TMP_COMMAND=$(echo "${1}" | jq -r .command) if [ "${CONTAINER}" == "null" ]; then return; fi
if [ "${PROJECT}" != "null" ]; then COMMAND=$(echo "${1}" | jq -r .command )
# Create bash script to detect all running containers. if [ "${COMMAND}" == "null" ]; then return; fi
if [ "${SCRIPT_NAME}" == "null" ]; then
SCRIPT_NAME=$(cat /proc/sys/kernel/random/uuid)
fi
cat << EOF > "${HOME_DIR}"/projects/"${SCRIPT_NAME}".sh
#!/usr/bin/env bash
set -e
CONTAINERS=\$(docker ps --format '{{.Names}}' | grep -E "^${PROJECT}_${CONTAINER}.[0-9]+") echo "docker exec ${DOCKERARGS} ${CONTAINER} ${COMMAND}"
for CONTAINER_NAME in \${CONTAINERS}; do
docker exec "${DOCKERARGS} \${CONTAINER_NAME} ${TMP_COMMAND}"
done
EOF
echo "/bin/bash ${HOME_DIR}/projects/${SCRIPT_NAME}.sh"
else
echo "docker exec ${DOCKERARGS} ${CONTAINER} ${TMP_COMMAND}"
fi
} }
make_cmd() { make_cmd() {
@ -176,84 +167,94 @@ function build_crontab() {
ONSTART=() ONSTART=()
while read -r i ; do while read -r i ; do
SCHEDULE=$(jq -r .["$i"].schedule "${CONFIG}" | sed 's/\*/\\*/g') KEY=$(jq -r .["$i"] "${CONFIG}")
SCHEDULE=$(echo "${KEY}" | jq -r '.schedule' | sed 's/\*/\\*/g')
if [ "${SCHEDULE}" == "null" ]; then if [ "${SCHEDULE}" == "null" ]; then
echo "'schedule' missing: $(jq -r .["$i"].schedule "${CONFIG}")" echo "'schedule' missing: '${KEY}"
continue continue
fi fi
SCHEDULE=$(parse_schedule "${SCHEDULE}" | sed 's/\\//g') SCHEDULE=$(parse_schedule "${SCHEDULE}" | sed 's/\\//g')
COMMAND=$(jq -r .["$i"].command "${CONFIG}") COMMAND=$(echo "${KEY}" | jq -r '.command')
if [ "${COMMAND}" == "null" ]; then if [ "${COMMAND}" == "null" ]; then
echo "'command' missing: '${COMMAND}'" echo "'command' missing: '${KEY}'"
continue continue
fi fi
COMMENT=$(jq -r .["$i"].comment "${CONFIG}") COMMENT=$(echo "${KEY}" | jq -r '.comment')
if [ "${COMMENT}" != "null" ]; then
COMMENT=" ${COMMENT}"
echo "#${COMMENT}" >> ${CRONTAB_FILE}
else
# Reset COMMENT to empty rather than keep the 'null' value.
COMMENT=" "
fi
SCRIPT_NAME=$(jq -r .["$i"].name "${CONFIG}") SCRIPT_NAME=$(echo "${KEY}" | jq -r '.name')
SCRIPT_NAME=$(slugify "${SCRIPT_NAME}") SCRIPT_NAME=$(slugify "${SCRIPT_NAME}")
if [ "${SCRIPT_NAME}" == "null" ]; then if [ "${SCRIPT_NAME}" == "null" ]; then
SCRIPT_NAME=$(cat /proc/sys/kernel/random/uuid) SCRIPT_NAME=$(cat /proc/sys/kernel/random/uuid)
fi fi
COMMAND="/bin/bash ${HOME_DIR}/jobs/${SCRIPT_NAME}.sh" CRON_COMMAND=$(make_cmd "${KEY}")
cat << EOF > "${HOME_DIR}"/jobs/"${SCRIPT_NAME}".sh
#!/usr/bin/env bash
set -e
echo "start cron job **${SCRIPT_NAME}**${COMMENT}" SCRIPT_PATH="${HOME_DIR}/jobs/${SCRIPT_NAME}.sh"
$(make_cmd "$(jq -c .["$i"] "${CONFIG}")")
EOF touch "${SCRIPT_PATH}"
TRIGGER=$(jq -r .["$i"].trigger "${CONFIG}") 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 if [ "${TRIGGER}" != "null" ]; then
while read -r j ; do while read -r j ; do
TRIGGER_COMMAND=$(jq .["$i"].trigger["$j"].command "${CONFIG}") TRIGGER_KEY=$(echo "${KEY}" | jq -r .trigger["$j"])
TRIGGER_COMMAND=$(echo "${TRIGGER_KEY}" | jq -r '.command')
if [ "${TRIGGER_COMMAND}" == "null" ]; then if [ "${TRIGGER_COMMAND}" == "null" ]; then
echo "'command' missing: '${TRIGGER_COMMAND}'"
continue continue
fi fi
make_cmd "${TRIGGER_COMMAND}" >> "${HOME_DIR}"/jobs/"${SCRIPT_NAME}".sh
done < <(jq -r '.['"$i"'].trigger|keys[]' "${CONFIG}") make_cmd "${TRIGGER_KEY}" >> "${SCRIPT_PATH}"
done < <(echo "${KEY}" | jq -r '.trigger | keys[]')
fi fi
echo "echo \"end cron job **${SCRIPT_NAME}**${COMMENT}\"" >> "${HOME_DIR}"/jobs/"${SCRIPT_NAME}".sh echo "echo \"end cron job __${SCRIPT_NAME}__\"" >> "${SCRIPT_PATH}"
echo "${SCHEDULE} ${COMMAND}" >> ${CRONTAB_FILE} if [ "${COMMENT}" != "null" ]; then
echo "# ${COMMENT}" >> ${CRONTAB_FILE}
if [ "$(jq -r .["$i"].onstart "${CONFIG}")" == "true" ]; then
ONSTART+=("${COMMAND}")
fi fi
done < <(jq -r '.|keys[]' "${CONFIG}") echo "${SCHEDULE} ${SCRIPT_PATH}" >> ${CRONTAB_FILE}
echo "##### crontab generation complete #####" 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} cat ${CRONTAB_FILE}
echo "##### run commands with onstart #####" printf "##### run commands with onstart #####\n"
for COMMAND in "${ONSTART[@]}"; do for ONSTART_COMMAND in "${ONSTART[@]}"; do
echo "${COMMAND}" printf "%s\n" "${ONSTART_COMMAND}"
${COMMAND} & ${ONSTART_COMMAND} &
done done
printf "##### cron running #####\n"
} }
start_app() { start_app() {
normalize_config normalize_config
export CONFIG=${HOME_DIR}/config.working.json export CONFIG=${HOME_DIR}/config.working.json
if [ ! -f "${CONFIG}" ]; then if [ ! -f "${CONFIG}" ]; then
echo "generated ${CONFIG} missing. exiting." printf "missing generated %s. exiting.\n" "${CONFIG}"
exit 1 exit 1
fi fi
if [ "${1}" == "crond" ]; then if [ "${1}" == "crond" ]; then
build_crontab build_crontab
fi fi
echo "${@}" printf "%s\n" "${@}"
exec "${@}" exec "${@}"
} }