chore: add the database container and DB initialization

This commit is contained in:
Emmanuel BENOîT 2025-01-03 16:48:13 +01:00
parent 523b98f8a3
commit 00c6b2ba1e
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg
5 changed files with 58 additions and 11 deletions

View file

@ -0,0 +1,3 @@
FROM postgres:17-alpine
ADD legacyworlds-server/legacyworlds-server-data/db-structure /opt/lwsql
ADD docker/init-sql.sh /docker-entrypoint-initdb.d

View file

@ -2,6 +2,23 @@ name: lwb6
services: services:
db:
build:
context: ../
dockerfile: docker/Dockerfile.database
restart: always
shm_size: 128mb
volumes:
- database:/var/lib/postgresql/data
environment:
LW_DB_NAME: ${LW_DB_NAME}
LW_DB_USER: ${LW_DB_USER}
LW_DB_USER_PASS_FILE: /run/secrets/lw_db_password
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
- lw_db_password
backend: backend:
build: build:
context: .. context: ..
@ -11,8 +28,8 @@ services:
environment: environment:
LW_CONFIGURE: 1 LW_CONFIGURE: 1
LW_DB_HOST: db LW_DB_HOST: db
LW_DB_NAME: lwb6 LW_DB_NAME: ${LW_DB_NAME}
LW_DB_USER: lwb6 LW_DB_USER: ${LW_DB_USER}
LW_DB_PASSWORD_FILE: /run/secrets/lw_db_password LW_DB_PASSWORD_FILE: /run/secrets/lw_db_password
secrets: secrets:
- lw_db_password - lw_db_password
@ -35,6 +52,11 @@ services:
environment: environment:
LW_BACKEND_HOST: backend LW_BACKEND_HOST: backend
volumes:
database:
secrets: secrets:
db_password:
environment: DB_PASSWORD
lw_db_password: lw_db_password:
environment: LW_DB_PASSWORD environment: LW_DB_PASSWORD

View file

@ -1,5 +1,9 @@
# Password for the database superuser # Password for the database superuser
DB_PASSWORD=hVVCS5cQG17aZBk32m0wDHAluvzgyj4n DB_PASSWORD=...
# Legacyworlds database
LW_DB_NAME=legacyworlds
# Legacyworlds database user
LW_DB_USER=legacyworlds
# Legacyworlds database password # Legacyworlds database password
LW_DB_PASSWORD=aXIQkMFFzjb4Ad7nvJgO5JhNtWxo62Du LW_DB_PASSWORD=...

14
docker/init-sql.sh Normal file
View file

@ -0,0 +1,14 @@
#!/bin/sh
set -e
cd /opt/lwsql
for var_name in `sed '/^.getenv/!d;s/.* //' database.sql`; do
echo "Checking variable $var_name" >&2
file_var_name="${var_name}_FILE"
eval file_var_value="\"\$$file_var_name\""
if [ ! -z "$file_var_value" ]; then
echo " -> Reading $var_name from $file_var_value" >&2
eval "export $var_name=\"`cat $file_var_value`\""
fi
done
psql -f database.sql

View file

@ -9,11 +9,15 @@
-- Copyright(C) 2004-2010, DeepClone Development -- Copyright(C) 2004-2010, DeepClone Development
-- -------------------------------------------------------- -- --------------------------------------------------------
-- Read configuration from file -- Set configuration defaults
\set pgadmin `grep ^admin= db-config.txt | sed -e s/.*=//` \set pgadmin postgres
\set dbname `grep ^db= db-config.txt | sed -e s/.*=//` \set dbname lwb6
\set dbuser `grep ^user= db-config.txt | sed -e s/.*=//` \set dbuser lwb6
\set dbupass ''''`grep ^password= db-config.txt | sed -e s/.*=// -e "s/'/''/g"`'''' -- Read configuration from environment vars
\getenv pgadmin DB_ADMIN
\getenv dbname LW_DB_NAME
\getenv dbuser LW_DB_USER
\getenv dbupass LW_DB_USER_PASS
-- Connect to the main system database -- Connect to the main system database
@ -24,7 +28,7 @@ DROP DATABASE IF EXISTS :dbname;
DROP ROLE IF EXISTS :dbuser; DROP ROLE IF EXISTS :dbuser;
-- Create the LW users -- Create the LW users
CREATE ROLE :dbuser WITH LOGIN ENCRYPTED PASSWORD :dbupass; CREATE ROLE :dbuser WITH LOGIN ENCRYPTED PASSWORD :'dbupass';
-- Create the database -- Create the database
CREATE DATABASE :dbname ENCODING='UTF8' TEMPLATE=template0; CREATE DATABASE :dbname ENCODING='UTF8' TEMPLATE=template0;
@ -51,4 +55,4 @@ BEGIN;
-- Process game update functions -- Process game update functions
\i parts/030-updates.sql \i parts/030-updates.sql
COMMIT; COMMIT;