20 Commits
0.1.1 ... 0.5.0

Author SHA1 Message Date
ac1760ce71 Merge pull request #27 from sergey-shambir/docs-for-docker-compose-use-case
Documented usage with docker-compose
2019-03-17 13:10:49 -06:00
68fa96e2e8 Merge pull request #25 from sergey-shambir/issue_20
Use another way to ensure docker socket is accessible
2019-03-17 13:10:08 -06:00
b0f5681faf Merge pull request #26 from sergey-shambir/docker-cmd-fix
Improve crond command in Dockerfile
2019-03-17 13:09:12 -06:00
945357ce4a Merge pull request #24 from sergey-shambir/envsubst
Added support for envsubst in image/container name
2019-03-17 13:08:19 -06:00
5d9d345dd5 Merge pull request #23 from iusmac/patch-1
Fix issue "ambiguous redirect"
2019-03-04 09:21:45 -07:00
5946f3545d Documented usage with docker-compose 2019-03-02 16:16:07 +03:00
46407e1e27 Turned off verbose logging
For busybox cron level 6 is enough to see errors and hide debug info
Debug info shown at level 5
Nothing shown at levels 0, 1, 2, 3, 4
See https://unix.stackexchange.com/questions/412805/crond-log-level-meaning
2019-03-02 16:02:57 +03:00
e3afe6c3e5 Fix crond command in Dockerfile
- added logging to docker logs
- run with /etc/crontabs dir
See also https://unix.stackexchange.com/questions/412805/crond-log-level-meaning
2019-03-02 16:02:52 +03:00
0a4e3f5753 Use another way to ensure docker socket is accessible
- check if socket group ID is presented in /etc/group
- if presented, just add 'docker' user to this group
- if not, create 'docker' group and add 'docker' user to this group
Fixes issue #20
2019-03-02 16:01:40 +03:00
71661b4971 Added support for envsubst in image/container name
Envsubst allows to use dynamic container name
We use image name env substitution with docker-compose and .env to select right image tag for local development
Within alpine, envsubst installed with gettext package.
2019-03-02 15:49:26 +03:00
cba3bcb0d0 Fix issue "ambiguous redirect"
Full error message: "/docker-entrypoint.sh: line 58: ${HOME_DIR}/projects/${SCRIPT_NAME}.sh: ambiguous redirect"

The error appears when in Config.json is present `name` and `container`.
2019-03-01 20:50:12 +01:00
02a89f6dac fix: clean up logic and only apply group if needed 2018-06-30 15:23:27 -06:00
b89e312569 fix: Change group id
Change group id to not conflict with host common id

Fix #13
2018-02-27 23:44:47 -07:00
f601ca262d Merge pull request #11 from kendokan/master
fixed failure if docker group already existed
2018-01-11 09:15:18 -07:00
b52842b07a fixed failure if docker group already existed
This error prevents the container from restarting.
2018-01-11 08:13:11 -08:00
147e900f8b Merge pull request #10 from kendokan/master
fixed https://github.com/willfarrell/docker-crontab/issues/9
2018-01-09 20:16:50 -07:00
40b8b93c2a fixed https://github.com/willfarrell/docker-crontab/issues/9 2018-01-09 13:17:49 -08:00
d5cfe3bc76 #6 Added adduser to the build script 2017-11-14 14:04:30 -07:00
d5f4764d25 Fix #7
slugifys name
2017-11-14 13:54:20 -07:00
61ef1db4bf Change documentation showing seconds in cron schedule in error 2017-10-23 09:45:24 -06:00
6 changed files with 64 additions and 20 deletions

1
.gitignore vendored
View File

@ -1,5 +1,4 @@
.idea
*.iml
home_dir
config.json

View File

@ -1,8 +1,9 @@
FROM library/docker:stable
ENV HOME_DIR=/opt/crontab
RUN apk add --no-cache --virtual .run-deps bash jq \
&& mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects
RUN apk add --no-cache --virtual .run-deps gettext bash jq \
&& mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects \
&& adduser -S docker -D
COPY docker-entrypoint /
ENTRYPOINT ["/docker-entrypoint"]
@ -10,4 +11,4 @@ ENTRYPOINT ["/docker-entrypoint"]
HEALTHCHECK --interval=5s --timeout=3s \
CMD ps aux | grep '[c]rond' || exit 1
CMD ["crond","-f"]
CMD ["crond", "-f", "-d", "6", "-c", "/etc/crontabs"]

View File

