github.com/blystad/deis@v0.11.0/router/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/router/$HOST}
    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_safe_mkdir {
    28    etcdctl --no-sync -C $ETCD mkdir $1 >/dev/null 2>&1 || true
    29  }
    30  
    31  function etcd_set_default {
    32    etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 >/dev/null 2>&1 || true
    33  }
    34  
    35  etcd_safe_mkdir /deis/controller
    36  etcd_safe_mkdir /deis/services
    37  etcd_safe_mkdir /deis/domains
    38  etcd_safe_mkdir /deis/builder
    39  etcd_set_default port ${PORT:-80}
    40  etcd_set_default gzip on
    41  etcd_set_default gzipHttpVersion 1.0
    42  etcd_set_default gzipCompLevel 2
    43  etcd_set_default gzipProxied any
    44  etcd_set_default gzipVary on
    45  etcd_set_default gzipDisable "\"msie6\""
    46  etcd_set_default gzipTypes "application/x-javascript, application/xhtml+xml, application/xml, application/xml+rss, application/json, text/css, text/javascript, text/plain, text/xml"
    47  
    48  # wait for confd to run once and install initial templates
    49  until confd -onetime -node $ETCD -config-file /app/confd.toml >/dev/null 2>/dev/null; do
    50      echo "router: waiting for confd to write initial templates..."
    51      sleep $(($ETCD_TTL/2))  # sleep for half the TTL
    52  done
    53  
    54  # spawn the service in the background
    55  echo "Starting Nginx..."
    56  /usr/sbin/nginx &
    57  SERVICE_PID=$!
    58  
    59  # smart shutdown on SIGINT and SIGTERM
    60  function on_exit() {
    61      kill -TERM $SERVICE_PID
    62      wait $SERVICE_PID 2>/dev/null
    63      exit 0
    64  }
    65  trap on_exit INT TERM
    66  
    67  # spawn confd in the background to update services based on etcd changes
    68  confd -node $ETCD -config-file /app/confd.toml &
    69  CONFD_PID=$!
    70  
    71  echo deis-router running...
    72  
    73  # publish the service to etcd using the injected PORT
    74  if [[ ! -z $PUBLISH ]]; then
    75  
    76    # configure service discovery
    77    PORT=${PORT:-80}
    78    PROTO=${PROTO:-tcp}
    79  
    80      set +e
    81  
    82    # wait for the service to become available on PUBLISH port
    83    sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
    84  
    85    # while the port is listening, publish to etcd
    86    while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do
    87      etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
    88      etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $PORT --ttl $ETCD_TTL >/dev/null
    89      sleep $(($ETCD_TTL/2)) # sleep for half the TTL
    90    done
    91  
    92    # if the loop quits, something went wrong
    93    exit 1
    94  
    95  fi
    96  
    97  wait