From 00c6b2ba1eafee36f1f4be25c03d7bdf2ab6435e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20BENO=C3=8ET?= Date: Fri, 3 Jan 2025 16:48:13 +0100 Subject: [PATCH] chore: add the database container and DB initialization --- docker/Dockerfile.database | 3 +++ docker/compose.yml | 26 +++++++++++++++++-- docker/env.example | 8 ++++-- docker/init-sql.sh | 14 ++++++++++ .../db-structure/database.sql | 18 ++++++++----- 5 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 docker/Dockerfile.database create mode 100644 docker/init-sql.sh diff --git a/docker/Dockerfile.database b/docker/Dockerfile.database new file mode 100644 index 0000000..914b2aa --- /dev/null +++ b/docker/Dockerfile.database @@ -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 diff --git a/docker/compose.yml b/docker/compose.yml index ef2df14..7e01a24 100644 --- a/docker/compose.yml +++ b/docker/compose.yml @@ -2,6 +2,23 @@ name: lwb6 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: build: context: .. @@ -11,8 +28,8 @@ services: environment: LW_CONFIGURE: 1 LW_DB_HOST: db - LW_DB_NAME: lwb6 - LW_DB_USER: lwb6 + LW_DB_NAME: ${LW_DB_NAME} + LW_DB_USER: ${LW_DB_USER} LW_DB_PASSWORD_FILE: /run/secrets/lw_db_password secrets: - lw_db_password @@ -35,6 +52,11 @@ services: environment: LW_BACKEND_HOST: backend +volumes: + database: + secrets: + db_password: + environment: DB_PASSWORD lw_db_password: environment: LW_DB_PASSWORD diff --git a/docker/env.example b/docker/env.example index 76c4f76..a4ae3d3 100644 --- a/docker/env.example +++ b/docker/env.example @@ -1,5 +1,9 @@ # 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 -LW_DB_PASSWORD=aXIQkMFFzjb4Ad7nvJgO5JhNtWxo62Du +LW_DB_PASSWORD=... diff --git a/docker/init-sql.sh b/docker/init-sql.sh new file mode 100644 index 0000000..8139d89 --- /dev/null +++ b/docker/init-sql.sh @@ -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 diff --git a/legacyworlds-server/legacyworlds-server-data/db-structure/database.sql b/legacyworlds-server/legacyworlds-server-data/db-structure/database.sql index 675eeb6..f89c478 100644 --- a/legacyworlds-server/legacyworlds-server-data/db-structure/database.sql +++ b/legacyworlds-server/legacyworlds-server-data/db-structure/database.sql @@ -9,11 +9,15 @@ -- Copyright(C) 2004-2010, DeepClone Development -- -------------------------------------------------------- --- Read configuration from file -\set pgadmin `grep ^admin= db-config.txt | sed -e s/.*=//` -\set dbname `grep ^db= db-config.txt | sed -e s/.*=//` -\set dbuser `grep ^user= db-config.txt | sed -e s/.*=//` -\set dbupass ''''`grep ^password= db-config.txt | sed -e s/.*=// -e "s/'/''/g"`'''' +-- Set configuration defaults +\set pgadmin postgres +\set dbname lwb6 +\set dbuser lwb6 +-- 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 @@ -24,7 +28,7 @@ DROP DATABASE IF EXISTS :dbname; DROP ROLE IF EXISTS :dbuser; -- Create the LW users -CREATE ROLE :dbuser WITH LOGIN ENCRYPTED PASSWORD :dbupass; +CREATE ROLE :dbuser WITH LOGIN ENCRYPTED PASSWORD :'dbupass'; -- Create the database CREATE DATABASE :dbname ENCODING='UTF8' TEMPLATE=template0; @@ -51,4 +55,4 @@ BEGIN; -- Process game update functions \i parts/030-updates.sql -COMMIT; \ No newline at end of file +COMMIT;