#!/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 was retained.\n" "$step" "$LINENO" >&2' ERR script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) source "$script_dir/services.sh" requested= root='/Library/Application Support/HealthApp' host=localhost topology= deployment= external_certificates= admin_file= prepare_only=false defer=false while [[ $# -gt 0 ]]; do case "$1" in --services|--host|--topology|--deployment|--certificates|--root|--postgres-admin-file) [[ $# -gt 1 ]] || fail 'Missing value.' case "$1" in --services) requested=$2;; --host) host=$2;; --topology) topology=$2;; --deployment) deployment=$2;; --certificates) external_certificates=$2;; --root) root=$2;; --postgres-admin-file) admin_file=$2;; esac shift 2;; --prepare-only) prepare_only=true; shift;; --defer-topology-check) defer=true; shift;; --help) printf '%s\n' 'Usage: sudo bash install.sh --services all|NAME[,NAME] [--host DNS_OR_IP] [--topology FILE] [--deployment GENERATED_DIRECTORY]' 'macOS installs the runtime/PostgreSQL and independent launchd daemons using the same application DLLs and provisioning utility.'; exit 0;; *) fail "Unknown argument: $1";; esac done [[ $(uname -s) == Darwin && $EUID -eq 0 ]] || fail 'Run under sudo on macOS.' select_services "$requested" for service in "${selected_services[@]}"; do [[ ! -e "$root/services/$service" && ! -e "/Library/LaunchDaemons/healthapp-$service.plist" ]] || fail "Already installed: $service." done mkdir -p "$root" chmod 0711 "$root" step='install .NET 10 ASP.NET Core runtime' dotnet_path="$root/runtime/dotnet" if command -v dotnet >/dev/null && dotnet --list-runtimes | grep -q 'Microsoft.AspNetCore.App 10\.'; then dotnet_path=$(command -v dotnet); fi if [[ ! -x "$dotnet_path" ]]; then curl --fail --silent --show-error https://dot.net/v1/dotnet-install.sh -o "$root/dotnet-install.sh" bash "$root/dotnet-install.sh" --channel 10.0 --runtime aspnetcore --install-dir "$root/runtime" --no-path chmod -R a+rX "$root/runtime" fi provision=("$dotnet_path" "$script_dir/provisioner/HealthApp.Provisioning.dll") step='generate credentials and HTTPS certificates' [[ -n "$deployment" ]] || deployment="$root/deployment" if [[ ! -f "$deployment/deployment.json" ]]; then args=(prepare --output "$deployment" --host "$host") [[ -z "$topology" ]] || args+=(--topology "$topology") [[ -z "$external_certificates" ]] || args+=(--certificates "$external_certificates") "${provision[@]}" "${args[@]}" fi [[ "$prepare_only" == false ]] || { printf 'Generated deployment in %s\n' "$deployment"; exit 0; } step='configure HTTPS trust' security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "$deployment/healthapp-ca.crt" create_identity() { local name=$1 uid=300 if id "$name" >/dev/null 2>&1; then return; fi while dscl . -search /Users UniqueID "$uid" | grep -q . || dscl . -search /Groups PrimaryGroupID "$uid" | grep -q .; do uid=$((uid+1)); done dscl . -create "/Groups/$name" dscl . -create "/Groups/$name" PrimaryGroupID "$uid" dscl . -create "/Users/$name" dscl . -create "/Users/$name" UniqueID "$uid" dscl . -create "/Users/$name" PrimaryGroupID "$uid" dscl . -create "/Users/$name" UserShell /usr/bin/false dscl . -create "/Users/$name" NFSHomeDirectory /var/empty dscl . -create "/Users/$name" IsHidden 1 } persistence=false for service in "${selected_services[@]}"; do case "$service" in patient-profile|observation) persistence=true;; esac; done if [[ "$persistence" == true && -z "$admin_file" ]]; then step='install PostgreSQL binaries' brew_user=${SUDO_USER:-} [[ -n "$brew_user" && "$brew_user" != root ]] || fail 'Run sudo from an administrator account so Homebrew can run as that user.' if ! xcode-select -p >/dev/null 2>&1; then touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress label=$(softwareupdate --list 2>&1 | sed -n 's/^.*Label: \(Command Line Tools.*\)/\1/p' | tail -n 1) [[ -n "$label" ]] || fail 'Apple did not offer Command Line Tools through softwareupdate.' softwareupdate --install "$label" --agree-to-license rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress fi brew_path=/opt/homebrew/bin/brew [[ -x "$brew_path" ]] || brew_path=/usr/local/bin/brew if [[ ! -x "$brew_path" ]]; then curl --fail --silent --show-error https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh -o "$root/homebrew-install.sh" chmod 0644 "$root/homebrew-install.sh" sudo -u "$brew_user" env NONINTERACTIVE=1 bash "$root/homebrew-install.sh" [[ -x /opt/homebrew/bin/brew ]] && brew_path=/opt/homebrew/bin/brew || brew_path=/usr/local/bin/brew fi sudo -u "$brew_user" "$brew_path" install postgresql@17 pg_prefix=$(sudo -u "$brew_user" "$brew_path" --prefix postgresql@17) step='create installer-owned PostgreSQL cluster' pg_identity=_healthapp_postgres create_identity "$pg_identity" admin_file="$root/postgres-admin.connection" if [[ ! -f "$admin_file" ]]; then port=5432 while nc -z localhost "$port" >/dev/null 2>&1; do port=$((port+1)); done "${provision[@]}" secret --output "$root/postgres.password" mkdir -p "$root/postgresql-data" chown "$pg_identity:$pg_identity" "$root/postgresql-data" "$root/postgres.password" sudo -u "$pg_identity" "$pg_prefix/bin/initdb" -D "$root/postgresql-data" -U postgres --auth-local=scram-sha-256 --auth-host=scram-sha-256 --pwfile "$root/postgres.password" printf '\nport = %s\n' "$port" >> "$root/postgresql-data/postgresql.conf" "${provision[@]}" admin-connection --password-file "$root/postgres.password" --port "$port" --output "$admin_file" pg_plist=/Library/LaunchDaemons/healthapp-postgresql.plist cat > "$pg_plist" < Labelhealthapp-postgresqlProgramArguments$pg_prefix/bin/postgres-D$root/postgresql-dataUserName$pg_identityRunAtLoadKeepAlive PLIST chmod 0644 "$pg_plist" launchctl bootstrap system "$pg_plist" fi port=$(sed -n 's/.*Port=\([0-9]*\).*/\1/p' "$admin_file") [[ -n "$port" ]] || port=5432 for attempt in {1..30}; do if "$pg_prefix/bin/pg_isready" -q -p "$port"; then break; fi; sleep 1; done fi for service in "${selected_services[@]}"; do step="configure $service" identity="_healthapp_${service//-/_}" create_identity "$identity" case "$service" in patient-profile|observation) "${provision[@]}" database --service-dir "$deployment/$service" --admin-file "$admin_file";; esac "${provision[@]}" configure --service-dir "$deployment/$service" --root "$root" --platform macos mkdir -p "$root/services" "$root/config" "$root/state" chmod 0711 "$root/services" "$root/config" "$root/state" cp -R "$script_dir/services/$service" "$root/services/$service" chmod -R a+rX "$root/services/$service" chgrp -R "$identity" "$root/config/$service" chmod 0750 "$root/config/$service" chmod 0640 "$root/config/$service/"* if [[ "$service" == api-gateway ]]; then mkdir -p "$root/state/api-gateway/keys" chown -R "$identity:$identity" "$root/state/api-gateway" chmod 0700 "$root/state/api-gateway" fi case "$service" in patient-profile|observation) "${provision[@]}" execute --service "$service" --assembly "$root/services/$service/$(assembly_for "$service").dll" --dotnet "$dotnet_path" --root "$root";; esac # plutil converts the JSON object to an XML plist without treating configuration as shell code. "$dotnet_path" "$script_dir/provisioner/HealthApp.Provisioning.dll" launchd --service "$service" --root "$root" --dotnet "$dotnet_path" --output "/Library/LaunchDaemons/healthapp-$service.plist" chmod 0600 "/Library/LaunchDaemons/healthapp-$service.plist" launchctl bootstrap system "/Library/LaunchDaemons/healthapp-$service.plist" done step='verify startup and HTTPS' for service in "${selected_services[@]}"; do launchctl print "system/healthapp-$service" >/dev/null; done "${provision[@]}" verify-startup --deployment "$deployment" --services "$(IFS=,; echo "${selected_services[*]}")" if [[ "$defer" == false ]]; then args=(verify --deployment "$deployment" --services "$(IFS=,; echo "${selected_services[*]}")") [[ "$requested" != all ]] || args+=(--complete true) "${provision[@]}" "${args[@]}" fi printf 'Installation completed. Account credentials: %s/accounts.json\n' "$deployment"