virtual-dsm/src/print.sh

102 lines
2.6 KiB
Bash
Raw Normal View History

2023-11-08 03:31:19 +01:00
#!/usr/bin/env bash
set -Eeuo pipefail
2024-01-14 16:01:15 +01:00
: "${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
2024-01-13 20:25:57 +01:00
file="/run/shm/dsm.url"
2024-01-20 19:59:44 +01:00
page="/run/shm/index.html"
2024-01-13 20:25:57 +01:00
address="/run/shm/qemu.ip"
shutdown="/run/shm/qemu.end"
2024-01-20 19:59:44 +01:00
template="/var/www/index.html"
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:"
curl_err="Failed to connect to guest: curl error"
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 "$curl_err $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
2024-01-14 00:00:59 +01:00
location=$(<"$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"
2024-01-20 19:59:44 +01:00
title="<title>Virtual DSM</title>"
body="The location of DSM is <a href='http://$location'>http://$location</a>"
script="<script>setTimeout(function(){ window.location.assign('http://$location'); }, 3000);</script>"
HTML=$(<"$template")
HTML="${HTML/\[1\]/$title}"
HTML="${HTML/\[2\]/$script}"
HTML="${HTML/\[3\]/$body}"
HTML="${HTML/\[4\]/}"
HTML="${HTML/\[5\]/}"
echo "$HTML" > "$page"
2023-11-08 03:31:19 +01:00
else
2024-01-14 00:00:59 +01:00
ip=$(<"$address")
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