Skip to content

Dev #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
.DS_Store
.env
addons/
config/
datadir/
log/
scripts/
postgresql/
filestore/
modulos.txt
.vscode/
versions/16.0/.vscode/*
versions/17.0/.vscode/*
102 changes: 102 additions & 0 deletions backup_restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

Comment on lines +1 to +2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enable strict shell error handling
Add set -euo pipefail immediately after the shebang to catch errors early:

 #!/bin/bash
+set -euo pipefail

# ========================
# CARGAR VARIABLES DESDE .env
# ========================
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
else
echo "❌ Archivo .env no encontrado."
exit 1
fi
Comment on lines +6 to +11
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Load .env robustly
Replace export $(grep … | xargs) with set -o allexport and source to avoid word-splitting issues:

-if [ -f .env ]; then
-    export $(grep -v '^#' .env | xargs)
-else
-    echo "❌ Archivo .env no encontrado."
-    exit 1
-fi
+if [ ! -f .env ]; then
+    echo "❌ Archivo .env no encontrado."
+    exit 1
+fi
+set -o allexport
+source .env
+set +o allexport
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
else
echo "❌ Archivo .env no encontrado."
exit 1
fi
if [ ! -f .env ]; then
echo "❌ Archivo .env no encontrado."
exit 1
fi
set -o allexport
source .env
set +o allexport
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 7-7: Quote this to prevent word splitting.

(SC2046)


PROD_URL="$PROD_URL"
PROD_MASTER_KEY="$PROD_MASTER_KEY"
PG_DB="$PG_DB"
ODOO_PORT="${ODOO_PORT:-8069}" # Por defecto 8069 si no está definido

BACKUP_FILE="odoo_backup.zip"
LOCAL_URL="http://localhost:$ODOO_PORT"

# ========================
# FUNCIÓN PARA VERIFICAR CONEXIÓN A ODOO
# ========================
function verificar_conexion() {
URL=$1
echo "🔍 Verificando conexión a $URL ..."
if curl -k -L -s --head --request GET "$URL/web/database/manager" | grep "200 OK" > /dev/null; then
echo "✅ Conexión exitosa a $URL"
else
echo "❌ No se pudo conectar a $URL"
exit 1
fi
}

# ========================
# FUNCIÓN DE BACKUP
# ========================
function backup_odoo() {
echo "🔵 Iniciando backup desde $PROD_URL de la base '$PG_DB'..."
verificar_conexion "$PROD_URL"
curl -k -L -X POST "https://$PROD_URL/web/database/backup" \
-d "master_pwd=$PROD_MASTER_KEY" \
-d "name=$PG_DB" \
-d "backup_format=zip" \
--output "$BACKUP_FILE"

if [ $? -eq 0 ]; then
echo "✅ Backup completado: $BACKUP_FILE"
else
echo "❌ Error durante el backup."
exit 1
fi
}

# ========================
# FUNCIÓN PARA ELIMINAR BASE DE DATOS LOCAL
# ========================
function drop_local_db() {
echo "🗑️ Eliminando base '$PG_DB' en entorno local ($LOCAL_URL)..."
verificar_conexion "$LOCAL_URL"
curl -k -L -X POST "$LOCAL_URL/web/database/drop" \
-d "master_pwd=$PROD_MASTER_KEY" \
-d "name=$PG_DB" \
-d "drop=true"
}

# ========================
# FUNCIÓN DE RESTORE EN LOCAL
# ========================
function restore_local() {
echo "🟡 Restaurando backup en entorno local ($LOCAL_URL) con nombre '$PG_DB'..."
verificar_conexion "$LOCAL_URL"
curl -k -L -X POST "$LOCAL_URL/web/database/restore" \
-F "master_pwd=$PROD_MASTER_KEY" \
-F "name=$PG_DB" \
-F "backup_file=@$BACKUP_FILE" \
-F "copy=true"

if [ $? -eq 0 ]; then
echo "✅ Restauración completada como '$PG_DB' en local"
else
echo "❌ Error durante la restauración."
exit 1
fi
}

# ========================
# FUNCIÓN PARA LIMPIAR ARCHIVOS TEMPORALES
# ========================
function limpiar_backup() {
echo "🧹 Eliminando archivo temporal $BACKUP_FILE..."
rm -f "$BACKUP_FILE"
echo "✅ Limpieza completada."
}

# ========================
# EJECUCIÓN COMPLETA
# ========================
limpiar_backup
backup_odoo
drop_local_db
restore_local
Comment on lines +96 to +102
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Move cleanup to end of workflow
Currently limpiar_backup runs before backup, leaving the ZIP after restore. Swap its invocation to after restore_local:

-# EJECUCIÓN COMPLETA
-limpiar_backup
-backup_odoo
-drop_local_db
-restore_local
+# EJECUCIÓN COMPLETA
+backup_odoo
+drop_local_db
+restore_local
+limpiar_backup

This ensures temporary files are removed only after successful restore.

49 changes: 49 additions & 0 deletions clean_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

Comment on lines +1 to +2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enable strict shell error handling
Add set -euo pipefail after the shebang to fail fast on errors, undefined variables, and pipeline issues:

 #!/bin/bash
+set -euo pipefail
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#!/bin/bash
#!/bin/bash
set -euo pipefail

# ========================
# CARGAR VARIABLES DESDE .env
# ========================
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
else
echo "❌ Archivo .env no encontrado."
exit 1
fi
Comment on lines +6 to +11
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Load .env more safely
Using export $(grep … | xargs) can break on spaces or special characters (SC2046). Instead, turn on automatic export and source the file:

-if [ -f .env ]; then
-    export $(grep -v '^#' .env | xargs)
-else
-    echo "❌ Archivo .env no encontrado."
-    exit 1
-fi
+if [ ! -f .env ]; then
+    echo "❌ Archivo .env no encontrado."
+    exit 1
+fi
+set -o allexport
+source .env
+set +o allexport
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
else
echo "❌ Archivo .env no encontrado."
exit 1
fi
if [ ! -f .env ]; then
echo "❌ Archivo .env no encontrado."
exit 1
fi
set -o allexport
source .env
set +o allexport
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 7-7: Quote this to prevent word splitting.

(SC2046)


PG_DB="$PG_DB"
PG_USER="$PG_USER"
PG_CONTAINER_NAME="$PG_CONTAINER_NAME"
ODOO_CONTAINER_NAME="$ODOO_CONTAINER_NAME"
PG_PASSWORD="$PG_PASSWORD"


# ========================
# COMANDOS SQL QUE VAMOS A EJECUTAR
# ========================
SQL=$(cat <<EOF
-- 1. Cambiar la contraseña del admin a 'admin'
UPDATE res_users
SET password = 'admin'
WHERE login = 'admin';

-- 2. Eliminar servidores de correo
DELETE FROM ir_mail_server;
DELETE FROM fetchmail_server;

-- 3. Desactivar todos los cron jobs
UPDATE ir_cron SET active = FALSE;
EOF
)

# ========================
# EJECUCIÓN DENTRO DEL CONTENEDOR DE POSTGRES
# ========================
echo "🔧 Ejecutando limpieza en base '$PG_DB' dentro del contenedor '$PG_CONTAINER_NAME'..."
docker exec -i "$PG_CONTAINER_NAME" psql -U "$PG_USER" -d "$PG_DB" -c "$SQL"

if [ $? -eq 0 ]; then
echo "✅ Cambios aplicados exitosamente en '$PG_DB'"
else
echo "❌ Error al aplicar los cambios en la base de datos."
exit 1
fi
80 changes: 80 additions & 0 deletions clone_repos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

# Cargar variables desde el archivo .env
if [ -f .env ]; then
set -o allexport
source .env
set +o allexport
fi

# Definir variables
MODULES_FILE="modulos.txt"
ADDONS_DIR=$CUSTOM_ADDONS

Comment on lines +11 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Validate CUSTOM_ADDONS before use
The script uses ADDONS_DIR=$CUSTOM_ADDONS but never checks if CUSTOM_ADDONS is set. If it's undefined or empty, mkdir -p will target the wrong directory. Add a guard to exit if CUSTOM_ADDONS is not defined.

# Validar que ODOO_VERSION esté definida
if [ -z "$ODOO_VERSION" ]; then
echo "❌ ERROR: La variable ODOO_VERSION no está definida en el archivo .env"
exit 1
fi

echo "🌍 Usando la versión de Odoo: $ODOO_VERSION"

# Crear la carpeta de destino si no existe
mkdir -p "$ADDONS_DIR"

# Verificar que el archivo de módulos existe y no está vacío
if [ ! -f "$MODULES_FILE" ] || [ ! -s "$MODULES_FILE" ]; then
echo "❌ ERROR: El archivo '$MODULES_FILE' no existe o está vacío."
exit 1
fi

echo "📂 Leyendo '$MODULES_FILE'..."

# Leer el archivo y extraer enlaces de GitHub y GitLab con sus posibles branches
while IFS= read -r line; do
echo "🔍 Procesando línea: $line"
repo_url=$(echo "$line" | grep -Eo "(https://github\.com/[^ ]+|https://gitlab\.com/[^ ]+)(\.git)?")
branch=$(echo "$line" | sed -n 's/.*--branch[ ]*\"\?\([^\"]*\)\"\?.*/\1/p')

