-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
drain.sh
executable file
·78 lines (67 loc) · 1.97 KB
/
drain.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
#!/usr/bin/env nix-shell
#!nix-shell -i bash ./shell.nix
set -eu
set -o pipefail
cfgOpt() {
touch build.cfg
ret=$(awk '$1 == "'"$1"'" { print $2; }' build.cfg)
if [ -z "$ret" ]; then
echo "Config option '$1' isn't specified in build.cfg" >&2
echo "Example format:"
echo "$1 value"
echo ""
exit 1
fi
echo "$ret"
}
if [ "${PACKET_AUTH_TOKEN:-x}" == "x" ]; then
PACKET_AUTH_TOKEN=$(cfgOpt "packetKey")
fi
if [ "${PACKET_PROJECT_ID:-x}" == "x" ]; then
PACKET_PROJECT_ID=$(cfgOpt "packetProjectId")
fi
drain() {
data=$((
curl \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header "X-Auth-Token: $PACKET_AUTH_TOKEN" \
--fail \
"https://api.packet.net/devices/${1}" \
| jq -r '.tags | .[]'
echo "skip-hydra"
# using jq -R . to convert the lines in to JSON strings,
# use jq -s . to convert the of JSON strings in to a JSON list
) | jq -R . | jq -s '{
id: $id,
tags: .
}' --arg id "$1")
curl -X PATCH \
--data "${data}" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header "X-Auth-Token: $PACKET_AUTH_TOKEN" \
--fail \
"https://api.packet.net/devices/${1}" 2> /dev/null > /dev/null
}
current_jobs() {
curl -q \
--header 'Accept: application/json' \
--fail \
"https://status.nixos.org/prometheus/api/v1/query?query=hydra_machine_current_jobs\{host=%22root@${1}%22\}" 2> /dev/null \
| jq -r '.data.result[0].value[1]'
}
id=$1
host=$2
drain "${id}"
echo "Draining builds ..."
starttime=$(date +%s)
while
current=$(current_jobs "$host")
printf "%s\t%s\n" "$(date)" "$current"
now=$(date +%s)
duration=$((now - starttime))
[ "$current" -gt 0 ] || [ "$duration" -gt 43200 ] # 12 hours
do
sleep 1
done