Browse Source

Add healthcheck to Docker image

ViViDboarder 3 weeks 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
49 49
 RUN mkdir -p /scripts/restore/before
50 50
 RUN mkdir -p /scripts/restore/after
51 51
 
52
-ADD backup.sh /
53
-ADD restore.sh /
54
-ADD start.sh /
55
-ADD verify.sh /
52
+COPY backup.sh /
53
+COPY restore.sh /
54
+COPY start.sh /
55
+COPY verify.sh /
56
+COPY healthcheck.sh /
57
+COPY cron-exec.sh /
58
+
59
+HEALTHCHECK CMD /healthcheck.sh
56 60
 
57 61
 CMD [ "/start.sh" ]

+ 8
- 4
Dockerfile.ubuntu View File

@@ -49,9 +49,13 @@ RUN mkdir -p /scripts/backup/after
49 49
 RUN mkdir -p /scripts/restore/before
50 50
 RUN mkdir -p /scripts/restore/after
51 51
 
52
-ADD backup.sh /
53
-ADD restore.sh /
54
-ADD start.sh /
55
-ADD verify.sh /
52
+COPY backup.sh /
53
+COPY restore.sh /
54
+COPY start.sh /
55
+COPY verify.sh /
56
+COPY healthcheck.sh /
57
+COPY cron-exec.sh /
58
+
59
+HEALTHCHECK CMD /healthcheck.sh
56 60
 
57 61
 CMD [ "/start.sh" ]

+ 10
- 0
cron-exec.sh View File

@@ -0,0 +1,10 @@
1
+#! /bin/bash
2
+
3
+ENV=/env
4
+LOG=/cron.log
5
+HEALTH_FILE=/unhealthy
6
+
7
+test -f $ENV || echo NO_ENV=true > $ENV
8
+
9
+# Execute command and write output to log
10
+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 @@
1
+#! /bin/bash
2
+
3
+HEALTH_FILE=/unhealthy
4
+
5
+test -f $HEALTH_FILE || exit 0 && exit 1

+ 7
- 7
start.sh View File

@@ -18,34 +18,34 @@ fi
18 18
 
19 19
 # If set to restore on start, restore if the data volume is empty
20 20
 if [ "$RESTORE_ON_EMPTY_START" == "true" -a -z "$(ls -A $PATH_TO_BACKUP)" ]; then
21
-    /restore.sh
21
+    /cron-exec.sh /restore.sh
22 22
 fi
23 23
 
24 24
 # Unless explicitly skipping, take a backup on startup
25 25
 if [ "$SKIP_ON_START" != "true" ]; then
26
-    /backup.sh
26
+    /cron-exec.sh /backup.sh
27 27
 fi
28 28
 
29 29
 if [ -n "$CRON_SCHEDULE" ]; then
30 30
     # Export the environment to a file so it can be loaded from cron
31
-    env | sed 's/^\(.*\)=\(.*\)$/export \1="\2"/g' > /env.sh
31
+    env > /env
32 32
     # Remove some vars we don't want to keep
33
-    sed -i '/\(HOSTNAME\|affinity\|SHLVL\|PWD\)/d' /env.sh
33
+    sed -i '/\(HOSTNAME\|affinity\|SHLVL\|PWD\)/d' /env
34 34
 
35 35
     # Use bash for cron
36 36
     echo "SHELL=/bin/bash" > /crontab.conf
37 37
 
38 38
     # Schedule the backups
39
-    echo "$CRON_SCHEDULE source /env.sh && /backup.sh 2>> /cron.log" >> /crontab.conf
39
+    echo "$CRON_SCHEDULE /cron-exec.sh /backup.sh" >> /crontab.conf
40 40
     echo "Backups scheduled as $CRON_SCHEDULE"
41 41
 
42 42
     if [ -n "$FULL_CRON_SCHEDULE" ]; then
43
-        echo "$FULL_CRON_SCHEDULE source /env.sh && /backup.sh full 2>> /cron.log" >> /crontab.conf
43
+        echo "$FULL_CRON_SCHEDULE /cron-exec.sh /backup.sh full" >> /crontab.conf
44 44
         echo "Full backup scheduled as $VERIFY_CRON_SCHEDULE"
45 45
     fi
46 46
 
47 47
     if [ -n "$VERIFY_CRON_SCHEDULE" ]; then
48
-        echo "$VERIFY_CRON_SCHEDULE source /env.sh && /verify.sh 2>> /cron.log" >> /crontab.conf
48
+        echo "$VERIFY_CRON_SCHEDULE /cron-exec.sh /verify.sh" >> /crontab.conf
49 49
         echo "Verify scheduled as $VERIFY_CRON_SCHEDULE"
50 50
     fi
51 51
 

+ 9
- 4
tests/test.sh View File

@@ -22,10 +22,10 @@ else
22 22
     mkdir -p /data && echo Test > /data/test.txt
23 23
 
24 24
     echo "Making backup..."
25
-    /backup.sh
25
+    /cron-exec.sh /backup.sh
26 26
 
27 27
     echo "Verify backup..."
28
-    /verify.sh
28
+    /cron-exec.sh /verify.sh
29 29
 
30 30
     echo "Delete test data..."
31 31
     rm -fr /data/*
@@ -34,7 +34,8 @@ else
34 34
     test -f /data/test.txt && exit 1 || echo "Gone"
35 35
 
36 36
     echo "Restore backup..."
37
-    /restore.sh
37
+    /cron-exec.sh /restore.sh
38
+    /healthcheck.sh
38 39
 
39 40
     echo "Verify restore..."
40 41
     test -f /data/test.txt
@@ -51,6 +52,7 @@ else
51 52
 
52 53
     echo "Simulate a restart with RESTORE_ON_EMPTY_START..."
53 54
     RESTORE_ON_EMPTY_START=true /start.sh
55
+    /healthcheck.sh
54 56
 
55 57
     echo "Verify restore happened..."
56 58
     test -f /data/test.txt
@@ -58,5 +60,8 @@ else
58 60
 
59 61
     echo "Verify restore with incorrect passphrase fails..."
60 62
     echo "Fail to restore backup..."
61
-    PASSPHRASE=Incorrect.Mule.Solar.Paperclip /restore.sh && exit 1 || echo "OK"
63
+    PASSPHRASE=Incorrect.Mule.Solar.Paperclip /cron-exec.sh /restore.sh && exit 1 || echo "OK"
64
+
65
+    echo "Verify failed healthcheck"
66
+    /healthcheck.sh && exit 1 || echo "OK"
62 67
 fi

Loading…
Cancel
Save