gbserver/src/scripts/containers/tables.sh

88 lines
3.4 KiB
Bash
Raw Normal View History

HOST_BASE="/opt/gbo/tenants/$PARAM_TENANT/tables"
HOST_DATA="$HOST_BASE/data"
HOST_CONF="$HOST_BASE/conf"
HOST_LOGS="$HOST_BASE/logs"
mkdir -p "$HOST_DATA" "$HOST_CONF" "$HOST_LOGS"
lxc launch images:debian/12 "$PARAM_TENANT"-tables -c security.privileged=true
until lxc exec "$PARAM_TENANT"-tables -- test -f /bin/bash; do
sleep 5
done
sleep 10
lxc exec "$PARAM_TENANT"-tables -- bash -c "
set -e
export DEBIAN_FRONTEND=noninteractive
2025-09-08 14:58:22 -03:00
apt-get update
2025-09-08 14:58:22 -03:00
apt-get install -y wget gnupg2 sudo lsb-release curl
2025-09-08 14:58:22 -03:00
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
apt install -y postgresql
2025-09-08 14:58:22 -03:00
systemctl stop postgresql
mkdir -p /etc/systemd/system/postgresql.service.d/
cat > /etc/systemd/system/postgresql.service.d/override.conf <<EOF
[Service]
Environment=\"PGPORT=$PARAM_TABLES_PORT\"
EOF
CONF_FILE=\$(find /etc/postgresql -name postgresql.conf | head -1)
if [ -f \"\$CONF_FILE\" ]; then
cp \"\$CONF_FILE\" \"\${CONF_FILE}.bak\"
sed -i \"s/^#*port *=.*/port = $PARAM_TABLES_PORT/\" \"\$CONF_FILE\"
sed -i \"s/^#*listen_addresses *=.*/listen_addresses = '*'/\" \"\$CONF_FILE\"
HBA_FILE=\$(find /etc/postgresql -name pg_hba.conf | head -1)
if [ -f \"\$HBA_FILE\" ]; then
echo 'host all all 0.0.0.0/0 md5' >> \"\$HBA_FILE\"
fi
fi
2025-09-08 14:58:22 -03:00
systemctl daemon-reload
systemctl start postgresql
systemctl enable postgresql
until sudo -u postgres psql -p $PARAM_TABLES_PORT -c '\q' 2>/dev/null; do
echo \"Waiting for PostgreSQL to start on port $PARAM_TABLES_PORT...\"
sleep 3
done
sudo -u postgres psql -p $PARAM_TABLES_PORT -c \"CREATE USER $PARAM_TENANT WITH PASSWORD '$PARAM_TABLES_PASSWORD';\"
sudo -u postgres psql -p $PARAM_TABLES_PORT -c \"CREATE DATABASE ${PARAM_TENANT}_db OWNER $PARAM_TENANT;\"
sudo -u postgres psql -p $PARAM_TABLES_PORT -c \"GRANT ALL PRIVILEGES ON DATABASE ${PARAM_TENANT}_db TO $PARAM_TENANT;\"
systemctl restart postgresql
"
2025-09-08 14:58:22 -03:00
lxc exec "$PARAM_TENANT"-tables -- systemctl stop postgresql
2025-09-08 14:58:22 -03:00
PG_DATA_DIR=$(lxc exec "$PARAM_TENANT"-tables -- bash -c "find /var/lib/postgresql -name main -type d | head -1")
PG_CONF_DIR=$(lxc exec "$PARAM_TENANT"-tables -- bash -c "find /etc/postgresql -name main -type d | head -1")
PG_LOGS_DIR=$(lxc exec "$PARAM_TENANT"-tables -- bash -c "find /var/log/postgresql -name postgresql-*.log -o -name postgresql.log | head -1 | xargs dirname 2>/dev/null || echo /var/log/postgresql")
lxc config device add "$PARAM_TENANT"-tables pgdata disk source="$HOST_DATA" path="$PG_DATA_DIR"
lxc config device add "$PARAM_TENANT"-tables pgconf disk source="$HOST_CONF" path="$PG_CONF_DIR"
lxc config device add "$PARAM_TENANT"-tables pglogs disk source="$HOST_LOGS" path="$PG_LOGS_DIR"
lxc exec "$PARAM_TENANT"-tables -- chown -R postgres:postgres "$PG_DATA_DIR"
lxc exec "$PARAM_TENANT"-tables -- chown -R postgres:postgres "$PG_CONF_DIR"
lxc exec "$PARAM_TENANT"-tables -- chown -R postgres:postgres "$PG_LOGS_DIR"
lxc exec "$PARAM_TENANT"-tables -- systemctl start postgresql
lxc config device remove "$PARAM_TENANT"-tables postgres-proxy 2>/dev/null || true
lxc config device add "$PARAM_TENANT"-tables postgres-proxy proxy \
listen=tcp:0.0.0.0:"$PARAM_TABLES_PORT" \
connect=tcp:127.0.0.1:"$PARAM_TABLES_PORT"
2025-09-08 14:58:22 -03:00
echo "PostgreSQL setup completed successfully!"
echo "Database: ${PARAM_TENANT}_db"
echo "User: $PARAM_TENANT"
echo "Password: $PARAM_TABLES_PASSWORD"
echo "Port: $PARAM_TABLES_PORT"