Przeglądaj źródła

Merge pull request #61 from kaelthasmanu/dev

fix(install): improve Alpine Linux support for install.sh
Kaelthas 9 miesięcy temu
rodzic
commit
bf767de37b
6 zmienionych plików z 542 dodań i 124 usunięć
  1. 235 0
      .github/workflows/test-install.yml
  2. 4 2
      app.py
  3. 3 0
      config.py
  4. 260 117
      install.sh
  5. 24 0
      utils/openRC
  6. 16 5
      utils/squidstats.service

+ 235 - 0
.github/workflows/test-install.yml

@@ -0,0 +1,235 @@
+name: Test Installation Script
+
+permissions:
+  contents: read
+
+on:
+  push:
+    branches: [ main, dev ]
+  pull_request:
+    branches: [ main , dev, workflow-install-test ]
+  workflow_dispatch:
+
+jobs:
+  test-install:
+    runs-on: ubuntu-latest
+    env:
+      DEBIAN_FRONTEND: noninteractive
+      TZ: Etc/UTC
+
+    strategy:
+      matrix:
+        distro: [alpine, ubuntu]
+        include:
+          - distro: alpine
+            container: alpine:latest
+            squid_package: squid
+            squid_service: squid
+            python_packages: "python3 py3-pip py3-virtualenv"
+          - distro: ubuntu
+            container: ubuntu:latest
+            squid_package: squid
+            squid_service: squid
+            python_packages: "python3 python3-pip python3-venv"
+
+    container:
+      image: ${{ matrix.container }}
+
+    steps:
+    - name: Install system dependencies
+      run: |
+        if [ "${{ matrix.distro }}" = "alpine" ]; then
+          apk update
+          apk add --no-cache \
+            bash \
+            git \
+            ${{ matrix.python_packages }} \
+            mariadb-dev \
+            curl \
+            build-base \
+            openssl-dev \
+            postgresql-dev \
+            ${{ matrix.squid_package }} \
+            openrc \
+            sudo \
+            procps \
+            grep \
+            tzdata
+        else
+          # Configurar entorno no interactivo para Ubuntu/Debian
+          export DEBIAN_FRONTEND=noninteractive
+          export TZ=UTC
+          
+          apt-get update
+          apt-get install -y \
+            bash \
+            git \
+            ${{ matrix.python_packages }} \
+            libmariadb-dev \
+            curl \
+            build-essential \
+            libssl-dev \
+            postgresql-server-dev-all \
+            ${{ matrix.squid_package }} \
+            sudo \
+            procps \
+            grep \
+            tzdata
+        fi
+
+    - name: Checkout code
+      uses: actions/checkout@v4
+
+    - name: Configure Squid (Alpine)
+      if: matrix.distro == 'alpine'
+      run: |
+        # Crear directorios necesarios
+        mkdir -p /var/log/squid /var/cache/squid /var/run/squid
+        
+        # Configurar squid básico
+        cat > /etc/squid/squid.conf << 'EOF'
+        # Basic Squid configuration for testing
+        http_port 3128
+        cache_dir ufs /var/cache/squid 100 16 256
+        access_log /var/log/squid/access.log squid
+        cache_log /var/log/squid/cache.log
+        pid_filename /var/run/squid/squid.pid
+        
+        # Allow local access
+        acl localnet src 127.0.0.0/8
+        http_access allow localnet
+        http_access deny all
+        EOF
+        
+        # Inicializar cache
+        squid -z || true
+        
+        # Iniciar squid en background
+        squid || echo "Squid started"
+
+    - name: Configure Squid (Ubuntu)
+      if: matrix.distro == 'ubuntu'
+      run: |
+        # Crear directorios necesarios
+        mkdir -p /var/log/squid /var/cache/squid /var/run/squid
+        
+        # Configurar squid básico
+        cat > /etc/squid/squid.conf << 'EOF'
+        # Basic Squid configuration for testing
+        http_port 3128
+        cache_dir ufs /var/cache/squid 100 16 256
+        access_log /var/log/squid/access.log squid
+        cache_log /var/log/squid/cache.log
+        pid_filename /var/run/squid/squid.pid
+        
+        # Allow local access
+        acl localnet src 127.0.0.0/8
+        http_access allow localnet
+        http_access deny all
+        EOF
+
+        chown -R proxy:proxy /var/log/squid /var/cache/squid /var/run/squid
+        chmod -R 755 /var/log/squid /var/cache/squid /var/run/squid
+        
+        # Inicializar cache
+        squid -z || true
+        
+        # Iniciar squid
+        service squid start || squid
+
+    - name: Generate Squid access log
+      run: |
+        # Esperar a que squid esté listo
+        sleep 2
+        
+        # Generar una entrada en el log accediendo a squid
+        curl -s -o /dev/null http://127.0.0.1:3128/ || echo "Squid access logged"
+        
+        # Verificar que el log existe
+        ls -la /var/log/squid/access.log || echo "Log file check"
+
+    - name: Make install script executable
+      run: |
+        chmod +x install.sh
+
+    - name: Run installation script (non-interactive)
+      run: |
+        # Ejecutar instalación no interactiva
+        # En CI, el script detecta app.py + requirements.txt y usa el directorio actual
+        ./install.sh --non-interactive
+      env:
+        DEBIAN_FRONTEND: noninteractive
+        TZ: UTC
+
+    - name: Start application (container mode)
+      run: |
+        # En contenedor no hay systemd/openrc, iniciar la app manualmente
+        # El script instaló el venv en el directorio actual (checkout)
+        INSTALL_DIR="$(pwd)"
+        
+        if [ ! -f "$INSTALL_DIR/app.py" ]; then
+          echo "app.py not found in $INSTALL_DIR"
+          ls -la "$INSTALL_DIR"
+          exit 1
+        fi
+        
+        # Start application in background with more robust method
+        "$INSTALL_DIR/venv/bin/python3" "$INSTALL_DIR/app.py" > "$INSTALL_DIR/app.log" 2>&1 &
+        APP_PID=$!
+        echo $APP_PID > "$INSTALL_DIR/squidstats.pid"
+        echo "Application started with PID $APP_PID from $INSTALL_DIR"
+        
+        # Give it a moment to start
+        sleep 2
+
+    - name: Wait for application to start
+      run: |
+        for i in $(seq 1 30); do
+          if curl -s --max-time 5 http://localhost:5000 > /dev/null 2>&1; then
+            echo "Application is responding on port 5000"
+            break
+          fi
+          if [ "$i" -eq 30 ]; then
+            echo "Timeout waiting for application"
+            cat "$(pwd)/app.log" 2>/dev/null || true
+            exit 1
+          fi
+          echo "Waiting for application to start... ($i/30)"
+          sleep 1
+        done
+
+    - name: Test application response
+      run: |
+        response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5000)
+        if [ "$response" = "200" ]; then
+          echo "✅ SUCCESS: Application returned HTTP 200"
+        else
+          echo "❌ FAILED: Application returned HTTP $response"
+          echo "=== Application logs ==="
+          tail -50 "$(pwd)/app.log" 2>/dev/null || echo "No app.log found"
+          echo "=== Process check ==="
+          if [ -f "$(pwd)/squidstats.pid" ]; then
+            pid=$(cat "$(pwd)/squidstats.pid")
+            ps -p $pid 2>/dev/null || echo "Process $pid not running"
+          fi
+          exit 1
+        fi
+
+    - name: Show application logs
+      if: always()
+      run: |
+        INSTALL_DIR="$(pwd)"
+        echo "=== Install Directory ==="
+        echo "$INSTALL_DIR"
+        ls -la "$INSTALL_DIR/venv/" 2>/dev/null || echo "No venv found"
+        
+        echo "=== Application Logs ==="
+        tail -50 "$INSTALL_DIR/app.log" 2>/dev/null || echo "No application logs found"
+        
+        echo "=== Process Status ==="
+        if [ -f "$INSTALL_DIR/squidstats.pid" ]; then
+          pid=$(cat "$INSTALL_DIR/squidstats.pid")
+          ps -p $pid 2>/dev/null || echo "Process $pid not running"
+        else
+          echo "No PID file found"
+        fi

