Files

55 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
set -Eeuo pipefail
phase='validate seed and credentials'
# Report the stage without printing commands, SQL, or credentials.
trap 'code=$?; printf "[init-db] FAILED: %s (line %s, exit %s)\n" "$phase" "$LINENO" "$code" >&2; exit "$code"' ERR
log() { printf '[init-db] %s %s\n' "$(date -u +%FT%TZ)" "$1"; }
import_sql() {
phase="import $1: ${2##*/}"
log "$phase"
mariadb -uroot "$1" < "$2"
}
log "$phase"
# Executed only on the first start of an empty DB volume.
[[ -s /seed/mangos.sql ]] || { echo "Provide database/mangos.sql before initialization" >&2; exit 1; }
password=$(cat /run/secrets/db_password)
[[ "$password" =~ ^[A-Za-z0-9]{32,}$ ]] || { echo "Invalid DB password" >&2; exit 1; }
export MYSQL_PWD="$MARIADB_ROOT_PASSWORD"
for database in realmd characters mangos logs; do
phase="create database $database"
log "$phase"
mariadb -uroot -e "CREATE DATABASE $database CHARACTER SET utf8 COLLATE utf8_general_ci;"
done
import_sql realmd /source-sql/logon.sql
import_sql characters /source-sql/characters.sql
import_sql logs /source-sql/logs.sql
import_sql mangos /seed/mangos.sql
# Use the historical base dump from June 2021, then migrations from THIS checkout.
for pair in world:mangos logon:realmd characters:characters logs:logs; do
prefix=${pair%%:*}
database=${pair##*:}
while IFS= read -r file; do
import_sql "$database" "$file"
done < <(find /source-sql/migrations -maxdepth 1 -name "*_${prefix}.sql" | sort)
done
# This optional file must be generated by prepare-russian.py.
if [[ -s /seed/ruRU.sql ]]; then
import_sql mangos /seed/ruRU.sql
fi
# Content from benilla-editor goes last: it builds on the core data and its translations.
while IFS= read -r file; do
import_sql mangos "$file"
done < <(find /source-sql/content/migrations -maxdepth 1 -name "*_world.sql" | sort)
phase='create game database user and grants'
log "$phase"
mariadb -uroot <<SQL
CREATE USER 'vmangos'@'%' IDENTIFIED BY '$password';
GRANT ALL PRIVILEGES ON realmd.* TO 'vmangos'@'%';
GRANT ALL PRIVILEGES ON characters.* TO 'vmangos'@'%';
GRANT ALL PRIVILEGES ON mangos.* TO 'vmangos'@'%';
GRANT ALL PRIVILEGES ON logs.* TO 'vmangos'@'%';
SQL
phase='write completion marker'
touch /var/lib/mysql/.vmangos-initialized
log 'initialization completed'