#!/usr/bin/env bash set -euo pipefail umask 077 step=arguments fail() { printf 'Installation failed at %s: %s\n' "$step" "$*" >&2; exit 1; } trap 'printf "Installation failed at %s (line %s). Existing data and generated material were retained.\n" "$step" "$LINENO" >&2' ERR script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) if [[ $(uname -s) == Darwin ]]; then exec bash "$script_dir/install-macos.sh" "$@"; fi source "$script_dir/services.sh" source "$script_dir/bootstrap.sh" bundle=$script_dir requested= target_root=/ dotnet_path=/usr/bin/dotnet host=localhost topology= deployment= external_certificates= admin_file= prepare_only=false defer_verification=false while [[ $# -gt 0 ]]; do case "$1" in --bundle|--services|--root|--dotnet|--host|--topology|--deployment|--certificates|--postgres-admin-file) [[ $# -gt 1 ]] || fail "Missing value for $1." case "$1" in --bundle) bundle=$2;; --services) requested=$2;; --root) target_root=$2;; --dotnet) dotnet_path=$2;; --host) host=$2;; --topology) topology=$2;; --deployment) deployment=$2;; --certificates) external_certificates=$2;; --postgres-admin-file) admin_file=$2;; esac shift 2;; --prepare-only) prepare_only=true; shift;; --defer-topology-check) defer_verification=true; shift;; --help) printf '%s\n' 'Usage: bash install.sh --services all|NAME[,NAME] [--host DNS_OR_IP] [--topology topology.json] [--deployment generated-directory]' 'Provisioning installs runtime/PostgreSQL as needed and generates credentials, HTTPS, configuration, migrations and running systemd services.' 'Optional --root DIRECTORY stages generated files offline; --dotnet PATH selects the available runtime.' '--prepare-only generates a deployment for automatic distribution with distribute.sh. --certificates DIR accepts external certificates.'; exit 0;; *) fail "Unknown argument: $1";; esac done select_services "$requested" [[ $(uname -s) == Linux ]] || fail 'Only Linux is supported.' [[ "$target_root" == /* && "$target_root" != *$'\n'* ]] || fail 'Root must be an absolute path.' [[ "$dotnet_path" =~ ^/[A-Za-z0-9_./+-]+$ ]] || fail 'Runtime path is invalid.' target_root=$(realpath -m -- "$target_root") step=preflight for service in "${selected_services[@]}"; do assembly=$(assembly_for "$service") [[ -f "$bundle/services/$service/$assembly.dll" && -f "$bundle/systemd/healthapp-$service.service" ]] || fail "Missing release payload: $service" [[ ! -e "$target_root/opt/healthapp/$service" && ! -e "$target_root/etc/healthapp/$service" && ! -e "$target_root/etc/systemd/system/healthapp-$service.service" ]] || fail "Already installed: $service; existing configuration was not changed." done [[ -f "$bundle/provisioner/HealthApp.Provisioning.dll" ]] || fail 'Missing release provisioning utility.' persistence=false for service in "${selected_services[@]}"; do case "$service" in patient-profile|observation) persistence=true;; esac; done if [[ "$target_root" == / && "$prepare_only" == false ]]; then [[ $EUID -eq 0 && -d /run/systemd/system ]] || fail 'Live installation requires root and a running systemd manager.' source /etc/os-release ensure_dependencies "$ID" "$VERSION_ID" "$dotnet_path" "$persistence" else [[ -x "$dotnet_path" ]] || fail 'Offline preparation needs an available .NET 10 runtime.' fi provision=("$dotnet_path" "$bundle/provisioner/HealthApp.Provisioning.dll") step='generate deployment credentials and HTTPS certificates' if [[ -z "$deployment" || ! -f "$deployment/deployment.json" ]]; then [[ -n "$deployment" ]] || deployment="$target_root/var/lib/healthapp-installation/deployment" arguments=(prepare --output "$deployment" --host "$host") [[ -z "$topology" ]] || arguments+=(--topology "$topology") [[ -z "$external_certificates" ]] || arguments+=(--certificates "$external_certificates") "${provision[@]}" "${arguments[@]}" fi if [[ "$prepare_only" == true ]]; then printf 'Deployment material generated in %s. It contains secrets; distribute.sh installs the scoped material on each host.\n' "$deployment" exit 0 fi install -d -m 0755 "$target_root/opt/healthapp" "$target_root/etc/systemd/system" install -d -m 0711 "$target_root/etc/healthapp" install -d -m 0755 "$target_root/usr/local/share/ca-certificates" ca_id=$(sha256sum "$deployment/healthapp-ca.crt" | cut -c1-16) install -m 0644 "$deployment/healthapp-ca.crt" "$target_root/usr/local/share/ca-certificates/healthapp-prototype-$ca_id.crt" if [[ "$target_root" == / ]]; then step='install local HTTPS trust' update-ca-certificates fi for service in "${selected_services[@]}"; do step="configure $service" if [[ "$target_root" == / ]]; then user="healthapp-$service" getent group "$user" >/dev/null || groupadd --system "$user" id "$user" >/dev/null 2>&1 || useradd --system --gid "$user" --home-dir /nonexistent --shell /usr/sbin/nologin "$user" fi if [[ "$service" == patient-profile || "$service" == observation ]]; then if [[ "$target_root" == / || -n "$admin_file" ]]; then step="create dedicated database and role for $service" args=(database --service-dir "$deployment/$service") [[ -z "$admin_file" ]] || args+=(--admin-file "$admin_file") "${provision[@]}" "${args[@]}" fi fi "${provision[@]}" configure --service-dir "$deployment/$service" --root "$target_root" install -d -m 0755 "$target_root/opt/healthapp/$service" cp -R "$bundle/services/$service/." "$target_root/opt/healthapp/$service/" chmod -R a+rX,go-w "$target_root/opt/healthapp/$service" sed -e "s|ExecStart=/usr/bin/dotnet |ExecStart=$dotnet_path |" \ -e "s|EnvironmentFile=/etc/healthapp/$service.env|EnvironmentFile=/etc/healthapp/$service/service.env|" \ -e "s/^User=healthapp$/User=healthapp-$service/" -e "s/^Group=healthapp$/Group=healthapp-$service/" \ "$bundle/systemd/healthapp-$service.service" > "$target_root/etc/systemd/system/healthapp-$service.service" chmod 0644 "$target_root/etc/systemd/system/healthapp-$service.service" if [[ "$target_root" == / ]]; then chmod 0750 "/etc/healthapp/$service" chgrp "$user" "/etc/healthapp/$service" "/etc/healthapp/$service/service.pfx" chmod 0640 "/etc/healthapp/$service/service.pfx" fi printf 'Installed selected application: %s\n' "$service" done if [[ "$target_root" != / ]]; then printf 'Offline deployment staged in %s. Package installation, systemd startup and live verification were not performed.\n' "$target_root" exit 0 fi step='register independent systemd services' systemctl daemon-reload for service in "${selected_services[@]}"; do case "$service" in patient-profile|observation) step="migrate $service database" assembly=$(assembly_for "$service") systemd-run --quiet --wait --pipe --collect --unit="healthapp-$service-migrate" \ --property="User=healthapp-$service" --property="Group=healthapp-$service" \ --property="EnvironmentFile=/etc/healthapp/$service/service.env" --property="WorkingDirectory=/opt/healthapp/$service" \ "$dotnet_path" "/opt/healthapp/$service/$assembly.dll" --migrate;; esac done systemctl daemon-reload for service in "${selected_services[@]}"; do step="enable/start $service" systemctl enable --now "healthapp-$service.service" done step='verify startup and HTTPS listening' pids=() for service in "${selected_services[@]}"; do systemctl is-active --quiet "healthapp-$service.service" || fail "$service is not running." pid=$(systemctl show --property MainPID --value "healthapp-$service.service") [[ "$pid" =~ ^[1-9][0-9]*$ && -d "/proc/$pid" ]] || fail "$service has no running process." for peer in "${pids[@]}"; do [[ "$pid" != "$peer" ]] || fail 'Selected services share a process.'; done pids+=("$pid") done "${provision[@]}" verify-startup --deployment "$deployment" --services "$(IFS=,; echo "${selected_services[*]}")" if [[ "$defer_verification" == true ]]; then printf '%s\n' 'Node started. Distributed topology verification is pending until all nodes have started.' else step='verify configured dependencies and representative functionality' arguments=(verify --deployment "$deployment" --services "$(IFS=,; echo "${selected_services[*]}")") [[ "$requested" != all ]] || arguments+=(--complete true) "${provision[@]}" "${arguments[@]}" printf 'Installation and topology verification completed. Account credentials: %s/accounts.json\n' "$deployment" fi