Compare commits

..

No commits in common. "main" and "0.1.0" have entirely different histories.
main ... 0.1.0

15 changed files with 83 additions and 465 deletions

12
.github/FUNDING.yml vendored
View File

@ -1,12 +0,0 @@
# These are supported funding model platforms
github: [willfarrell]# Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

View File

@ -1,49 +0,0 @@
name: build
on:
push:
branches:
- main
tags:
- '*'
schedule:
- cron: '0 0 * * *'
jobs:
multi:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
-
name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- if: github.ref == 'refs/heads/main'
name: Conditional(Set tag as `latest`)
run: echo "tag=willfarrell/crontab:latest" >> $GITHUB_ENV
- if: startsWith(github.ref, 'refs/tags/')
name: Conditional(Set tag as `{version}`)
run: echo "tag=willfarrell/crontab:${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
-
name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ env.tag }}

3
.gitignore vendored
View File

@ -1,6 +1,5 @@
.idea
*.iml
home_dir
config.json
.vscode
.DS_Store

View File

@ -1,26 +1,13 @@
FROM alpine:3.12 as rq-build
ENV RQ_VERSION=1.0.2
WORKDIR /root/
RUN apk --update add upx \
&& wget https://github.com/dflemstr/rq/releases/download/v${RQ_VERSION}/rq-v${RQ_VERSION}-x86_64-unknown-linux-musl.tar.gz \
&& tar -xvf rq-v1.0.2-x86_64-unknown-linux-musl.tar.gz \
&& upx --brute rq
FROM library/docker:stable
COPY --from=rq-build /root/rq /usr/local/bin
ENV HOME_DIR=/opt/crontab
RUN apk add --no-cache --virtual .run-deps gettext jq bash tini \
&& mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects \
&& adduser -S docker -D
RUN apk add --no-cache --virtual .run-deps bash jq \
&& mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects
COPY docker-entrypoint /
ENTRYPOINT ["/sbin/tini", "--", "/docker-entrypoint"]
ENTRYPOINT ["/docker-entrypoint"]
HEALTHCHECK --interval=5s --timeout=3s \
CMD ps aux | grep '[c]rond' || exit 1
CMD ["crond", "-f", "-d", "6", "-c", "/etc/crontabs"]
CMD ["crond","-f"]

