github.com/spg/deis@v1.7.3/builder/rootfs/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 if [[ -f /etc/environment_proxy ]]; then 10 source /etc/environment_proxy 11 fi 12 13 # set debug based on envvar 14 [[ $DEBUG ]] && set -x 15 16 # configure etcd 17 export ETCD_PORT=${ETCD_PORT:-4001} 18 export ETCD="$HOST:$ETCD_PORT" 19 export ETCD_PATH=${ETCD_PATH:-/deis/builder} 20 export ETCD_TTL=${ETCD_TTL:-20} 21 22 # wait for etcd to be available 23 until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do 24 echo "waiting for etcd at $ETCD..." 25 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 26 done 27 28 # wait until etcd has discarded potentially stale values 29 sleep $(($ETCD_TTL+1)) 30 31 function etcd_safe_mkdir { 32 set +e 33 ERROR="$(etcdctl --no-sync -C $ETCD mkdir $1 2>&1 >/dev/null)" 34 if [[ $? -ne 0 && $(echo $ERROR | grep -ive "key already exists") ]]; then 35 echo "etcd_safe_mkdir: an etcd error occurred ($ERROR)" 36 echo "aborting..." 37 exit 1 38 fi 39 set -e 40 } 41 42 etcd_safe_mkdir $ETCD_PATH/users 43 44 # wait for confd to run once and install initial templates 45 until confd -onetime -node $ETCD --log-level error; do 46 echo "builder: waiting for confd to write initial templates..." 47 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 48 done 49 50 # spawn confd in the background to update services based on etcd changes 51 confd -node $ETCD --log-level error --interval 5 & 52 CONFD_PID=$! 53 54 # remove any pre-existing docker.sock 55 test -e /var/run/docker.sock && rm -f /var/run/docker.sock 56 57 # force overlayfs if the Docker daemon on the host is using it 58 mkdir --parents --mode=0700 / 59 fstype=$(findmnt --noheadings --output FSTYPE --target /) 60 if [[ "$fstype" == "overlay" ]]; then 61 DRIVER_OVERRIDE="--storage-driver=overlay" 62 fi 63 64 # spawn a docker daemon to run builds 65 docker -d --bip=172.19.42.1/16 $DRIVER_OVERRIDE --insecure-registry 10.0.0.0/8 --insecure-registry 172.16.0.0/12 --insecure-registry 192.168.0.0/16 --insecure-registry 100.64.0.0/10 & 66 DOCKER_PID=$! 67 68 # wait for docker to start 69 while [[ ! -e /var/run/docker.sock ]]; do 70 sleep 1 71 done 72 73 # build required images 74 docker build -t deis/slugbuilder /usr/local/src/slugbuilder/ 75 docker build -t deis/slugrunner /usr/local/src/slugrunner/ 76 77 # start an SSH daemon to process `git push` requests 78 /usr/sbin/sshd -D -e & 79 SSHD_PID=$! 80 81 # start a cleanup script to remote old repositories and images 82 /bin/cleanup & 83 CLEANUP_PID=$! 84 85 # smart shutdown on SIGINT and SIGTERM 86 function on_exit() { 87 kill -TERM $DOCKER_PID $SSHD_PID $CLEANUP_PID 88 wait $DOCKER_PID $SSHD_PID $CLEANUP_PID 2>/dev/null 89 exit 0 90 } 91 trap on_exit INT TERM EXIT 92 93 echo deis-builder running... 94 95 # publish the service to etcd using the injected EXTERNAL_PORT 96 if [[ ! -z $EXTERNAL_PORT ]]; then 97 98 # configure service discovery 99 PORT=${PORT:-22} 100 PROTO=${PROTO:-tcp} 101 102 set +e 103 104 # wait for the service to become available on PORT 105 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 106 107 # while the port is listening, publish to etcd 108 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 109 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 110 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 111 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 112 done 113 114 # if the loop quits, something went wrong 115 exit 1 116 117 fi 118 119 wait