Skip to content

Commit 01f2225

Browse files
Gabriel MonroyMatthew Fisher
authored andcommitted
refactor(bin/boot) consolidate into a single script with debug and optional publishing
1 parent 0a217fc commit 01f2225

27 files changed

Lines changed: 270 additions & 354 deletions

File tree

builder/bin/boot

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
#
55

66
# configure etcd
7-
export ETCD=${ETCD:-127.0.0.1:4001}
7+
export ETCD=${ETCD:-172.17.42.1:4001}
88
export ETCD_PATH=${ETCD_PATH:-/deis/builder}
99
export ETCD_TTL=${ETCD_TTL:-10}
1010

1111
# fail hard and fast even on pipelines
1212
set -eo pipefail
1313

14-
# configure service discovery
15-
export HOST=${HOST:-localhost}
16-
export PORT=${PORT:-22}
17-
export PROTO=${PROTO:-tcp}
18-
1914
# wait for etcd to be available
2015
until etcdctl -C $ETCD ls >/dev/null; do
2116
echo "waiting for etcd at $ETCD..."
@@ -26,34 +21,69 @@ done
2621
sleep $(($ETCD_TTL+1))
2722

2823
# seed initial service configuration if necessary
29-
$(dirname ${BASH_SOURCE[0]})/seed >/dev/null
24+
if ! etcdctl -C $ETCD ls $ETCD_PATH >/dev/null 2>&1 ; then
25+
etcdctl -C $ETCD mkdir $ETCD_PATH/users >/dev/null 2>&1 || true
26+
fi
3027

3128
# wait for confd to run once and install initial templates
3229
until confd -onetime -node $ETCD -config-file /app/confd.toml; do
3330
echo "waiting for confd to write initial templates..."
3431
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
3532
done
3633

37-
# spawn the service in the background
38-
$(dirname ${BASH_SOURCE[0]})/start &
39-
SERVICE_PID=$!
34+
# remove any pre-existing docker.sock
35+
rm -f /var/run/docker.sock
36+
37+
# spawn a docker daemon to run builds
38+
docker -d &
39+
DOCKER_PID=$!
40+
41+
# wait for docker to start
42+
while [[ ! -e /var/run/docker.sock ]]; do
43+
sleep 1
44+
done
45+
46+
# pull required images
47+
docker pull deis/slugbuilder:latest
48+
docker pull deis/slugrunner:latest
49+
50+
# start an SSH daemon to process `git push` requests
51+
/usr/sbin/sshd -D -e &
52+
SSHD_PID=$!
4053

4154
# smart shutdown on SIGINT and SIGTERM
4255
function on_exit() {
4356
rm -f /var/run/docker.pid
44-
kill -TERM $SERVICE_PID
45-
wait $SERVICE_PID 2>/dev/null
57+
kill -TERM $DOCKER_PID $SSHD_PID
58+
wait $DOCKER_PID $SSHD_PID 2>/dev/null
4659
}
4760
trap on_exit INT TERM EXIT
4861

4962
# spawn confd in the background to update services based on etcd changes
5063
confd -node $ETCD -config-file /app/confd.toml &
5164
CONFD_PID=$!
5265

53-
# wait for the service to become available
54-
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
66+
# publish the service to etcd using the injected HOST and PORT
67+
if [[ ! -z $PUBLISH ]]; then
68+
69+
# configure service discovery
70+
HOST=${HOST:-localhost}
71+
PORT=${PORT:-22}
72+
PROTO=${PROTO:-tcp}
73+
74+
# wait for the service to become available on PUBLISH port
75+
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
76+
77+
# while the port is listening, publish to etcd
78+
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do
79+
etcdctl -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
80+
etcdctl -C $ETCD set $ETCD_PATH/port $PORT --ttl $ETCD_TTL >/dev/null
81+
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
82+
done
83+
84+
# if the loop quits, something went wrong
85+
exit 1
5586

