This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
generated from homecentr/docker-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
499 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
* text=auto | ||
*.sh eol=lf | ||
*.sh eol=lf | ||
cron-tick-execute eol=lf | ||
cron-tick eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
FROM alpine | ||
FROM homecentr/base:2.3.0-alpine | ||
|
||
ENV CRON_SCHEDULE="" | ||
ENV PUSH_GATEWAY_URL="" | ||
|
||
# Copy s6 configuration and scripts | ||
COPY ./fs/ / | ||
|
||
RUN rm /etc/crontabs/root && \ | ||
apk add --no-cache \ | ||
# Required to push metrics to push gateway | ||
curl=7.67.0-r0 \ | ||
# Required for UUID generation | ||
util-linux=2.34-r1 && \ | ||
chmod a+x /usr/sbin/cron-tick-execute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,55 @@ | ||
# HomeCentr - $$IMAGE_NAME$$ | ||
Template repository for Docker container repositories | ||
[](https://github.com/homecentr/docker-cron-base/graphs/commit-activity) | ||
[](https://github.com/homecentr/docker-cron-base/labels/bug) | ||
[](https://hub.docker.com/repository/docker/homecentr/cron-base) | ||
[](https://hub.docker.com/repository/docker/homecentr/cron-base) | ||
[](https://hub.docker.com/repository/docker/homecentr/cron-base) | ||
|
||
## Project status | ||
 | ||
 | ||
|
||
## Usage (Docker compose) | ||
|
||
### Env. variables | ||
### Exposed ports | ||
# HomeCentr - cron-base | ||
This docker image is used as base image for all homecentr images which require a cron scheduler but can also be used on its own. The image executes a mounted script at the specified schedule and reports the results into a Prometheus [push-gateway](https://github.com/prometheus/pushgateway) for easy monitoring and alerting. | ||
|
||
## Usage | ||
|
||
```yml | ||
version: "3.7" | ||
services: | ||
cron-base: | ||
build: . | ||
image: homecentr/cron-base | ||
restart: unless-stopped | ||
environment: | ||
CRON_SCHEDULE: "* * * * *" # Run script every minute | ||
PUSH_GATEWAY_URL: "http://push_gateway:9091/metrics/job/cron/label-name/label-value" | ||
volumes: | ||
- ./example/success:/config # must contain cron-tick script | ||
``` | ||
## Environment variables | ||
| Name | Default value | Description | | ||
|------|---------------|-------------| | ||
| PUID | 7077 | UID of the user the cron-tick script should be running as. | | ||
| PGID | 7077 | GID of the group the cron-tick script should be running as. | | ||
| CRON_SCHEDULE | | [Cron expression](https://crontab.guru/) which defines when/how often the script will be executed. This variable is **mandatory**. | | ||
| PUSH_GATEWAY_URL | | URL of the [push gateway](https://github.com/prometheus/pushgateway) job where the metrics should be reported. The reporting is skipped if the variable is not set. | | ||
## Exposed ports | ||
The image does not expose any ports. | ||
## Volumes | ||
| Container path | Description | | ||
|-------------|----------------| | ||
| /config | Directory containing the script which should be executed, the script must be named `cron-tick`. | | ||
|
||
## Security | ||
The container is regularly scanned for vulnerabilities and updated. Further info can be found in the [Security tab](https://github.com/homecentr/docker-cron-base/security). | ||
|
||
### Container user | ||
The container supports privilege drop. Even though the container starts as root, it will use the permissions only to perform the initial set up. The cron-tick script is executed as UID/GID provided in the PUID and PGID environment variables. | ||
|
||
### Vulnerabilities | ||
:warning: Do not change the container user directly using the `user` Docker compose property or using the `--user` argument. This would break the privilege drop logic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
version: "3.7" | ||
services: | ||
$$IMAGE_NAME$$: | ||
cron: | ||
build: . | ||
image: homecentr/$$IMAGE_NAME$$ | ||
restart: unless-stopped | ||
image: homecentr/cron-base:local | ||
environment: | ||
CRON_SCHEDULE: "* * * * *" | ||
PUSH_GATEWAY_URL: "http://push_gateway:9091/metrics/job/cron/instance/base" | ||
volumes: | ||
- ./example/cron-tick:/config/cron-tick | ||
#- ./example/cron-tick-fail:/config/cron-tick | ||
push_gateway: | ||
image: prom/pushgateway | ||
ports: | ||
- 9091:9091 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env ash | ||
|
||
sleep 2 | ||
|
||
exit 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env ash | ||
|
||
|
||
mktemp -d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/with-contenv sh | ||
|
||
cp /config/cron-tick /usr/sbin/cron-tick | ||
chmod a+x /usr/sbin/cron-tick |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/with-contenv sh | ||
|
||
if [ "$CRON_SCHEDULE" == "" ] | ||
then | ||
echo "The env. variable CRON_SCHEDULE is not set but is mandatory." | ||
exit 1 | ||
fi | ||
|
||
USER_NAME=$(getent passwd "$PUID" | cut -d: -f1) | ||
echo "$CRON_SCHEDULE /usr/sbin/cron-tick-execute" > /etc/crontabs/$USER_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/execlineb -S1 | ||
|
||
s6-svscanctl -t /var/run/s6/services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/with-contenv sh | ||
|
||
# crond must be ALWAYS started as root (requires setpgid) | ||
# exec s6-setuidgid "$PUID:$PGID" crond -f -d 2 | ||
|
||
exec crond -f -l 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env ash | ||
EXEC_ID=$(uuidgen) | ||
ALREADY_RUNNING=$(pgrep -f /usr/sbin/cron-tick-execute > /dev/null 2> /dev/null || "YES") | ||
|
||
if [ "$ALREADY_RUNNING" == "YES" ] | ||
then | ||
echo "The last execution is still running. Skipping this one..." | ||
exit 0 | ||
fi | ||
|
||
echo "====== Execution started ($(date), $EXEC_ID) =======" | ||
|
||
OUTPUT=$(time -f "TIME_OUTPUT|%x|%e" /usr/sbin/cron-tick 2>&1) | ||
|
||
TICK_EXIT_CODE=$(echo "$OUTPUT" | grep "TIME_OUTPUT" | cut -d'|' -f2 | tr -d '[:space:]') | ||
TICK_DURATION=$(echo "$OUTPUT" | grep "TIME_OUTPUT" | cut -d'|' -f3 | tr -d '[:space:]') | ||
|
||
echo "Script exit code: $TICK_EXIT_CODE, execution duration: $TICK_DURATION seconds" | ||
|
||
if [ "$PUSH_GATEWAY_URL" == "" ] | ||
then | ||
echo "PUSH_GATEWAY_URL variable not set, skipping pushing metrics to push gateway." | ||
else | ||
echo "Pushing metrics to $PUSH_GATEWAY_URL" | ||
|
||
cat <<EOF | curl -s --data-binary @- "$PUSH_GATEWAY_URL" | ||
# TYPE exit_code gauge | ||
exit_code $TICK_EXIT_CODE | ||
# TYPE duration_seconds gauge | ||
duration_seconds $TICK_DURATION | ||
EOF | ||
|
||
fi | ||
|
||
echo "====== Execution finished ($(date), $EXEC_ID) =======" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
{ | ||
"name": "homecentr-$$IMAGE_NAME$$", | ||
"name": "homecentr-cron-base", | ||
"version": "1.0.0", | ||
"description": "", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/homecentr/docker-$$IMAGE_NAME$$.git" | ||
"url": "git+https://github.com/homecentr/docker-cron-base.git" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/homecentr/docker-$$IMAGE_NAME$$/issues" | ||
"url": "https://github.com/homecentr/docker-cron-base/issues" | ||
}, | ||
"homepage": "https://github.com/homecentr/docker-$$IMAGE_NAME$$#readme" | ||
"homepage": "https://github.com/homecentr/docker-cron-base#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.