docker-crontab/docker-entrypoint

160 lines
4.4 KiB
Plaintext
Raw Normal View History

2017-02-12 16:28:53 -07:00
#!/usr/bin/env bash
set -e
# for local testing only
#HOME_DIR=.
CONFIG=${HOME_DIR}/config.json
DOCKER_SOCK=/var/run/docker.sock
CRONTAB_FILE=${HOME_DIR}/docker
make_image_cmd() {
2017-02-12 17:05:46 -07:00
DOCKERARGS=$(echo ${1} | jq -r .dockerargs)
2017-02-12 16:28:53 -07:00
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
2017-02-12 17:05:46 -07:00
IMAGE=$(echo ${1} | jq -r .image)
TMP_COMMAND=$(echo ${1} | jq -r .command)
2017-02-12 16:28:53 -07:00
echo "docker run ${DOCKERARGS} ${IMAGE} ${TMP_COMMAND}"
}
make_container_cmd() {
2017-02-12 17:05:46 -07:00
DOCKERARGS=$(echo ${1} | jq -r .dockerargs)
2017-02-12 16:28:53 -07:00
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
2017-02-12 17:05:46 -07:00
PROJECT=$(echo ${1} | jq -r .project)
CONTAINER=$(echo ${1} | jq -r .container)
TMP_COMMAND=$(echo ${1} | jq -r .command)
2017-02-12 16:28:53 -07:00
COMMAND_ARR=()
if [ "${PROJECT}" != "null" ]; then
# create bash script to detect all running containers
SCRIPT_NAME=$(cat /proc/sys/kernel/random/uuid)
cat << EOF > ${HOME_DIR}/${SCRIPT_NAME}
2017-02-12 21:37:00 -07:00
#!/usr/bin/env bash
2017-02-12 16:28:53 -07:00
set -e
2017-02-12 21:37:00 -07:00
CONTAINERS=\$(docker ps --format '{{.Names}}')
2017-02-12 16:28:53 -07:00
for CONTAINER_NAME in \$CONTAINERS; do
if [[ "\${CONTAINER_NAME}" =~ ^${PROJECT}_${CONTAINER}.+ ]]; then
docker exec ${DOCKERARGS} \${CONTAINER_NAME} ${TMP_COMMAND}
fi
done
EOF
2017-02-12 21:37:00 -07:00
echo "/bin/bash ${HOME_DIR}/${SCRIPT_NAME}"
2017-02-12 16:28:53 -07:00
else
echo "docker exec ${DOCKERARGS} ${CONTAINER} ${TMP_COMMAND}"
fi
}
make_cmd() {
2017-02-12 21:08:45 -07:00
if [ "$(echo ${1} | jq -r .image)" != "null" ]; then
make_image_cmd "$1"
elif [ "$(echo ${1} | jq -r .container)" != "null" ]; then
make_container_cmd "$1"
2017-02-12 16:28:53 -07:00
else
2017-02-12 21:08:45 -07:00
echo ${1} | jq -r .command
2017-02-12 16:28:53 -07:00
fi
}
parse_schedule() {
case $1 in
"@yearly")
echo "0 0 0 1 1 *"
;;
"@annually")
echo "0 0 0 1 1 *"
;;
"@monthly")
echo "0 0 0 1 * *"
;;
"@weekly")
echo "0 0 0 * * 0"
;;
"@daily")
echo "0 0 0 * * *"
;;
"@midnight")
echo "0 0 0 * * *"
;;
"@hourly")
echo "0 0 * * * *"
;;
"@every")
TIME=$2
TOTAL=0
M=$(echo $TIME | grep -o '[0-9]\+m')
H=$(echo $TIME | grep -o '[0-9]\+h')
D=$(echo $TIME | grep -o '[0-9]\+d')
if [ -n "${M}" ]; then
TOTAL=$(($TOTAL + ${M::-1}))
fi
if [ -n "${H}" ]; then
TOTAL=$(($TOTAL + ${H::-1} * 60))
fi
if [ -n "${D}" ]; then
TOTAL=$(($TOTAL + ${D::-1} * 60 * 24))
fi
echo "*/${TOTAL} * * * * *"
;;
*)
echo "${@}"
;;
esac
}
function build_crontab() {
rm -rf ${CRONTAB_FILE}
while read i ; do
SCHEDULE=$(jq -r .[$i].schedule ${CONFIG} | sed 's/\*/\\*/g')
if [ "${SCHEDULE}" == "null" ]; then
echo "Schedule Missing: $(jq -r .[$i].schedule ${CONFIG})"
continue
fi
SCHEDULE=$(parse_schedule ${SCHEDULE} | sed 's/\\//g')
if [ "$(jq -r .[$i].command ${CONFIG})" == "null" ]; then
echo "Command Missing: $(jq -r .[$i].command ${CONFIG})"
continue
fi
2017-02-12 17:05:46 -07:00
COMMAND=$(make_cmd "$(jq -c .[$i] ${CONFIG})")
2017-02-12 16:28:53 -07:00
if [ "$(jq -r .[$i].trigger ${CONFIG})" != "null" ]; then
while read j ; do
if [ "$(jq .[$i].trigger[$j].command ${CONFIG})" == "null" ]; then
echo "Command Missing: $(jq -r .[$i].trigger[$j].command ${CONFIG})"
continue
fi
2017-02-12 17:05:46 -07:00
TRIGGER_COMMANDS=$(make_cmd "$(jq -c .[$i].trigger[$j] ${CONFIG})")
COMMAND="${COMMAND} && ${TRIGGER_COMMANDS}"
2017-02-12 16:28:53 -07:00
done < <(jq -r '.['$i'].trigger|keys[]' ${CONFIG})
fi
NAME=$(jq -r .[$i].name ${CONFIG})
COMMENT=$(jq -r .[$i].comment ${CONFIG})
if [ "${NAME}" != "null" ] && [ "${COMMENT}" != "null" ]; then
echo "# ${NAME}: ${COMMENT}" >> ${CRONTAB_FILE}
elif [ "${COMMENT}" != "null" ]; then
echo "# ${COMMENT}" >> ${CRONTAB_FILE}
fi
echo "${SCHEDULE} ${COMMAND}" >> ${CRONTAB_FILE}
done < <(jq -r '.|keys[]' ${CONFIG})
echo "crontab generation complete"
cat ${CRONTAB_FILE}
}
2017-02-12 16:57:19 -07:00
if [ "$1" = "crond" ]; then
if [ -f ${CONFIG} ]; then
build_crontab
else
echo "Unable to find ${HOME_DIR}/config.json"
fi
2017-02-12 16:28:53 -07:00
fi
echo "$@"
exec "$@"