197 lines
7.2 KiB
Bash
Executable File
197 lines
7.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||
readonly REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
|
||
|
||
say() { printf '%s\n' "$*"; }
|
||
ask() {
|
||
local prompt="$1" default="$2" answer
|
||
read -r -p "${prompt} [${default}] " answer
|
||
printf '%s' "${answer:-$default}"
|
||
}
|
||
if [[ ! -t 0 ]]; then
|
||
echo "Ce script nécessite un terminal interactif." >&2
|
||
exit 2
|
||
fi
|
||
for command in docker python3 openssl; do
|
||
command -v "$command" >/dev/null || { echo "Commande requise absente : $command" >&2; exit 1; }
|
||
done
|
||
docker compose version >/dev/null
|
||
|
||
source_url="$(git -C "$REPO_ROOT" remote get-url origin 2>/dev/null || true)"
|
||
source_url="${source_url%.git}"
|
||
if [[ "$source_url" =~ ^https?:// ]]; then
|
||
default_manifest="${source_url%/}/raw/branch/main/docker/server-image.json"
|
||
else
|
||
default_manifest=""
|
||
fi
|
||
[[ -n "$default_manifest" ]] || { echo "Impossible de déterminer l’URL source. Clonez le dépôt ou configurez son remote origin HTTPS." >&2; exit 1; }
|
||
|
||
say "Installation de Heimdall Central (serveur + PostgreSQL)."
|
||
manifest_url="$(ask 'URL du JSON docker/server-image.json' "$default_manifest")"
|
||
destination="$(ask 'Dossier d’installation' "$HOME/heimdall")"
|
||
web_port="$(ask 'Port HTTP du serveur' '5000')"
|
||
cors_origin="$(ask 'Origine(s) autorisée(s) par CORS' '*')"
|
||
image="$(python3 - "$manifest_url" <<'PY'
|
||
import json, sys, urllib.request
|
||
import re
|
||
url = sys.argv[1]
|
||
with urllib.request.urlopen(url, timeout=20) as response:
|
||
data = json.load(response)
|
||
image = data.get("image")
|
||
if not isinstance(image, str) or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._:/@-]*", image):
|
||
raise SystemExit("Le JSON source doit contenir une propriété image valide")
|
||
print(image.strip())
|
||
PY
|
||
)"
|
||
[[ "$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; }
|
||
|
||
mkdir -p "$destination/secrets" "$destination/optional"
|
||
mkdir -p "$destination/optional/secrets"
|
||
chmod 700 "$destination" "$destination/secrets" "$destination/optional"
|
||
if [[ -e "$destination/docker-compose.yml" || -e "$destination/.env" ]]; then
|
||
echo "Le dossier contient déjà une installation Heimdall; aucun fichier n’a été remplacé." >&2
|
||
exit 1
|
||
fi
|
||
|
||
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 > "$destination/.env" <<EOF
|
||
SERVER_IMAGE_REFERENCE=${image}
|
||
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 "$destination/.env"
|
||
|
||
cat > "$destination/docker-compose.yml" <<'COMPOSE'
|
||
name: heimdall
|
||
|
||
services:
|
||
postgresql:
|
||
image: postgres:17-alpine
|
||
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: ${SERVER_IMAGE_REFERENCE:?missing SERVER_IMAGE_REFERENCE}
|
||
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", "--spider", "http://localhost:8080/healthz"]
|
||
interval: 30s
|
||
timeout: 10s
|
||
retries: 3
|
||
start_period: 60s
|
||
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 "$destination/docker-compose.yml"
|
||
|
||
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")
|
||
cp "$REPO_ROOT/docker/docker-compose.${service}.yml" "$destination/optional/"
|
||
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…"
|
||
docker compose --env-file "$destination/.env" -f "$destination/docker-compose.yml" config --quiet
|
||
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 "$destination/.env" -f "$destination/docker-compose.yml" 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"
|
||
cp "$REPO_ROOT"/docker/scripts/backup*.sh "$destination/optional/scripts/"
|
||
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."
|