56-
# as long as the service remains up, keep publishing to etcd with a TTL
57-
$(dirname ${BASH_SOURCE[0]})/publish &
87+
fi
5888

5989
wait

builder/bin/publish

Lines changed: 0 additions & 16 deletions
This file was deleted.

builder/bin/seed

Lines changed: 0 additions & 7 deletions
This file was deleted.

builder/bin/start

Lines changed: 0 additions & 20 deletions
This file was deleted.

builder/wrapper

Lines changed: 0 additions & 2 deletions
This file was deleted.

cache/bin/boot

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
# This script is designed to be run inside the container
44
#
55

6-
# configure etcd
7-
export ETCD=${ETCD:-127.0.0.1:4001}
8-
export ETCD_PATH=${ETCD_PATH:-/deis/cache}
9-
export ETCD_TTL=${ETCD_TTL:-10}
10-
116
# fail hard and fast even on pipelines
127
set -eo pipefail
138

14-
# configure service discovery
15-
export HOST=${HOST:-localhost}
16-
export PORT=${PORT:-6379}
17-
export PROTO=${PROTO:-tcp}
9+
# set debug based on envvar
10+
[[ $DEBUG ]] && set -x
11+
12+
# configure etcd
13+
export ETCD=${ETCD:-172.17.42.1:4001}
14+
export ETCD_PATH=${ETCD_PATH:-/deis/cache}
15+
export ETCD_TTL=${ETCD_TTL:-10}
1816

1917
# wait for etcd to be available
2018
until etcdctl -C $ETCD ls >/dev/null; do
@@ -26,7 +24,9 @@ done
2624
sleep $(($ETCD_TTL+1))
2725

2826
# seed initial service configuration if necessary
29-
$(dirname ${BASH_SOURCE[0]})/seed >/dev/null
27+
if ! etcdctl -C $ETCD ls $ETCD_PATH >/dev/null 2>&1 ; then
28+
etcdctl -C $ETCD mkdir $ETCD_PATH >/dev/null 2>&1 || true
29+
fi
3030

3131
# wait for confd to run once and install initial templates
3232
until confd -onetime -node $ETCD -config-file /app/confd.toml; do
@@ -35,7 +35,7 @@ until confd -onetime -node $ETCD -config-file /app/confd.toml; do
3535
done
3636

3737
# spawn the service in the background
38-
$(dirname ${BASH_SOURCE[0]})/start &
38+
sudo -u redis /usr/bin/redis-server /etc/redis/redis.conf &
3939
SERVICE_PID=$!
4040

4141
# smart shutdown on SIGINT and SIGTERM
@@ -49,10 +49,27 @@ trap on_exit INT TERM
4949
confd -node $ETCD -config-file /app/confd.toml &
5050
CONFD_PID=$!
5151

52-
# wait for the service to become available
53-
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
52+
# publish the service to etcd using the injected HOST and PORT
53+
if [[ ! -z $PUBLISH ]]; then
54+
55+
# configure service discovery
56+
HOST=${HOST:-localhost}
57+
PORT=${PORT:-6379}
58+
PROTO=${PROTO:-tcp}
59+
60+
# wait for the service to become available on PUBLISH port
61+
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
62+
63+
# while the port is listening, publish to etcd
64+
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do
65+
etcdctl -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
66+
etcdctl -C $ETCD set $ETCD_PATH/port $PORT --ttl $ETCD_TTL >/dev/null
67+
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
68+
done
69+
70+
# if the loop quits, something went wrong
71+
exit 1
5472

55-
# as long as the service remains up, keep publishing to etcd with a TTL
56-
$(dirname ${BASH_SOURCE[0]})/publish &
73+
fi
5774

5875
wait

cache/bin/publish

Lines changed: 0 additions & 16 deletions
This file was deleted.

cache/bin/seed

Lines changed: 0 additions & 14 deletions
This file was deleted.

cache/bin/start

Lines changed: 0 additions & 8 deletions
This file was deleted.

controller/bin/boot

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
# This script is designed to be run inside the container
44
#
55

