135 lines
5.3 KiB
Shell
135 lines
5.3 KiB
Shell
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
usage() {
|
|
echo "usage: install-jd-language-primary-target.sh <guanghu-os-source> <source-commit>" >&2
|
|
exit 64
|
|
}
|
|
|
|
test "$#" = 2 || usage
|
|
test "${EUID}" = 0 || exit 77
|
|
|
|
source_root="$(readlink -f "$1")"
|
|
source_commit=$2
|
|
expected_machine_id="caa7b1019517470f9d1368b6e79db49e"
|
|
expected_instance_id="f3d4b730-7f02-452f-975b-7091a4800431"
|
|
install_root="/usr/local/libexec/guanghu-os"
|
|
state_root="/var/lib/guanghu/language-primary"
|
|
receipt_root="${state_root}/receipts"
|
|
backup_root="${state_root}/backups/${source_commit}"
|
|
previous_default="$(systemctl get-default)"
|
|
installed=0
|
|
default_changed=0
|
|
|
|
[[ ${source_commit} =~ ^[0-9a-f]{40}$ ]]
|
|
test -f "${source_root}/.guanghu-source-commit"
|
|
test "$(cat "${source_root}/.guanghu-source-commit")" = "${source_commit}"
|
|
test "$(cat /etc/machine-id)" = "${expected_machine_id}"
|
|
test "$(tr '[:upper:]' '[:lower:]' </sys/class/dmi/id/product_uuid)" = \
|
|
"${expected_instance_id}"
|
|
test -f "${source_root}/packaging/systemd/guanghu-language-primary.target"
|
|
test -f "${source_root}/packaging/systemd/guanghu-world-gate.service"
|
|
test -f "${source_root}/packaging/systemd/guanghu-boot-acceptance.service"
|
|
test -f "${source_root}/scripts/verify-jd-language-primary-world.sh"
|
|
test -f "${source_root}/scripts/verify-jd-language-primary-acceptance.sh"
|
|
test "${previous_default}" = graphical.target
|
|
test ! -e "${backup_root}"
|
|
|
|
/guanghu/bin/ghctl authorize /guanghu/current install_world_version |
|
|
grep -q '^GUANGHU_ACTION_AUTHORIZED$'
|
|
|
|
for path in \
|
|
/etc/systemd/system/guanghu-language-primary.target \
|
|
/etc/systemd/system/guanghu-world-gate.service \
|
|
/etc/systemd/system/guanghu-boot-acceptance.service \
|
|
"${install_root}/verify-jd-language-primary-world" \
|
|
"${install_root}/verify-jd-language-primary-acceptance"; do
|
|
test ! -e "${path}"
|
|
done
|
|
|
|
rollback() {
|
|
if test "${default_changed}" = 1; then
|
|
systemctl set-default "${previous_default}" >/dev/null || true
|
|
fi
|
|
if test "${installed}" = 1; then
|
|
systemctl stop guanghu-language-primary.target \
|
|
guanghu-boot-acceptance.service guanghu-world-gate.service \
|
|
>/dev/null 2>&1 || true
|
|
rm -f \
|
|
/etc/systemd/system/guanghu-language-primary.target \
|
|
/etc/systemd/system/guanghu-world-gate.service \
|
|
/etc/systemd/system/guanghu-boot-acceptance.service \
|
|
"${install_root}/verify-jd-language-primary-world" \
|
|
"${install_root}/verify-jd-language-primary-acceptance"
|
|
systemctl daemon-reload || true
|
|
fi
|
|
}
|
|
trap rollback ERR
|
|
|
|
install -d -o root -g root -m 0755 "${install_root}"
|
|
install -d -o root -g root -m 0700 \
|
|
"${receipt_root}" "${backup_root}"
|
|
printf '%s\n' "${previous_default}" >"${backup_root}/previous-default-target"
|
|
|
|
install -o root -g root -m 0644 \
|
|
"${source_root}/packaging/systemd/guanghu-language-primary.target" \
|
|
/etc/systemd/system/guanghu-language-primary.target
|
|
install -o root -g root -m 0644 \
|
|
"${source_root}/packaging/systemd/guanghu-world-gate.service" \
|
|
/etc/systemd/system/guanghu-world-gate.service
|
|
install -o root -g root -m 0644 \
|
|
"${source_root}/packaging/systemd/guanghu-boot-acceptance.service" \
|
|
/etc/systemd/system/guanghu-boot-acceptance.service
|
|
install -o root -g root -m 0755 \
|
|
"${source_root}/scripts/verify-jd-language-primary-world.sh" \
|
|
"${install_root}/verify-jd-language-primary-world"
|
|
install -o root -g root -m 0755 \
|
|
"${source_root}/scripts/verify-jd-language-primary-acceptance.sh" \
|
|
"${install_root}/verify-jd-language-primary-acceptance"
|
|
installed=1
|
|
|
|
systemctl daemon-reload
|
|
systemctl start guanghu-language-primary.target
|
|
test "$(systemctl is-active guanghu-language-primary.target)" = active
|
|
test "$(systemctl is-active guanghu-world-gate.service)" = active
|
|
test "$(systemctl is-active guanghu-boot-acceptance.service)" = active
|
|
|
|
systemctl set-default guanghu-language-primary.target >/dev/null
|
|
default_changed=1
|
|
test "$(systemctl get-default)" = guanghu-language-primary.target
|
|
|
|
boot_id="$(cat /proc/sys/kernel/random/boot_id)"
|
|
boot_receipt="${receipt_root}/JD-FD-PRIMARY-LANGUAGE-PRIMARY-BOOT-${boot_id}.json"
|
|
test -f "${boot_receipt}"
|
|
jq -e '.result == "PASS_100" and .boot_changed == false' \
|
|
"${boot_receipt}" >/dev/null
|
|
|
|
install_receipt="${receipt_root}/JD-FD-PRIMARY-LANGUAGE-PRIMARY-INSTALL-${source_commit}.json"
|
|
jq -n \
|
|
--arg source_commit "${source_commit}" \
|
|
--arg previous_default "${previous_default}" \
|
|
--arg boot_receipt "${boot_receipt}" \
|
|
--arg observed_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
|
|
'{
|
|
schema: "guanghu.language-primary-install-receipt/v1",
|
|
receipt_id: ("JD-FD-PRIMARY-LANGUAGE-PRIMARY-INSTALL-" + $source_commit),
|
|
target_node_id: "JD-FD-PRIMARY",
|
|
source_repository: "REPO-014",
|
|
source_commit: $source_commit,
|
|
previous_default_target: $previous_default,
|
|
current_default_target: "guanghu-language-primary.target",
|
|
boot_receipt: $boot_receipt,
|
|
control_role: "GUANGHU_OS_LANGUAGE_PRIMARY",
|
|
linux_role: "COOPERATIVE_EXECUTION_AND_RESCUE_SUBSTRATE",
|
|
native_candidate_preserved: true,
|
|
disk_or_bootloader_changed: false,
|
|
rollback: ("systemctl set-default " + $previous_default),
|
|
observed_at: $observed_at,
|
|
result: "PASS_100"
|
|
}' >"${install_receipt}.tmp"
|
|
chmod 0600 "${install_receipt}.tmp"
|
|
mv "${install_receipt}.tmp" "${install_receipt}"
|
|
|
|
trap - ERR
|
|
printf 'PASS_100 target=guanghu-language-primary.target receipt=%s\n' \
|
|
"${install_receipt}"
|