-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner
executable file
·138 lines (108 loc) · 3.16 KB
/
runner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
#
# runner
#
# Starts a Docker container from a CouchDb service definition
#
set -u
function error
{
echo "error: $1" >&2
exit 1
}
function stop
{
local service_address="$1"
local container_ip="$2"
local container_port="$3"
trap '' EXIT TERM INT # ignore signals
ipvsadm -d -t "$service_address" -r "${container_ip}:${container_port}"
systemd-notify STOPPING=1 STATUS="Stopping containers"
sleep 5
echo "[stopping container]"
docker stop "$container_id"
}
function wait_for_tcp
{
local ip="$1"
local port="$2"
echo "[waiting for health check tcp://$ip:$port]"
for ((i = 0; i <= 500; i++))
do
ncat -z "$ip" "$port"
if [ "$?" -eq 0 ]
then
return 0
fi
sleep 1
done
error "Container health check failed"
}
function wait_for_http
{
local ip="$1"
local port="$2"
echo "[waiting for health check http://$ip:$port]"
curl --fail --silent \
--retry-max-time 500 \
--retry-delay 1 \
--retry-connrefused \
--retry 600 \
"http://${ip}:${port}/"
[ "$?" -eq 0 ] || error "Container health check failed"
}
function main
{
local service_name="$1"
local service_instance="$2"
local service_address="$3"
config=$(curl -sf "${COUCHDB_URL}/${service_name}?rev=${service_instance}")
[ "$?" -eq 0 ] || error "Failed fetch config document"
set -e
container_name="${service_name}.${service_instance}"
container_image=$(jq -r '.image' <<< "$config")
container_port=$(jq -r '.port' <<< "$config")
args=(
'--detach'
'--log-driver' 'journald'
'--name' "$container_name"
'--label' "service_name=$service_name"
'-p' "$container_port"
'-e' "SYSTEM_HOSTNAME=$(hostname)"
)
while read arg; do args+=("-e" "$arg"); done < \
<(jq -r '.environment // {} | to_entries[] | .key + "=" + .value' <<< "$config")
while read arg; do args+=("-v" "$arg"); done < \
<(jq -r '.volumes // [] | .[]' <<< "$config")
args+=("$container_image")
set +e
# Remove previous container
if [ "$(docker ps -aq -f status=exited -f name=$container_name)" ]
then
echo "[removing exited container]"
docker rm "$container_name"
fi
# Start container
echo "[starting container]"
container_id=$(docker run "${args[@]}")
[ "$?" -eq 0 ] || error "Failed to launch container"
# Find container IP address
container_ip=$(docker inspect "$container_id" | jq -r '.[0].NetworkSettings.IPAddress')
# Health check
jq -e '.health_check == "tcp"' > /dev/null <<< "$config" && \
wait_for_tcp "$container_ip" "$container_port" || \
wait_for_http "$container_ip" "$container_port"
# Add to load balancer
ipvsadm -a -t "$service_address" -r "${container_ip}:${container_port}" -m
systemd-notify READY=1 STATUS="Up and running"
# Setup shutdown hooks
trap "stop $service_address $container_ip $container_port" EXIT TERM INT
echo "[waiting for container to exit]"
docker wait "$container_id"
# Get the exit code and exit
rc=$(docker inspect --format '{{.State.ExitCode}}' $container_id)
echo "[container exited with status code $rc]"
exit 0
}
(( $# != 3 )) && error "usage: $0 service-name service-instance service-address"
main "$1" "$2" "$3"