Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cron support #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Dockerfiles/base/data/docker-entrypoint.d/100-base-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ set -o pipefail
### Log to stdout/stderr
###
log() {
set -o noglob # prevent message from being expanded

local type="${1}" # ok, warn or err
local message="${2}" # msg to print
local debug="${3}" # 0: only warn and error, >0: ok and info
Expand All @@ -38,7 +40,10 @@ log() {
else
printf "${clr_err}[???] %s${clr_rst}\n" "${message}" 1>&2 # stdout -> stderr
fi

set +o noglob
}
export -f log


###
Expand All @@ -57,7 +62,7 @@ run() {
fi
/bin/sh -c "LANG=C LC_ALL=C ${cmd}"
}

export -f run

###
### Is argument a positive integer?
Expand All @@ -73,7 +78,7 @@ isint() {
env_set() {
printenv "${1}" >/dev/null 2>&1
}

export -f env_set

###
### Get env variable by name
Expand All @@ -91,6 +96,7 @@ env_get() {
# Just output the env value
printenv "${1}"
}
export -f env_get


############################################################
Expand Down
53 changes: 53 additions & 0 deletions Dockerfiles/base/data/docker-entrypoint.d/102-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail


###########################################################
# Functions
############################################################

# add line to devilbox's crontab, if not already there
# and start cron service
cron_add() {
# save the entire line in one variable
line="$*"

DEBUG_LEVEL="$( env_get "DEBUG_ENTRYPOINT" "0" )"

# check if line already exists in crontab
crontab -l -u devilbox | grep "$line" > /dev/null
status=$?

if [ $status -ne 0 ]
then
log "info" "cron: adding line '${line}' ..." "$DEBUG_LEVEL"
(crontab -l -u devilbox; echo "$line";) | crontab -u devilbox -
fi

# make sure the cron service is running
if ! service cron status >/dev/null
then
service cron start
fi
}
export -f cron_add

cron_remove() {
# save the entire line in one variable
line=$*

DEBUG_LEVEL="$( env_get "DEBUG_ENTRYPOINT" "0" )"

# check if line already exists in crontab
crontab -l -u devilbox | grep "$line" > /dev/null
status=$?

if [ $status -eq 0 ]; then
log "info" "cron: removing line '${line}' ..." "$DEBUG_LEVEL"
(crontab -l -u devilbox | grep -v "$line";) | crontab -u devilbox -
fi
}
export -f cron_remove
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set -o pipefail
############################################################

###
### Execute custom uesr-supplied scripts
### Execute custom user-supplied scripts
###
execute_custom_scripts() {
local script_dir="${1}"
Expand All @@ -27,7 +27,7 @@ execute_custom_scripts() {
for script_f in ${script_files}; do
script_name="$( basename "${script_f}" )"
log "info" "Executing custom startup script: ${script_name}" "${debug}"
if ! bash "${script_f}"; then
if ! bash "${script_f}" "${debug}"; then
log "err" "Failed to execute script" "${debug}"
exit 1
fi
Expand All @@ -43,6 +43,10 @@ if ! command -v find >/dev/null 2>&1; then
echo "find not found, but required."
exit 1
fi
if ! command -v sort >/dev/null 2>&1; then
echo "sort not found, but required."
exit 1
fi
if ! command -v basename >/dev/null 2>&1; then
echo "basename not found, but required."
exit 1
Expand Down