github.com/rvaralda/deis@v1.4.1/controller/bin/boot (about)

     1  #!/bin/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:-10}
    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  	etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 >/dev/null 2>&1
    30  	if [[ $? -ne 0 && $? -ne 4 ]]; then
    31  		echo "etcd_set_default: an etcd error occurred. aborting..."
    32  		exit 1
    33  	fi
    34  	set -e
    35  }
    36  
    37  function etcd_safe_mkdir {
    38  	set +e
    39  	etcdctl --no-sync -C $ETCD mkdir $1 >/dev/null 2>&1
    40  	if [[ $? -ne 0 && $? -ne 4 ]]; then
    41  		echo "etcd_safe_mkdir: an etcd error occurred. aborting..."
    42  		exit 1
    43  	fi
    44  	set -e
    45  }
    46  
    47  etcd_set_default protocol ${DEIS_PROTOCOL:-http}
    48  etcd_set_default secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
    49  etcd_set_default builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
    50  etcd_set_default registrationEnabled 1
    51  etcd_set_default webEnabled 0
    52  etcd_set_default unitHostname default
    53  
    54  # safely create required keyspaces
    55  etcd_safe_mkdir /deis/services
    56  etcd_safe_mkdir /deis/domains
    57  etcd_safe_mkdir /deis/platform
    58  
    59  # wait for confd to run once and install initial templates
    60  until confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null; do
    61  	echo "controller: waiting for confd to write initial templates..."
    62  	sleep $(($ETCD_TTL/2))  # sleep for half the TTL
    63  done
    64  
    65  cd /app
    66  
    67  mkdir -p /data/logs
    68  chmod 777 /data/logs
    69  
    70  # run an idempotent database migration
    71  sudo -E -u deis ./manage.py syncdb --migrate --noinput
    72  
    73  # spawn a gunicorn server in the background
    74  sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi &
    75  
    76  # smart shutdown on SIGINT and SIGTERM
    77  function on_exit() {
    78  	GUNICORN_PID=$(cat /tmp/gunicorn.pid)
    79  	kill -TERM $GUNICORN_PID 2>/dev/null
    80  	wait $GUNICORN_PID 2>/dev/null
    81  	exit 0
    82  }
    83  trap on_exit INT TERM
    84  
    85  # spawn confd in the background to update services based on etcd changes
    86  confd -node $ETCD -config-file /app/confd.toml &
    87  CONFD_PID=$!
    88  
    89  echo deis-controller running...
    90  
    91  # publish the service to etcd using the injected EXTERNAL_PORT
    92  if [[ ! -z $EXTERNAL_PORT ]]; then
    93  
    94  	# configure service discovery
    95  	PORT=${PORT:-8000}
    96  	PROTO=${PROTO:-tcp}
    97  
    98  	set +e
    99  
   100  	# wait for the service to become available on PORT
   101  	sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
   102  
   103  	# while the port is listening, publish to etcd
   104  	while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do
   105  		etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
   106  		etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null
   107  		sleep $(($ETCD_TTL/2)) # sleep for half the TTL
   108  	done
   109  
   110  	# if the loop quits, something went wrong
   111  	exit 1
   112  
   113  fi
   114  
   115  wait