guanghu-ice-heart/skills/codex/guanghu-os-server/scripts/native-recover-hosted.sh

66 lines
2 KiB
Shell
Executable file

#!/usr/bin/env bash
set -euo pipefail
if [[ ${GH_OS_TARGET:-BS-SH-005} != BS-SH-005 ]]; then
echo "BS_SH_005_ADAPTER_TARGET_MISMATCH target=${GH_OS_TARGET}" >&2
exit 65
fi
target=124.223.10.33
target_alias="${GH_OS_SSH_ALIAS:-guanghu-os-bs-sh-005}"
ssh_config="${GH_OS_SSH_CONFIG:-/Users/bingshuolingdianyuanhe/.ssh/guanghu-os-bs-sh-005.conf}"
login_pattern=484c44502d47484f532d4c4f47494e21
recovery_pattern=484c44502d5245434f5645522d4f5321
if ssh \
-F "${ssh_config}" \
-o BatchMode=yes \
-o ConnectTimeout=5 \
-o StrictHostKeyChecking=yes \
"${target_alias}" \
'printf "GUANGHU_HOSTED_RECOVERY_NOT_NEEDED\nboot_id="; cat /proc/sys/kernel/random/boot_id'
then
exit 0
fi
if nc -z -w 2 "${target}" 22 >/dev/null 2>&1; then
echo "tcp22 is open but the pinned hosted SSH identity did not verify" >&2
exit 71
fi
login_output=$(ping -D -c 1 -s 32 -p "${login_pattern}" "${target}")
grep -q 'ttl=39' <<<"${login_output}"
echo "GUANGHU_NATIVE_RUNTIME_OK"
echo "authorization=GH-OS-AUTH-BINGSHUO-BS-SH-005-001"
echo "action=reboot_and_recover_bs_sh_005"
recovery_output=$(ping -D -c 1 -s 32 -p "${recovery_pattern}" "${target}")
grep -q 'ttl=39' <<<"${recovery_output}"
echo "GHNRP_RECOVERY_COMMAND_REPLIED"
for attempt in $(seq 1 120); do
if hosted_output=$(ssh \
-F "${ssh_config}" \
-o BatchMode=yes \
-o ConnectTimeout=5 \
-o StrictHostKeyChecking=yes \
"${target_alias}" \
'set -eu
printf "boot_id="
cat /proc/sys/kernel/random/boot_id
systemctl is-active --quiet guanghu-broadcast-tower.service
systemctl is-active --quiet guanghu-code-channel.service
sudo -n systemctl is-enabled --quiet guanghu-native-recovery-beacon-clear.service
echo "GHNRP_HOSTED_RECOVERY_OK"' 2>/dev/null)
then
printf '%s\n' "${hosted_output}"
exit 0
fi
if (( attempt % 10 == 0 )); then
printf 'waiting_for_ghnrp_hosted_recovery attempt=%s\n' "${attempt}"
fi
sleep 3
done
echo "GHNRP recovery reply was observed but hosted SSH did not return" >&2
exit 69