forked from hakwerk/labca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
82 lines (70 loc) · 2.12 KB
/
utils.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
#!/usr/bin/env bash
set -e
export PS_LABCA="bin/labca-gui"
export PS_BOULDER="bin/boulder"
export PS_BOULDER_COUNT=22
export PS_MYSQL="mysqld"
export PS_CONTROL="tcpserver"
export PS_NGINX="nginx:"
export PS_CONSUL="consul"
LOOPCOUNT=120
count() {
local pattern="${1/___/ }" # escape spaces, e.g. PS_CONTROL="sudo___tcpserver"
local prefix=""
case $pattern in
$PS_LABCA)
prefix="docker exec $(docker ps --format "{{.Names}}" | grep -- -labca-) "
;;
$PS_BOULDER)
prefix="docker exec $(docker ps --format "{{.Names}}" | grep -- -boulder-) "
;;
$PS_MYSQL)
prefix="docker exec $(docker ps --format "{{.Names}}" | grep -- -bmysql-) "
;;
$PS_CONTROL)
prefix="docker exec $(docker ps --format "{{.Names}}" | grep -- -control-) "
;;
$PS_CONSUL)
prefix="docker exec $(docker ps --format "{{.Names}}" | grep -- -bconsul-) "
;;
*)
;;
esac
local res=$(${prefix}ps -eo pid,cmd 2>/dev/null | grep "$pattern" | grep -v grep | wc -l)
if [ "$pattern" == "$PS_CONSUL" ]; then
res=$(${prefix}ps -eo pid,args 2>/dev/null | grep "$pattern" | grep -v grep | wc -l)
fi
echo $res
}
wait_count() {
local pattern="$1"
local count="$2"
local lc=0
# Allow more time for the boulder container...
if [ $count -gt 1 ]; then
LOOPCOUNT=240
fi
local c=$(count $pattern)
while ( [ $count -gt 0 ] && [ $c -lt $count ] ) || ( [ $count -eq 0 ] && [ $c -gt $count ] ) && [ $lc -lt $LOOPCOUNT ]; do
let lc=lc+1
sleep 1
c=$(count $pattern)
done
if ( [ $count -gt 0 ] && [ $c -ge $count ] ) || ( [ $count -eq 0 ] && [ $c -eq $count ] ); then
return
fi
if [ $lc -ge $LOOPCOUNT ]; then
pattern="${pattern/___/ }"
if [ $count -gt 1 ]; then
echo "FAILED to get $count of $pattern (only have $c)"
else
echo "FAILED to get $count of $pattern"
fi
fi
}
wait_up() {
wait_count "$1" "${2:-1}"
}
wait_down() {
wait_count "$1" 0
}