Implement US-16 independent Linux service installation

This commit is contained in:
Steffen committed 2026-10-05 16:12:33 +02:00
1 parent b6342afdad
commit 950f67d8b7
10 files changed
+837

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
#!/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
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Deployment metadata only; each application remains independently published and configured.
all_services=(api-gateway patient-profile observation insights health-overview)
select_services() {
local requested="$1" item
selected_services=()
if [[ "$requested" == all ]]; then
selected_services=("${all_services[@]}")
return
fi
IFS=',' read -r -a selected_services <<< "$requested"
[[ ${#selected_services[@]} -gt 0 && "$requested" != *, ]] || fail "Select services by comma-separated name or all."
local -A seen=()
for item in "${selected_services[@]}"; do
case "$item" in api-gateway|patient-profile|observation|insights|health-overview) ;; *) fail "Unknown service: $item" ;; esac
[[ ! ${seen[$item]+exists} ]] || fail "Repeated service: $item"
seen[$item]=1
done
}
assembly_for() {
case "$1" in
api-gateway) printf '%s' ApiGateway;;
patient-profile) printf '%s' PatientProfileService;;
observation) printf '%s' ObservationService;;
insights) printf '%s' InsightsService;;
health-overview) printf '%s' HealthOverviewService;;
esac
}
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
set -euo pipefail
fail() { printf 'Error: %s\n' "$*" >&2; exit 1; }
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
source "$script_dir/services.sh"
requested=
probe_dir=
cycle=false
while [[ $# -gt 0 ]]; do
case "$1" in
--services) [[ $# -gt 1 ]] || fail 'Missing services.'; requested=$2; shift 2;;
--probe-dir) [[ $# -gt 1 ]] || fail 'Missing probe directory.'; probe_dir=$2; shift 2;;
--cycle) cycle=true; shift;;
--help) printf '%s\n' 'Usage: bash verify.sh --services all|NAME[,NAME] --probe-dir DIRECTORY [--cycle]' 'Each NAME.curl contains curl configuration for a configured HTTPS endpoint and the required credentials.' 'Probes must return HTTP 200 (no redirects). --cycle explicitly permits stop/start/restart of each selected service.'; exit 0;;
*) fail "Unknown argument: $1";;
esac
done
select_services "$requested"
[[ -d "$probe_dir" ]] || fail 'Provide service-specific curl probe configuration.'
command -v curl >/dev/null || fail 'curl is required for network verification.'
[[ -d /run/systemd/system ]] || fail 'Verification requires a running systemd system manager.'
for service in "${selected_services[@]}"; do
[[ -f "$probe_dir/$service.curl" ]] || fail "Missing probe configuration: $service"
done
pids=()
for service in "${selected_services[@]}"; do
unit="healthapp-$service.service"
systemctl is-active --quiet "$unit" || fail "$service is not active."
pid=$(systemctl show --property MainPID --value "$unit")
[[ "$pid" =~ ^[1-9][0-9]*$ && -d "/proc/$pid" ]] || fail "$service has no running process."
for other in "${pids[@]}"; do [[ "$pid" != "$other" ]] || fail 'Services share a process.'; done
pids+=("$pid")
if [[ "$cycle" == true ]]; then
# Confirm stopping this service does not directly stop another selected unit.
systemctl stop "$unit"
systemctl is-active --quiet "$unit" && fail "$service did not stop."
for peer in "${selected_services[@]}"; do
[[ "$peer" == "$service" ]] || systemctl is-active --quiet "healthapp-$peer.service" || fail "Stopping $service stopped $peer."
done
systemctl start "$unit"
systemctl restart "$unit"
systemctl is-active --quiet "$unit" || fail "$service did not restart."
fi
# curl settings are data, not sourced shell code; suppress bodies and credentials.
status=$(curl --config "$probe_dir/$service.curl" --silent --output /dev/null --write-out '%{http_code}' --max-time 15 --max-redirs 0 --retry 5 --retry-delay 1 --retry-connrefused) || fail "Network probe failed for $service."
[[ "$status" == 200 ]] || fail "Unexpected HTTP status for $service: $status"
systemctl is-active --quiet "$unit" || fail "$service exited during its network probe."
printf 'Verified process and configured network probe: %s\n' "$service"
done
printf '%s\n' 'Verification passed. For a complete system also run the representative API workflow documented in INSTALLATION.md.'