+ 4 - 2
app.py

@@ -3,8 +3,9 @@ import os
 import signal
 import sys
 import threading
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, timezone
 
+from apscheduler.schedulers.background import BackgroundScheduler
 from dotenv import load_dotenv
 from flask import Flask
 from flask_apscheduler import APScheduler
@@ -76,7 +77,8 @@ def create_app():
     csrf.init_app(app)
 
     # Initialize extensions
-    scheduler = APScheduler()
+    # Pass explicit timezone to avoid tzlocal.get_localzone() failures in containers
+    scheduler = APScheduler(scheduler=BackgroundScheduler(timezone=timezone.utc))
     scheduler.init_app(app)
     scheduler.start()
 

+ 3 - 0
config.py

@@ -145,5 +145,8 @@ class Config:
     HTTPS_PROXY = safe_get_env("HTTPS_PROXY", "")
     NO_PROXY = safe_get_env("NO_PROXY", "")
 
+    # Timezone setting
+    TZ = safe_get_env("TZ", "UTC")
+
     # Squid configuration path
     SQUID_CONFIG_PATH = safe_get_env("SQUID_CONFIG_PATH", "/etc/squid/squid.conf")

+ 260 - 117
install.sh

@@ -1,82 +1,143 @@
-#! /bin/bash
+#!/bin/sh
 
 # Variable global para modo no interactivo
 NON_INTERACTIVE=false
