virtual-dsm/src/print.sh

86 lines
2.2 KiB
Bash
Raw Normal View History

2023-11-08 03:31:19 +01:00
#!/usr/bin/env bash
set -Eeuo pipefail
: ${DHCP:='N'}
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
file="/run/dsm.url"
shutdown="/run/qemu.count"
url="http://127.0.0.1:2210/read?command=10"
2023-12-17 09:57:50 +01:00
resp_err="Guest returned an invalid response:"
jq_err="Failed to parse response from guest: jq error"
while [ ! -f "$file" ]
2023-11-08 04:12:09 +01:00
do
2023-11-08 03:44:12 +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
[ -f "$shutdown" ] && exit 1
[ -f "$file" ] && break
2023-11-08 12:07:38 +01:00
# Retrieve network info from guest VM
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || :
[ -f "$shutdown" ] && exit 1
(( rc != 0 )) && error "Failed to connect to guest: curl error $rc" && continue
2023-11-08 03:44:12 +01:00
{ result=$(echo "$json" | jq -r '.status'); rc=$?; } || :
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
[[ "$result" == "null" ]] && error "$resp_err $json" && continue
2023-11-08 03:44:12 +01:00
if [[ "$result" != "success" ]] ; then
{ msg=$(echo "$json" | jq -r '.message'); rc=$?; } || :
error "Guest replied $result: $msg" && continue
2023-11-08 04:12:09 +01:00
fi
2023-11-08 03:44:12 +01:00
{ port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || :
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
[[ "$port" == "null" ]] && error "$resp_err $json" && continue
[ -z "$port" ] && continue
2023-11-08 04:12:09 +01:00
{ ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || :
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
[[ "$ip" == "null" ]] && error "$resp_err $json" && continue
if [ -z "$ip" ]; then
[[ "$DHCP" == [Yy1]* ]] && continue
ip="20.20.20.21"
fi
2023-11-08 04:12:09 +01:00
echo "$ip:$port" > $file
2023-11-08 04:12:09 +01:00
done
2023-11-08 03:44:12 +01:00
[ -f "$shutdown" ] && exit 1
location=$(cat "$file")
2023-11-08 03:44:12 +01:00
if [[ "$location" != "20.20"* ]]; then
2023-11-08 03:31:19 +01:00
msg="http://$location"
2023-11-08 03:31:19 +01:00
else
ip=$(ip address show dev eth0 | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
port="${location##*:}"
if [[ "$ip" == "172."* ]]; then
msg="port $port"
else
msg="http://$ip:$port"
fi
2023-11-08 03:31:19 +01:00
fi
2023-11-08 12:07:38 +01:00
echo "" >&2
info "-----------------------------------------------------------"
info " You can now login to DSM at $msg"
info "-----------------------------------------------------------"
2023-11-08 12:07:38 +01:00
echo "" >&2