2023-04-21 08:04:53 +02:00
|
|
|
#!/usr/bin/env bash
|
2023-12-09 21:45:31 +01:00
|
|
|
set -Eeuo pipefail
|
2023-04-21 08:04:53 +02:00
|
|
|
|
2023-05-11 02:52:25 +02:00
|
|
|
[ ! -f "/run/qemu.pid" ] && echo "QEMU not running yet.." && exit 0
|
2023-11-15 19:58:51 +01:00
|
|
|
[ -f "/run/qemu.count" ] && echo "QEMU is shutting down.." && exit 1
|
2023-05-11 02:50:45 +02:00
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
file="/run/dsm.url"
|
2023-04-21 08:30:00 +02:00
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
if [ ! -f "$file" ]; then
|
2023-05-08 18:02:13 +02:00
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
# Retrieve IP from guest VM for Docker healthcheck
|
2023-05-08 18:02:13 +02:00
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
{ json=$(curl -m 30 -sk http://127.0.0.1:2210/read?command=10); rc=$?; } || :
|
|
|
|
(( rc != 0 )) && echo "Failed to connect to guest: curl error $rc" && exit 1
|
2023-05-11 16:23:59 +02:00
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
{ result=$(echo "$json" | jq -r '.status'); rc=$?; } || :
|
|
|
|
(( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1
|
|
|
|
[[ "$result" == "null" ]] && echo "Guest returned invalid response: $json" && exit 1
|
2023-11-15 19:58:51 +01:00
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
if [[ "$result" != "success" ]] ; then
|
|
|
|
{ msg=$(echo "$json" | jq -r '.message'); rc=$?; } || :
|
2023-12-09 21:19:08 +01:00
|
|
|
echo "Guest replied $result: $msg" && exit 1
|
2023-11-15 19:58:51 +01:00
|
|
|
fi
|
2023-04-21 08:37:39 +02:00
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
{ port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || :
|
|
|
|
(( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1
|
|
|
|
[[ "$port" == "null" ]] && echo "Guest has not set a portnumber yet.." && exit 1
|
2023-12-09 21:19:08 +01:00
|
|
|
[ -z "$port" ] && echo "Guest has not set a portnumber yet.." && exit 1
|
2023-11-15 19:58:51 +01:00
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
{ ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || :
|
|
|
|
(( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1
|
|
|
|
[[ "$ip" == "null" ]] && echo "Guest returned invalid response: $json" && exit 1
|
2023-12-09 21:19:08 +01:00
|
|
|
[ -z "$ip" ] && echo "Guest has not received an IP yet.." && exit 1
|
2023-11-15 19:58:51 +01:00
|
|
|
|
2023-12-09 21:19:08 +01:00
|
|
|
echo "$ip:$port" > $file
|
2023-11-15 19:58:51 +01:00
|
|
|
|
|
|
|
fi
|
2023-05-08 18:02:13 +02:00
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
location=$(cat "$file")
|
2023-05-08 19:43:31 +02:00
|
|
|
|
2023-12-09 21:19:08 +01:00
|
|
|
if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then
|
|
|
|
echo "Failed to reach http://$location"
|
2023-04-21 08:19:19 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
echo "Healthcheck OK"
|
2023-04-21 08:04:53 +02:00
|
|
|
exit 0
|