+# Tipo de distribución detectada: debian | alpine
+DISTRO_TYPE=""
 
-function error() {
+error() {
     echo -e "\n\033[1;41m$1\033[0m\n"
 }
 
-function ok() {
+ok() {
     echo -e "\n\033[1;42m$1\033[0m\n"
 }
 
-function checkSudo() {
-    if [ "$EUID" -ne 0 ]; then
-        error "ERROR: Este script debe ejecutarse con privilegios de superusuario.\nPor favor, ejecútelo con el usuario: root $0"
+isContainer() {
+    grep -qaE 'docker|containerd' /proc/1/cgroup 2>/dev/null && return 0
+    [ -f /.dockerenv ] && return 0
+    return 1
+}
+
+detectDistro() {
+    if [ -f /etc/os-release ]; then
+        . /etc/os-release
+        case "$ID" in
+            alpine)
+                DISTRO_TYPE="alpine"
+                ;;
+            debian|ubuntu|linuxmint|pop|raspbian|kali)
+                DISTRO_TYPE="debian"
+                ;;
+            *)
+                # Intentar detectar por familia
+                if echo "$ID_LIKE" | grep -qi "debian"; then
+                    DISTRO_TYPE="debian"
+                elif echo "$ID_LIKE" | grep -qi "alpine"; then
+                    DISTRO_TYPE="alpine"
+                else
+                    error "Distribución no soportada: $ID ($PRETTY_NAME)\nDistribuciones soportadas: Debian/Ubuntu, Alpine Linux"
+                    exit 1
+                fi
+                ;;
+        esac
+    elif [ -f /etc/alpine-release ]; then
+        DISTRO_TYPE="alpine"
+    elif [ -f /etc/debian_version ]; then
+        DISTRO_TYPE="debian"
+    else
+        error "No se pudo detectar la distribución del sistema operativo"
+        exit 1
+    fi
+    echo "Distribución detectada: $DISTRO_TYPE"
+}
+
+checkSudo() {
+    if [ "$(id -u)" -ne 0 ]; then
+        error "ERROR: Este script debe ejecutarse como root."
         exit 1
     fi
 }
 
