From 6c7bb04b93161580946c2ee96d0002f3bd7858d1 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Tue, 29 Jun 2021 21:33:47 -0700 Subject: [PATCH] Docker: Improve mariadb init Signed-off-by: Kevin Morris --- docker-compose.yml | 8 +++++++- docker/mariadb-entrypoint.sh | 29 ++++++++++++++++++++++++++++- docker/scripts/run-mariadb.sh | 26 -------------------------- docker/scripts/run-pytests.sh | 3 ++- docker/test-mysql-entrypoint.sh | 3 ++- 5 files changed, 39 insertions(+), 30 deletions(-) delete mode 100755 docker/scripts/run-mariadb.sh diff --git a/docker-compose.yml b/docker-compose.yml index 6bf36166..40d9bc5b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,8 +32,10 @@ services: mariadb: image: aurweb:latest init: true + environment: + - DB_HOST="%" entrypoint: /docker/mariadb-entrypoint.sh - command: /docker/scripts/run-mariadb.sh mysqld_safe --datadir=/var/lib/mysql + command: /usr/bin/mysqld_safe --datadir=/var/lib/mysql ports: # This will expose mariadbd on 127.0.0.1:13306 in the host. # Ex: `mysql -uaur -paur -h 127.0.0.1 -P 13306 aurweb` @@ -136,6 +138,7 @@ services: init: true environment: - AUR_CONFIG=/aurweb/conf/config + - DB_HOST=mariadb entrypoint: /docker/php-entrypoint.sh command: /docker/scripts/run-php.sh healthcheck: @@ -170,6 +173,7 @@ services: init: true environment: - AUR_CONFIG=conf/config + - DB_HOST=mariadb entrypoint: /docker/fastapi-entrypoint.sh command: /docker/scripts/run-fastapi.sh "${FASTAPI_BACKEND}" healthcheck: @@ -269,6 +273,7 @@ services: init: true environment: - AUR_CONFIG=conf/config + - DB_HOST=mariadb entrypoint: /docker/test-mysql-entrypoint.sh command: /docker/scripts/run-pytests.sh clean stdin_open: true @@ -324,6 +329,7 @@ services: init: true environment: - AUR_CONFIG=conf/config + - DB_HOST=mariadb entrypoint: /docker/tests-entrypoint.sh command: /docker/scripts/run-tests.sh stdin_open: true diff --git a/docker/mariadb-entrypoint.sh b/docker/mariadb-entrypoint.sh index e33c61c7..48e87045 100755 --- a/docker/mariadb-entrypoint.sh +++ b/docker/mariadb-entrypoint.sh @@ -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 "$@" diff --git a/docker/scripts/run-mariadb.sh b/docker/scripts/run-mariadb.sh deleted file mode 100755 index d27d8124..00000000 --- a/docker/scripts/run-mariadb.sh +++ /dev/null @@ -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 "$@" diff --git a/docker/scripts/run-pytests.sh b/docker/scripts/run-pytests.sh index 021603b1..c6baa939 100755 --- a/docker/scripts/run-pytests.sh +++ b/docker/scripts/run-pytests.sh @@ -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 diff --git a/docker/test-mysql-entrypoint.sh b/docker/test-mysql-entrypoint.sh index ea4df868..9594318f 100755 --- a/docker/test-mysql-entrypoint.sh +++ b/docker/test-mysql-entrypoint.sh @@ -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"