corregido algunos errores internos
This commit is contained in:
parent
4ea56f3247
commit
e355003f65
3 changed files with 29 additions and 14 deletions
28
install.sh
28
install.sh
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Script de instalación completo y robusto para el Agregador RSS.
|
||||
# Instala y configura PostgreSQL, Python, y la aplicación como un servicio systemd.
|
||||
# Instala y configura PostgreSQL, Python, y la aplicación como un servicio systemd usando Waitress.
|
||||
|
||||
set -e
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ APP_USER="x" # El usuario no-root que ejecutará la aplicación.
|
|||
APP_DIR="/home/$APP_USER/$APP_NAME"
|
||||
PYTHON_ENV="$APP_DIR/venv"
|
||||
SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
|
||||
FLASK_APP_ENTRY="app:app" # Formato para Gunicorn: "nombre_fichero:nombre_variable_app"
|
||||
WSGI_APP_ENTRY="app:app" # Formato para Waitress/Gunicorn: "nombre_fichero:nombre_variable_app"
|
||||
|
||||
# ========= 1. INSTALAR DEPENDENCIAS DEL SISTEMA =========
|
||||
echo "🟢 Instalando dependencias del sistema (PostgreSQL, Python, etc.)..."
|
||||
|
|
@ -46,8 +46,8 @@ else
|
|||
echo "✅ Base de datos '$DB_NAME' ya existe."
|
||||
fi
|
||||
|
||||
# ========= 3. CREAR TABLAS DE LA APLICACIÓN =========
|
||||
echo "📐 Creando tablas en la base de datos (si no existen)..."
|
||||
# ========= 3. CREAR TABLAS CON RESTRICCIONES 'UNIQUE' =========
|
||||
echo "📐 Creando tablas con protección anti-duplicados..."
|
||||
export PGPASSWORD="$DB_PASS"
|
||||
psql -U "$DB_USER" -h localhost -d "$DB_NAME" <<EOF
|
||||
CREATE TABLE IF NOT EXISTS continentes (id SERIAL PRIMARY KEY, nombre VARCHAR(50) NOT NULL UNIQUE);
|
||||
|
|
@ -60,11 +60,14 @@ unset PGPASSWORD
|
|||
echo "✅ Tablas creadas/verificadas."
|
||||
|
||||
# ========= 4. INSERTAR DATOS INICIALES (OPCIONAL) =========
|
||||
# Nota: Si se ejecuta el script de nuevo, los INSERT darán un error de "violación de unicidad"
|
||||
# y no se insertarán duplicados, lo cual es el comportamiento deseado.
|
||||
for sql_file in continentes.sql paises.sql categorias.sql; do
|
||||
if [ -f "$APP_DIR/$sql_file" ]; then
|
||||
echo "🗂️ Insertando datos desde $sql_file..."
|
||||
export PGPASSWORD="$DB_PASS"
|
||||
psql -U "$DB_USER" -h localhost -d "$DB_NAME" -f "$APP_DIR/$sql_file"
|
||||
# El '|| true' al final evita que el script se detenga si los datos ya existen
|
||||
psql -U "$DB_USER" -h localhost -d "$DB_NAME" -f "$APP_DIR/$sql_file" || true
|
||||
unset PGPASSWORD
|
||||
else
|
||||
echo "⚠️ Archivo de datos iniciales no encontrado, omitiendo: $sql_file"
|
||||
|
|
@ -76,27 +79,26 @@ echo "🐍 Creando entorno virtual en $PYTHON_ENV"
|
|||
python3 -m venv "$PYTHON_ENV"
|
||||
|
||||
echo "📦 Instalando dependencias desde requirements.txt..."
|
||||
# Usamos el python del venv para ejecutar pip
|
||||
"$PYTHON_ENV/bin/python" -m pip install --upgrade pip
|
||||
"$PYTHON_ENV/bin/python" -m pip install -r "$APP_DIR/requirements.txt"
|
||||
|
||||
# ========= 6. CREAR SERVICIO SYSTEMD =========
|
||||
echo "⚙️ Creando servicio systemd en: $SERVICE_FILE"
|
||||
# ========= 6. CREAR SERVICIO SYSTEMD (CON WAITRESS) =========
|
||||
echo "⚙️ Creando servicio systemd con Waitress..."
|
||||
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Servicio del Agregador RSS de Noticias (Simple)
|
||||
Description=Servicio del Agregador RSS (con Waitress)
|
||||
After=network.target postgresql.service
|
||||
Requires=postgresql.service
|
||||
|
||||
[Service]
|
||||
# Mejora de seguridad: Ejecutar como usuario no-root.
|
||||
# Ejecutar como un usuario normal por seguridad
|
||||
User=$APP_USER
|
||||
Group=$APP_USER
|
||||
|
||||
WorkingDirectory=$APP_DIR
|
||||
|
||||
# Comando final: Usa gunicorn, escucha en el puerto 8000, y tiene un timeout extendido.
|
||||
ExecStart=$PYTHON_ENV/bin/gunicorn --bind 0.0.0.0:8000 --timeout 120 $FLASK_APP_ENTRY
|
||||
# Comando final: Usa Waitress, un servidor simple y eficiente sin workers.
|
||||
ExecStart=$PYTHON_ENV/bin/waitress-serve --host 0.0.0.0 --port 8000 $WSGI_APP_ENTRY
|
||||
|
||||
Restart=always
|
||||
Environment="PYTHONUNBUFFERED=1"
|
||||
|
|
@ -109,8 +111,6 @@ EOF
|
|||
echo "🚀 Recargando systemd y activando el servicio '$APP_NAME'..."
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now "$APP_NAME"
|
||||
|
||||
# --now hace start, pero un restart explícito asegura que usa la última conf.
|
||||
sudo systemctl restart "$APP_NAME"
|
||||
|
||||
# ========= ESTADO FINAL =========
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue