feat(guanghu-os): bridge JD code plane through supervisor
This commit is contained in:
parent
e4750c57a7
commit
0388526898
5 changed files with 202 additions and 1 deletions
|
|
@ -8,6 +8,7 @@ Requires=lake-lamp-authz.service
|
|||
Requires=lake-lamp-architecture-provision.service
|
||||
Requires=lake-lamp-deployment-event-worker.service
|
||||
Requires=guanghu-boot-acceptance.service
|
||||
Requires=guanghu-supervisor.service
|
||||
After=multi-user.target
|
||||
After=guanghu-world-gate.service
|
||||
After=bingshuo-tcs-living-controller.service
|
||||
|
|
@ -16,5 +17,5 @@ After=lake-lamp-authz.service
|
|||
After=lake-lamp-architecture-provision.service
|
||||
After=lake-lamp-deployment-event-worker.service
|
||||
After=guanghu-boot-acceptance.service
|
||||
After=guanghu-supervisor.service
|
||||
AllowIsolate=yes
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
[Unit]
|
||||
Description=Guanghu OS master control bridge for Linux collaboration services
|
||||
Before=hlcc-jd-candidate.service
|
||||
After=local-fs.target docker.service
|
||||
Wants=docker.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/sbin/guanghu-supervisor-control-bridge activate
|
||||
RemainAfterExit=yes
|
||||
RuntimeDirectory=guanghu/supervisor
|
||||
RuntimeDirectoryMode=0700
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectHome=true
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/run/guanghu/supervisor
|
||||
|
||||
[Install]
|
||||
WantedBy=guanghu-language-primary.target
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[Unit]
|
||||
Requires=guanghu-supervisor.service
|
||||
After=guanghu-supervisor.service
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
umask 077
|
||||
|
||||
die() {
|
||||
echo "GUANGHU_SUPERVISOR_BRIDGE_FAIL_0: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
test_mode=${GH_SUPERVISOR_TEST_MODE:-0}
|
||||
if [[ "${test_mode}" == 1 ]]; then
|
||||
config_path=${GH_SUPERVISOR_CONFIG:?test config is required}
|
||||
dmi_path=${GH_SUPERVISOR_DMI_PATH:?test DMI path is required}
|
||||
grub_cfg=${GH_SUPERVISOR_GRUB_CFG:?test GRUB path is required}
|
||||
state_root=${GH_SUPERVISOR_STATE_ROOT:?test state root is required}
|
||||
systemctl_bin=${GH_SUPERVISOR_SYSTEMCTL:?test systemctl is required}
|
||||
current_link=${GH_SUPERVISOR_CURRENT_LINK:?test current link is required}
|
||||
else
|
||||
[[ "${EUID}" == 0 ]] || die "root execution is required"
|
||||
for override in GH_SUPERVISOR_CONFIG GH_SUPERVISOR_DMI_PATH GH_SUPERVISOR_GRUB_CFG GH_SUPERVISOR_STATE_ROOT GH_SUPERVISOR_SYSTEMCTL GH_SUPERVISOR_CURRENT_LINK; do
|
||||
[[ -z "${!override:-}" ]] || die "production path override is forbidden: ${override}"
|
||||
done
|
||||
config_path=/etc/guanghu/supervisor-bridge.conf
|
||||
dmi_path=/sys/class/dmi/id/product_uuid
|
||||
grub_cfg=/boot/grub/grub.cfg
|
||||
state_root=/run/guanghu/supervisor
|
||||
systemctl_bin=/usr/bin/systemctl
|
||||
current_link=/guanghu/current
|
||||
[[ -f "${config_path}" && ! -L "${config_path}" ]] || die "root-owned supervisor config is missing"
|
||||
[[ "$(stat -c '%u:%a' "${config_path}")" =~ ^0:(600|640|644)$ ]] ||
|
||||
die "supervisor config owner or mode is unsafe"
|
||||
fi
|
||||
|
||||
schema=
|
||||
node_id=
|
||||
instance_id=
|
||||
backend_id=
|
||||
backend_command=
|
||||
rescue_slot_id=
|
||||
current_root=
|
||||
code_unit=
|
||||
while IFS='=' read -r key value; do
|
||||
[[ -n "${key}" ]] || continue
|
||||
case "${key}" in
|
||||
schema|node_id|instance_id|backend_id|backend_command|rescue_slot_id|current_root|code_unit)
|
||||
printf -v "${key}" '%s' "${value}"
|
||||
;;
|
||||
*) die "unknown config field: ${key}" ;;
|
||||
esac
|
||||
done <"${config_path}"
|
||||
|
||||
[[ "${schema}" == "guanghu.supervisor-control-bridge/v1" ]] || die "config schema mismatch"
|
||||
[[ "${node_id}" == "JD-FD-PRIMARY" ]] || die "target node mismatch"
|
||||
[[ "${instance_id}" =~ ^[0-9a-f-]{36}$ ]] || die "instance id is invalid"
|
||||
[[ "${backend_id}" =~ ^[a-z0-9-]+$ ]] || die "backend id is invalid"
|
||||
[[ "${rescue_slot_id}" =~ ^[A-Za-z0-9._-]+$ ]] || die "rescue slot id is invalid"
|
||||
if [[ "${test_mode}" == 1 ]]; then
|
||||
[[ "${current_root}" == /* ]] || die "test current root is invalid"
|
||||
else
|
||||
[[ "${current_root}" =~ ^/guanghu/versions/[A-Za-z0-9._-]+$ ]] || die "current root is invalid"
|
||||
fi
|
||||
[[ "${code_unit}" == "hlcc-jd-candidate.service" ]] || die "code unit binding mismatch"
|
||||
[[ -x "${backend_command}" && ! -L "${backend_command}" ]] || die "subcontrol backend is unavailable"
|
||||
[[ -x "${systemctl_bin}" ]] || die "systemctl is unavailable"
|
||||
|
||||
verify_machine() {
|
||||
local observed
|
||||
observed=$(tr '[:upper:]' '[:lower:]' <"${dmi_path}")
|
||||
[[ "${observed}" == "${instance_id}" ]] || die "machine identity mismatch"
|
||||
}
|
||||
|
||||
verify_rescue() {
|
||||
grep -F -- 'menuentry ' "${grub_cfg}" | grep -Fq -- "'${rescue_slot_id}'" ||
|
||||
die "independent Linux rescue slot is unavailable"
|
||||
}
|
||||
|
||||
verify_world() {
|
||||
[[ "$(readlink -f "${current_link}")" == "$(readlink -f "${current_root}")" ]] ||
|
||||
die "active Guanghu world mismatch"
|
||||
}
|
||||
|
||||
verify_code_unit() {
|
||||
[[ "$("${systemctl_bin}" show -p LoadState --value "${code_unit}")" == loaded ]] ||
|
||||
die "Linux Forgejo bridge unit is unavailable"
|
||||
}
|
||||
|
||||
preflight() {
|
||||
verify_machine
|
||||
verify_rescue
|
||||
verify_world
|
||||
verify_code_unit
|
||||
"${backend_command}" preflight "${backend_id}" >/dev/null
|
||||
[[ "$("${backend_command}" observe "${backend_id}")" == dormant ]] ||
|
||||
die "on-demand Linux subcontrol is not dormant"
|
||||
}
|
||||
|
||||
action=${1:-}
|
||||
case "${action}" in
|
||||
preflight)
|
||||
preflight
|
||||
echo "GUANGHU_SUPERVISOR_BRIDGE_PREFLIGHT_OK"
|
||||
;;
|
||||
activate)
|
||||
preflight
|
||||
mkdir -p "${state_root}"
|
||||
temporary="${state_root}/control.json.tmp"
|
||||
printf '{"schema":"guanghu.supervisor-control-state/v1","node_id":"%s","control":"GUANGHU_OS","linux_code_bridge":"%s","linux_subcontrol":"dormant","linux_rescue":"%s"}\n' \
|
||||
"${node_id}" "${code_unit}" "${rescue_slot_id}" >"${temporary}"
|
||||
chmod 600 "${temporary}"
|
||||
mv "${temporary}" "${state_root}/control.json"
|
||||
echo "GUANGHU_SUPERVISOR_BRIDGE_ACTIVE"
|
||||
;;
|
||||
observe)
|
||||
[[ -f "${state_root}/control.json" ]] || die "control state is unavailable"
|
||||
cat "${state_root}/control.json"
|
||||
;;
|
||||
*)
|
||||
die "usage: $0 <preflight|activate|observe>"
|
||||
;;
|
||||
esac
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
source_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
|
||||
bridge="${source_root}/scripts/guanghu-supervisor-control-bridge.sh"
|
||||
fixture=$(mktemp -d)
|
||||
trap 'rm -rf "${fixture}"' EXIT
|
||||
|
||||
mkdir -p "${fixture}/bin" "${fixture}/state" "${fixture}/versions/test-supervisor-world"
|
||||
ln -sfn "${fixture}/versions/test-supervisor-world" "${fixture}/current"
|
||||
printf '%s\n' 'f3d4b730-7f02-452f-975b-7091a4800431' >"${fixture}/dmi"
|
||||
printf '%s\n' "menuentry 'Ubuntu' --id 'rescue-slot' {" >"${fixture}/grub.cfg"
|
||||
|
||||
cat >"${fixture}/backend" <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
[[ "${1:-}" == preflight || "${1:-}" == observe ]] || exit 1
|
||||
if [[ "${1:-}" == observe ]]; then echo dormant; fi
|
||||
EOF
|
||||
chmod +x "${fixture}/backend"
|
||||
|
||||
cat >"${fixture}/systemctl" <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
[[ "$*" == 'show -p LoadState --value hlcc-jd-candidate.service' ]] || exit 1
|
||||
echo loaded
|
||||
EOF
|
||||
chmod +x "${fixture}/systemctl"
|
||||
|
||||
cat >"${fixture}/bridge.conf" <<EOF
|
||||
schema=guanghu.supervisor-control-bridge/v1
|
||||
node_id=JD-FD-PRIMARY
|
||||
instance_id=f3d4b730-7f02-452f-975b-7091a4800431
|
||||
backend_id=jd-linux-on-demand
|
||||
backend_command=${fixture}/backend
|
||||
rescue_slot_id=rescue-slot
|
||||
current_root=${fixture}/versions/test-supervisor-world
|
||||
code_unit=hlcc-jd-candidate.service
|
||||
EOF
|
||||
|
||||
export GH_SUPERVISOR_TEST_MODE=1
|
||||
export GH_SUPERVISOR_CONFIG="${fixture}/bridge.conf"
|
||||
export GH_SUPERVISOR_DMI_PATH="${fixture}/dmi"
|
||||
export GH_SUPERVISOR_GRUB_CFG="${fixture}/grub.cfg"
|
||||
export GH_SUPERVISOR_STATE_ROOT="${fixture}/state"
|
||||
export GH_SUPERVISOR_SYSTEMCTL="${fixture}/systemctl"
|
||||
export GH_SUPERVISOR_CURRENT_LINK="${fixture}/current"
|
||||
|
||||
"${bridge}" preflight
|
||||
"${bridge}" activate
|
||||
"${bridge}" observe | grep -Fq '"control":"GUANGHU_OS"'
|
||||
|
||||
printf '%s\n' wrong >"${fixture}/dmi"
|
||||
if "${bridge}" preflight; then
|
||||
echo "wrong machine identity was accepted" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "GUANGHU_SUPERVISOR_CONTROL_BRIDGE_OK"
|
||||
Loading…
Reference in a new issue