github.com/misfo/deis@v1.0.1-0.20141111224634-e0eee0392b8a/builder/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/builder} 16 export ETCD_TTL=${ETCD_TTL:-10} 17 export STORAGE_DRIVER=${STORAGE_DRIVER:-btrfs} 18 19 # wait for etcd to be available 20 until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do 21 echo "waiting for etcd at $ETCD..." 22 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 23 done 24 25 # wait until etcd has discarded potentially stale values 26 sleep $(($ETCD_TTL+1)) 27 28 function etcd_safe_mkdir { 29 set +e 30 etcdctl --no-sync -C $ETCD mkdir $1 >/dev/null 2>&1 31 if [[ $? -ne 0 && $? -ne 4 ]]; then 32 echo "etcd_safe_mkdir: an etcd error occurred. aborting..." 33 exit 1 34 fi 35 set -e 36 } 37 38 etcd_safe_mkdir $ETCD_PATH/users 39 40 # wait for confd to run once and install initial templates 41 until confd -onetime -node $ETCD -config-file /app/confd.toml; do 42 echo "builder: waiting for confd to write initial templates..." 43 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 44 done 45 46 # spawn confd in the background to update services based on etcd changes 47 confd -node $ETCD -config-file /app/confd.toml & 48 CONFD_PID=$! 49 50 # remove any pre-existing docker.sock 51 test -e /var/run/docker.sock && rm -f /var/run/docker.sock 52 53 # spawn a docker daemon to run builds 54 docker -d --storage-driver=$STORAGE_DRIVER --bip=172.19.42.1/16 & 55 DOCKER_PID=$! 56 57 # wait for docker to start 58 while [[ ! -e /var/run/docker.sock ]]; do 59 sleep 1 60 done 61 62 # HACK: load progrium/cedarish tarball for faster boot times 63 # see https://github.com/deis/deis/issues/1027 64 if ! docker history progrium/cedarish >/dev/null 2>/dev/null ; then 65 echo "Loading cedarish..." 66 docker load -i /progrium_cedarish.tar 67 else 68 echo "Cedarish already loaded" 69 fi 70 71 # build required images 72 docker build -t deis/slugbuilder /app/slugbuilder/ 73 docker build -t deis/slugrunner /app/slugrunner/ 74 75 # start an SSH daemon to process `git push` requests 76 /usr/sbin/sshd -D -e & 77 SSHD_PID=$! 78 79 # smart shutdown on SIGINT and SIGTERM 80 function on_exit() { 81 kill -TERM $DOCKER_PID $SSHD_PID 82 wait $DOCKER_PID $SSHD_PID 2>/dev/null 83 exit 0 84 } 85 trap on_exit INT TERM EXIT 86 87 echo deis-builder running... 88 89 # publish the service to etcd using the injected EXTERNAL_PORT 90 if [[ ! -z $EXTERNAL_PORT ]]; then 91 92 # configure service discovery 93 PORT=${PORT:-22} 94 PROTO=${PROTO:-tcp} 95 96 set +e 97 98 # wait for the service to become available on PORT 99 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 100 101 # while the port is listening, publish to etcd 102 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 103 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 104 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 105 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 106 done 107 108 # if the loop quits, something went wrong 109 exit 1 110 111 fi 112 113 wait