#!/usr/bin/env bash # init.sh — Ejecutor en el servidor de producción (192.168.10.160) # Solo arranca lo que ya está instalado. No compila, no sube código. set -euo pipefail SESSION="drop-sigmac-app" SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" cd "$SCRIPT_DIR" # 1. Asegurar existencia de .env if [[ ! -f "$SCRIPT_DIR/.env" ]]; then if [[ -f "$SCRIPT_DIR/.env.example" ]]; then echo "==> Creando .env inicial desde .env.example..." cp "$SCRIPT_DIR/.env.example" "$SCRIPT_DIR/.env" # Configurar puerto de producción 2008 y URL por defecto sed -i 's/^PORT=.*/PORT=2008/' "$SCRIPT_DIR/.env" sed -i 's|^BASE_URL=.*|BASE_URL=https://drop.sigmac.app|' "$SCRIPT_DIR/.env" else echo "⚠️ Advertencia: No se encontró .env ni .env.example." fi fi # 2. Asegurar directorios de datos y uploads mkdir -p "$SCRIPT_DIR/data/uploads" # 3. Permisos de ejecución al binario chmod +x "$SCRIPT_DIR/app_compartir" # 4. Modo de arranque: SIAX Monitor API, systemd o tmux if curl -sI http://localhost:8080/api/apps 2>/dev/null | grep -q "200 OK"; then echo "==> Reiniciando DROP_SIGMAC_APP vía API de SIAX Monitor (:8080)..." RESP=$(curl -s -X POST http://localhost:8080/api/apps/DROP_SIGMAC_APP/restart) echo " Respuesta: $RESP" if echo "$RESP" | grep -q '"success":true'; then echo "✅ Servicio reiniciado exitosamente por SIAX Monitor." exit 0 fi fi SERVICE_NAME="" if systemctl is-active --quiet siax-app-DROP_SIGMAC_APP.service 2>/dev/null || [ -f /etc/systemd/system/siax-app-DROP_SIGMAC_APP.service ]; then SERVICE_NAME="siax-app-DROP_SIGMAC_APP.service" elif systemctl is-active --quiet drop-sigmac-app.service 2>/dev/null || [ -f /etc/systemd/system/drop-sigmac-app.service ]; then SERVICE_NAME="drop-sigmac-app.service" fi if [[ -n "$SERVICE_NAME" ]]; then echo "==> Reiniciando servicio systemd '$SERVICE_NAME'..." if sudo -n true 2>/dev/null; then sudo systemctl restart "$SERVICE_NAME" else systemctl restart "$SERVICE_NAME" 2>/dev/null || true fi exit 0 fi # 5. Arranque en tmux (si no hay systemd) echo "==> Deteniendo sesión tmux anterior si existe..." tmux kill-session -t "$SESSION" 2>/dev/null || true echo "==> Iniciando drop.sigmac.app en sesión tmux '$SESSION'..." tmux new-session -d -s "$SESSION" "cd '$SCRIPT_DIR' && ./app_compartir" sleep 1 if tmux has-session -t "$SESSION" 2>/dev/null; then echo "✅ Servidor drop.sigmac.app iniciado con éxito en sesión tmux '$SESSION'." echo " Puerto: $(grep '^PORT=' "$SCRIPT_DIR/.env" 2>/dev/null | cut -d= -f2 || echo '2006')" echo " Para ver logs en vivo: tmux attach -t $SESSION" echo " Para salir del log sin matar el proceso: Ctrl+B, luego D" else echo "❌ Error al iniciar la sesión tmux." exit 1 fi