-function installDependencies() {
+installDependencies() {
     local install_dir="${1:-/opt/SquidStats}"
     local venv_dir="$install_dir/venv"
 
     if [ ! -d "$venv_dir" ]; then
         echo "El entorno virtual no existe en $venv_dir, creándolo..."
-        python3 -m venv "$venv_dir"
-        
-        if [ $? -ne 0 ]; then
+        python3 -m venv "$venv_dir" || {
             error "Error al crear el entorno virtual en $venv_dir"
             return 1
-        fi
-        
+        }
         ok "Entorno virtual creado correctamente en $venv_dir"
     fi
 
-    echo "Activando entorno virtual y instalando dependencias..."
-    source "$venv_dir/bin/activate"
+    echo "Instalando dependencias en el entorno virtual..."
 
-    pip install --upgrade pip
-    pip install -r "$install_dir/requirements.txt"
-
-    if [ $? -ne 0 ]; then
+    "$venv_dir/bin/pip" install --upgrade pip || return 1
+    "$venv_dir/bin/pip" install -r "$install_dir/requirements.txt" || {
         error "Error al instalar dependencias"
-        deactivate
         return 1
-    fi
+    }
 
     ok "Dependencias instaladas correctamente en el entorno virtual"
-    deactivate
     return 0
 }
 #  python3-pymysql delete from packages
-function checkPackages() {
-    local packages=("git" "python3" "python3-pip" "python3-venv" "libmariadb-dev" "curl" "build-essential" "libssl-dev" "python3-dev" "libpq-dev") # C-ICAP Implementation not using for now libicapapi-dev
-    local missing=()
+checkPackages() {
+    #local packages=("git" "python3" "python3-pip" "python3-venv" "libmariadb-dev" "curl" "build-essential" "libssl-dev" "python3-dev" "libpq-dev") # C-ICAP Implementation not using for now libicapapi-dev
+    local packages=""
+    local missing=""
 
-    for pkg in "${packages[@]}"; do
-        if ! dpkg -l | grep -q "^ii  $pkg "; then
-            missing+=("$pkg")
-        fi
-    done
+    if [ "$DISTRO_TYPE" = "alpine" ]; then
+        packages="git python3 py3-pip py3-virtualenv mariadb-dev curl build-base openssl-dev python3-dev postgresql-dev openrc"
+
+        for pkg in $packages; do
+            if ! apk info -e "$pkg" >/dev/null 2>&1; then
+                missing="$missing $pkg"
+            fi
+        done
 
-    if [ ${#missing[@]} -ne 0 ]; then
-        echo "Instalando paquetes faltantes: ${missing[*]}"
-        apt-get update
+        if [ -n "$missing" ]; then
+            echo "Instalando paquetes faltantes:$missing"
+            apk update
 
-        if ! apt-get install -y "${missing[@]}"; then
-            error "ERROR: Compruebe la versión de su OS se recomienda Ubuntu20.04+ o Debian12+"
-            exit 1
-        fi
+            if ! apk add --no-cache $missing; then
+                error "ERROR: Compruebe la versión de su Alpine Linux (se recomienda 3.18+)"
+                exit 1
+            fi
 
-        ok "Paquetes instalados correctamente"
+            ok "Paquetes instalados correctamente"
+        else
+            echo "Todos los paquetes necesarios ya están instalados"
+        fi
     else
-        echo "Todos los paquetes necesarios ya están instalados"
+        packages="git python3 python3-pip python3-venv libmariadb-dev curl build-essential libssl-dev python3-dev libpq-dev"
+
+        for pkg in $packages; do
+            if ! dpkg -l | grep -q "^ii  $pkg "; then
+                missing="$missing $pkg"
+            fi
+        done
+
+        if [ -n "$missing" ]; then
+            echo "Instalando paquetes faltantes:$missing"
+            export DEBIAN_FRONTEND=noninteractive
+            apt-get update
+
+            if ! apt-get install -y $missing; then
+                error "ERROR: Compruebe la versión de su OS se recomienda Ubuntu20.04+ o Debian12+"
+                exit 1
+            fi
+
+            ok "Paquetes instalados correctamente"
+        else
+            echo "Todos los paquetes necesarios ya están instalados"
+        fi
     fi
 }
 
-function checkSquidLog() {
+checkSquidLog() {
     local log_file="/var/log/squid/access.log"
 
     if [ ! -f "$log_file" ]; then
@@ -88,15 +149,15 @@ function checkSquidLog() {
     fi
 }
 
-function findInstallDir() {
-    local destinos=("/opt/SquidStats" "/usr/share/squidstats" "/home/manuel/Desktop/Projects/SquidStats")
+findInstallDir() {
+    local destinos="/opt/SquidStats /usr/share/squidstats"
 
     # Agregar el directorio actual si contiene archivos de SquidStats
     if [ -f "app.py" ] && [ -f "requirements.txt" ] && [ -d ".git" ]; then
-        destinos+=("$(pwd)")
+        destinos="$(pwd) $destinos"
     fi
 
-    for dir in "${destinos[@]}"; do
+    for dir in $destinos; do
         if [ -d "$dir" ]; then
             echo "$dir"
             return 0
@@ -106,7 +167,7 @@ function findInstallDir() {
     return 1
 }
 
-function updateOrCloneRepo() {
+updateOrCloneRepo() {
     local repo_url="https://github.com/kaelthasmanu/SquidStats.git"
     local branch="main"
     local env_exists=false
@@ -127,7 +188,7 @@ function updateOrCloneRepo() {
 
     if [ "$found_dir" = "/usr/share/squidstats" ]; then
         echo "ℹ️ Instalación detectada en /usr/share/squidstats. Esta versión fue instalada desde un .deb y no puede actualizarse con git."
-        echo "Por favor, use el gestor de paquetes (apt/dpkg) para actualizar."
+        echo "Por favor, use el gestor de paquetes de su distribución para actualizar."
         return 1
     fi
 
@@ -155,7 +216,7 @@ function updateOrCloneRepo() {
     fi
 }
 
-function updateEnvVersion() {
+updateEnvVersion() {
     local install_dir="${1:-/opt/SquidStats}"
     local CURRENT_VERSION="2.2.1"  # Variable de versión actual del script
     local env_file="$install_dir/.env"
@@ -189,7 +250,7 @@ function updateEnvVersion() {
     return 0
 }
 
-function createEnvFile() {
+createEnvFile() {
     local install_dir="${1:-/opt/SquidStats}"
     local env_file="$install_dir/.env"
 
@@ -216,7 +277,7 @@ HTTPS_PROXY=
 NO_PROXY=
 SQUID_CONFIG_PATH=/etc/squid/squid.conf
 ACL_FILES_DIR=/etc/squid/config/acls
-LISTEN_HOST=127.0.0.1
+LISTEN_HOST=0.0.0.0
 LISTEN_PORT=5000
 FIRST_PASSWORD="admin"
 JWT_EXPIRY_HOURS=24
@@ -235,52 +296,84 @@ EOF
     fi
 }
 
-function createService() {
+createService() {
     local install_dir="${1:-/opt/SquidStats}"
-    local service_file="/etc/systemd/system/squidstats.service"
 
-    if [ -f "$service_file" ]; then
-        echo "El servicio ya existe en $service_file, no se realizan cambios."
+    if isContainer; then
+        echo "⚠️ Entorno contenedor detectado. No se creará servicio."
+        echo "Para iniciar manualmente:"
+        echo "$install_dir/venv/bin/python3 $install_dir/app.py"
+        ok "Instalación completada en modo contenedor"
         return 0
     fi
 
-    echo "Creando servicio en $service_file..."
-    cat >"$service_file" <<EOF
-[Unit]
-Description=SquidStats Web Application
-After=network.target
-
-[Service]
-Type=simple
-User=root
-WorkingDirectory=$install_dir
-ExecStart=$install_dir/venv/bin/python3 $install_dir/app.py
-Restart=always
-RestartSec=5
-EnvironmentFile=$install_dir/.env
-Environment=PATH=$install_dir/venv/bin:$PATH
-
-# Resource limits
-MemoryLimit=2048M
-TimeoutStartSec=30
-TimeoutStopSec=10
-
-# Logging
-StandardOutput=journal
-StandardError=journal
-SyslogIdentifier=squidstats
-
-[Install]
-WantedBy=multi-user.target
-EOF
+    if [ "$DISTRO_TYPE" = "alpine" ]; then
+        
+        local service_file="/etc/init.d/squidstats"
+        local template="$install_dir/utils/openRC"
+
+        if [ -f "$service_file" ]; then
+            echo "El servicio ya existe en $service_file, no se realizan cambios."
+            return 0
+        fi
+
+        if [ ! -f "$template" ]; then
+            error "Plantilla de servicio OpenRC no encontrada en $template"
+            return 1
+        fi
+
+        mkdir -p /etc/init.d
+
+        echo "Creando servicio OpenRC en $service_file..."
+        sed "s|__INSTALL_DIR__|$install_dir|g" "$template" > "$service_file"
+        chmod +x "$service_file"
+
+        if command -v rc-update >/dev/null 2>&1; then
+            rc-update add squidstats default
+            chmod 755 /etc/init.d/squidstats
+
+            # Verifica si OpenRC ya está inicializado
+            if [ -f /run/openrc/softlevel ]; then
+                echo "✓ OpenRC ya está inicializado"
+                rc-status --all | head -5
+                
+            else
+                echo "✗ OpenRC NO está inicializado... intentando inicializarlo..."
+                openrc boot
+            fi
+            
+            rc-service squidstats start
+            ok "Servicio OpenRC creado y iniciado correctamente"
+        else
+            echo "⚠️ OpenRC no está activo (probable entorno Docker/contenedor). El servicio se ha creado en $service_file."
+            echo "Para iniciarlo manualmente: $install_dir/venv/bin/python3 $install_dir/app.py"
+            ok "Archivo de servicio OpenRC creado correctamente"
+        fi
+    else
+        local service_file="/etc/systemd/system/squidstats.service"
+        local template="$install_dir/utils/squidstats.service"
 
-    systemctl daemon-reload
-    systemctl enable squidstats.service
-    systemctl start squidstats.service
-    ok "Servicio creado y iniciado correctamente"
+        if [ -f "$service_file" ]; then
+            echo "El servicio ya existe en $service_file, no se realizan cambios."
+            return 0
+        fi
+
+        if [ ! -f "$template" ]; then
+            error "Plantilla de servicio systemd no encontrada en $template"
+            return 1
+        fi
+
+        echo "Creando servicio systemd en $service_file..."
+        sed "s|__INSTALL_DIR__|$install_dir|g" "$template" > "$service_file"
+
+        systemctl daemon-reload
+        systemctl enable squidstats.service
+        systemctl start squidstats.service
+        ok "Servicio systemd creado y iniciado correctamente"
+    fi
 }
 
-function configureDatabase() {
+configureDatabase() {
     local install_dir="${1:-/opt/SquidStats}"
     local env_file="$install_dir/.env"
 
@@ -309,15 +402,16 @@ function configureDatabase() {
         while true; do
             read -p "Ingrese cadena de conexión (mysql+pymysql://user:clave@host:port/db): " conn_str
 
-            if [[ "$conn_str" != mysql+pymysql://* ]]; then
-                error "Formato inválido. Debe comenzar con: mysql+pymysql://"
-                continue
-            fi
+            case "$conn_str" in
+            mysql+pymysql://*) ;;
+            *) error "Formato inválido. Debe comenzar con: mysql+pymysql://"
+               continue ;;
+            esac
 
             validation_result=$(python3 $install_dir/utils/validateString.py "$conn_str" 2>&1)
             exit_code=$?
 
-            if [[ $exit_code -eq 0 ]]; then
+            if [ "$exit_code" -eq 0 ]; then
                 sed -i "s|^DATABASE_TYPE=.*|DATABASE_TYPE=MARIADB|" "$env_file"
 
                 # validation_result tiene la cadena codificada, escapamos para sed
@@ -340,9 +434,59 @@ function configureDatabase() {
     esac
 }
 
-function uninstallSquidStats() {
+stopAndRemoveService() {
+    if [ "$DISTRO_TYPE" = "alpine" ]; then
+        local service_file="/etc/init.d/squidstats"
+        if [ -f "$service_file" ]; then
+            if command -v rc-service >/dev/null 2>&1; then
+                echo "Deteniendo servicio squidstats..."
+                rc-service squidstats stop 2>/dev/null || true
+
+                echo "Deshabilitando servicio squidstats..."
+                rc-update del squidstats default 2>/dev/null || true
+            fi
+
+            echo "Eliminando archivo de servicio..."
+            rm -f "$service_file"
+
+            ok "Servicio squidstats (OpenRC) eliminado"
+        else
+            echo "Servicio squidstats no encontrado"
+        fi
+    else
+        local service_file="/etc/systemd/system/squidstats.service"
+        if [ -f "$service_file" ]; then
+            echo "Deteniendo servicio squidstats..."
+            systemctl stop squidstats.service 2>/dev/null || true
+
+            echo "Deshabilitando servicio squidstats..."
+            systemctl disable squidstats.service 2>/dev/null || true
+
+            echo "Eliminando archivo de servicio..."
+            rm -f "$service_file"
+
+            systemctl daemon-reload
+            ok "Servicio squidstats (systemd) eliminado"
+        else
+            echo "Servicio squidstats no encontrado"
+        fi
+    fi
+}
+
+restartService() {
+    if [ "$DISTRO_TYPE" = "alpine" ]; then
+        if command -v rc-service >/dev/null 2>&1; then
+            rc-service squidstats restart
+        else
+            echo "⚠️ OpenRC no disponible, reinicie manualmente la aplicación"
+        fi
+    else
+        systemctl restart squidstats.service
+    fi
+}
+
+uninstallSquidStats() {
     local destino="/opt/SquidStats"
-    local service_file="/etc/systemd/system/squidstats.service"
 
     echo -e "\n\033[1;43mDESINSTALACIÓN DE SQUIDSTATS\033[0m"
     echo "Esta operación eliminará completamente SquidStats del sistema."
@@ -356,28 +500,14 @@ function uninstallSquidStats() {
         read -p "Respuesta: " confirm
     fi
 
-    if [[ ! "$confirm" =~ ^[sS]$ ]]; then
+    if [ "$confirm" != "s" ] && [ "$confirm" != "S" ]; then
         echo "Desinstalación cancelada."
         return 0
     fi
 
     echo "Iniciando desinstalación..."
 
-    if [ -f "$service_file" ]; then
-        echo "Deteniendo servicio squidstats..."
-        systemctl stop squidstats.service 2>/dev/null || true
-
-        echo "Deshabilitando servicio squidstats..."
-        systemctl disable squidstats.service 2>/dev/null || true
-
-        echo "Eliminando archivo de servicio..."
-        rm -f "$service_file"
-
-        systemctl daemon-reload
-        ok "Servicio squidstats eliminado"
-    else
-        echo "Servicio squidstats no encontrado"
-    fi
+    stopAndRemoveService
     
     if [ -d "$destino" ]; then
         echo "Eliminando directorio de instalación $destino..."
@@ -390,8 +520,9 @@ function uninstallSquidStats() {
     ok "SquidStats ha sido desinstalado completamente del sistema"
 }
 
-function main() {
+main() {
     checkSudo
+    detectDistro
 
     # Procesar parámetros de modo no interactivo
     local action=""
@@ -430,7 +561,7 @@ function main() {
         
         if [ "$install_dir" = "/opt/SquidStats" ] || [ "$install_dir" = "/usr/share/squidstats" ]; then
             echo "Reiniciando Servicio..."
-            systemctl restart squidstats.service
+            restartService
         else
             echo "Instalación de desarrollo detectada en $install_dir. No se reinicia el servicio del sistema."
             echo "Para ejecutar en modo desarrollo, use: python3 app.py"
@@ -442,12 +573,24 @@ function main() {
     else
         echo "Instalando aplicación web..."
         checkPackages
-        updateOrCloneRepo
-        checkSquidLog
-        installDependencies "/opt/SquidStats"
-        createEnvFile "/opt/SquidStats"
-        configureDatabase "/opt/SquidStats"
-        createService "/opt/SquidStats"
+
+        # Detectar si ya estamos en el directorio del repo (CI/desarrollo local)
+        if [ "$NON_INTERACTIVE" = true ] && [ -f "app.py" ] && [ -f "requirements.txt" ]; then
+            echo "Modo CI/local detectado: usando repositorio actual en $(pwd)"
+            install_dir="$(pwd)"
+        else
+            if ! updateOrCloneRepo; then
+                error "Error al obtener el repositorio"
+                exit 1
+            fi
+            install_dir="/opt/SquidStats"
+        fi
+
+        checkSquidLog || true
+        installDependencies "$install_dir"
+        createEnvFile "$install_dir"
+        configureDatabase "$install_dir"
+        createService "$install_dir"
 
         ok "Instalación completada! Acceda en: \033[1;37mhttp://IP:5000\033[0m"
     fi

+ 24 - 0
utils/openRC

@@ -0,0 +1,24 @@
+#!/sbin/openrc-run
+# Description: SquidStats Web Application
+
+name="SquidStats"
+description="SquidStats Web Application"
+command="__INSTALL_DIR__/venv/bin/python3"
+command_args="__INSTALL_DIR__/app.py"
+directory="__INSTALL_DIR__"
+pidfile="/run/${RC_SVCNAME}.pid"
+command_background=true
+
+depend() {
+    need net
+    after firewall
+}
+
+start_pre() {
+    if [ -f "__INSTALL_DIR__/.env" ]; then
+        set -a
+        . "__INSTALL_DIR__/.env"
+        set +a
+    fi
+    export PATH="__INSTALL_DIR__/venv/bin:$PATH"
+}

+ 16 - 5
utils/squidstats.service

@@ -1,15 +1,26 @@
 [Unit]
-Description=SquidStats
+Description=SquidStats Web Application
 After=network.target
 
 [Service]
 Type=simple
 User=root
-WorkingDirectory=/opt/SquidStats
-ExecStart=/opt/SquidStats/venv/bin/python /opt/SquidStats/app.py
+WorkingDirectory=__INSTALL_DIR__
+ExecStart=__INSTALL_DIR__/venv/bin/python3 __INSTALL_DIR__/app.py
 Restart=always
 RestartSec=5
-EnvironmentFile=/opt/SquidStats/.env
+EnvironmentFile=__INSTALL_DIR__/.env
+Environment=PATH=__INSTALL_DIR__/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+
+# Resource limits
+MemoryLimit=2048M
+TimeoutStartSec=30
+TimeoutStopSec=10
+
+# Logging
+StandardOutput=journal
+StandardError=journal
+SyslogIdentifier=squidstats
 
 [Install]
-WantedBy=multi-user.target
+WantedBy=multi-user.target