6+
# fail hard and fast even on pipelines
7+
set -eo pipefail
8+
9+
# set debug based on envvar
10+
[[ $DEBUG ]] && set -x
11+
612
# configure etcd
713
export ETCD=${ETCD:-172.17.42.1:4001}
814
export ETCD_PATH=${ETCD_PATH:-/deis/controller}
915
export ETCD_TTL=${ETCD_TTL:-10}
1016

11-
# configure service discovery
12-
export HOST=${HOST:-localhost}
13-
export PORT=${PORT:-8000}
14-
export PROTO=${PROTO:-tcp}
15-
1617
# wait for etcd to be available
1718
until etcdctl -C $ETCD ls >/dev/null; do
1819
echo "waiting for etcd at $ETCD..."
@@ -23,7 +24,11 @@ done
2324
sleep $(($ETCD_TTL+1))
2425

2526
# seed initial service configuration if necessary
26-
$(dirname ${BASH_SOURCE[0]})/seed >/dev/null 2>&1
27+
if ! etcdctl -C $ETCD ls $ETCD_PATH >/dev/null 2>/dev/null; then
28+
etcdctl -C $ETCD set $ETCD_PATH/protocol ${DEIS_PROTOCOL:-http}
29+
etcdctl -C $ETCD set $ETCD_PATH/secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
30+
etcdctl -C $ETCD set $ETCD_PATH/builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
31+
fi
2732

2833
# wait for confd to run once and install initial templates
2934
until confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null; do
@@ -38,25 +43,51 @@ while grep -q '<no value>' /templates/confd_settings.py; do
3843
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
3944
done
4045

41-
# spawn the service in the background
42-
$(dirname ${BASH_SOURCE[0]})/start &
43-
SERVICE_PID=$!
46+
cd /app
47+
48+
# run an idempotent database migration
49+
sudo -E -u deis ./manage.py syncdb --migrate --noinput
50+
51+
# spawn celery worker in the background
52+
sudo -E -u deis celery worker --app=deis --loglevel=INFO --workdir=/app --pidfile=/tmp/celery.pid &
53+
CELERY_PID=$!
54+
55+
# spawn a gunicorn server in the foreground
56+
sudo -E -u deis ./manage.py run_gunicorn -b 0.0.0.0 -w 8 -t 600 -n deis --log-level debug --pid=/tmp/gunicorn.pid --preload & # -k gevent
57+
GUNICORN_PID=$!
4458

4559
# smart shutdown on SIGINT and SIGTERM
4660
function on_exit() {
47-
kill -TERM $SERVICE_PID
48-
wait $SERVICE_PID 2>/dev/null
61+
kill -TERM $CELERY_PID $GUNICORN_PID
62+
wait $CELERY_PID $GUNICORN_PID 2>/dev/null
4963
}
5064
trap on_exit INT TERM
5165

5266
# spawn confd in the background to update services based on etcd changes
5367
confd -node $ETCD -config-file /app/confd.toml &
5468
CONFD_PID=$!
5569

56-
# wait for the service to become available
57-
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
70+
# publish the service to etcd using the injected HOST and PORT
71+
if [[ ! -z $PUBLISH ]]; then
72+
73+
# configure service discovery
74+
HOST=${HOST:-localhost}
75+
PORT=${PORT:-8000}
76+
PROTO=${PROTO:-tcp}
77+
78+
# wait for the service to become available on PUBLISH port
79+
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
80+
81+
# while the port is listening, publish to etcd
82+
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do
83+
etcdctl -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
84+
etcdctl -C $ETCD set $ETCD_PATH/port $PORT --ttl $ETCD_TTL >/dev/null
85+
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
86+
done
87+
88+
# if the loop quits, something went wrong
89+
exit 1
5890

59-
# as long as the service remains up, keep publishing to etcd with a TTL
60-
$(dirname ${BASH_SOURCE[0]})/publish &
91+
fi
6192

6293
wait

0 commit comments

Comments
 (0)