This commit is contained in:
oliver committed 2026-09-27 10:39:11 -03:00
1 parent d3d9614825
commit 2e9c775599
34 files changed
+390 -1833

No files matched your search

+55 -17
View File
@@ -1,5 +1,48 @@
#!/bin/bash
# ---------------------------------------------------------------------------
# Logging — use these everywhere instead of bare echo
# ---------------------------------------------------------------------------
log() { echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $*"; }
warn() { echo "$(date '+%Y-%m-%d %H:%M:%S') [WARN] $*" >&2; }
die() { echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $*" >&2; exit 1; }
# Resolve the absolute directory of a script.
# Always pass ${BASH_SOURCE[0]} — it holds the real path even when the
# script is invoked via PATH (unlike $0 which may be just a bare name).
# Usage in top-level scripts: SBIN="$(sbin_dir "${BASH_SOURCE[0]}")"
# Sub-scripts one level deep: SBIN_ROOT="$(dirname "$(sbin_dir "${BASH_SOURCE[0]}")")"
sbin_dir() { dirname "$(realpath "$1")"; }
# ---------------------------------------------------------------------------
# load_container_config <container_id>
# Sources /4server/data/<container_id>/config/container.env and exports:
# SOFTWARE — n8n | ODOO_17 | ODOO_18 | ODOO_19
# DB_NAME — postgres database/user (may differ from CONTAINER_ID for migrations)
# ---------------------------------------------------------------------------
load_container_config() {
local container_id="${1:-$CONTAINER_ID}"
local cfg="/4server/data/$container_id/config/container.env"
if [ -f "$cfg" ]; then
while IFS= read -r line; do
case "$line" in
''|'#'*) continue ;;
esac
local _k="${line%%=*}"
local _v="${line#*=}"
case "$_k" in
*[!A-Z_]*) continue ;;
esac
export "$_k=$_v"
done < "$cfg"
else
warn "no container.env found at $cfg"
fi
}
# ---------------------------------------------------------------------------
# Postgres defaults (overridable via environment)
# ---------------------------------------------------------------------------
POSTGRES_HOST="${POSTGRES_HOST:-beedb}"
POSTGRES_PORT="${POSTGRES_PORT:-5432}"
POSTGRES_ADMIN_USER="${POSTGRES_ADMIN_USER:-1gtT0sf8klB9lDbYZD9}"
@@ -7,19 +50,19 @@ POSTGRES_ADMIN_PASSWORD="${POSTGRES_ADMIN_PASSWORD:-ZpSwWNafyy9GhY2gzHw}"
DB_PATH="/4server/data/contracts.db"
# ---------------------------------------------------------------------------
# get_contract_info
# Reads the contracts.db row for $CONTAINER_ID and exports all columns as env vars.
# Also parses SECRET_LIST and exports ODOO_DB_PASSWORD from the psql key.
# ---------------------------------------------------------------------------
get_contract_info() {
log "get_contract_info $CONTAINER_ID"
echo "get_contract_info $UUID"
# Single CTE: the table is scanned once and all projections come from it
while IFS="=" read -r key value; do
if [ -n "$key" ]; then
export "$key=$value"
fi
[ -n "$key" ] && export "$key=$value"
done < <(sqlite3 "$DB_PATH" "
WITH c AS (SELECT * FROM containers WHERE UUID='$UUID' LIMIT 1)
SELECT 'UUID=' || COALESCE(UUID, '') FROM c UNION ALL
WITH c AS (SELECT * FROM containers WHERE UUID='$CONTAINER_ID' LIMIT 1)
SELECT 'CONTAINER_ID=' || COALESCE(UUID, '') FROM c UNION ALL
SELECT 'EXPIRES=' || COALESCE(expires, '') FROM c UNION ALL
SELECT 'IMAGE=' || COALESCE(image, '') FROM c UNION ALL
SELECT 'STATUS=' || COALESCE(status, '') FROM c UNION ALL
@@ -33,18 +76,13 @@ get_contract_info() {
SELECT 'DOMAINS_SLOTS=' || COALESCE(domains_slots, '') FROM c;
")
# Parse SECRET_LIST: backslash-separated key=value pairs (e.g. worex1=1\pswl=22)
# - printf avoids echo interpreting escape sequences
# - tr -d '\r\n' strips any embedded newlines the DB value may contain
# - tr '\\' '\n' turns each backslash separator into a real newline
# - %%=* / #*= split on the FIRST = only, so base64 values like tok=ab== are safe
# Parse SECRET_LIST: backslash-separated key=value pairs
# %%=* / #*= split on the FIRST = only, so base64 values like tok=ab== are safe
if [ -n "$SECRET_LIST" ]; then
while IFS= read -r pair || [ -n "$pair" ]; do
_key="${pair%%=*}"
_value="${pair#*=}"
if [ -n "$_key" ]; then
export "$_key=$_value"
fi
[ -n "$_key" ] && export "$_key=$_value"
done < <(printf '%s' "$SECRET_LIST" | tr -d '\r\n' | tr '\\' '\n')
fi
export ODOO_DB_PASSWORD=$psql