#!/usr/bin/env bash set -euo pipefail fail() { printf 'Error: %s\n' "$*" >&2; exit 1; } script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) source "$script_dir/services.sh" requested= probe_dir= cycle=false while [[ $# -gt 0 ]]; do case "$1" in --services) [[ $# -gt 1 ]] || fail 'Missing services.'; requested=$2; shift 2;; --probe-dir) [[ $# -gt 1 ]] || fail 'Missing probe directory.'; probe_dir=$2; shift 2;; --cycle) cycle=true; shift;; --help) printf '%s\n' 'Usage: bash verify.sh --services all|NAME[,NAME] --probe-dir DIRECTORY [--cycle]' 'Each NAME.curl contains curl configuration for a configured HTTPS endpoint and the required credentials.' 'Probes must return HTTP 200 (no redirects). --cycle explicitly permits stop/start/restart of each selected service.'; exit 0;; *) fail "Unknown argument: $1";; esac done select_services "$requested" [[ -d "$probe_dir" ]] || fail 'Provide service-specific curl probe configuration.' command -v curl >/dev/null || fail 'curl is required for network verification.' [[ -d /run/systemd/system ]] || fail 'Verification requires a running systemd system manager.' for service in "${selected_services[@]}"; do [[ -f "$probe_dir/$service.curl" ]] || fail "Missing probe configuration: $service" done pids=() for service in "${selected_services[@]}"; do unit="healthapp-$service.service" systemctl is-active --quiet "$unit" || fail "$service is not active." pid=$(systemctl show --property MainPID --value "$unit") [[ "$pid" =~ ^[1-9][0-9]*$ && -d "/proc/$pid" ]] || fail "$service has no running process." for other in "${pids[@]}"; do [[ "$pid" != "$other" ]] || fail 'Services share a process.'; done pids+=("$pid") if [[ "$cycle" == true ]]; then # Confirm stopping this service does not directly stop another selected unit. systemctl stop "$unit" systemctl is-active --quiet "$unit" && fail "$service did not stop." for peer in "${selected_services[@]}"; do [[ "$peer" == "$service" ]] || systemctl is-active --quiet "healthapp-$peer.service" || fail "Stopping $service stopped $peer." done systemctl start "$unit" systemctl restart "$unit" systemctl is-active --quiet "$unit" || fail "$service did not restart." fi # curl settings are data, not sourced shell code; suppress bodies and credentials. status=$(curl --config "$probe_dir/$service.curl" --silent --output /dev/null --write-out '%{http_code}' --max-time 15 --max-redirs 0 --retry 5 --retry-delay 1 --retry-connrefused) || fail "Network probe failed for $service." [[ "$status" == 200 ]] || fail "Unexpected HTTP status for $service: $status" systemctl is-active --quiet "$unit" || fail "$service exited during its network probe." printf 'Verified process and configured network probe: %s\n' "$service" done printf '%s\n' 'Verification passed. For a complete system also run the representative API workflow documented in INSTALLATION.md.'