#!/usr/bin/env bash set -euo pipefail umask 077 stage=preflight fail() { echo "Installation failed at $stage: $1 Existing installation material retained." >&2; exit 1; } trap 'fail "Command failed; consult protected stage diagnostics."' ERR bundle=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) mode=local host=localhost port=5443 admin_file='' certificate='' certificate_password_file='' ca='' dotnet=${HEALTHAPP_DOTNET:-/usr/bin/dotnet} while (($#)); do case "$1" in --mode) mode=$2; shift 2;; --host) host=$2; shift 2;; --port) port=$2; shift 2;; --postgres-admin-file) admin_file=$2; shift 2;; --dotnet) dotnet=$2; shift 2;; --certificate) certificate=$2; shift 2;; --certificate-password-file) certificate_password_file=$2; shift 2;; --ca) ca=$2; shift 2;; *) fail 'Unknown installation option.';; esac done [[ $EUID -eq 0 ]] || fail 'Administrative/root access is required.' [[ "$mode" == local || "$mode" == remote ]] || fail 'Select local or remote database mode.' [[ -d /run/systemd/system ]] || fail 'A running systemd host is required.' [[ -f "$bundle/app/HealthApp.dll" && -f "$bundle/provisioner/HealthApp.Provisioning.dll" ]] || fail 'Release payload missing.' for path in /opt/healthapp-b /etc/healthapp-b /var/lib/healthapp-b /var/lib/healthapp-b-installation /etc/systemd/system/healthapp-b.service; do [[ ! -e "$path" ]] || fail 'Existing target path would be overwritten.'; done if [[ "$mode" == remote ]]; then [[ -n "$admin_file" && -f "$admin_file" ]] || fail 'Remote mode needs a protected administrative connection file with VerifyFull and trusted CA.'; fi if [[ -n "$admin_file" ]]; then [[ $(stat -c '%a' "$admin_file") == 600 || $(stat -c '%a' "$admin_file") == 400 ]] || fail 'Administrative connection file must be owner-only.'; fi install -d -m 700 /var/lib/healthapp-b-installation exec > >(tee -a /var/lib/healthapp-b-installation/install.log) 2>&1 source "$bundle/bootstrap.sh" ensure_dependencies stage='protected preparation' install -d -m 700 /var/lib/healthapp-b-installation plan=/var/lib/healthapp-b-installation/deployment prepare=(prepare --plan "$plan" --host "$host" --port "$port" --root /) if [[ -n "$certificate" ]]; then prepare+=(--certificate "$certificate" --certificate-password-file "$certificate_password_file" --ca "$ca"); fi "$dotnet" "$bundle/provisioner/HealthApp.Provisioning.dll" "${prepare[@]}" stage='database roles schemas and migration' database=(database --plan "$plan" --migrations "$bundle/migrations") if [[ "$mode" == remote ]]; then database+=(--admin-file "$admin_file"); fi "$dotnet" "$bundle/provisioner/HealthApp.Provisioning.dll" "${database[@]}" stage='application configuration and identity' id -u healthapp-b >/dev/null 2>&1 || useradd --system --user-group --home-dir /var/lib/healthapp-b --shell /usr/sbin/nologin healthapp-b install -d -m 755 /opt/healthapp-b cp -a "$bundle/app/." /opt/healthapp-b/ "$dotnet" "$bundle/provisioner/HealthApp.Provisioning.dll" configure --plan "$plan" --root / chown -R root:healthapp-b /etc/healthapp-b chmod 750 /etc/healthapp-b chmod 640 /etc/healthapp-b/appsettings.json /etc/healthapp-b/application.pfx chmod 644 /etc/healthapp-b/*.crt chown -R healthapp-b:healthapp-b /var/lib/healthapp-b chmod 700 /var/lib/healthapp-b /var/lib/healthapp-b/keys stage='native systemd registration' [[ "$dotnet" =~ ^/[a-zA-Z0-9_./-]+$ ]] || fail 'The runtime must have a safe absolute executable path.' sed "s|@DOTNET@|$dotnet|g" "$bundle/healthapp-b.service" > /etc/systemd/system/healthapp-b.service chmod 644 /etc/systemd/system/healthapp-b.service systemctl daemon-reload systemctl enable --now healthapp-b.service stage='HTTPS database and representative workflow verification' ready=false for attempt in {1..30}; do if "$dotnet" "$bundle/provisioner/HealthApp.Provisioning.dll" verify --plan "$plan"; then ready=true; break; fi; sleep 1; done [[ "$ready" == true ]] || fail 'Representative verification did not complete.' stage='independent stop restart verification' before_pid=$(systemctl show healthapp-b.service --property MainPID --value) [[ "$before_pid" =~ ^[1-9][0-9]*$ ]] || fail 'Application PID is missing.' if [[ "$mode" == local ]]; then pg_isready -q -h 127.0.0.1 -p 5432 || fail 'Database was unavailable before application stop.'; fi systemctl stop healthapp-b.service "$dotnet" "$bundle/provisioner/HealthApp.Provisioning.dll" verify-database --plan "$plan" if [[ "$mode" == local ]]; then pg_isready -q -h 127.0.0.1 -p 5432 || fail 'Application stop affected PostgreSQL.'; fi systemctl start healthapp-b.service ready=false for attempt in {1..30}; do if "$dotnet" "$bundle/provisioner/HealthApp.Provisioning.dll" verify --plan "$plan"; then ready=true; break; fi; sleep 1; done [[ "$ready" == true ]] || fail 'Workflow did not recover after restart.' after_pid=$(systemctl show healthapp-b.service --property MainPID --value) [[ "$after_pid" =~ ^[1-9][0-9]*$ && "$after_pid" != "$before_pid" ]] || fail 'Restarted application PID not verified.' stage=complete printf 'Architecture B installed and verified: one HTTPS application, independent PostgreSQL lifecycle.\n' printf 'Protected generated account material: /var/lib/healthapp-b-installation/deployment/accounts.json\n' printf 'Export /etc/healthapp-b/healthapp-ca.crt to establish client trust.\n'