guanghu-ice-heart/server-tools/persona-team-handshake/deploy-on-demand-lifecycle.sh

149 lines
4.8 KiB
Shell
Executable file

#!/usr/bin/env bash
set -Eeuo pipefail
test "$#" = 1
test "$(id -u)" = 0
expected_machine_id="caa7b1019517470f9d1368b6e79db49e"
expected_instance_id="f3d4b730-7f02-452f-975b-7091a4800431"
source_commit=$1
release_root="$(cd "$(dirname "$0")/../.." && pwd)"
source_commit_file="${release_root}/SOURCE-COMMIT"
receipt_root="/var/lib/guanghu/personas/guanghu/fifth-domain-daily/receipts"
backup_root="/var/lib/guanghu/personas/guanghu/fifth-domain-daily/lifecycle-backups/${source_commit}"
receipt="${receipt_root}/JD-PERSONA-ON-DEMAND-LIFECYCLE-${source_commit}.json"
temporary="$(mktemp -d)"
completed=0
mutated=0
handshakes=(
chenglu-team-handshake.service
guideng-team-handshake.service
kezhou-team-handshake.service
)
units=(
"${handshakes[@]}"
zhuyuan-persona-team-session.target
zhuyuan-persona-fifth-domain-daily.service
)
rollback() {
systemctl stop zhuyuan-persona-team-session.target "${handshakes[@]}" \
>/dev/null 2>&1 || true
for unit in "${units[@]}"; do
if test -f "${temporary}/${unit}"; then
install -o root -g root -m 0644 \
"${temporary}/${unit}" "/etc/systemd/system/${unit}"
else
rm -f "/etc/systemd/system/${unit}"
fi
done
systemctl daemon-reload
for unit in "${handshakes[@]}"; do
systemctl enable --now "$unit" >/dev/null 2>&1 || true
done
}
on_exit() {
status=$?
trap - EXIT
if test "${completed}" = 0 && test "${mutated}" = 1; then
rollback
fi
rm -rf "${temporary}"
exit "${status}"
}
trap on_exit EXIT
test "${source_commit}" = "$(tr -d '\n' <"${source_commit_file}")"
test "$(basename "${release_root}")" = "${source_commit}"
test "$(cat /etc/machine-id)" = "${expected_machine_id}"
test "$(tr '[:upper:]' '[:lower:]' </sys/class/dmi/id/product_uuid)" = \
"${expected_instance_id}"
test "$(systemctl get-default)" = "guanghu-language-primary.target"
test "$(systemctl is-active zhuyuan-persona-fifth-domain-daily.timer)" = active
test ! -e "${backup_root}"
test ! -e "${receipt}"
/guanghu/bin/ghctl authorize /guanghu/current install_world_version |
grep -q '^GUANGHU_ACTION_AUTHORIZED$'
install -d -o guanghu -g guanghu -m 0750 "${receipt_root}"
install -d -o root -g root -m 0700 "${backup_root}"
for unit in "${units[@]}"; do
if test -f "/etc/systemd/system/${unit}"; then
cp "/etc/systemd/system/${unit}" "${temporary}/${unit}"
cp "/etc/systemd/system/${unit}" "${backup_root}/${unit}"
fi
done
mutated=1
for unit in "${units[@]}"; do
sed "s|__RELEASE_ROOT__|${release_root}|g" \
"${release_root}/server-tools/persona-team-handshake/${unit}" \
>"/etc/systemd/system/${unit}"
chmod 0644 "/etc/systemd/system/${unit}"
done
systemctl disable "${handshakes[@]}" >/dev/null 2>&1 || true
systemctl stop zhuyuan-persona-team-session.target "${handshakes[@]}" \
>/dev/null 2>&1 || true
systemctl daemon-reload
for unit in "${handshakes[@]}"; do
test "$(systemctl is-active "${unit}" || true)" != active
test "$(systemctl is-enabled "${unit}" || true)" != enabled
done
for port in 3932 3933 3934; do
! ss -ltnH | awk '{print $4}' | grep -Eq "(^|:)${port}$"
done
systemctl start zhuyuan-persona-fifth-domain-daily.service
test "$(systemctl is-active zhuyuan-persona-fifth-domain-daily.timer)" = active
test "$(systemctl is-active zhuyuan-persona-fifth-domain-daily.service || true)" != active
test "$(systemctl is-active zhuyuan-persona-team-session.target || true)" != active
for unit in "${handshakes[@]}"; do
test "$(systemctl is-active "${unit}" || true)" != active
done
latest_daily_receipt="$(
find "${receipt_root}" -maxdepth 1 -type f \
-name 'ZY-PERSONA-DAILY-*.json' -printf '%T@ %p\n' |
sort -nr |
head -n 1 |
cut -d' ' -f2-
)"
test -n "${latest_daily_receipt}"
test "$(jq -r .result "${latest_daily_receipt}")" = PASS
jq -n \
--arg source_commit "${source_commit}" \
--arg daily_receipt "${latest_daily_receipt}" \
--arg observed_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
'{
schema: "guanghu.persona-on-demand-lifecycle-receipt/v1",
receipt_id: ("JD-PERSONA-ON-DEMAND-LIFECYCLE-" + $source_commit),
target_node_id: "JD-FD-PRIMARY",
source_repository: "REPO-012",
source_commit: $source_commit,
controller: "ICE-P-ZY001",
members: [
"CHENGLU-AGENT-001",
"GUIDENG-AGENT-001",
"ICE-GL-KZ-001"
],
daily_timer: "ACTIVE",
idle_runtime_state: "STOPPED",
wake_path: "zhuyuan-persona-fifth-domain-daily.service",
session_target: "zhuyuan-persona-team-session.target",
daily_receipt: $daily_receipt,
identity_and_memory_preserved: true,
old_writers_remain_disabled: true,
observed_at: $observed_at,
result: "PASS_100"
}' >"${receipt}.tmp"
chmod 0600 "${receipt}.tmp"
mv "${receipt}.tmp" "${receipt}"
completed=1
trap - EXIT
rm -rf "${temporary}"
printf 'PERSONA_ON_DEMAND_LIFECYCLE_PASS_100 receipt=%s\n' "${receipt}"