Files
Heimdall/install/install-server.sh
T

332 lines
14 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_PATH="${BASH_SOURCE[0]:-}"
SCRIPT_DIR=""
if [[ -n "$SCRIPT_PATH" ]]; then
SCRIPT_DIR="$(cd -- "$(dirname -- "$SCRIPT_PATH")" && pwd)"
fi
readonly SOURCE_BASE_URL="${HEIMDALL_SOURCE_BASE_URL:-https://sources.tordais.cc/arnaud.dartois/Heimdall/raw/branch/main}"
readonly PACKAGE_RELEASE_VERSION="unversioned"
readonly RELEASE_API_URL="${HEIMDALL_RELEASE_API_URL:-https://sources.tordais.cc/api/v1/repos/arnaud.dartois/Heimdall/releases}"
REPO_ROOT=""
if [[ -n "$SCRIPT_DIR" && -f "${SCRIPT_DIR}/../docker/docker-compose.ollama.yml" ]]; then
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
fi
say() { printf '%s\n' "$*"; }
ask() {
local prompt="$1" default="$2" answer
if [[ -r /dev/tty && -w /dev/tty ]]; then
read -r -p "${prompt} [${default}] " answer </dev/tty
else
read -r -p "${prompt} [${default}] " answer
fi
printf '%s' "${answer:-$default}"
}
if [[ ! -t 0 && (! -r /dev/tty || ! -w /dev/tty) ]]; then
echo "Ce script nécessite un terminal interactif." >&2
exit 2
fi
for command in docker openssl curl jq unzip; do
command -v "$command" >/dev/null || { echo "Commande requise absente : $command" >&2; exit 1; }
done
docker compose version >/dev/null
# A raw-branch bootstrap resolves the latest stable installer bundle, then
# re-enters the version-pinned script contained in that bundle. Packaged copies
# already carry their version and never resolve a moving release again.
installer_version="$PACKAGE_RELEASE_VERSION"
if [[ "$PACKAGE_RELEASE_VERSION" == unversioned ]]; then
release_api="$RELEASE_API_URL/latest"
if [[ -n "${HEIMDALL_INSTALLER_VERSION:-}" ]]; then
installer_version="$HEIMDALL_INSTALLER_VERSION"
[[ "$installer_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$ ]] || {
echo 'Version Heimdall invalide.' >&2; exit 1;
}
release_api="$RELEASE_API_URL/tags/$installer_version"
fi
bootstrap_dir="$(mktemp -d "${TMPDIR:-/tmp}/heimdall-installer.XXXXXX")"
cleanup_bootstrap() { rm -rf -- "$bootstrap_dir"; }
trap cleanup_bootstrap EXIT
if ! curl --fail --silent --show-error --location "$release_api" --output "$bootstrap_dir/release.json"; then
echo 'Impossible de récupérer la dernière release Heimdall.' >&2; exit 1
fi
resolved_version="$(jq -er '.tag_name | select(type == "string" and test("^[0-9]+\\.[0-9]+\\.[0-9]+([.-][0-9A-Za-z.-]+)?$"))' "$bootstrap_dir/release.json")" || {
echo 'La release Heimdall retournée a une version invalide.' >&2; exit 1;
}
installer_asset="heimdall-installers-$resolved_version.zip"
asset_json="$(jq -ce --arg name "$installer_asset" '.assets[] | select(.name == $name)' "$bootstrap_dir/release.json")" || {
echo 'La release ne contient pas son bundle d’installation versionné.' >&2; exit 1;
}
installer_url="$(jq -er '.browser_download_url | select(startswith("https://"))' <<<"$asset_json")" || {
echo 'URL HTTPS du bundle d’installation invalide.' >&2; exit 1;
}
asset_digest_type="$(jq -r 'if has("digest") then (.digest | type) else "missing" end' <<<"$asset_json")"
asset_digest="$(jq -r '.digest // empty' <<<"$asset_json")"
if [[ "$asset_digest_type" != null && "$asset_digest_type" != missing ]]; then
[[ "$asset_digest_type" == string && "$asset_digest" =~ ^sha256:[A-Fa-f0-9]{64}$ ]] || {
echo 'Le digest Gitea du bundle est invalide.' >&2; exit 1;
}
expected_sha256="${asset_digest#sha256:}"
else
checksum_asset_json="$(jq -ce --arg name "$installer_asset.sha256" '.assets[]? | select(.name == $name)' "$bootstrap_dir/release.json")" || {
echo 'La release ne fournit pas le checksum publié du bundle.' >&2; exit 1;
}
checksum_url="$(jq -er '.browser_download_url | select(startswith("https://"))' <<<"$checksum_asset_json")" || {
echo 'URL HTTPS du checksum du bundle invalide.' >&2; exit 1;
}
curl --fail --silent --show-error --location "$checksum_url" --output "$bootstrap_dir/installers.zip.sha256"
expected_sha256="$(awk -v name="$installer_asset" '$2 == name && $1 ~ /^[A-Fa-f0-9]{64}$/ { print tolower($1); count++ } END { if (count != 1) exit 1 }' "$bootstrap_dir/installers.zip.sha256")" || {
echo 'Le checksum publié du bundle est invalide.' >&2; exit 1;
}
fi
curl --fail --silent --show-error --location "$installer_url" --output "$bootstrap_dir/installers.zip"
actual_sha256="$(sha256sum "$bootstrap_dir/installers.zip" | cut -d ' ' -f 1)"
[[ "${actual_sha256,,}" == "${expected_sha256,,}" ]] || { echo 'Le SHA-256 du bundle téléchargé ne correspond pas à la release.' >&2; exit 1; }
unzip -q "$bootstrap_dir/installers.zip" -d "$bootstrap_dir/bundle"
packaged_script="$bootstrap_dir/bundle/install/install-server.sh"
[[ -f "$packaged_script" ]] || { echo 'Le bundle ne contient pas install/install-server.sh.' >&2; exit 1; }
HEIMDALL_INSTALLER_VERSION="$resolved_version" bash "$packaged_script" "$@"
exit $?
fi
copy_source_asset() {
2026-10-28 06:38:24 +01:00
local relative_path="$1" destination_path="$2" asset_tmp="${2}.tmp.$$"
if [[ -n "$REPO_ROOT" && -f "$REPO_ROOT/$relative_path" ]]; then
2026-10-28 06:38:24 +01:00
cp "$REPO_ROOT/$relative_path" "$asset_tmp"
else
2026-10-28 06:38:24 +01:00
if ! curl --fail --silent --show-error --location \
"$SOURCE_BASE_URL/$relative_path" --output "$asset_tmp"; then
rm -f "$asset_tmp"
return 1
fi
fi
2026-10-28 06:38:24 +01:00
if [[ -e "$destination_path" ]]; then
cp -p "$destination_path" "$destination_path.bak.$(date -u +%Y%m%dT%H%M%SZ)"
fi
mv -f "$asset_tmp" "$destination_path"
}
say "Installation de Heimdall Central (serveur + PostgreSQL)."
destination="$(ask 'Dossier d’installation' "$HOME/heimdall")"
2026-10-28 06:38:24 +01:00
compose_path="$destination/docker-compose.yml"
env_path="$destination/.env"
is_existing=no
if [[ -e "$compose_path" || -e "$env_path" ]]; then
if [[ ! -f "$compose_path" || ! -f "$env_path" || ! -f "$destination/secrets/db_password" ]]; then
echo "Installation existante incomplète; aucun fichier n’a été modifié." >&2
exit 1
fi
if [[ "$(ask 'Installation existante : mettre à jour le Compose (sauvegarde du précédent), en conservant .env, secrets et volumes ?' 'o')" =~ ^([nN]|[nN][oO][nN]?)$ ]]; then
say "Installation laissée inchangée."
exit 0
fi
is_existing=yes
else
web_port="$(ask 'Port HTTP du serveur' '5000')"
cors_origin="$(ask 'Origine(s) autorisée(s) par CORS' '*')"
[[ "$web_port" =~ ^[0-9]{1,5}$ ]] && (( web_port >= 1 && web_port <= 65535 )) || { echo "Port invalide." >&2; exit 1; }
[[ "$cors_origin" =~ ^[A-Za-z0-9*.,:/_-]+$ ]] || { echo "Origine CORS invalide." >&2; exit 1; }
fi
2026-10-28 06:38:24 +01:00
mkdir -p "$destination/secrets" "$destination/optional" "$destination/optional/secrets"
chmod 700 "$destination" "$destination/secrets" "$destination/optional"
2026-10-28 06:38:24 +01:00
if [[ "$is_existing" == no ]]; then
db_password="$(openssl rand -hex 32)"
jwt_key="$(openssl rand -hex 32)"
jwt_refresh_key="$(openssl rand -hex 32)"
printf '%s' "$db_password" > "$destination/secrets/db_password"
chmod 600 "$destination/secrets/db_password"
cat > "$env_path" <<EOF
POSTGRES_DB=heimdall
POSTGRES_USER=heimdall
DATABASE_URL=Host=postgresql;Port=5432;Database=heimdall;Username=heimdall;Password=${db_password}
JWT__KEY=${jwt_key}
JWT__REFRESHKEY=${jwt_refresh_key}
JWT__ISSUER=heimdall
JWT__AUDIENCE=heimdall-api
ASPNETCORE_ENVIRONMENT=Production
WEB_PORT=${web_port}
Cors__AllowedOrigins=${cors_origin}
TZ=Europe/Paris
REDIS_PASSWORD=$(openssl rand -hex 32)
HEIMDALL_DATA_VOLUME=heimdall_heimdall_data
PLATE_OCR_ENABLED=false
EOF
2026-10-28 06:38:24 +01:00
chmod 600 "$env_path"
else
ensure_env_value() {
local key="$1" value="$2"
if ! grep -q "^${key}=" "$env_path"; then
printf '\n%s=%s\n' "$key" "$value" >> "$env_path"
fi
}
if ! grep -q '^DATABASE_URL=' "$env_path"; then
echo "DATABASE_URL est absent du .env existant; complète-le sans changer le mot de passe PostgreSQL avant de relancer." >&2
exit 1
fi
ensure_env_value JWT__KEY "$(openssl rand -hex 32)"
ensure_env_value JWT__REFRESHKEY "$(openssl rand -hex 32)"
ensure_env_value POSTGRES_DB heimdall
ensure_env_value POSTGRES_USER heimdall
ensure_env_value JWT__ISSUER heimdall
ensure_env_value JWT__AUDIENCE heimdall-api
ensure_env_value ASPNETCORE_ENVIRONMENT Production
ensure_env_value WEB_PORT 5000
ensure_env_value Cors__AllowedOrigins '*'
ensure_env_value TZ Europe/Paris
ensure_env_value REDIS_PASSWORD "$(openssl rand -hex 32)"
ensure_env_value HEIMDALL_DATA_VOLUME heimdall_heimdall_data
ensure_env_value PLATE_OCR_ENABLED false
2026-10-28 06:38:24 +01:00
chmod 600 "$env_path"
fi
2026-10-28 06:38:24 +01:00
compose_tmp="$(mktemp "$destination/.docker-compose.yml.XXXXXX")"
trap 'rm -f "${compose_tmp:-}"' EXIT
cat > "$compose_tmp" <<'COMPOSE'
name: heimdall
services:
postgresql:
image: postgres:17-alpine
2026-10-28 06:38:24 +01:00
pull_policy: always
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets: [db_password]
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
restart: unless-stopped
networks: [heimdall-net]
heimdall:
image: eltruncho/heimdall:latest
2026-10-28 06:38:24 +01:00
pull_policy: always
labels:
com.centurylinklabs.watchtower.enable: "true"
environment:
DATABASE_URL: ${DATABASE_URL:?missing DATABASE_URL}
JWT__Issuer: ${JWT__ISSUER}
JWT__Audience: ${JWT__AUDIENCE}
JWT__KEY: ${JWT__KEY:?missing JWT__KEY}
JWT__REFRESHKEY: ${JWT__REFRESHKEY:?missing JWT__REFRESHKEY}
ASPNETCORE_ENVIRONMENT: ${ASPNETCORE_ENVIRONMENT}
ASPNETCORE_URLS: http://+:8080
Cors__AllowedOrigins: ${Cors__AllowedOrigins}
PlateOcr__Enabled: ${PLATE_OCR_ENABLED:-false}
ports: ["${WEB_PORT}:8080"]
volumes:
- heimdall_data:/var/lib/heimdall
- heimdall_data:/app/data
depends_on:
postgresql:
condition: service_healthy
healthcheck:
2026-10-28 06:38:24 +01:00
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--output-document=/dev/null", "http://127.0.0.1:8080/healthz"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
restart: unless-stopped
networks: [heimdall-net]
2026-10-28 06:38:24 +01:00
watchtower:
image: nickfedor/watchtower:1.23.2
pull_policy: always
command: ["--cleanup", "--label-enable", "--interval", "86400"]
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: unless-stopped
networks: [heimdall-net]
volumes:
pgdata:
heimdall_data:
secrets:
db_password:
file: ./secrets/db_password
networks:
heimdall-net:
name: heimdall-net
driver: bridge
COMPOSE
2026-10-28 06:38:24 +01:00
chmod 600 "$compose_tmp"
docker compose --env-file "$env_path" -f "$compose_tmp" config --quiet
if [[ "$is_existing" == yes ]]; then
backup_path="$compose_path.bak.$(date -u +%Y%m%dT%H%M%SZ)"
cp -p "$compose_path" "$backup_path"
say "Ancien Compose sauvegardé dans $backup_path"
fi
mv -f "$compose_tmp" "$compose_path"
compose_tmp=""
trap - EXIT
declare -a selected=()
for service in ollama ntfy rustfs coturn redis backup; do
if [[ "$service" == backup ]]; then
prompt="Activer les sauvegardes planifiées (nécessite une configuration dédiée) ?"
else
prompt="Installer le service optionnel ${service} ?"
fi
if [[ "$(ask "$prompt" 'n')" =~ ^([oO]|[oO][uU][iI]|[yY]|[yY][eE][sS])$ ]]; then
selected+=("$service")
copy_source_asset "docker/docker-compose.${service}.yml" \
"$destination/optional/docker-compose.${service}.yml"
chmod 600 "$destination/optional/docker-compose.${service}.yml"
fi
done
say "Compose généré dans $destination/docker-compose.yml"
say "Variables protégées dans $destination/.env (mode 600), secret PostgreSQL dans secrets/ (mode 600)."
say "Validation du Compose avant démarrage…"
start_core=no
if [[ "$(ask 'Démarrer PostgreSQL et Heimdall maintenant ?' 'o')" =~ ^([oO]|[oO][uU][iI]|[yY]|[yY][eE][sS])$ ]]; then
2026-10-28 06:38:24 +01:00
docker compose --env-file "$env_path" -f "$compose_path" up -d
start_core=yes
fi
for service in "${selected[@]}"; do
compose_project_directory="$destination"
if [[ "$service" == backup ]]; then
compose_project_directory="$destination/optional"
fi
case "$service" in
rustfs)
printf '%s' "heimdall$(openssl rand -hex 8)" > "$destination/optional/secrets/rustfs_access_key"
openssl rand -hex 32 > "$destination/optional/secrets/rustfs_secret_key"
chmod 600 "$destination/optional/secrets/rustfs_access_key" "$destination/optional/secrets/rustfs_secret_key"
;;
coturn)
openssl rand -hex 32 > "$destination/optional/secrets/coturn_secret"
chmod 600 "$destination/optional/secrets/coturn_secret"
;;
backup)
mkdir -p "$destination/optional/scripts"
for script_name in backup.sh backup-protected-data.sh backup-rustfs.sh restore.sh restore-protected-data.sh; do
copy_source_asset "docker/scripts/$script_name" \
"$destination/optional/scripts/$script_name"
done
chmod 700 "$destination/optional/scripts" "$destination/optional/scripts"/*.sh
cp "$destination/secrets/db_password" "$destination/optional/secrets/db_password"
chmod 600 "$destination/optional/secrets/db_password"
;;
esac
if [[ "$start_core" == yes ]] && [[ "$(ask "Démarrer le service optionnel ${service} ?" 'o')" =~ ^([oO]|[oO][uU][iI]|[yY]|[yY][eE][sS])$ ]]; then
docker compose --project-directory "$compose_project_directory" --project-name "heimdall-${service}" \
--env-file "$destination/.env" \
-f "$destination/optional/docker-compose.${service}.yml" up -d
else
say "Service ${service} préparé dans $destination/optional/."
fi
done
say "Configurez ensuite les intégrations activées depuis les paramètres de Central; leurs valeurs restent gérées par l’IHM."