2023-11-08 03:31:19 +01:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
set -Eeuo pipefail
|
|
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
|
info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n" >&2; }
|
|
|
|
|
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; }
|
2023-11-08 03:31:19 +01:00
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
file="/run/dsm.url"
|
2023-12-10 15:58:37 +01:00
|
|
|
|
shutdown="/run/qemu.count"
|
2023-12-10 09:22:35 +01:00
|
|
|
|
url="http://127.0.0.1:2210/read?command=10"
|
2023-12-17 09:57:50 +01:00
|
|
|
|
|
2023-12-10 16:54:02 +01:00
|
|
|
|
resp_err="Guest returned an invalid response:"
|
|
|
|
|
jq_err="Failed to parse response from guest: jq error"
|
2023-12-10 15:58:37 +01:00
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
while [ ! -f "$file" ]
|
2023-11-08 04:12:09 +01:00
|
|
|
|
do
|
2023-11-08 03:44:12 +01:00
|
|
|
|
|
2023-12-10 15:58:37 +01:00
|
|
|
|
# Check if not shutting down
|
|
|
|
|
[ -f "$shutdown" ] && exit 1
|
|
|
|
|
|
2023-11-11 17:06:56 +01:00
|
|
|
|
sleep 3
|
2023-11-08 12:07:38 +01:00
|
|
|
|
|
2023-12-10 15:58:37 +01:00
|
|
|
|
[ -f "$shutdown" ] && exit 1
|
2023-12-10 09:22:35 +01:00
|
|
|
|
[ -f "$file" ] && break
|
2023-11-08 12:07:38 +01:00
|
|
|
|
|
2023-12-10 09:22:35 +01:00
|
|
|
|
# Retrieve IP from guest VM
|
|
|
|
|
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || :
|
2023-12-10 15:58:37 +01:00
|
|
|
|
|
|
|
|
|
[ -f "$shutdown" ] && exit 1
|
2023-12-07 23:18:47 +01:00
|
|
|
|
(( rc != 0 )) && error "Failed to connect to guest: curl error $rc" && continue
|
2023-11-08 03:44:12 +01:00
|
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
|
{ result=$(echo "$json" | jq -r '.status'); rc=$?; } || :
|
2023-12-10 16:54:02 +01:00
|
|
|
|
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
|
|
|
|
[[ "$result" == "null" ]] && error "$resp_err $json" && continue
|
2023-11-08 03:44:12 +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
|
|
|
|
error "Guest replied $result: $msg" && continue
|
2023-11-08 04:12:09 +01:00
|
|
|
|
fi
|
2023-11-08 03:44:12 +01:00
|
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
|
{ port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || :
|
2023-12-10 16:54:02 +01:00
|
|
|
|
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
|
|
|
|
[[ "$port" == "null" ]] && error "$resp_err $json" && continue
|
2023-12-09 21:19:08 +01:00
|
|
|
|
[ -z "$port" ] && continue
|
2023-11-08 04:12:09 +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=$?; } || :
|
2023-12-10 16:54:02 +01:00
|
|
|
|
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
|
|
|
|
[[ "$ip" == "null" ]] && error "$resp_err $json" && continue
|
2023-12-09 21:19:08 +01:00
|
|
|
|
[ -z "$ip" ] && continue
|
2023-11-08 04:12:09 +01:00
|
|
|
|
|
2023-12-09 21:19:08 +01:00
|
|
|
|
echo "$ip:$port" > $file
|
2023-11-08 04:12:09 +01:00
|
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
|
done
|
2023-11-08 03:44:12 +01:00
|
|
|
|
|
2023-12-10 15:58:37 +01:00
|
|
|
|
[ -f "$shutdown" ] && exit 1
|
|
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
|
location=$(cat "$file")
|
2023-11-08 03:44:12 +01:00
|
|
|
|
|
2023-12-07 23:18:47 +01:00
|
|
|
|
if [[ "$location" != "20.20"* ]]; then
|
2023-11-08 03:31:19 +01:00
|
|
|
|
|
2023-12-09 21:19:08 +01:00
|
|
|
|
msg="http://$location"
|
2023-11-15 19:58:51 +01:00
|
|
|
|
|
2023-11-08 03:31:19 +01:00
|
|
|
|
else
|
2023-12-07 23:18:47 +01:00
|
|
|
|
|
|
|
|
|
ip=$(ip address show dev eth0 | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
|
|
|
|
|
port="${location##*:}"
|
|
|
|
|
|
|
|
|
|
if [[ "$ip" == "172."* ]]; then
|
2023-12-09 21:19:08 +01:00
|
|
|
|
msg="port $port"
|
2023-12-07 23:18:47 +01:00
|
|
|
|
else
|
2023-12-09 21:19:08 +01:00
|
|
|
|
msg="http://$ip:$port"
|
2023-12-07 23:18:47 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2023-11-08 03:31:19 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2023-11-08 12:07:38 +01:00
|
|
|
|
echo "" >&2
|
2023-12-09 21:19:08 +01:00
|
|
|
|
info "-----------------------------------------------------------"
|
|
|
|
|
info " You can now login to DSM at $msg"
|
|
|
|
|
info "-----------------------------------------------------------"
|
2023-11-08 12:07:38 +01:00
|
|
|
|
echo "" >&2
|