File size: 11,591 Bytes
e9d86db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
#!/bin/bash
# Script de déploiement pour FireWatch AI
# Créé par Marino ATOHOUN
set -e
# Couleurs
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m'
# Configuration
PROJECT_NAME="firewatch_project"
DEPLOY_USER="firewatch"
DEPLOY_PATH="/opt/firewatch"
SERVICE_NAME="firewatch"
NGINX_SITE="firewatch"
log() {
echo -e "${GREEN}[$(date +'%H:%M:%S')] $1${NC}"
}
error() {
echo -e "${RED}[$(date +'%H:%M:%S')] ERROR: $1${NC}"
exit 1
}
warning() {
echo -e "${YELLOW}[$(date +'%H:%M:%S')] WARNING: $1${NC}"
}
info() {
echo -e "${BLUE}[$(date +'%H:%M:%S')] INFO: $1${NC}"
}
# Banner
echo -e "${PURPLE}"
echo "🚀 ========================================= 🚀"
echo " FireWatch AI - Déploiement Production"
echo " Créé par Marino ATOHOUN"
echo "🚀 ========================================= 🚀"
echo -e "${NC}"
# Vérification des droits root
if [[ $EUID -ne 0 ]]; then
error "Ce script doit être exécuté en tant que root (sudo)"
fi
# Mode de déploiement
echo "Modes de déploiement disponibles :"
echo "1. Docker (Recommandé)"
echo "2. Installation manuelle"
echo "3. Mise à jour"
read -p "Choisissez un mode (1-3): " deploy_mode
case $deploy_mode in
1)
log "Déploiement avec Docker..."
deploy_docker
;;
2)
log "Installation manuelle..."
deploy_manual
;;
3)
log "Mise à jour..."
update_deployment
;;
*)
error "Mode invalide"
;;
esac
deploy_docker() {
log "Installation de Docker et Docker Compose..."
# Installation de Docker
if ! command -v docker &> /dev/null; then
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
rm get-docker.sh
fi
# Installation de Docker Compose
if ! command -v docker-compose &> /dev/null; then
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
fi
# Création du répertoire de déploiement
mkdir -p $DEPLOY_PATH
cd $DEPLOY_PATH
# Copie des fichiers
if [ -f "/tmp/firewatch-deploy.tar.gz" ]; then
tar -xzf /tmp/firewatch-deploy.tar.gz
else
error "Archive de déploiement non trouvée"
fi
# Configuration des variables d'environnement
if [ ! -f ".env" ]; then
cp .env.example .env
# Génération des mots de passe
DB_PASSWORD=$(openssl rand -base64 32)
SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(50))")
sed -i "s/SECRET_KEY=.*/SECRET_KEY=$SECRET_KEY/" .env
sed -i "s/firewatch123/$DB_PASSWORD/g" .env
warning "Veuillez éditer .env avec vos configurations spécifiques"
fi
# Démarrage des services
log "Démarrage des services Docker..."
docker-compose up -d
# Attendre que les services soient prêts
sleep 30
# Vérification
if docker-compose ps | grep -q "Up"; then
log "✓ Services Docker démarrés avec succès"
else
error "Échec du démarrage des services"
fi
# Configuration Nginx (optionnel)
setup_nginx_docker
}
deploy_manual() {
log "Installation manuelle..."
# Installation des dépendances système
apt update
apt install -y python3.11 python3.11-venv python3-pip nginx postgresql redis-server
apt install -y libgl1-mesa-glx libglib2.0-0 libsm6 libxext6 libxrender-dev
# Création de l'utilisateur de déploiement
if ! id "$DEPLOY_USER" &>/dev/null; then
useradd -m -s /bin/bash $DEPLOY_USER
log "Utilisateur $DEPLOY_USER créé"
fi
# Création du répertoire de déploiement
mkdir -p $DEPLOY_PATH
chown $DEPLOY_USER:$DEPLOY_USER $DEPLOY_PATH
# Copie et extraction du code
if [ -f "/tmp/firewatch-deploy.tar.gz" ]; then
cd $DEPLOY_PATH
tar -xzf /tmp/firewatch-deploy.tar.gz
chown -R $DEPLOY_USER:$DEPLOY_USER .
else
error "Archive de déploiement non trouvée"
fi
# Configuration de l'environnement virtuel
sudo -u $DEPLOY_USER python3.11 -m venv $DEPLOY_PATH/venv
sudo -u $DEPLOY_USER $DEPLOY_PATH/venv/bin/pip install -r $DEPLOY_PATH/requirements.txt
# Configuration de PostgreSQL
setup_postgresql
# Configuration de Redis
systemctl enable redis-server
systemctl start redis-server
# Configuration de l'application
setup_application
# Configuration du service systemd
setup_systemd_service
# Configuration Nginx
setup_nginx_manual
log "Installation manuelle terminée"
}
setup_postgresql() {
log "Configuration de PostgreSQL..."
# Démarrage de PostgreSQL
systemctl enable postgresql
systemctl start postgresql
# Création de la base de données et de l'utilisateur
sudo -u postgres psql << EOF
CREATE DATABASE firewatch_db;
CREATE USER firewatch WITH PASSWORD 'firewatch_secure_password_2024';
GRANT ALL PRIVILEGES ON DATABASE firewatch_db TO firewatch;
\q
EOF
log "✓ PostgreSQL configuré"
}
setup_application() {
log "Configuration de l'application..."
cd $DEPLOY_PATH
# Configuration des variables d'environnement
if [ ! -f ".env" ]; then
cp .env.example .env
SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(50))")
sed -i "s/SECRET_KEY=.*/SECRET_KEY=$SECRET_KEY/" .env
sed -i "s/DEBUG=True/DEBUG=False/" .env
sed -i "s/DATABASE_URL=.*/DATABASE_URL=postgresql:\/\/firewatch:firewatch_secure_password_2024@localhost:5432\/firewatch_db/" .env
fi
# Migrations et collecte des fichiers statiques
sudo -u $DEPLOY_USER $DEPLOY_PATH/venv/bin/python manage.py migrate
sudo -u $DEPLOY_USER $DEPLOY_PATH/venv/bin/python manage.py collectstatic --noinput
# Création des répertoires
mkdir -p $DEPLOY_PATH/media $DEPLOY_PATH/logs
chown -R $DEPLOY_USER:$DEPLOY_USER $DEPLOY_PATH/media $DEPLOY_PATH/logs
log "✓ Application configurée"
}
setup_systemd_service() {
log "Configuration du service systemd..."
cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
[Unit]
Description=FireWatch AI Django Application
After=network.target postgresql.service redis.service
[Service]
Type=exec
User=$DEPLOY_USER
Group=$DEPLOY_USER
WorkingDirectory=$DEPLOY_PATH
Environment=PATH=$DEPLOY_PATH/venv/bin
ExecStart=$DEPLOY_PATH/venv/bin/gunicorn --bind 127.0.0.1:8000 --workers 4 --timeout 120 $PROJECT_NAME.wsgi:application
ExecReload=/bin/kill -s HUP \$MAINPID
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable $SERVICE_NAME
systemctl start $SERVICE_NAME
log "✓ Service systemd configuré et démarré"
}
setup_nginx_manual() {
log "Configuration de Nginx..."
cat > /etc/nginx/sites-available/$NGINX_SITE << EOF
server {
listen 80;
server_name _;
client_max_body_size 50M;
location /static/ {
alias $DEPLOY_PATH/staticfiles/;
expires 1y;
add_header Cache-Control "public, immutable";
}
location /media/ {
alias $DEPLOY_PATH/media/;
expires 1y;
add_header Cache-Control "public";
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
EOF
# Activation du site
ln -sf /etc/nginx/sites-available/$NGINX_SITE /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
# Test et redémarrage
nginx -t
systemctl enable nginx
systemctl restart nginx
log "✓ Nginx configuré"
}
setup_nginx_docker() {
log "Configuration de Nginx pour Docker..."
cat > nginx.conf << EOF
events {
worker_connections 1024;
}
http {
upstream firewatch_app {
server web:8000;
}
server {
listen 80;
server_name _;
client_max_body_size 50M;
location /static/ {
alias /app/staticfiles/;
}
location /media/ {
alias /app/media/;
}
location / {
proxy_pass http://firewatch_app;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
}
EOF
log "✓ Configuration Nginx pour Docker créée"
}
update_deployment() {
log "Mise à jour du déploiement..."
if [ -f "$DEPLOY_PATH/docker-compose.yml" ]; then
# Mise à jour Docker
cd $DEPLOY_PATH
docker-compose down
# Sauvegarde
backup_dir="/tmp/firewatch-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p $backup_dir
cp -r media models .env $backup_dir/
# Extraction de la nouvelle version
if [ -f "/tmp/firewatch-deploy.tar.gz" ]; then
tar -xzf /tmp/firewatch-deploy.tar.gz
fi
# Restauration des données
cp -r $backup_dir/* .
# Redémarrage
docker-compose up -d
else
# Mise à jour manuelle
systemctl stop $SERVICE_NAME
cd $DEPLOY_PATH
# Sauvegarde
backup_dir="/tmp/firewatch-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p $backup_dir
cp -r media models .env $backup_dir/
# Extraction de la nouvelle version
if [ -f "/tmp/firewatch-deploy.tar.gz" ]; then
tar -xzf /tmp/firewatch-deploy.tar.gz
fi
# Restauration des données
cp -r $backup_dir/* .
chown -R $DEPLOY_USER:$DEPLOY_USER .
# Mise à jour des dépendances
sudo -u $DEPLOY_USER $DEPLOY_PATH/venv/bin/pip install -r requirements.txt
# Migrations
sudo -u $DEPLOY_USER $DEPLOY_PATH/venv/bin/python manage.py migrate
sudo -u $DEPLOY_USER $DEPLOY_PATH/venv/bin/python manage.py collectstatic --noinput
# Redémarrage
systemctl start $SERVICE_NAME
systemctl restart nginx
fi
log "✓ Mise à jour terminée"
}
# Vérification finale
log "Vérification du déploiement..."
sleep 10
if curl -f http://localhost/ > /dev/null 2>&1; then
log "✅ Déploiement réussi ! FireWatch AI est accessible"
echo ""
echo -e "${GREEN}🎉 FireWatch AI est maintenant en production ! 🎉${NC}"
echo -e "${BLUE}URL: http://votre-serveur/${NC}"
echo -e "${BLUE}Admin: http://votre-serveur/admin/${NC}"
echo ""
echo -e "${YELLOW}N'oubliez pas de :${NC}"
echo "1. Configurer un nom de domaine"
echo "2. Installer un certificat SSL"
echo "3. Configurer les sauvegardes"
echo "4. Placer vos modèles YOLOv8 dans models/"
echo ""
echo -e "${PURPLE}Créé par Marino ATOHOUN${NC}"
else
error "Échec du déploiement - L'application n'est pas accessible"
fi
|