github.com/inflatablewoman/deis@v1.0.1-0.20141111034523-a4511c46a6ce/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  
    53  # safely create required keyspaces
    54  etcd_safe_mkdir /deis/services
    55  etcd_safe_mkdir /deis/domains
    56  etcd_safe_mkdir /deis/platform
    57  
    58  # wait for confd to run once and install initial templates
    59  until confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null; do
    60  	echo "controller: waiting for confd to write initial templates..."
    61  	sleep $(($ETCD_TTL/2))  # sleep for half the TTL
    62  done
    63  
    64  cd /app
    65  
    66  mkdir -p /data/logs
    67  chmod 777 /data/logs
    68  
    69  # run an idempotent database migration
    70  sudo -E -u deis ./manage.py syncdb --migrate --noinput
    71  
    72  # spawn a gunicorn server in the background
    73  sudo -E -u deis gunicorn deis.wsgi -b 0.0.0.0 -w 8 -n deis --timeout=1200 --pid=/tmp/gunicorn.pid \
    74                           --log-level info --error-logfile - --access-logfile - \
    75                           --access-logformat '%(h)s "%(r)s" %(s)s %(b)s "%(a)s"' &
    76  
    77  # smart shutdown on SIGINT and SIGTERM
    78  function on_exit() {
    79  	GUNICORN_PID=$(cat /tmp/gunicorn.pid)
    80  	kill -TERM $GUNICORN_PID 2>/dev/null
    81  	wait $GUNICORN_PID 2>/dev/null
    82  	exit 0
    83  }
    84  trap on_exit INT TERM
    85  
    86  # spawn confd in the background to update services based on etcd changes
    87  confd -node $ETCD -config-file /app/confd.toml &
    88  CONFD_PID=$!
    89  
    90  echo deis-controller running...
    91  
    92  # publish the service to etcd using the injected EXTERNAL_PORT
    93  if [[ ! -z $EXTERNAL_PORT ]]; then
    94  
    95  	# configure service discovery
    96  	PORT=${PORT:-8000}
    97  	PROTO=${PROTO:-tcp}
    98  
    99  	set +e
   100  
   101  	# wait for the service to become available on PORT
   102  	sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
   103  
   104  	# while the port is listening, publish to etcd
   105  	while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do
   106  		etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
   107  		etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null
   108  		sleep $(($ETCD_TTL/2)) # sleep for half the TTL
   109  	done
   110  
   111  	# if the loop quits, something went wrong
   112  	exit 1
   113  
   114  fi
   115  
   116  wait