108 lines
4.4 KiB
Bash
Executable File
108 lines
4.4 KiB
Bash
Executable File
#!/bin/bash
|
|
# Shared library for all Odoo containers (17 / 18 / 19).
|
|
# Sourced by start/odoo, nuke/odoo, and admin tools.
|
|
# Requires: helpers already sourced (provides log/warn/die).
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# dump_config — print all resolved variables for debugging
|
|
# ---------------------------------------------------------------------------
|
|
dump_config() {
|
|
log "========== Odoo Container Configuration =========="
|
|
log "CONTAINER_ID: $CONTAINER_ID"
|
|
log "DB_NAME: $DB_NAME"
|
|
log "IMAGE_TAG: $IMAGE_TAG"
|
|
log "BRANCH: $BRANCH"
|
|
log ""
|
|
log "PostgreSQL Host: $POSTGRES_HOST"
|
|
log "PostgreSQL Port: $POSTGRES_PORT"
|
|
log "ODOO DB User: $ODOO_DB_USER"
|
|
log ""
|
|
log "BASEURL: $BASEURL"
|
|
log "SERVER_IP: $SERVER_IP"
|
|
log "PORT: $PORT"
|
|
log "=================================================="
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# build_traefik_rule
|
|
# Reads CONTAINER_ID and BASEURL from env. Resolves each candidate domain via
|
|
# nslookup and builds a Traefik Host() rule containing only domains that point
|
|
# to this server. Exports DOMAIN_LABEL.
|
|
# ---------------------------------------------------------------------------
|
|
build_traefik_rule() {
|
|
local server_ip
|
|
server_ip=$(ip -4 addr show eth0 | awk '/inet/ {print $2}' | cut -d/ -f1 | head -n1)
|
|
|
|
local domains=("$CONTAINER_ID.odoo4projects.com")
|
|
if [ -f "$BASEURL/etc/domain" ]; then
|
|
while IFS= read -r domain || [ -n "$domain" ]; do
|
|
[ -z "$domain" ] && continue
|
|
domains+=("$domain")
|
|
done < "$BASEURL/etc/domain"
|
|
fi
|
|
|
|
local rule=""
|
|
for d in "${domains[@]}"; do
|
|
local resolved_ip
|
|
resolved_ip=$(nslookup "$d" 2>/dev/null | awk '/^Address: / { print $2 }' | head -n1 || true)
|
|
if [ -z "$resolved_ip" ]; then
|
|
warn "build_traefik_rule: could not resolve $d, skipping"
|
|
continue
|
|
fi
|
|
if [ "$resolved_ip" = "$server_ip" ]; then
|
|
rule+=" || Host(\`$d\`)"
|
|
else
|
|
warn "build_traefik_rule: $d resolves to $resolved_ip, not $server_ip — skipping"
|
|
fi
|
|
done
|
|
|
|
rule="${rule# || }"
|
|
if [ -z "$rule" ]; then
|
|
die "build_traefik_rule: no valid domains resolved for $CONTAINER_ID — cannot start container"
|
|
fi
|
|
|
|
DOMAIN_LABEL="traefik.http.routers.$CONTAINER_ID.rule=$rule"
|
|
log "Traefik rule: $DOMAIN_LABEL"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# check_and_create_db
|
|
# Creates the postgres user and database if they don't exist yet.
|
|
# On a brand-new container this triggers a restore from default.zip.
|
|
# Requires: DB_NAME, ODOO_DB_USER, ODOO_DB_PASSWORD, POSTGRES_* from env.
|
|
# ---------------------------------------------------------------------------
|
|
check_and_create_db() {
|
|
local _dbname="${DB_NAME:-$CONTAINER_ID}"
|
|
local _dbuser="${ODOO_DB_USER:-$_dbname}"
|
|
|
|
log "check_and_create_db: user=$_dbuser db=$_dbname host=$POSTGRES_HOST"
|
|
|
|
# --- user ---
|
|
local _user_result
|
|
_user_result=$(PGPASSWORD="$POSTGRES_ADMIN_PASSWORD" psql \
|
|
-h "$POSTGRES_HOST" -U "$POSTGRES_ADMIN_USER" -p "$POSTGRES_PORT" \
|
|
-d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$_dbuser';" 2>&1) \
|
|
|| die "check_and_create_db: psql connection failed: $_user_result"
|
|
|
|
if ! echo "$_user_result" | grep -q 1; then
|
|
log "Creating PostgreSQL user $_dbuser"
|
|
PGPASSWORD="$POSTGRES_ADMIN_PASSWORD" psql \
|
|
-h "$POSTGRES_HOST" -U "$POSTGRES_ADMIN_USER" -p "$POSTGRES_PORT" \
|
|
-d postgres -c "CREATE USER \"$_dbuser\" WITH PASSWORD '$ODOO_DB_PASSWORD';" \
|
|
|| die "check_and_create_db: failed to create user $_dbuser"
|
|
fi
|
|
|
|
# --- database ---
|
|
local _db_result
|
|
_db_result=$(PGPASSWORD="$POSTGRES_ADMIN_PASSWORD" psql \
|
|
-h "$POSTGRES_HOST" -U "$POSTGRES_ADMIN_USER" -p "$POSTGRES_PORT" \
|
|
-d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$_dbname';" 2>&1) \
|
|
|| die "check_and_create_db: psql connection failed: $_db_result"
|
|
|
|
if ! echo "$_db_result" | grep -q 1; then
|
|
log "Database $_dbname not found — restoring from default.zip"
|
|
"$SBIN_ROOT/odoo/restore" "$CONTAINER_ID" default.zip \
|
|
|| die "check_and_create_db: restore failed for $_dbname"
|
|
fi
|
|
}
|