#!/bin/sh set -eu PARENT_URL="${1:-}" UPDATE_JOB_ID="${2:-}" MODE="${5:-system}" CONF="${3:-/etc/pizur-monitor/agent.json}" BASE="${4:-/opt/pizur-monitor-agent}" EXPECTED_RELEASE_KEY_SHA256="C0F5B6B1AAACD1DDF1E51BC42B894532C167BA10B494996F660C0F4AB8ACFA59" case "$MODE" in system) if [ "$(id -u)" -ne 0 ] || [ "$CONF" != "/etc/pizur-monitor/agent.json" ] || [ "$BASE" != "/opt/pizur-monitor-agent" ]; then echo "System agent updates require the root-owned system installation." >&2 exit 1 fi ;; user) EXPECTED_CONF="$HOME/.config/pizur-monitor/agent.json" EXPECTED_BASE="$HOME/.local/lib/pizur-monitor-agent" if [ "$(id -u)" -eq 0 ] || [ "$CONF" != "$EXPECTED_CONF" ] || [ "$BASE" != "$EXPECTED_BASE" ]; then echo "User agent updates require the current user's dedicated installation." >&2 exit 1 fi ;; *) echo "Unsupported agent installation mode." >&2; exit 1 ;; esac if [ ! -f "$CONF" ] || [ ! -f "$BASE/agent.py" ]; then echo "Pizur Monitor agent is not installed on this host." >&2 exit 1 fi if [ -z "$PARENT_URL" ]; then PARENT_URL="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8"))["parent_url"])' "$CONF")" fi case "$PARENT_URL" in https://*) ;; *) echo "The Parent URL must use HTTPS." >&2 exit 1 ;; esac PARENT_URL="${PARENT_URL%/}" CA_CERT="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8")).get("ca_cert", ""))' "$CONF")" if [ "$MODE" = "user" ]; then python3 - "$CONF" "$BASE" <<'PY' import json, pathlib, sys config = json.loads(pathlib.Path(sys.argv[1]).read_text(encoding="utf-8")) if config.get("agent_service_mode") != "user" or config.get("agent_install_dir") != sys.argv[2]: raise SystemExit("Agent configuration does not authorize user-service updates.") PY systemctl --user is-active --quiet pizur-monitor-agent.service || { echo "The Pizur Monitor user service is not active." >&2 exit 1 } fi VERIFY_DIR="$(mktemp -d)" report_update() { [ -n "$UPDATE_JOB_ID" ] || return 0 python3 - "$CONF" "$UPDATE_JOB_ID" "$1" "$2" <<'PY' || true import json, ssl, sys, urllib.request config_path, job_id, state, message = sys.argv[1:] try: config = json.load(open(config_path, encoding="utf-8")) payload = json.dumps({"state": state, "message": message[:500]}).encode() request = urllib.request.Request(config["parent_url"].rstrip("/") + "/v1/agent-update-jobs/" + job_id + "/status", data=payload, headers={"Authorization": "Bearer " + config["agent_token"], "Content-Type": "application/json"}) context = ssl.create_default_context(cafile=config["ca_cert"]) if config.get("ca_cert") else None urllib.request.urlopen(request, timeout=240, context=context).read() except Exception: pass PY } download_artifact() { artifact="$1" destination="$2" if [ -n "$CA_CERT" ] && [ -f "$CA_CERT" ]; then curl --proto '=https' --tlsv1.2 --fail --silent --show-error --max-redirs 0 --cacert "$CA_CERT" \ "$PARENT_URL/downloads/$artifact" -o "$destination" else curl --proto '=https' --tlsv1.2 --fail --silent --show-error --max-redirs 0 \ "$PARENT_URL/downloads/$artifact" -o "$destination" fi } cleanup_update() { result=$? rm -rf "$VERIFY_DIR" if [ "$result" -ne 0 ]; then report_update failed "Linux agent update failed with exit code $result. Review the agent service journal." fi } trap cleanup_update EXIT report_update running "Verifying the signed Linux agent package." for artifact in agent.py agent.py.sig release-signing-public.pem; do download_artifact "$artifact" "$VERIFY_DIR/$artifact" done ACTUAL_RELEASE_KEY_SHA256="$(sha256sum "$VERIFY_DIR/release-signing-public.pem" | awk '{print toupper($1)}')" if [ "$ACTUAL_RELEASE_KEY_SHA256" != "$EXPECTED_RELEASE_KEY_SHA256" ]; then echo "Agent release signing key fingerprint mismatch." >&2 exit 1 fi openssl dgst -sha256 -verify "$VERIFY_DIR/release-signing-public.pem" \ -signature "$VERIFY_DIR/agent.py.sig" "$VERIFY_DIR/agent.py" >/dev/null || { echo "Agent package signature verification failed." >&2 exit 1 } FILE_MODE=0755 [ "$MODE" = "user" ] && FILE_MODE=0700 install -m "$FILE_MODE" "$VERIFY_DIR/agent.py" "$BASE/agent.py.new" python3 - "$BASE/agent.py.new" <<'PY' import pathlib, sys source = pathlib.Path(sys.argv[1]).read_text(encoding="utf-8") compile(source, sys.argv[1], "exec") PY cp -p "$BASE/agent.py" "$BASE/agent.py.previous" mv -f "$BASE/agent.py.new" "$BASE/agent.py" restart_agent() { if command -v systemctl >/dev/null 2>&1; then if [ "$MODE" = "user" ]; then systemctl --user restart pizur-monitor-agent.service && systemctl --user --no-pager --quiet is-active pizur-monitor-agent.service else systemctl restart pizur-monitor-agent.service && systemctl --no-pager --quiet is-active pizur-monitor-agent.service fi elif command -v rc-service >/dev/null 2>&1; then rc-service pizur-monitor-agent restart && rc-service pizur-monitor-agent status else return 1 fi } if ! restart_agent; then mv -f "$BASE/agent.py.previous" "$BASE/agent.py" restart_agent || true echo "Upgrade failed; restored the previous Linux agent." >&2 exit 1 fi rm -f "$BASE/agent.py.previous" report_update succeeded "Linux agent upgraded to the latest signed release." echo "Pizur Monitor Linux agent upgraded."