# Si no se encuentra una branch en la línea, usar ODOO_VERSION
branch=${branch:-$ODOO_VERSION}

if [ -n "$repo_url" ]; then
echo "🔗 URL detectada: $repo_url"
echo "🌿 Branch seleccionada: $branch"

repo_name=$(basename "$repo_url" .git)
target_dir="$ADDONS_DIR/$repo_name"

# Verificar si el repositorio ya existe
if [ -d "$target_dir" ]; then
echo "✅ El repositorio '$repo_name' ya existe en '$ADDONS_DIR', omitiendo clonación."
else
echo "🚀 Clonando '$repo_url' en '$target_dir' (branch '$branch')..."

# Autenticación para GitLab
if [[ "$repo_url" == *"gitlab.com"* ]] && [ -n "$GITLAB_USER" ] && [ -n "$GITLAB_PASSWORD" ]; then
repo_url_with_auth=$(echo "$repo_url" | sed "s#https://#https://$GITLAB_USER:$GITLAB_PASSWORD@#")
else
repo_url_with_auth="$repo_url"
fi

# Intentar clonar con la branch especificada
if git clone --branch "$branch" --single-branch "$repo_url_with_auth" "$target_dir" 2>/dev/null; then
echo "✅ Repositorio '$repo_name' clonado en la branch '$branch'."
else
echo "⚠️ No se encontró la branch '$branch' en '$repo_url'. Clonando la branch por defecto..."
if git clone "$repo_url_with_auth" "$target_dir"; then
echo "✅ Repositorio '$repo_name' clonado en la branch por defecto."
else
echo "❌ Error al clonar '$repo_url'."
fi
fi
fi
else
echo "⚠️ No se encontró una URL válida en la línea: $line"
fi

