Files
HealthAppRepository/deploy/installer/install.sh
T

89 lines
5.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
umask 077
fail() { printf 'Error: %s\n' "$*" >&2; exit 1; }
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
source "$script_dir/services.sh"
bundle=$script_dir
requested=
config_dir=
target_root=/
dotnet_path=/usr/bin/dotnet
start=false
migrate=false
while [[ $# -gt 0 ]]; do
case "$1" in
--bundle) [[ $# -gt 1 ]] || fail 'Missing bundle directory.'; bundle=$2; shift 2;;
--services) [[ $# -gt 1 ]] || fail 'Missing services.'; requested=$2; shift 2;;
--config-dir) [[ $# -gt 1 ]] || fail 'Missing configuration directory.'; config_dir=$2; shift 2;;
--root) [[ $# -gt 1 ]] || fail 'Missing staging root.'; target_root=$2; shift 2;;
--dotnet) [[ $# -gt 1 ]] || fail 'Missing runtime path.'; dotnet_path=$2; shift 2;;
--start) start=true; shift;;
--migrate) migrate=true; shift;;
--help) printf '%s\n' 'Usage: bash install.sh --services all|NAME[,NAME] --config-dir DIRECTORY [--bundle DIRECTORY] [--dotnet /absolute/path] [--migrate] [--start]' 'Optional --root DIRECTORY stages an offline filesystem image; it does not start or verify services.' 'Names: api-gateway,patient-profile,observation,insights,health-overview' 'Existing installations/configuration are never overwritten. Databases and certificates must be provisioned externally.'; exit 0;;
*) fail "Unknown argument: $1";;
esac
done
[[ $(uname -s) == Linux ]] || fail 'Only Linux is supported.'
select_services "$requested"
[[ -d "$bundle" && -d "$config_dir" ]] || fail 'Bundle and prepared configuration directories are required.'
[[ "$target_root" == /* && "$target_root" != *$'\n'* && "$target_root" != *'/../'* && "$target_root" != */.. ]] || fail 'Root must be an absolute filesystem path without parent traversal.'
[[ "$dotnet_path" =~ ^/[A-Za-z0-9_./+-]+$ ]] || fail 'Runtime must be an absolute path using letters, digits, slash, dot, underscore, plus or hyphen.'
target_root=$(realpath -m -- "$target_root")
if [[ "$target_root" != / ]]; then
[[ "$start" == false && "$migrate" == false ]] || fail 'Cannot start services or migrate databases in an offline staging root.'
else
[[ $EUID -eq 0 ]] || fail 'Live system installation requires root.'
[[ -d /run/systemd/system ]] || fail 'A running systemd system manager is required.'
[[ -x "$dotnet_path" ]] || fail 'Install the .NET 10 ASP.NET Core runtime first.'
runtime_list=$("$dotnet_path" --list-runtimes)
[[ "$runtime_list" == *'Microsoft.AspNetCore.App 10.'* ]] || fail 'The .NET 10 ASP.NET Core runtime is required.'
fi
# Preflight all selected services before copying anything or registering units.
for service in "${selected_services[@]}"; do
assembly=$(assembly_for "$service")
[[ -f "$bundle/services/$service/$assembly.dll" && -f "$bundle/services/$service/$assembly.runtimeconfig.json" && -f "$bundle/services/$service/$assembly.deps.json" ]] || fail "Missing published application: $service"
[[ -f "$bundle/systemd/healthapp-$service.service" && -f "$config_dir/$service.env" ]] || fail "Missing unit or prepared configuration: $service"
[[ ! -e "$target_root/opt/healthapp/$service" && ! -e "$target_root/etc/healthapp/$service.env" && ! -e "$target_root/etc/systemd/system/healthapp-$service.service" ]] || fail "Already installed or configured: $service. Existing files were not changed."
if grep -q 'REPLACE_ME\|REPLACE_WITH' "$config_dir/$service.env"; then fail "Replace example configuration placeholders for $service."; fi
done
if [[ "$target_root" == / ]]; then
getent group healthapp >/dev/null || groupadd --system healthapp
id healthapp >/dev/null 2>&1 || useradd --system --gid healthapp --home-dir /nonexistent --shell /usr/sbin/nologin healthapp
fi
install -d -m 0755 -- "$target_root/opt/healthapp" "$target_root/etc/systemd/system"
install -d -m 0750 -- "$target_root/etc/healthapp"
if [[ "$target_root" == / ]]; then chgrp healthapp -- "$target_root/etc/healthapp"; fi
for service in "${selected_services[@]}"; do
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"
install -m 0600 -- "$config_dir/$service.env" "$target_root/etc/healthapp/$service.env"
sed "s|ExecStart=/usr/bin/dotnet |ExecStart=$dotnet_path |" "$bundle/systemd/healthapp-$service.service" > "$target_root/etc/systemd/system/healthapp-$service.service"
chmod 0644 -- "$target_root/etc/systemd/system/healthapp-$service.service"
printf 'Installed %s\n' "$service"
done
if [[ "$target_root" == / ]]; then
systemctl daemon-reload
if [[ "$migrate" == true ]]; then
for service in "${selected_services[@]}"; do
case "$service" in patient-profile|observation)
assembly=$(assembly_for "$service")
systemd-run --quiet --wait --pipe --collect --unit="healthapp-$service-migrate" \
--property=User=healthapp --property=Group=healthapp \
--property="EnvironmentFile=/etc/healthapp/$service.env" \
--property="WorkingDirectory=/opt/healthapp/$service" \
"$dotnet_path" "/opt/healthapp/$service/$assembly.dll" --migrate
;;
esac
done
fi
for service in "${selected_services[@]}"; do
systemctl enable "healthapp-$service.service"
if [[ "$start" == true ]]; then systemctl start "healthapp-$service.service"; fi
done
printf '%s\n' 'Installation complete. Run verify.sh with the selected services and HTTP probe configuration.'
else
printf '%s\n' 'Offline files staged. Provision the runtime/service account and enable units on the target host before verification.'
fi