21
LICENSE
View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2019 will Farrell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -5,13 +5,12 @@ A simple wrapper over `docker` to all complex cron job to be run in other contai
## Supported tags and Dockerfile links
- [`latest` (*Dockerfile*)](https://github.com/willfarrell/docker-crontab/blob/master/Dockerfile)
- [`1.0.0` (*Dockerfile*)](https://github.com/willfarrell/docker-crontab/blob/1.0.0/Dockerfile)
- [`0.6.0` (*Dockerfile*)](https://github.com/willfarrell/docker-crontab/blob/0.6.0/Dockerfile)
![](https://img.shields.io/docker/pulls/willfarrell/crontab "Total docker pulls") [![](https://images.microbadger.com/badges/image/willfarrell/crontab.svg)](http://microbadger.com/images/willfarrell/crontab "Get your own image badge on microbadger.com")
[![](https://images.microbadger.com/badges/version/willfarrell/crontab.svg)](http://microbadger.com/images/willfarrell/crontab "Get your own version badge on microbadger.com") [![](https://images.microbadger.com/badges/image/willfarrell/crontab.svg)](http://microbadger.com/images/willfarrell/crontab "Get your own image badge on microbadger.com")
## 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) (280MB), it was the main inspiration for this project.
A great project, don't get me wrong. It was just missing certain key enterprise features I felt were required to support where docker is heading.
## Features
@ -22,13 +21,10 @@ A great project, don't get me wrong. It was just missing certain key enterprise
- Run command on a instances of a scaled container using `project`.
- Ability to trigger scripts in other containers on completion cron job using `trigger`.
## 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)
- `name`: Human readable name that will be used as the job filename. Will be converted into a slug. Optional.
## Config.json
- `name`: Human readable name that will be used as teh job filename. 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://godoc.org/github.com/robfig/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.
@ -37,7 +33,7 @@ The config file can be specifed in any of `json`, `toml`, or `yaml`, and can be
- `trigger`: Array of docker-crontab subset objects. Subset includes: `image`,`project`,`container`,`command`,`dockerargs`
- `onstart`: Run the command on `crontab` container start, set to `true`. Optional, defaults to falsey.
See [`config-samples`](config-samples) for examples.
See [`config.sample.json`](https://github.com/willfarrell/docker-crontab/blob/master/config.sample.json) for examples.
```json
[{
@ -61,7 +57,6 @@ See [`config-samples`](config-samples) for examples.
## How to use
### Command Line
```bash
docker build -t crontab .
docker run -d \
@ -72,19 +67,7 @@ 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
@ -93,7 +76,6 @@ COPY config.json ${HOME_DIR}/
```
### Logrotate Dockerfile
```Dockerfile
FROM willfarrell/crontab
@ -105,15 +87,15 @@ 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?)
- [ ] Write tests
- [ ] Setup TravisCI
- [ ] Setup TravisCI

View File

@ -1,60 +0,0 @@
[
{
"comment": "cron with triggered commands",
"schedule": "* * * * *",
"command": "echo hello",
"project": "crontab",
"container": "myapp",
"trigger": [
{
"command": "echo world",
"container": "crontab_myapp_1"
}
]
},
{
"comment": "map a volume",
"schedule": "* * * * *",
"dockerargs": "-d -v /tmp:/tmp",
"command": "echo new",
"image": "alpine:3.5"
},
{
"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"
},
{
"comment": "trigger every 2 min",
"schedule": "@every 2m",
"command": "echo 2 minute",
"image": "alpine:3.5",
"trigger": [
{
"command": "echo world",
"container": "crontab_myapp_1"
}
]
},
{
"schedule": "*/5 * * * *",
"command": "/usr/sbin/logrotate /etc/logrotate.conf"
},
{
"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 webapp_nginx_tls_cert:/etc/ssl -v webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge",
"image": "willfarrell/letsencrypt",
"trigger": [
{
"command": "sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'",
"project": "conduit",
"container": "nginx"
}
],
"onstart": true
}
]

View File

@ -1,50 +0,0 @@
{
"cron with triggered commands": {
"comment": "cron with triggered commands",
"schedule": "* * * * *",
"command": "echo hello",
"project": "crontab",
"container": "myapp",
"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": [{ "command": "echo world", "container": "crontab_myapp_1" }]
},
"null": {
"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 webapp_nginx_tls_cert:/etc/ssl -v webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge",
"image": "willfarrell/letsencrypt",
"trigger": [
{
"command": "sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'",
"project": "conduit",
"container": "nginx"
}
],
"onstart": true
}
}

View File

@ -1,46 +0,0 @@
cron with triggered commands:
command: echo hello
comment: cron with triggered commands
container: myapp
project: crontab
schedule: '* * * * *'
trigger:
- command: echo world
container: crontab_myapp_1
map a volume:
command: echo new
comment: map a volume
dockerargs: -d -v /tmp:/tmp
image: alpine:3.5
schedule: '* * * * *'
use an ENV from inside a container:
command: sh -c 'echo hourly ${FOO}'
comment: use an ENV from inside a container
dockerargs: -d -e FOO=BAR
image: alpine:3.5
schedule: '@hourly'
trigger every 2 min:
command: echo 2 minute
comment: trigger every 2 min
image: alpine:3.5
schedule: '@every 2m'
trigger:
- command: echo world
container: crontab_myapp_1
null:
command: /usr/sbin/logrotate /etc/logrotate.conf
schedule: '*/5 * * * *'
Regenerate Certificate then reload nginx:
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
image: willfarrell/letsencrypt
onstart: true
schedule: 43 6,18 * * *
trigger:
- command: sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t &&
/usr/sbin/nginx -s reload'
container: nginx
project: conduit

View File

@ -1,50 +0,0 @@
# toml files can only have top-loevl mappings, so this is the only sample
["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"

View File

@ -1,40 +0,0 @@
- command: echo hello
comment: cron with triggered commands
container: myapp
project: crontab
schedule: '* * * * *'
trigger:
- command: echo world
container: crontab_myapp_1
- command: echo new
comment: map a volume
dockerargs: -d -v /tmp:/tmp
image: alpine:3.5
schedule: '* * * * *'
- command: sh -c 'echo hourly ${FOO}'
comment: use an ENV from inside a container
dockerargs: -d -e FOO=BAR
image: alpine:3.5
schedule: '@hourly'
- command: echo 2 minute
comment: trigger every 2 min
image: alpine:3.5
schedule: '@every 2m'
trigger:
- command: echo world
container: crontab_myapp_1
- command: /usr/sbin/logrotate /etc/logrotate.conf
schedule: '*/5 * * * *'
- 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
image: willfarrell/letsencrypt
onstart: true
schedule: 43 6,18 * * *
trigger:
- command: sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t &&
/usr/sbin/nginx -s reload'
container: nginx
project: conduit

47
config.sample.json Normal file
View File

@ -0,0 +1,47 @@
[{
"comment":"cron with triggered commands",
"schedule":"* * * * *",
"command":"echo hello",
"project":"crontab",
"container":"myapp",
"trigger":[{
"command":"echo world",
"container":"crontab_myapp_1"
}]
},{
"comment":"map a volume",
"schedule":"* * * * *",
"dockerargs":"-d -v /tmp:/tmp",
"command":"echo new",
"image":"alpine:3.5"
},{
"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"
},{
"comment":"trigger every 2 min",
"schedule":"@every 2m",
"command":"echo 2 minute",
"image":"alpine:3.5",
"trigger":[{
"command":"echo world",
"container":"crontab_myapp_1"
}]
},{
"schedule":"*/5 * * * *",
"command":"/usr/sbin/logrotate /etc/logrotate.conf"
},{
"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 webapp_nginx_tls_cert:/etc/ssl -v webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge",
"image":"willfarrell/letsencrypt",
"trigger":[{
"command":"sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'",
"project":"conduit",
"container":"nginx"
}],
"onstart":true
}]

View File

@ -11,4 +11,5 @@ services:
restart: always
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "${PWD}/config-samples/config.sample.mapping.json:/opt/crontab/config.json:rw"
# - "/usr/bin/docker:/usr/bin/docker:ro"
- "/Users/willfarrell/Development/docker/docker-crontab/home_dir:/opt/crontab:rw"

View File

@ -12,71 +12,21 @@ 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
get_config() {
if [ -f "${HOME_DIR}/config.json" ]; then
jq 'map(.)' ${HOME_DIR}/config.json > ${HOME_DIR}/config.working.json
elif [ -f "${HOME_DIR}/config.toml" ]; then
rq -t <<< $(cat ${HOME_DIR}/config.toml) | jq 'map(.)' > ${HOME_DIR}/config.json
elif [ -f "${HOME_DIR}/config.yml" ]; then
rq -y <<< $(cat ${HOME_DIR}/config.yml) | jq 'map(.)' > ${HOME_DIR}/config.json
elif [ -f "${HOME_DIR}/config.yaml" ]; then
rq -y <<< $(cat ${HOME_DIR}/config.yaml) | jq 'map(.)' > ${HOME_DIR}/config.json
fi
}
CONFIG=${HOME_DIR}/config.json
DOCKER_SOCK=/var/run/docker.sock
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)
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)
IMAGE=$(echo ${1} | jq -r .image)
TMP_COMMAND=$(echo ${1} | jq -r .command)
echo "docker run ${DOCKERARGS} ${IMAGE} ${TMP_COMMAND}"
}
@ -85,9 +35,8 @@ 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 | envsubst)
CONTAINER=$(echo ${1} | jq -r .container)
TMP_COMMAND=$(echo ${1} | jq -r .command)
if [ "${PROJECT}" != "null" ]; then
@ -100,13 +49,12 @@ 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]+")
CONTAINERS=\$(docker ps --format '{{.Names}}' | grep -E "^${PROJECT}_${CONTAINER}_[0-9]+")
for CONTAINER_NAME in \$CONTAINERS; do
docker exec ${DOCKERARGS} \${CONTAINER_NAME} ${TMP_COMMAND}
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
@ -133,25 +81,25 @@ make_cmd() {
parse_schedule() {
case $1 in
"@yearly")
echo "0 0 1 1 *"
echo "0 0 0 1 1 *"
;;
"@annually")
echo "0 0 1 1 *"
echo "0 0 0 1 1 *"
;;
"@monthly")
echo "0 0 1 * *"
echo "0 0 0 1 * *"
;;
"@weekly")
echo "0 0 * * 0"
echo "0 0 0 * * 0"
;;
"@daily")
echo "0 0 * * *"
echo "0 0 0 * * *"
;;
"@midnight")
echo "0 0 * * *"
echo "0 0 0 * * *"
;;
"@hourly")
echo "0 * * * *"
echo "0 0 * * * *"
;;
"@every")
TIME=$2
@ -171,7 +119,7 @@ parse_schedule() {
TOTAL=$(($TOTAL + ${D::-1} * 60 * 24))
fi
echo "*/${TOTAL} * * * *"
echo "*/${TOTAL} * * * * *"
;;
*)
echo "${@}"
@ -180,7 +128,6 @@ parse_schedule() {
}
function build_crontab() {
rm -rf ${CRONTAB_FILE}
ONSTART=()
@ -204,7 +151,6 @@ 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
@ -258,27 +204,13 @@ EOF
done
}
ensure_docker_socket_accessible
start_app() {
get_config
if [ -f "${HOME_DIR}/config.working.json" ]; then
export CONFIG=${HOME_DIR}/config.working.json
elif [ -f "${HOME_DIR}/config.json" ]; then
export CONFIG=${HOME_DIR}/config.json
if [ "$1" = "crond" ]; then
if [ -f ${CONFIG} ]; then
build_crontab
else
echo "NO CONFIG FILE FOUND"
echo "Unable to find ${HOME_DIR}/config.json"
fi
if [ "$1" = "crond" ]; then
if [ -f ${CONFIG} ]; then
build_crontab
else
echo "Unable to find ${CONFIG}"
fi
fi
echo "$@"
exec "$@"
}
fi
start_app "$@"
echo "$@"
exec "$@"

View File

@ -1,8 +1,6 @@
#!/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"