#!/usr/bin/env bash # setup-vps.sh — Premier déploiement complet sur VPS Ubuntu/Debian # Usage : bash setup-vps.sh set -euo pipefail DOMAIN="openfin.open-squared.tech" EMAIL="opensquaredgeneva@gmail.com" REPO="https://gitea.open-squared.tech/admin/OpenFin.git" DEPLOY_DIR="/opt/openfin" GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; NC='\033[0m' info() { echo -e "${GREEN}[INFO]${NC} $*"; } warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } abort() { echo -e "${RED}[ERR]${NC} $*"; exit 1; } # ── 1. Docker ───────────────────────────────────────────────────────────────── info "Vérification de Docker..." if ! command -v docker &>/dev/null; then info "Installation de Docker..." curl -fsSL https://get.docker.com | sh systemctl enable docker systemctl start docker fi if ! docker compose version &>/dev/null; then info "Installation du plugin docker compose..." apt-get install -y docker-compose-plugin fi info "Docker $(docker --version | cut -d' ' -f3) OK" # ── 2. Clone du repo ────────────────────────────────────────────────────────── if [ -d "$DEPLOY_DIR" ]; then warn "Le dossier $DEPLOY_DIR existe — pull de la dernière version..." git -C "$DEPLOY_DIR" pull else info "Clone du repo dans $DEPLOY_DIR..." git clone "$REPO" "$DEPLOY_DIR" fi cd "$DEPLOY_DIR/deploy" # ── 3. Fichier .env ─────────────────────────────────────────────────────────── if [ ! -f .env ]; then cp .env.example .env warn "Fichier .env créé. Remplis ta clé OpenAI :" warn " nano $DEPLOY_DIR/deploy/.env" warn "Puis relance ce script." exit 0 fi if grep -q "sk-\.\.\." .env; then abort "La clé OPENAI_API_KEY n'est pas configurée dans .env" fi # ── 4. Créer les dossiers nécessaires ───────────────────────────────────────── mkdir -p nginx/certs nginx/certbot-webroot # ── 5. Premier démarrage HTTP pour obtenir le cert SSL ──────────────────────── info "Démarrage initial en HTTP pour la vérification Let's Encrypt..." cp nginx/nginx-http-init.conf nginx/nginx.conf docker compose up -d --build info "Attente que le frontend soit prêt..." sleep 15 # ── 6. Certificat SSL Let's Encrypt ────────────────────────────────────────── info "Obtention du certificat SSL pour $DOMAIN..." docker run --rm \ -v "$DEPLOY_DIR/deploy/nginx/certs:/etc/letsencrypt" \ -v "$DEPLOY_DIR/deploy/nginx/certbot-webroot:/var/www/certbot" \ certbot/certbot certonly \ --webroot \ --webroot-path=/var/www/certbot \ -d "$DOMAIN" \ --email "$EMAIL" \ --agree-tos \ --non-interactive \ --expand # ── 7. Passage en HTTPS ─────────────────────────────────────────────────────── info "Activation de la config HTTPS..." cp nginx/nginx-https.conf nginx/nginx.conf docker compose restart nginx # ── 8. Renouvellement automatique du cert (cron mensuel) ───────────────────── CRON_CMD="0 3 1 * * docker run --rm -v $DEPLOY_DIR/deploy/nginx/certs:/etc/letsencrypt -v $DEPLOY_DIR/deploy/nginx/certbot-webroot:/var/www/certbot certbot/certbot renew --quiet && docker compose -f $DEPLOY_DIR/deploy/docker-compose.yml restart nginx" (crontab -l 2>/dev/null | grep -v certbot; echo "$CRON_CMD") | crontab - info "Renouvellement SSL automatique configuré (1er de chaque mois à 3h)" # ── 9. Résumé ───────────────────────────────────────────────────────────────── echo "" info "=== Déploiement terminé ===" info "Cockpit disponible sur : https://$DOMAIN" info "Statut des containers :" docker compose ps