Docker: Improve mariadb init

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-06-29 21:33:47 -07:00
parent 4442ba6703
commit 6c7bb04b93
5 changed files with 39 additions and 30 deletions

View file

@ -1,6 +1,33 @@
#!/bin/bash
set -eou pipefail
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
MYSQL_DATA=/var/lib/mysql
DB_HOST="localhost"
mariadb-install-db --user=mysql --basedir=/usr --datadir=$MYSQL_DATA
# Start it up.
mysqld_safe --datadir=$MYSQL_DATA --skip-networking &
while ! mysqladmin ping 2>/dev/null; do
sleep 1s
done
# Configure databases.
DATABASE="aurweb" # Persistent database for fastapi/php-fpm.
TEST_DB="aurweb_test" # Test database (ephemereal).
echo "Taking care of primary database '${DATABASE}'..."
mysql -u root -e "CREATE USER IF NOT EXISTS 'aur'@'$DB_HOST' IDENTIFIED BY 'aur';"
mysql -u root -e "CREATE DATABASE IF NOT EXISTS $DATABASE;"
mysql -u root -e "GRANT ALL ON ${DATABASE}.* TO 'aur'@'$DB_HOST';"
# Drop and create our test database.
echo "Dropping test database '$TEST_DB'..."
mysql -u root -e "DROP DATABASE IF EXISTS $TEST_DB;"
mysql -u root -e "CREATE DATABASE $TEST_DB;"
mysql -u root -e "GRANT ALL ON ${TEST_DB}.* TO 'aur'@'$DB_HOST';"
echo "Created new '$TEST_DB'!"
mysqladmin -uroot shutdown
exec "$@"

View file

@ -1,26 +0,0 @@
#!/bin/bash
mysqld_safe --datadir=/var/lib/mysql --skip-networking &
until mysqladmin ping --silent; do
sleep 1s
done
# Create test database.
mysql -u root -e "CREATE USER 'aur'@'%' IDENTIFIED BY 'aur'" \
2>/dev/null || /bin/true
# Create a brand new 'aurweb_test' DB.
mysql -u root -e "DROP DATABASE aurweb_test" 2>/dev/null || /bin/true
mysql -u root -e "CREATE DATABASE aurweb_test"
mysql -u root -e "GRANT ALL PRIVILEGES ON aurweb_test.* TO 'aur'@'%'"
# Create the 'aurweb' DB if it does not yet exist.
mysql -u root -e "CREATE DATABASE aurweb" 2>/dev/null || /bin/true
mysql -u root -e "GRANT ALL PRIVILEGES ON aurweb.* TO 'aur'@'%'"
mysql -u root -e "FLUSH PRIVILEGES"
# Shutdown mariadb.
mysqladmin -uroot shutdown
exec "$@"

View file

@ -23,7 +23,8 @@ while [ $# -ne 0 ]; do
done
# Initialize the new database; ignore errors.
python -m aurweb.initdb 2>/dev/null || /bin/true
python -m aurweb.initdb 2>/dev/null || \
(echo "Error: aurweb.initdb failed; already initialized?" && /bin/true)
# Run pytest with optional targets in front of it.
make -C test "${PARAMS[@]}" pytest

View file

@ -1,8 +1,9 @@
#!/bin/bash
set -eou pipefail
[[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1
DB_NAME="aurweb_test"
DB_HOST="mariadb"
DB_USER="aur"
DB_PASS="aur"