Krzysztof Opasiak | bc6a667 | 2020-11-23 22:02:51 +0100 | [diff] [blame^] | 1 | #!/usr/bin/dumb-init /bin/sh |
| 2 | set -e |
| 3 | set -x |
| 4 | |
| 5 | # Note above that we run dumb-init as PID 1 in order to reap zombie processes |
| 6 | # as well as forward signals to all processes in its session. Normally, sh |
| 7 | # wouldn't do either of these functions so we'd leak zombies as well as do |
| 8 | # unclean termination of all our sub-processes. |
| 9 | # As of docker 1.13, using docker run --init achieves the same outcome. |
| 10 | |
| 11 | # You can set CONSUL_BIND_INTERFACE to the name of the interface you'd like to |
| 12 | # bind to and this will look up the IP and pass the proper -bind= option along |
| 13 | # to Consul. |
| 14 | CONSUL_BIND= |
| 15 | if [ -n "$CONSUL_BIND_INTERFACE" ]; then |
| 16 | CONSUL_BIND_ADDRESS=$(ip -o -4 addr list $CONSUL_BIND_INTERFACE | head -n1 | awk '{print $4}' | cut -d/ -f1) |
| 17 | if [ -z "$CONSUL_BIND_ADDRESS" ]; then |
| 18 | echo "Could not find IP for interface '$CONSUL_BIND_INTERFACE', exiting" |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | CONSUL_BIND="-bind=$CONSUL_BIND_ADDRESS" |
| 23 | echo "==> Found address '$CONSUL_BIND_ADDRESS' for interface '$CONSUL_BIND_INTERFACE', setting bind option..." |
| 24 | fi |
| 25 | |
| 26 | # You can set CONSUL_CLIENT_INTERFACE to the name of the interface you'd like to |
| 27 | # bind client intefaces (HTTP, DNS, and RPC) to and this will look up the IP and |
| 28 | # pass the proper -client= option along to Consul. |
| 29 | CONSUL_CLIENT= |
| 30 | if [ -n "$CONSUL_CLIENT_INTERFACE" ]; then |
| 31 | CONSUL_CLIENT_ADDRESS=$(ip -o -4 addr list $CONSUL_CLIENT_INTERFACE | head -n1 | awk '{print $4}' | cut -d/ -f1) |
| 32 | if [ -z "$CONSUL_CLIENT_ADDRESS" ]; then |
| 33 | echo "Could not find IP for interface '$CONSUL_CLIENT_INTERFACE', exiting" |
| 34 | exit 1 |
| 35 | fi |
| 36 | |
| 37 | CONSUL_CLIENT="-client=$CONSUL_CLIENT_ADDRESS" |
| 38 | echo "==> Found address '$CONSUL_CLIENT_ADDRESS' for interface '$CONSUL_CLIENT_INTERFACE', setting client option..." |
| 39 | fi |
| 40 | |
| 41 | # CONSUL_DATA_DIR is exposed as a volume for possible persistent storage. The |
| 42 | # CONSUL_CONFIG_DIR isn't exposed as a volume but you can compose additional |
| 43 | # config files in there if you use this image as a base, or use CONSUL_LOCAL_CONFIG |
| 44 | # below. |
| 45 | CONSUL_DATA_DIR=/consul/data |
| 46 | CONSUL_CONFIG_DIR=/consul/config |
| 47 | |
| 48 | # You can also set the CONSUL_LOCAL_CONFIG environemnt variable to pass some |
| 49 | # Consul configuration JSON without having to bind any volumes. |
| 50 | if [ -n "$CONSUL_LOCAL_CONFIG" ]; then |
| 51 | echo "$CONSUL_LOCAL_CONFIG" > "$CONSUL_CONFIG_DIR/local.json" |
| 52 | fi |
| 53 | |
| 54 | # If the user is trying to run Consul directly with some arguments, then |
| 55 | # pass them to Consul. |
| 56 | if [ "${1:0:1}" = '-' ]; then |
| 57 | set -- consul "$@" |
| 58 | fi |
| 59 | |
| 60 | # Look for Consul subcommands. |
| 61 | if [ "$1" = 'agent' ]; then |
| 62 | shift |
| 63 | set -- consul agent \ |
| 64 | -data-dir="$CONSUL_DATA_DIR" \ |
| 65 | -config-dir="$CONSUL_CONFIG_DIR" \ |
| 66 | $CONSUL_BIND \ |
| 67 | $CONSUL_CLIENT \ |
| 68 | "$@" |
| 69 | elif [ "$1" = 'version' ]; then |
| 70 | # This needs a special case because there's no help output. |
| 71 | set -- consul "$@" |
| 72 | elif consul --help "$1" 2>&1 | grep -q "consul $1"; then |
| 73 | # We can't use the return code to check for the existence of a subcommand, so |
| 74 | # we have to use grep to look for a pattern in the help output. |
| 75 | set -- consul "$@" |
| 76 | fi |
| 77 | |
| 78 | # If we are running Consul, make sure it executes as the proper user. |
| 79 | if [ "$1" = 'consul' ]; then |
| 80 | # If the data or config dirs are bind mounted then chown them. |
| 81 | # Note: This checks for root ownership as that's the most common case. |
| 82 | if [ "$(stat -c %u /consul/data)" != "$(id -u consul)" ]; then |
| 83 | chown consul:consul /consul/data |
| 84 | fi |
| 85 | if [ "$(stat -c %u /consul/config)" != "$(id -u consul)" ]; then |
| 86 | chown consul:consul /consul/config |
| 87 | fi |
| 88 | |
| 89 | # If requested, set the capability to bind to privileged ports before |
| 90 | # we drop to the non-root user. Note that this doesn't work with all |
| 91 | # storage drivers (it won't work with AUFS). |
| 92 | if [ ! -z ${CONSUL_ALLOW_PRIVILEGED_PORTS+x} ]; then |
| 93 | setcap "cap_net_bind_service=+ep" /bin/consul |
| 94 | fi |
| 95 | |
| 96 | # Instead of using this we run our pod as a non-root user. |
| 97 | # set -- su-exec consul:consul "$@" |
| 98 | fi |
| 99 | |
| 100 | exec "$@" |