[COMMON] Enforce checkbashisms tox profile
[oom.git] / kubernetes / msb / components / msb-consul / resources / docker-entrypoint.sh
1 #!/bin/sh
2
3 #!/usr/bin/dumb-init /bin/sh
4 # As of docker 1.13, using docker run --init achieves the same outcome than dumb-init.
5
6 set -e
7 set -x
8
9 CONSUL_BIND=
10 if [ -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..."
19 fi
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.
24 CONSUL_CLIENT=
25 if [ -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..."
34 fi
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.
40 CONSUL_DATA_DIR=/consul/data
41 CONSUL_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.
45 if [ -n "$CONSUL_LOCAL_CONFIG" ]; then
46     echo "$CONSUL_LOCAL_CONFIG" > "$CONSUL_CONFIG_DIR/local.json"
47 fi
48
49 # If the user is trying to run Consul directly with some arguments, then
50 # pass them to Consul.
51 if echo "$1" | grep '^-' >/dev/null; then
52     set -- consul "$@"
53 fi
54
55 # Look for Consul subcommands.
56 if [ "$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         "$@"
64 elif [ "$1" = 'version' ]; then
65     # This needs a special case because there's no help output.
66     set -- consul "$@"
67 elif 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 "$@"
71 fi
72
73 # If we are running Consul, make sure it executes as the proper user.
74 if [ "$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 "$@"
93 fi
94
95 exec "$@"