Provision US-16 dependencies credentials HTTPS and native service configurations

This commit is contained in:
Steffen committed 2026-10-06 09:11:28 +02:00
1 parent 950f67d8b7
commit eedf2a941b
34 files changed
+1703 -373

No files matched your search

+129 -67
View File
@@ -1,88 +1,150 @@
#!/usr/bin/env bash
set -euo pipefail
umask 077
fail() { printf 'Error: %s\n' "$*" >&2; exit 1; }
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=
config_dir=
target_root=/
dotnet_path=/usr/bin/dotnet
start=false
migrate=false
host=localhost
topology=
deployment=
external_certificates=
admin_file=
prepare_only=false
defer_verification=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;;
--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
[[ $(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.'
[[ $(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")
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.
step=preflight
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
[[ -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
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.'
[[ -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
printf '%s\n' 'Offline files staged. Provision the runtime/service account and enable units on the target host before verification.'
[[ -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