-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwait.sh
executable file
·87 lines (72 loc) · 1.83 KB
/
wait.sh
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
#!/usr/bin/env bash
set -euo pipefail
if [ -z "${COMPOSE_PROJECT_NAME:-}" ]; then
# shellcheck disable=SC1091
source .env
fi
function ping() {
local connect_timeout=2
local string=greenpeace
local network=${COMPOSE_PROJECT_NAME}_proxy
local endpoint=${APP_HOSTNAME:-http://www.planet4.test}
if [[ -n "${APP_HOSTPATH:-}" ]]
then
endpoint="$endpoint/$APP_HOSTPATH/"
fi
if docker run --network "$network" --rm appropriate/curl --connect-timeout $connect_timeout -s -k "$endpoint" | grep -s "$string" > /dev/null
then
success=$((success+1))
echo -en "\\xE2\\x9C\\x94"
else
echo -n "."
success=0
fi
}
function check_services() {
# shellcheck disable=SC2207
services=( $(docker-compose ps --services) )
for s in "${services[@]}"
do
if ! grep -q "$(docker-compose ps -q "$s")" <<< "$(docker ps -q --no-trunc)"
then
echo
docker ps -a | >&2 grep -E "Exited"
echo
docker-compose logs "$s" | >&2 tail -20
echo
>&2 echo "ERROR: $s is not running"
echo
return 1
fi
done
}
function main() {
# ~6 seconds * 100 == 10+ minutes
# Actual interval varies depending on platform due to docker calls
local interval=1
local loop=120
# Number of consecutive successes to qualify as 'up'
local threshold=3
local success=0
echo
echo "Starting P4 Wordpress docker-compose stack."
echo "Note, this may take up to 10 minutes on the first run!"
echo
printf "Waiting for services to start "
until [[ $success -ge $threshold ]]
do
ping
check_services
loop=$((loop-1))
if [[ $loop -lt 1 ]]
then
>&2 echo "[ERROR] Timeout waiting for docker-compose to start"
>&2 docker-compose logs
return 1
fi
[[ $success -ge $threshold ]] || sleep $interval
done
echo
echo "Services started successfully"
}
main