done < "$MODULES_FILE"

echo "🔄 Proceso de clonación finalizado."
49 changes: 49 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
networks:
odoo:

services:
postgres:
#container_name: ${PG_CONTAINER_NAME}
image: ${PG_IMAGE}:${PG_VERSION}
user: root
environment:
- POSTGRES_DB=${PG_DB}
- POSTGRES_USER=${PG_USER}
- POSTGRES_PASSWORD=${PG_PASSWORD}
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- ${PG_PORT}:5432
volumes:
- ${PG_PATH}:/var/lib/postgresql/data
networks:
- odoo
Comment on lines +5 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid running PostgreSQL container as root
Running the database service with user: root elevates privileges unnecessarily and can be a security risk. Unless there is a strong reason, drop the user: root line to use the container’s default postgres user.
Also, the image: ${PG_IMAGE}:${PG_VERSION} entry will fail if those environment variables are unset. Consider adding default values in an .env file or using Compose’s env_file directive to guarantee required variables.

🤖 Prompt for AI Agents (early access)
In docker-compose.yml lines 5 to 19, remove the line setting `user: root` to avoid running the PostgreSQL container with elevated privileges and rely on the default postgres user for security. Additionally, ensure that the environment variables used in the image tag (`${PG_IMAGE}` and `${PG_VERSION}`) have default values by adding them to an `.env` file or by using the `env_file` directive in the Compose configuration to prevent failures when these variables are unset.


odoo:
#container_name: ${ODOO_CONTAINER_NAME}
#image: ${ODOO_IMAGE}:${ODOO_VERSION}
build: versions/16.0/.
restart: unless-stopped
entrypoint: ["/scripts/entrypoint.sh"]
user: root
depends_on:
- postgres
ports:
- ${ODOO_PORT}:8069
volumes:
- ./entrypoint.sh:/scripts/entrypoint.sh # Monta entrypoint.sh en el contenedor
- ./modulos.txt:/scripts/modulos.txt # Monta modulos.txt en el contenedor
- ${CONF_PATH}:/opt/etc/odoo
- ${DATA_PATH}:/opt/odoo/data
- ${LOG_PATH}:/var/log/odoo
- ${CUSTOM_ADDONS}:/mnt/extra-addons
- ${SCRIPT_PATH}:/scripts
- ${FILE_PATH}:/var/lib/odoo
environment:
- HOST=postgres
- CUSTOM_ADDONS=${CUSTOM_ADDONS}
- MAIN_MODULE=${MAIN_MODULE}
- POSTGRES_DB=${PG_DB}
- POSTGRES_USER=${PG_USER}
- POSTGRES_PASSWORD=${PG_PASSWORD}
networks:
- odoo
96 changes: 96 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash
set -e

# Validar variables de entorno
: "${HOST:?Variable HOST no definida}"
: "${POSTGRES_DB:?Variable POSTGRES_DB no definida}"
: "${POSTGRES_USER:?Variable POSTGRES_USER no definida}"
: "${POSTGRES_PASSWORD:?Variable POSTGRES_PASSWORD no definida}"
: "${MAIN_MODULE:?Variable MAIN_MODULE no definida}"

