Browse Source

Add healthcheck to Docker image

health-check
ViViDboarder 6 months ago
parent
commit
fa9c0f9798
6 changed files with 47 additions and 19 deletions
  1. 8
    4
      Dockerfile.raspbian
  2. 8
    4
      Dockerfile.ubuntu
  3. 10
    0
      cron-exec.sh
  4. 5
    0
      healthcheck.sh
  5. 7
    7
      start.sh
  6. 9
    4
      tests/test.sh

+ 8
- 4
Dockerfile.raspbian 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" ]

+ 8
- 4
Dockerfile.ubuntu 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
- 0
cron-exec.sh 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
- 0
healthcheck.sh View File

@@ -0,0 +1,5 @@
#! /bin/bash

HEALTH_FILE=/unhealthy

test -f $HEALTH_FILE || exit 0 && exit 1

+ 7
- 7
start.sh 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


+ 9
- 4
tests/test.sh 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

Loading…
Cancel
Save