Files
Heimdall/install/install-server.sh
T
Arnaud Dartois 8a70d2ee1f
Heimdall Gitea / Detect CI scope (pull_request) Successful in 4s
Heimdall Gitea / Build and tests (pull_request) Successful in 18m29s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 7m58s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 11m0s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 2s
feat(deploy): add Central auto-updates
2026-10-28 06:38:24 +01:00

265 lines
9.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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}"
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; do
command -v "$command" >/dev/null || { echo "Commande requise absente : $command" >&2; exit 1; }
done
docker compose version >/dev/null
copy_source_asset() {
local relative_path="$1" destination_path="$2" asset_tmp="${2}.tmp.$$"
if [[ -n "$REPO_ROOT" && -f "$REPO_ROOT/$relative_path" ]]; then
cp "$REPO_ROOT/$relative_path" "$asset_tmp"
else
if ! curl --fail --silent --show-error --location \
"$SOURCE_BASE_URL/$relative_path" --output "$asset_tmp"; then
rm -f "$asset_tmp"
return 1
fi
fi
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")"
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
mkdir -p "$destination/secrets" "$destination/optional" "$destination/optional/secrets"
chmod 700 "$destination" "$destination/secrets" "$destination/optional"
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
EOF
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
chmod 600 "$env_path"
fi
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
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
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}
ports: ["${WEB_PORT}:8080"]
volumes:
- heimdall_data:/var/lib/heimdall
- heimdall_data:/app/data
depends_on:
postgresql:
condition: service_healthy
healthcheck:
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]
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
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
docker compose --env-file "$env_path" -f "$compose_path" up -d
start_core=yes
fi
for service in "${selected[@]}"; do
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-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."