diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ad9d606 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM alpine + +RUN apk add --no-cache docker bash + +ENV NEXTCLOUD_CONTAINER_NAME= +ENV NEXTCLOUD_PROJECT_NAME= +ENV NEXTCLOUD_CRON_MINUTE_INTERVAL=15 + +COPY scripts/*.sh / + +ENTRYPOINT ["/entrypoint.sh"] + +HEALTHCHECK --timeout=5s \ + CMD ["/healthcheck.sh"] diff --git a/scripts/cron-tasks.sh b/scripts/cron-tasks.sh new file mode 100755 index 0000000..4e1fd09 --- /dev/null +++ b/scripts/cron-tasks.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -ex + +containerName="$1" +docker exec "$containerName" php -f /var/www/html/cron.php diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh new file mode 100755 index 0000000..7fdf8c9 --- /dev/null +++ b/scripts/entrypoint.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +set -ex + +if [[ -z "$NEXTCLOUD_CONTAINER_NAME" ]]; then + echo "NEXTCLOUD_CONTAINER_NAME is a required variable" + exit 1 +fi + +if [[ ! -z "$NEXTCLOUD_PROJECT_NAME" ]]; then + containerName="${NEXTCLOUD_PROJECT_NAME}_" +else + matchEnd="," +fi + +containerName="${containerName}${NEXTCLOUD_CONTAINER_NAME}" + +# Get the ID of the container so we can exec something in it later +export containerId=$(/find-container.sh "$containerName" "$matchEnd") + +if [[ -z "$containerId" ]]; then + echo "ERROR: Unable to find the Nextcloud container" + exit 1 +fi + +echo "$containerId" > /tmp/containerId + +echo "*/$NEXTCLOUD_CRON_MINUTE_INTERVAL * * * * /cron-tasks.sh $containerId" \ + > /var/spool/cron/crontabs/root + +exec crond -f -l 0 -L /dev/stdout diff --git a/scripts/find-container.sh b/scripts/find-container.sh new file mode 100755 index 0000000..19ebd8d --- /dev/null +++ b/scripts/find-container.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -e + +containerName="$1" +matchEnd="$2" + +docker ps --format '{{.Names}},{{.ID}}' | \ + egrep "^${containerName}${matchEnd}" | \ + awk '{split($0,a,","); print a[2]}' diff --git a/scripts/healthcheck.sh b/scripts/healthcheck.sh new file mode 100755 index 0000000..c58f6d1 --- /dev/null +++ b/scripts/healthcheck.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -x + +# Make sure cron daemon is still running +ps -o comm | grep crond || exit 1 + +# Make sure the target container is still running/available +docker inspect -f '{{.State.Running}}' "$(cat /tmp/containerId)" | grep true || exit 1