@ -22,9 +22,9 @@ A great project, don't get me wrong. It was just missing certain key enterprise
- Ability to trigger scripts in other containers on completion cron job using `trigger`.
## Config.json
- `name`: Human readable name that will be used as teh job filename. 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.
- `schedule`: Crontab schedule syntax as described in https://godoc.org/github.com/robfig/cron. Ex `@hourly`, `@every 1h30m`, `* * * * * *`. Required.
- `schedule`: Crontab schedule syntax as described in https://en.wikipedia.org/wiki/Cron. Ex `@hourly`, `@every 1h30m`, `* * * * *`. 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.
- `project`: Docker Compose/Swarm project name. Optional, only applies when `contain` is included.
@ -57,6 +57,7 @@ See [`config.sample.json`](https://github.com/willfarrell/docker-crontab/blob/ma
## How to use
### Command Line
```bash
docker build -t crontab .
docker run -d \
@ -67,7 +68,19 @@ docker run -d \
crontab
```
### Use with docker-compose
1. Figure out which network name used for your docker-compose containers
* use `docker network ls` to see existing networks
* if your `docker-compose.yml` is in `my_dir` directory, you probably has network `my_dir_default`
* otherwise [read the docker-compose docs](https://docs.docker.com/compose/networking/)
2. Add `dockerargs` to your docker-crontab `config.json`
* use `--network NETWORK_NAME` to connect new container into docker-compose network
* use `--rm --name NAME` to use named container
* e.g. `"dockerargs": "--network my_dir_default --rm --name my-best-cron-job"`
### Dockerfile
```Dockerfile
FROM willfarrell/crontab
@ -76,6 +89,7 @@ COPY config.json ${HOME_DIR}/
```
### Logrotate Dockerfile
```Dockerfile
FROM willfarrell/crontab
@ -87,13 +101,13 @@ CMD ["crond", "-f"]
```
### Logging - In Dev
All `stdout` is captured, formatted, and saved to `/var/log/crontab/jobs.log`. Set `LOG_FILE` to `/dev/null` to disable logging.
example: `e6ced859-1563-493b-b1b1-5a190b29e938 2017-06-18T01:27:10+0000 [info] Start Cronjob **map-a-vol** map a volume`
grok: `CRONTABLOG %{DATA:request_id} %{TIMESTAMP_ISO8601:timestamp} \[%{LOGLEVEL:severity}\] %{GREEDYDATA:message}`
## TODO
- [ ] Have ability to auto regenerate crontab on file change (signal HUP?)
- [ ] Run commands on host machine (w/ --privileged?)

View File

@ -12,4 +12,4 @@ services:
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
# - "/usr/bin/docker:/usr/bin/docker:ro"
- "/Users/willfarrell/Development/docker/docker-crontab/home_dir:/opt/crontab:rw"
- "/Users/willfarrell/Development/docker/docker-crontab/config.json:/opt/crontab/config.json:rw"

View File

@ -12,7 +12,7 @@ if [ "${LOG_FILE}" == "" ]; then
LOG_DIR=/var/log/crontab
LOG_FILE=${LOG_DIR}/jobs.log
mkdir -p ${LOG_DIR}
touch ${LOG_FILE}
touch ${LOG_FILE}
fi
CONFIG=${HOME_DIR}/config.json
@ -22,11 +22,35 @@ CRONTAB_FILE=/etc/crontabs/docker
# Ensure dir exist - in case of volume mapping
mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects
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 A-Z a-z
}
make_image_cmd() {
DOCKERARGS=$(echo ${1} | jq -r .dockerargs)
if [ "${DOCKERARGS}" == "null" ]; then DOCKERARGS=; fi
IMAGE=$(echo ${1} | jq -r .image)
IMAGE=$(echo ${1} | jq -r .image | envsubst)
TMP_COMMAND=$(echo ${1} | jq -r .command)
echo "docker run ${DOCKERARGS} ${IMAGE} ${TMP_COMMAND}"
}
@ -35,8 +59,9 @@ make_container_cmd() {
DOCKERARGS=$(echo ${1} | jq -r .dockerargs)
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)
CONTAINER=$(echo ${1} | jq -r .container | envsubst)
TMP_COMMAND=$(echo ${1} | jq -r .command)
if [ "${PROJECT}" != "null" ]; then
@ -81,25 +106,25 @@ make_cmd() {
parse_schedule() {
case $1 in
"@yearly")
echo "0 0 0 1 1 *"
echo "0 0 1 1 *"
;;
"@annually")
echo "0 0 0 1 1 *"
echo "0 0 1 1 *"
;;
"@monthly")
echo "0 0 0 1 * *"
echo "0 0 1 * *"
;;
"@weekly")
echo "0 0 0 * * 0"
echo "0 0 * * 0"
;;
"@daily")
echo "0 0 0 * * *"
echo "0 0 * * *"
;;
"@midnight")
echo "0 0 0 * * *"
echo "0 0 * * *"
;;
"@hourly")
echo "0 0 * * * *"
echo "0 * * * *"
;;
"@every")
TIME=$2
@ -119,7 +144,7 @@ parse_schedule() {
TOTAL=$(($TOTAL + ${D::-1} * 60 * 24))
fi
echo "*/${TOTAL} * * * * *"
echo "*/${TOTAL} * * * *"
;;
*)
echo "${@}"
@ -151,6 +176,7 @@ function build_crontab() {
fi
SCRIPT_NAME=$(jq -r .[$i].name ${CONFIG})
SCRIPT_NAME=$(slugify $SCRIPT_NAME)
if [ "${SCRIPT_NAME}" == "null" ]; then
SCRIPT_NAME=$(cat /proc/sys/kernel/random/uuid)
fi
@ -204,6 +230,8 @@ EOF
done
}
ensure_docker_socket_accessible
if [ "$1" = "crond" ]; then
if [ -f ${CONFIG} ]; then
build_crontab

View File

@ -1,6 +1,8 @@
#!/usr/bin/env bash
set -e
# This file is for testing the logging of docker output #8
LOG_FILE=./jobs.log
touch ${LOG_FILE}
UUID="xxxxxxxxxxxxxxxxx"