144 lines
5.5 KiB
Bash
144 lines
5.5 KiB
Bash
#!/usr/bin/env bash
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
# 🦊 Foxy Dev Team — Service Installer
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
# Installs systemd user services for:
|
|
# - foxy-api (FastAPI backend)
|
|
# - foxy-telegram (Telegram bot v3)
|
|
#
|
|
# Usage:
|
|
# chmod +x install-services.sh
|
|
# ./install-services.sh
|
|
#
|
|
# The services run under the current user (systemd --user).
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
|
|
SERVICE_DIR="$HOME/.config/systemd/user"
|
|
ENV_FILE="$PROJECT_DIR/backend/.env"
|
|
|
|
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m'
|
|
|
|
log() { echo -e "${CYAN}[INSTALL]${NC} $1"; }
|
|
ok() { echo -e "${GREEN} ✅${NC} $1"; }
|
|
warn() { echo -e "${YELLOW} ⚠️${NC} $1"; }
|
|
err() { echo -e "${RED} ❌${NC} $1"; }
|
|
|
|
# ─── Pre-checks ───────────────────────────────────────────────────────────────
|
|
|
|
log "🦊 Foxy Dev Team — Installation des services"
|
|
echo ""
|
|
|
|
if ! command -v python3 &>/dev/null; then
|
|
err "python3 non trouvé. Installez Python 3.10+."
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v uvicorn &>/dev/null; then
|
|
warn "uvicorn non trouvé. Installation des dépendances..."
|
|
pip3 install -r "$PROJECT_DIR/backend/requirements.txt"
|
|
fi
|
|
|
|
if [ ! -f "$ENV_FILE" ]; then
|
|
warn "Fichier .env non trouvé. Copie du template..."
|
|
cp "$PROJECT_DIR/backend/.env.example" "$ENV_FILE"
|
|
warn "⚠️ IMPORTANT : Éditez $ENV_FILE avec vos valeurs réelles !"
|
|
fi
|
|
|
|
# ─── Create systemd directory ─────────────────────────────────────────────────
|
|
|
|
mkdir -p "$SERVICE_DIR"
|
|
|
|
# ─── foxy-api.service ─────────────────────────────────────────────────────────
|
|
|
|
log "Création du service foxy-api..."
|
|
cat > "$SERVICE_DIR/foxy-api.service" <<EOF
|
|
[Unit]
|
|
Description=🦊 Foxy Dev Team — FastAPI Backend
|
|
After=network.target
|
|
StartLimitIntervalSec=60
|
|
StartLimitBurst=3
|
|
|
|
[Service]
|
|
Type=simple
|
|
WorkingDirectory=$PROJECT_DIR/backend
|
|
EnvironmentFile=$ENV_FILE
|
|
ExecStart=$(command -v python3) -m uvicorn app.main:app --host 0.0.0.0 --port 7000
|
|
Restart=on-failure
|
|
RestartSec=10
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
EOF
|
|
ok "foxy-api.service créé"
|
|
|
|
# ─── foxy-telegram.service ────────────────────────────────────────────────────
|
|
|
|
log "Création du service foxy-telegram..."
|
|
cat > "$SERVICE_DIR/foxy-telegram.service" <<EOF
|
|
[Unit]
|
|
Description=🦊 Foxy Dev Team — Telegram Bot v3
|
|
After=network.target foxy-api.service
|
|
Wants=foxy-api.service
|
|
StartLimitIntervalSec=60
|
|
StartLimitBurst=3
|
|
|
|
[Service]
|
|
Type=simple
|
|
WorkingDirectory=$PROJECT_DIR/scripts
|
|
EnvironmentFile=$ENV_FILE
|
|
ExecStart=$(command -v python3) $PROJECT_DIR/scripts/foxy-telegram-bot-v3.py
|
|
Restart=on-failure
|
|
RestartSec=15
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
EOF
|
|
ok "foxy-telegram.service créé"
|
|
|
|
# ─── Enable and start ─────────────────────────────────────────────────────────
|
|
|
|
log "Rechargement de systemd..."
|
|
systemctl --user daemon-reload
|
|
ok "daemon-reload OK"
|
|
|
|
log "Activation des services (démarrage automatique)..."
|
|
systemctl --user enable foxy-api.service
|
|
ok "foxy-api activé"
|
|
systemctl --user enable foxy-telegram.service
|
|
ok "foxy-telegram activé"
|
|
|
|
# Enable lingering so services survive logout
|
|
loginctl enable-linger "$(whoami)" 2>/dev/null || true
|
|
|
|
log "Démarrage des services..."
|
|
systemctl --user start foxy-api.service
|
|
ok "foxy-api démarré"
|
|
|
|
# Wait for API to be ready before starting the bot
|
|
sleep 3
|
|
systemctl --user start foxy-telegram.service
|
|
ok "foxy-telegram démarré"
|
|
|
|
echo ""
|
|
log "═══════════════════════════════════════════════════════════"
|
|
log "🦊 Installation terminée !"
|
|
echo ""
|
|
log " Commandes utiles :"
|
|
echo " systemctl --user status foxy-api"
|
|
echo " systemctl --user status foxy-telegram"
|
|
echo " journalctl --user -u foxy-api -f"
|
|
echo " journalctl --user -u foxy-telegram -f"
|
|
echo ""
|
|
log " Dashboard : http://localhost:5173"
|
|
log " API : http://localhost:7000"
|
|
log " API Docs : http://localhost:7000/docs"
|
|
log "═══════════════════════════════════════════════════════════"
|