Add healthcheck to Docker image

This commit is contained in:
ViViDboarder 2019-02-01 13:48:00 -08:00
parent b2fd9d4259
commit fa9c0f9798
6 changed files with 47 additions and 19 deletions

View File

@ -49,9 +49,13 @@ RUN mkdir -p /scripts/backup/after
RUN mkdir -p /scripts/restore/before
RUN mkdir -p /scripts/restore/after
ADD backup.sh /
ADD restore.sh /
ADD start.sh /
ADD verify.sh /
COPY backup.sh /
COPY restore.sh /
COPY start.sh /
COPY verify.sh /
COPY healthcheck.sh /
COPY cron-exec.sh /
HEALTHCHECK CMD /healthcheck.sh
CMD [ "/start.sh" ]

View File

@ -49,9 +49,13 @@ RUN mkdir -p /scripts/backup/after
RUN mkdir -p /scripts/restore/before
RUN mkdir -p /scripts/restore/after
ADD backup.sh /
ADD restore.sh /
ADD start.sh /
ADD verify.sh /
COPY backup.sh /
COPY restore.sh /
COPY start.sh /
COPY verify.sh /
COPY healthcheck.sh /
COPY cron-exec.sh /
HEALTHCHECK CMD /healthcheck.sh
CMD [ "/start.sh" ]

10
cron-exec.sh Executable file
View File

@ -0,0 +1,10 @@
#! /bin/bash
ENV=/env
LOG=/cron.log
HEALTH_FILE=/unhealthy
test -f $ENV || echo NO_ENV=true > $ENV
# Execute command and write output to log
env `cat $ENV | xargs` $@ 2>> $LOG && rm -f $HEALTH_FILE || { touch $HEALTH_FILE; exit 1; }

5
healthcheck.sh Executable file
View File

@ -0,0 +1,5 @@
#! /bin/bash
HEALTH_FILE=/unhealthy
test -f $HEALTH_FILE || exit 0 && exit 1

View File

@ -18,34 +18,34 @@ fi
# If set to restore on start, restore if the data volume is empty
if [ "$RESTORE_ON_EMPTY_START" == "true" -a -z "$(ls -A $PATH_TO_BACKUP)" ]; then
/restore.sh
/cron-exec.sh /restore.sh
fi
# Unless explicitly skipping, take a backup on startup
if [ "$SKIP_ON_START" != "true" ]; then
/backup.sh
/cron-exec.sh /backup.sh
fi
if [ -n "$CRON_SCHEDULE" ]; then
# Export the environment to a file so it can be loaded from cron
env | sed 's/^\(.*\)=\(.*\)$/export \1="\2"/g' > /env.sh
env > /env
# Remove some vars we don't want to keep
sed -i '/\(HOSTNAME\|affinity\|SHLVL\|PWD\)/d' /env.sh
sed -i '/\(HOSTNAME\|affinity\|SHLVL\|PWD\)/d' /env
# Use bash for cron
echo "SHELL=/bin/bash" > /crontab.conf
# Schedule the backups
echo "$CRON_SCHEDULE source /env.sh && /backup.sh 2>> /cron.log" >> /crontab.conf
echo "$CRON_SCHEDULE /cron-exec.sh /backup.sh" >> /crontab.conf
echo "Backups scheduled as $CRON_SCHEDULE"
if [ -n "$FULL_CRON_SCHEDULE" ]; then
echo "$FULL_CRON_SCHEDULE source /env.sh && /backup.sh full 2>> /cron.log" >> /crontab.conf
echo "$FULL_CRON_SCHEDULE /cron-exec.sh /backup.sh full" >> /crontab.conf
echo "Full backup scheduled as $VERIFY_CRON_SCHEDULE"
fi
if [ -n "$VERIFY_CRON_SCHEDULE" ]; then
echo "$VERIFY_CRON_SCHEDULE source /env.sh && /verify.sh 2>> /cron.log" >> /crontab.conf
echo "$VERIFY_CRON_SCHEDULE /cron-exec.sh /verify.sh" >> /crontab.conf
echo "Verify scheduled as $VERIFY_CRON_SCHEDULE"
fi

View File

@ -22,10 +22,10 @@ else
mkdir -p /data && echo Test > /data/test.txt
echo "Making backup..."
/backup.sh
/cron-exec.sh /backup.sh
echo "Verify backup..."
/verify.sh
/cron-exec.sh /verify.sh
echo "Delete test data..."
rm -fr /data/*
@ -34,7 +34,8 @@ else
test -f /data/test.txt && exit 1 || echo "Gone"
echo "Restore backup..."
/restore.sh
/cron-exec.sh /restore.sh
/healthcheck.sh
echo "Verify restore..."
test -f /data/test.txt
@ -51,6 +52,7 @@ else
echo "Simulate a restart with RESTORE_ON_EMPTY_START..."
RESTORE_ON_EMPTY_START=true /start.sh
/healthcheck.sh
echo "Verify restore happened..."
test -f /data/test.txt
@ -58,5 +60,8 @@ else
echo "Verify restore with incorrect passphrase fails..."
echo "Fail to restore backup..."
PASSPHRASE=Incorrect.Mule.Solar.Paperclip /restore.sh && exit 1 || echo "OK"
PASSPHRASE=Incorrect.Mule.Solar.Paperclip /cron-exec.sh /restore.sh && exit 1 || echo "OK"
echo "Verify failed healthcheck"
/healthcheck.sh && exit 1 || echo "OK"
fi