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