|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Substitute variables in config file. |
| 4 | +/bin/sed -i "s/{{VIRTUAL_IP}}/${VIRTUAL_IP}/g" /etc/keepalived/keepalived.conf |
| 5 | +/bin/sed -i "s/{{VIRTUAL_MASK}}/${VIRTUAL_MASK}/g" /etc/keepalived/keepalived.conf |
| 6 | +/bin/sed -i "s/{{CHECK_IP}}/${CHECK_IP}/g" /etc/keepalived/keepalived.conf |
| 7 | +/bin/sed -i "s/{{CHECK_PORT}}/${CHECK_PORT}/g" /etc/keepalived/keepalived.conf |
| 8 | +/bin/sed -i "s/{{VRID}}/${VRID}/g" /etc/keepalived/keepalived.conf |
| 9 | +/bin/sed -i "s/{{INTERFACE}}/${INTERFACE}/g" /etc/keepalived/keepalived.conf |
| 10 | + |
| 11 | +# Make sure we react to these signals by running stop() when we see them - for clean shutdown |
| 12 | +# And then exiting |
| 13 | +trap "stop; exit 0;" SIGTERM SIGINT |
| 14 | + |
| 15 | +stop() |
| 16 | +{ |
| 17 | + # We're here because we've seen SIGTERM, likely via a Docker stop command or similar |
| 18 | + # Let's shutdown cleanly |
| 19 | + echo "SIGTERM caught, terminating keepalived process..." |
| 20 | + # Record PIDs |
| 21 | + pid=$(pidof keepalived) |
| 22 | + # Kill them |
| 23 | + kill -TERM $pid > /dev/null 2>&1 |
| 24 | + # Wait till they have been killed |
| 25 | + wait $pid |
| 26 | + echo "Terminated." |
| 27 | + exit 0 |
| 28 | +} |
| 29 | + |
| 30 | +# Make sure the variables we need to run are populated and (roughly) valid |
| 31 | + |
| 32 | +if ! [[ $VIRTUAL_IP =~ ^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-2][0-3])\.(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[1-4])$ ]]; then |
| 33 | + echo "The VIRTUAL_IP environment variable is null or not a valid IP address, exiting..." |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | +
|
| 37 | +if ! [[ $VIRTUAL_MASK =~ ^([0-9]|[1-2][0-9]|3[0-2])$ ]]; then |
| 38 | + echo "The VIRTUAL_MASK environment variable is null or not a valid subnet mask, exiting..." |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | +
|
| 42 | +if ! [[ $VRID =~ ^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then |
| 43 | + echo "The VRID environment variable is null or not a number between 1 and 255, exiting..." |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | +
|
| 47 | +# Possibly some interfaces are named and don't end in a number so beware of this one |
| 48 | +if ! [[ $INTERFACE =~ ^.*[0-9]$ ]]; then |
| 49 | + echo "The INTERFACE environment variable is null or doesn't end in a number, exiting..." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | +
|
| 53 | +# Make sure to clean up VIP before start (in case of ungraceful shutdown) |
| 54 | +if [[ $(ip a s $INTERFACE | grep $VIRTUAL_IP) ]] |
| 55 | + then |
| 56 | + ip addr del $VIRTUAL_IP/$VIRTUAL_MASK dev $INTERFACE |
| 57 | +fi |
| 58 | +
|
| 59 | +# This loop runs till until we've started up successfully |
| 60 | +while true; do |
| 61 | +
|
| 62 | + # Check if Keepalived is running by recording it's PID (if it's not running $pid will be null): |
| 63 | + pid=$(pidof keepalived) |
| 64 | +
|
| 65 | + # If $pid is null, do this to start or restart Keepalived: |
| 66 | + while [ -z "$pid" ]; do |
| 67 | + #Obviously optional: |
| 68 | + #echo "Starting Confd population of files..." |
| 69 | + #/usr/bin/confd -onetime |
| 70 | + echo "Displaying resulting /etc/keepalived/keepalived.conf contents..." |
| 71 | + cat /etc/keepalived/keepalived.conf |
| 72 | + echo "Starting Keepalived in the background..." |
| 73 | + /usr/sbin/keepalived --dont-fork --dump-conf --log-console --log-detail --vrrp & |
| 74 | + # Check if Keepalived is now running by recording it's PID (if it's not running $pid will be null): |
| 75 | + pid=$(pidof keepalived) |
| 76 | +
|
| 77 | + # If $pid is null, startup failed; log the fact and sleep for 2s |
| 78 | + # We'll then automatically loop through and try again |
| 79 | + if [ -z "$pid" ]; then |
| 80 | + echo "Startup of Keepalived failed, sleeping for 2s, then retrying..." |
| 81 | + sleep 2 |
| 82 | + fi |
| 83 | +
|
| 84 | + done |
| 85 | +
|
| 86 | + # Break this outer loop once we've started up successfully |
| 87 | + # Otherwise, we'll silently restart and Rancher won't know |
| 88 | + break |
| 89 | +
|
| 90 | +done |
| 91 | +
|
| 92 | +# Wait until the Keepalived processes stop (for some reason) |
| 93 | +wait $pid |
| 94 | +echo "The Keepalived process is no longer running, exiting..." |
| 95 | +# Exit with an error |
| 96 | +exit 1 |
0 commit comments