github.com/chasestarr/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/controller/bin/boot (about)

     1  #!/usr/bin/env bash
     2  #
     3  # This script is designed to be run inside the container
     4  #
     5  
     6  # fail hard and fast even on pipelines
     7  set -eo pipefail
     8  
     9  # set debug based on envvar
    10  [[ $DEBUG ]] && set -x
    11  
    12  # configure etcd
    13  export ETCD_PORT=${ETCD_PORT:-4001}
    14  export ETCD="$HOST:$ETCD_PORT"
    15  export ETCD_PATH=${ETCD_PATH:-/deis/controller}
    16  export ETCD_TTL=${ETCD_TTL:-20}
    17  
    18  # wait for etcd to be available
    19  until etcdctl --no-sync -C "$ETCD" ls >/dev/null 2>&1; do
    20  	echo "waiting for etcd at $ETCD..."
    21  	sleep $((ETCD_TTL/2))  # sleep for half the TTL
    22  done
    23  
    24  # wait until etcd has discarded potentially stale values
    25  sleep $((ETCD_TTL+1))
    26  
    27  function etcd_set_default {
    28  	set +e
    29  	ERROR="$(etcdctl --no-sync -C "$ETCD" mk "$ETCD_PATH/$1" "$2" 2>&1 >/dev/null)"
    30  
    31  	if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then
    32  		echo "etcd_set_default: an etcd error occurred ($ERROR)"
    33  		echo "aborting..."
    34  		exit 1
    35  	fi
    36  	set -e
    37  }
    38  
    39  function etcd_safe_mkdir {
    40  	set +e
    41  	ERROR="$(etcdctl --no-sync -C "$ETCD" mkdir "$1" 2>&1 >/dev/null)"
    42  
    43  	if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then
    44  		echo "etcd_safe_mkdir: an etcd error occurred ($ERROR)"
    45  		echo "aborting..."
    46  		exit 1
    47  	fi
    48  	set -e
    49  }
    50  
    51  etcd_set_default protocol "${DEIS_PROTOCOL:-http}"
    52  etcd_set_default secretKey "${DEIS_SECRET_KEY:-$(openssl rand -base64 64 | tr -d '\n')}"
    53  etcd_set_default builderKey "${DEIS_BUILDER_KEY:-$(openssl rand -base64 64 | tr -d '\n')}"
    54  etcd_set_default registrationMode "enabled"
    55  etcd_set_default webEnabled 0
    56  etcd_set_default unitHostname default
    57  
    58  # safely create required keyspaces
    59  etcd_safe_mkdir /deis/domains
    60  etcd_safe_mkdir /deis/platform
    61  etcd_safe_mkdir /deis/scheduler
    62  etcd_safe_mkdir /deis/services
    63  
    64  # run etcd data migrations
    65  echo "controller: running etcd data migrations..."
    66  for script in /app/migrations/data/*.sh;
    67  do
    68  	# shellcheck disable=SC1090
    69      . "$script";
    70  done
    71  echo "controller: done running etcd data migrations."
    72  
    73  # wait for confd to run once and install initial templates
    74  until confd -onetime -node "$ETCD" --confdir /app --log-level error; do
    75  	echo "controller: waiting for confd to write initial templates..."
    76  	sleep $((ETCD_TTL/2))  # sleep for half the TTL
    77  done
    78  
    79  cd /app
    80  
    81  mkdir -p /data/logs
    82  chmod 777 /data/logs
    83  
    84  # allow deis user permission to Docker
    85  if addgroup -g "$(stat -c "%g" /var/run/docker.sock)" docker; then
    86  	addgroup deis docker
    87  fi
    88  
    89  # allow deis user permission to fleet
    90  if addgroup -g "$(stat -c "%g" /var/run/fleet.sock)" fleet; then
    91      addgroup deis fleet
    92  fi
    93  
    94  # run an idempotent database migration
    95  sudo -E -u deis ./manage.py syncdb --migrate --noinput
    96  
    97  # spawn a gunicorn server in the background
    98  sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi &
    99  
   100  ./manage.py load_db_state_to_etcd
   101  
   102  # smart shutdown on SIGTERM (SIGINT is handled by gunicorn)
   103  function on_exit() {
   104  	GUNICORN_PID=$(cat /tmp/gunicorn.pid)
   105  	kill -TERM "$GUNICORN_PID" 2>/dev/null
   106  	wait "$GUNICORN_PID" 2>/dev/null
   107  	exit 0
   108  }
   109  trap on_exit TERM
   110  
   111  # spawn confd in the background to update services based on etcd changes
   112  confd -node "$ETCD" --confdir /app --log-level error --interval 5 &
   113  
   114  echo deis-controller running...
   115  
   116  # publish the service to etcd using the injected EXTERNAL_PORT
   117  if [[ ! -z $EXTERNAL_PORT ]]; then
   118  
   119  	# configure service discovery
   120  	PORT=${PORT:-8000}
   121  	PROTO=${PROTO:-http}
   122  
   123  	set +e
   124  
   125  	# wait for the service to become available on PORT
   126  	sleep 1 && while ! curl -sf "${PROTO}://localhost:${PORT}/healthz" > /dev/null ; do sleep 1; done
   127  
   128  	# while the port is listening, publish to etcd
   129  	while curl -sf "${PROTO}://localhost:${PORT}/healthz" > /dev/null ; do
   130  		etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/host" "$HOST" --ttl "$ETCD_TTL" >/dev/null
   131  		etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/port" "$EXTERNAL_PORT" --ttl "$ETCD_TTL" >/dev/null
   132  		sleep $((ETCD_TTL/2)) # sleep for half the TTL
   133  	done
   134  
   135  	# if the loop quits, something went wrong
   136  	exit 1
   137  
   138  fi
   139  
   140  wait