Repository navigation
Expand file tree
/
Copy pathcleanup.bash
More file actions
57 lines (47 loc) · 1.75 KB
/
Copy pathcleanup.bash
File metadata and controls
57 lines (47 loc) · 1.75 KB
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
#!/usr/bin/env bash
set -uo pipefail
PARENT="${PARENT:?PARENT env var required}"
SERVICE_NAME="${K_SERVICE:?K_SERVICE env var required}"
delete_service() {
echo "Flask appears down → Deleting service ${PARENT}/services/${SERVICE_NAME}"
# Get an OAuth2 access token from the GCE/Cloud Run metadata server
local token
token=$(curl -s -H "Metadata-Flavor: Google" \
"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" \
| jq -r .access_token)
if [ -z "$token" ]; then
echo "Delete failed: could not obtain access token"
return 1
fi
local http_code
http_code=$(curl -s -o /tmp/delete_response.json -w "%{http_code}" \
-X DELETE \
-H "Authorization: Bearer ${token}" \
"https://run.googleapis.com/v2/${PARENT}/services/${SERVICE_NAME}")
if [[ "$http_code" == 2* ]]; then
echo "Service deletion request accepted (HTTP ${http_code})"
elif [[ "$http_code" == 404 ]]; then
echo "Service already deleted (HTTP ${http_code})"
else
echo "Delete failed (HTTP ${http_code}): $(cat /tmp/delete_response.json)"
return 1
fi
}
# Once the service is deleted, the instance keeps running (and being billed) until its
# open requests, like event streams, reach the request timeout: stop it right away
stop_instance() {
echo "Stopping instance"
supervisorctl -c /etc/supervisord.conf shutdown
}
while true; do
sleep 30
response=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "http://127.0.0.1/geode/health")
echo "response ${response}"
if [ "$response" != "200" ]; then
if delete_service; then
stop_instance
fi
break
fi
done
echo "Health check loop ended."