# Rutas
ADDONS_DIR="/mnt/extra-addons"
ORIGINAL_CONFIG="/etc/odoo/odoo.conf"
MODIFIED_CONFIG="/var/lib/odoo/odoo.conf"

echo "Esperando a que PostgreSQL esté disponible..."
for i in {1..30}; do
if pg_isready -h "$HOST" -p 5432; then
echo "PostgreSQL está listo."
break
fi
echo "Esperando... intento $i/30"
sleep 1
done

if ! pg_isready -h "$HOST" -p 5432; then
echo "❌ No se pudo conectar a PostgreSQL. Abortando..."
exit 1
fi

# Obtener rutas actuales de addons
CUSTOM_ADDONS_PATHS=$(odoo --config="$ORIGINAL_CONFIG" --print-addon-paths 2>/dev/null | tr ':' '\n')

echo "📂 Rutas de módulos en Odoo actualmente:"
echo "$CUSTOM_ADDONS_PATHS"

# Leer todas las carpetas dentro de /mnt/extra-addons
EXTRA_PATHS=()
for dir in "$ADDONS_DIR"/*/; do
if [ -d "$dir" ]; then
echo "Encontrado módulo/carpeta: $dir"
if ! echo "$CUSTOM_ADDONS_PATHS" | grep -q "$dir"; then
echo "➕ Agregando $dir a addons_path."
EXTRA_PATHS+=("$dir")
else
echo "✅ $dir ya está en addons_path."
fi
fi
done

# Generar nuevo archivo de configuración si hay nuevas rutas
if [ ${#EXTRA_PATHS[@]} -gt 0 ]; then
echo "🛠️ Generando archivo de configuración modificado en $MODIFIED_CONFIG..."

# Si el archivo de destino no existe, copiar el original
if [ ! -f "$MODIFIED_CONFIG" ]; then
cp "$ORIGINAL_CONFIG" "$MODIFIED_CONFIG"
echo "📄 Copia base creada desde el archivo original."
else
echo "📄 Usando archivo de configuración existente en el destino."
fi

# Leer línea actual de addons_path
ADDONS_PATH=$(grep -E '^addons_path\s*=' "$MODIFIED_CONFIG" | cut -d'=' -f2 | tr -d ' ')

# Concatenar las nuevas rutas
NEW_ADDONS_PATH="$ADDONS_PATH,$(IFS=,; echo "${EXTRA_PATHS[*]}")"

# Reemplazar en el archivo copiado o existente
sed -i "s|^addons_path\s*=.*|addons_path = $NEW_ADDONS_PATH|" "$MODIFIED_CONFIG"

Comment on lines +67 to +71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Escape paths when updating addons_path
Using unescaped $NEW_ADDONS_PATH in sed may break if any path contains / or special characters. Quote and escape the variable properly to avoid sed syntax errors.

echo "✅ addons_path actualizado con éxito."
else
echo "No se encontraron nuevas rutas para agregar al addons_path."

# Si no hay cambios, usar el config original solo si no existe el modificado
if [ ! -f "$MODIFIED_CONFIG" ]; then
cp "$ORIGINAL_CONFIG" "$MODIFIED_CONFIG"
echo "📄 Copia base creada desde el archivo original (sin cambios en addons_path)."
else
echo "📄 Archivo de configuración ya existe. No se realizaron cambios."
fi
fi


# Verificar si el módulo principal está instalado
echo "🔍 Verificando si el módulo $MAIN_MODULE ya está instalado..."
INSTALLED_MODULES=$(PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$HOST" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -t -c "SELECT name FROM ir_module_module WHERE state = 'installed';" | tr -d ' ' | tr ',' '\n')

if echo "$INSTALLED_MODULES" | grep -Fxq "$MAIN_MODULE"; then
echo "✅ El módulo $MAIN_MODULE ya está instalado. Iniciando Odoo normalmente..."
exec odoo --config="$MODIFIED_CONFIG" -d "$POSTGRES_DB" --db-filter="$POSTGRES_DB" --db_host="$HOST" --db_port=5432 --db_user="$POSTGRES_USER" --db_password="$POSTGRES_PASSWORD"
else
echo "🚀 El módulo $MAIN_MODULE no está instalado. Procediendo con la instalación..."
exec odoo --config="$MODIFIED_CONFIG" -i "$MAIN_MODULE" -d "$POSTGRES_DB" --db-filter="$POSTGRES_DB" --db_host="$HOST" --db_port=5432 --db_user="$POSTGRES_USER" --db_password="$POSTGRES_PASSWORD" --without-demo=True
fi
Loading