93 lines
2.9 KiB
Bash
Executable File
93 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Add ExecReload to sankofa-phoenix-api-hub.service on CT 7800 if missing (matches install template).
|
|
#
|
|
# Usage:
|
|
# bash scripts/deployment/ensure-sankofa-phoenix-api-hub-systemd-exec-reload-7800.sh --dry-run --vmid 7800
|
|
# PROXMOX_OPS_APPLY=1 PROXMOX_OPS_ALLOWED_VMIDS=7800 bash scripts/deployment/ensure-sankofa-phoenix-api-hub-systemd-exec-reload-7800.sh --apply --vmid 7800
|
|
set -euo pipefail
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
|
# shellcheck source=/dev/null
|
|
source "${PROJECT_ROOT}/scripts/lib/load-project-env.sh"
|
|
# shellcheck source=/dev/null
|
|
source "${PROJECT_ROOT}/scripts/lib/proxmox-production-guard.sh"
|
|
|
|
UNIT="${SANKOFA_PHOENIX_HUB_UNIT:-/etc/systemd/system/sankofa-phoenix-api-hub.service}"
|
|
APPLY=false
|
|
DRY_RUN=false
|
|
VMID="${SANKOFA_PHOENIX_VMID:-7800}"
|
|
SSH_OPTS="-o BatchMode=yes -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new"
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--apply) APPLY=true ;;
|
|
--dry-run) DRY_RUN=true ;;
|
|
--vmid) VMID="${2:?}"; shift ;;
|
|
*) echo "Unknown arg: $1" >&2; exit 2 ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
PROXMOX_HOST="${PROXMOX_HOST:-$(get_host_for_vmid "$VMID")}"
|
|
|
|
echo "=== ensure-sankofa-phoenix-api-hub-systemd-exec-reload-7800 ==="
|
|
echo "PVE: root@${PROXMOX_HOST} VMID=${VMID} unit=${UNIT}"
|
|
echo ""
|
|
|
|
if $DRY_RUN || ! $APPLY; then
|
|
# shellcheck disable=SC2029
|
|
ssh $SSH_OPTS "root@${PROXMOX_HOST}" "pct exec ${VMID} -- bash -lc \"
|
|
[[ -f '${UNIT}' ]] || { echo 'missing ${UNIT}'; exit 0; }
|
|
grep -n ExecReload '${UNIT}' || echo '(no ExecReload yet)'
|
|
\""
|
|
echo "For apply: --apply and PROXMOX_OPS_APPLY=1 PROXMOX_OPS_ALLOWED_VMIDS=${VMID}"
|
|
exit 0
|
|
fi
|
|
|
|
if ! pguard_require_apply_flag true; then
|
|
echo "Refused: set PROXMOX_OPS_APPLY=1" >&2
|
|
exit 3
|
|
fi
|
|
if ! pguard_vmid_allowed "$VMID"; then
|
|
exit 3
|
|
fi
|
|
|
|
WORKDIR="$(mktemp -d)"
|
|
trap 'rm -rf "$WORKDIR"' EXIT
|
|
REMOTE_SH="${WORKDIR}/remote.sh"
|
|
{
|
|
printf 'export UNIT=%q\n' "$UNIT"
|
|
cat <<'EOS'
|
|
set -euo pipefail
|
|
if [[ ! -f "$UNIT" ]]; then
|
|
echo "ERROR: missing $UNIT" >&2
|
|
exit 2
|
|
fi
|
|
if grep -q '^ExecReload=' "$UNIT"; then
|
|
echo "OK: ExecReload already present"
|
|
exit 0
|
|
fi
|
|
cp -a "$UNIT" "${UNIT}.bak.execreload-$(date +%Y%m%d%H%M%S)"
|
|
python3 <<'PY'
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
unit = Path(os.environ["UNIT"])
|
|
text = unit.read_text()
|
|
if re.search(r"^ExecReload=", text, flags=re.MULTILINE):
|
|
raise SystemExit(0)
|
|
m = re.search(r"^(ExecStart=.*)$", text, flags=re.MULTILINE)
|
|
if not m:
|
|
raise SystemExit("ERROR: ExecStart= not found in unit")
|
|
insert = m.group(1) + "\nExecReload=/usr/sbin/nginx -s reload -c /etc/sankofa-phoenix-api-hub/nginx.conf"
|
|
unit.write_text(text.replace(m.group(1), insert, 1))
|
|
print("OK: inserted ExecReload after ExecStart=")
|
|
PY
|
|
systemctl daemon-reload
|
|
echo "OK: daemon-reloaded (reload hub when needed: systemctl reload sankofa-phoenix-api-hub)"
|
|
EOS
|
|
} >"$REMOTE_SH"
|
|
|
|
ssh $SSH_OPTS "root@${PROXMOX_HOST}" "pct exec ${VMID} -- bash -s" <"$REMOTE_SH"
|