145 lines
5.2 KiB
Bash
145 lines
5.2 KiB
Bash
#!/bin/bash
|
|
|
|
|
|
dump_config (){
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') ========== Odoo Container Configuration =========="
|
|
echo "UUID: $UUID"
|
|
echo "BRANCH: $BRANCH"
|
|
echo "STAGING: $STAGING"
|
|
echo
|
|
echo "PostgreSQL Host: $POSTGRES_HOST"
|
|
echo "PostgreSQL Port: $POSTGRES_PORT"
|
|
echo "PostgreSQL Admin: $POSTGRES_ADMIN_USER"
|
|
echo "ODOO DB User: $ODOO_DB_USER"
|
|
echo
|
|
echo "BASEURL: $BASEURL"
|
|
echo "DATA_DIR: $DATA_DIR"
|
|
echo "CUSTOM_DIR: $CUSTOM_DIR"
|
|
echo "ENTERPRISE_DIR: $ENTERPRISE_DIR"
|
|
echo "LOGS_DIR: $LOGS_DIR"
|
|
echo "CONFIG_DIR: $CONFIG_DIR"
|
|
echo "CC_DIR: $CC_DIR"
|
|
echo "BACKUP_DIR: $BACKUP_DIR"
|
|
echo "GIT_DIR: $GIT_DIR"
|
|
echo "ETC_DIR: $ETC_DIR"
|
|
echo "INSTALL_DIR: $INSTALL_DIR"
|
|
echo "SSH_DIR: $SSH_DIR"
|
|
echo "HUGO_DIR: $HUGO_DIR"
|
|
echo
|
|
echo "SERVER_IP: $SERVER_IP"
|
|
echo "=================================================="
|
|
|
|
}
|
|
|
|
|
|
|
|
# -----------------------------
|
|
# Function: Create PostgreSQL user
|
|
# -----------------------------
|
|
|
|
# Run a query as postgres admin. Returns psql's exit status so callers can tell
|
|
# "query returned nothing" apart from "could not connect".
|
|
pg_admin() {
|
|
PGPASSWORD="$POSTGRES_ADMIN_PASSWORD" psql -h "$POSTGRES_HOST" -U "$POSTGRES_ADMIN_USER" -p "$POSTGRES_PORT" -d postgres -tAc "$1"
|
|
}
|
|
|
|
# Wait until Postgres accepts connections (default: up to 60s).
|
|
wait_for_postgres() {
|
|
local tries="${1:-30}"
|
|
local i
|
|
for ((i = 1; i <= tries; i++)); do
|
|
if pg_admin "SELECT 1;" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') Postgres at $POSTGRES_HOST:$POSTGRES_PORT not reachable yet ($i/$tries), retrying in 2s"
|
|
sleep 2
|
|
done
|
|
return 1
|
|
}
|
|
|
|
check_and_create_db() {
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') Checking user and database for $UUID (DB user $ODOO_DB_USER) as $POSTGRES_ADMIN_USER on $POSTGRES_HOST:$POSTGRES_PORT"
|
|
|
|
# -----------------------------
|
|
# Never guess: if Postgres cannot be queried we must NOT assume the DB is
|
|
# missing, because a restore wipes the filestore.
|
|
# -----------------------------
|
|
if ! wait_for_postgres; then
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: cannot connect to Postgres at $POSTGRES_HOST:$POSTGRES_PORT - skipping DB check, NO restore performed"
|
|
return 1
|
|
fi
|
|
|
|
# -----------------------------
|
|
# Check if user exists
|
|
# -----------------------------
|
|
USER_EXISTS=$(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='$ODOO_DB_USER';" | grep -q 1 && echo "yes" || echo "no")
|
|
if [ "$USER_EXISTS" = "no" ]; then
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') PostgreSQL user $ODOO_DB_USER does not exist, creating it"
|
|
PGPASSWORD="$POSTGRES_ADMIN_PASSWORD" psql -h "$POSTGRES_HOST" -U "$POSTGRES_ADMIN_USER" -p "$POSTGRES_PORT" -d postgres -c "CREATE USER \"$ODOO_DB_USER\" WITH PASSWORD '$ODOO_DB_PASSWORD';"
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') CREATE USER $ODOO_DB_USER finished with exit code $?"
|
|
else
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') PostgreSQL user $ODOO_DB_USER already exists"
|
|
fi
|
|
|
|
# -----------------------------
|
|
# Check if database exists
|
|
# -----------------------------
|
|
DB_RESULT=$(pg_admin "SELECT 1 FROM pg_database WHERE datname='$UUID';")
|
|
DB_RC=$?
|
|
if [ $DB_RC -ne 0 ]; then
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: existence query for database $UUID failed (rc=$DB_RC) - NO restore performed"
|
|
return 1
|
|
fi
|
|
|
|
if [ "$DB_RESULT" = "1" ]; then
|
|
DB_EXISTS="yes"
|
|
else
|
|
DB_EXISTS="no"
|
|
fi
|
|
|
|
if [ "$DB_EXISTS" = "no" ]; then
|
|
FILESTORE_HOST="/4server/data/$UUID/odoo/odoo-web-data/filestore"
|
|
if [ -d "$FILESTORE_HOST" ] && [ -n "$(ls -A "$FILESTORE_HOST" 2>/dev/null)" ]; then
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: database $UUID not found but a filestore exists at $FILESTORE_HOST - refusing automatic restore of default.zip (would delete the filestore). Restore manually if intended."
|
|
return 1
|
|
fi
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') Database $UUID does not exist and no filestore found, restoring default.zip"
|
|
/4server/sbin/ODOO_19/restore $UUID default.zip
|
|
local restore_rc=$?
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') Restore of default.zip for $UUID finished with exit code $restore_rc"
|
|
return $restore_rc
|
|
else
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') Database $UUID already exists, nothing to do"
|
|
fi
|
|
}
|
|
|
|
|
|
|
|
# -----------------------------
|
|
# Function: Check DNS and build Traefik labels
|
|
# -----------------------------
|
|
check_domains() {
|
|
local domains="$1"
|
|
local server_ip="$2"
|
|
|
|
echo "Checking DNS resolution for domains: $domains"
|
|
local filtered_domains=""
|
|
for domain in $domains; do
|
|
ns_ip=$(nslookup "$domain" 2>/dev/null | grep -Eo 'Address: ([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $2}' | tail -n1)
|
|
if [[ "$ns_ip" == "$server_ip" ]]; then
|
|
filtered_domains+=" $domain"
|
|
fi
|
|
done
|
|
filtered_domains=$(echo "$filtered_domains" | xargs)
|
|
|
|
DOMAIN_LABEL=""
|
|
for domain in $filtered_domains; do
|
|
if [ -z "$DOMAIN_LABEL" ]; then
|
|
DOMAIN_LABEL="traefik.http.routers.$UUID.rule=Host(\`$domain\`)"
|
|
else
|
|
DOMAIN_LABEL+=" || Host(\`$domain\`)"
|
|
fi
|
|
done
|
|
echo "$DOMAIN_LABEL"
|
|
}
|