refactor(docker): New mariadb_init service

Provides a single source of truth for mariadb database
initialization. Previously, php-fpm and fastapi were
racing against each other; while this wasn't an issue,
it was very messy.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-08 17:14:55 -07:00
parent 2e3f69ab12
commit 0fd31b8d36
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
5 changed files with 64 additions and 40 deletions

View file

@ -49,8 +49,6 @@ services:
mariadb: mariadb:
image: aurweb:latest image: aurweb:latest
init: true init: true
environment:
- DB_HOST="%"
entrypoint: /docker/mariadb-entrypoint.sh entrypoint: /docker/mariadb-entrypoint.sh
command: /usr/bin/mysqld_safe --datadir=/var/lib/mysql command: /usr/bin/mysqld_safe --datadir=/var/lib/mysql
ports: ports:
@ -63,11 +61,23 @@ services:
healthcheck: healthcheck:
test: "bash /docker/health/mariadb.sh" test: "bash /docker/health/mariadb.sh"
mariadb_init:
image: aurweb:latest
init: true
environment:
- DB_HOST=mariadb
entrypoint: /docker/mariadb-init-entrypoint.sh
command: echo "MariaDB tables initialized."
depends_on:
mariadb:
condition: service_healthy
git: git:
image: aurweb:latest image: aurweb:latest
init: true init: true
environment: environment:
- AUR_CONFIG=/aurweb/conf/config - AUR_CONFIG=/aurweb/conf/config
- DB_HOST=mariadb
entrypoint: /docker/git-entrypoint.sh entrypoint: /docker/git-entrypoint.sh
command: /docker/scripts/run-sshd.sh command: /docker/scripts/run-sshd.sh
ports: ports:
@ -75,11 +85,9 @@ services:
healthcheck: healthcheck:
test: "bash /docker/health/sshd.sh" test: "bash /docker/health/sshd.sh"
depends_on: depends_on:
mariadb: mariadb_init:
condition: service_healthy condition: service_started
volumes: volumes:
- mariadb_run:/var/run/mysqld
- mariadb_data:/var/lib/mysql
- git_data:/aurweb/aur.git - git_data:/aurweb/aur.git
- ./cache:/cache - ./cache:/cache
@ -96,8 +104,6 @@ services:
mariadb: mariadb:
condition: service_healthy condition: service_healthy
volumes: volumes:
- mariadb_run:/var/run/mysqld
- mariadb_data:/var/lib/mysql
- git_data:/aurweb/aur.git - git_data:/aurweb/aur.git
- ./cache:/cache - ./cache:/cache
- smartgit_run:/var/run/smartgit - smartgit_run:/var/run/smartgit
@ -114,8 +120,6 @@ services:
depends_on: depends_on:
git: git:
condition: service_healthy condition: service_healthy
php-fpm:
condition: service_healthy
volumes: volumes:
- git_data:/aurweb/aur.git - git_data:/aurweb/aur.git
@ -131,8 +135,6 @@ services:
depends_on: depends_on:
git: git:
condition: service_healthy condition: service_healthy
fastapi:
condition: service_healthy
volumes: volumes:
- git_data:/aurweb/aur.git - git_data:/aurweb/aur.git
@ -151,13 +153,9 @@ services:
condition: service_started condition: service_started
git: git:
condition: service_healthy condition: service_healthy
mariadb:
condition: service_healthy
memcached: memcached:
condition: service_healthy condition: service_healthy
volumes: volumes:
- mariadb_run:/var/run/mysqld # Bind socket in this volume.
- mariadb_data:/var/lib/mysql
- ./cache:/cache - ./cache:/cache
- ./aurweb:/aurweb/aurweb - ./aurweb:/aurweb/aurweb
- ./migrations:/aurweb/migrations - ./migrations:/aurweb/migrations
@ -186,11 +184,7 @@ services:
condition: service_healthy condition: service_healthy
redis: redis:
condition: service_healthy condition: service_healthy
mariadb:
condition: service_healthy
volumes: volumes:
- mariadb_run:/var/run/mysqld # Bind socket in this volume.
- mariadb_data:/var/lib/mysql
- ./cache:/cache - ./cache:/cache
- ./aurweb:/aurweb/aurweb - ./aurweb:/aurweb/aurweb
- ./migrations:/aurweb/migrations - ./migrations:/aurweb/migrations
@ -268,10 +262,9 @@ services:
stdin_open: true stdin_open: true
tty: true tty: true
depends_on: depends_on:
mariadb: mariadb_init:
condition: service_healthy condition: service_started
volumes: volumes:
- mariadb_run:/var/run/mysqld
- git_data:/aurweb/aur.git - git_data:/aurweb/aur.git
- ./cache:/cache - ./cache:/cache
- ./aurweb:/aurweb/aurweb - ./aurweb:/aurweb/aurweb
@ -292,7 +285,6 @@ services:
stdin_open: true stdin_open: true
tty: true tty: true
volumes: volumes:
- mariadb_run:/var/run/mysqld
- git_data:/aurweb/aur.git - git_data:/aurweb/aur.git
- ./cache:/cache - ./cache:/cache
- ./aurweb:/aurweb/aurweb - ./aurweb:/aurweb/aurweb
@ -314,10 +306,9 @@ services:
stdin_open: true stdin_open: true
tty: true tty: true
depends_on: depends_on:
mariadb: mariadb_init:
condition: service_healthy condition: service_started
volumes: volumes:
- mariadb_run:/var/run/mysqld
- git_data:/aurweb/aur.git - git_data:/aurweb/aur.git
- ./cache:/cache - ./cache:/cache
- ./aurweb:/aurweb/aurweb - ./aurweb:/aurweb/aurweb

View file

@ -1,11 +1,21 @@
#!/bin/bash #!/bin/bash
set -eou pipefail set -eou pipefail
dir="$(dirname $0)"
bash $dir/test-mysql-entrypoint.sh [[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1
DB_NAME="aurweb"
DB_USER="aur"
DB_PASS="aur"
# Setup a config for our mysql db.
cp -vf conf/config.dev conf/config
sed -i "s;YOUR_AUR_ROOT;$(pwd);g" conf/config
sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" conf/config
sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" conf/config
sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" conf/config
sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" conf/config
sed -ri "s;^(aur_location) = .+;\1 = https://localhost:8444;" conf/config sed -ri "s;^(aur_location) = .+;\1 = https://localhost:8444;" conf/config
sed -ri 's/^(name) = .+/\1 = aurweb/' conf/config
# Setup Redis for FastAPI. # Setup Redis for FastAPI.
sed -ri 's/^(cache) = .+/\1 = redis/' conf/config sed -ri 's/^(cache) = .+/\1 = redis/' conf/config
@ -14,7 +24,4 @@ sed -ri 's|^(redis_address) = .+|\1 = redis://redis|' conf/config
sed -ri "s|^(git_clone_uri_anon) = .+|\1 = https://localhost:8444/%s.git|" conf/config.defaults sed -ri "s|^(git_clone_uri_anon) = .+|\1 = https://localhost:8444/%s.git|" conf/config.defaults
sed -ri "s|^(git_clone_uri_priv) = .+|\1 = ssh://aur@localhost:2222/%s.git|" conf/config.defaults sed -ri "s|^(git_clone_uri_priv) = .+|\1 = ssh://aur@localhost:2222/%s.git|" conf/config.defaults
# Initialize the new database; ignore errors.
python -m aurweb.initdb 2>/dev/null || /bin/true
exec "$@" exec "$@"

View file

@ -3,8 +3,6 @@ set -eou pipefail
MYSQL_DATA=/var/lib/mysql MYSQL_DATA=/var/lib/mysql
[[ -z "$DB_HOST" ]] && DB_HOST="localhost"
mariadb-install-db --user=mysql --basedir=/usr --datadir=$MYSQL_DATA mariadb-install-db --user=mysql --basedir=/usr --datadir=$MYSQL_DATA
# Start it up. # Start it up.

View file

@ -0,0 +1,20 @@
#!/bin/bash
set -eou pipefail
[[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1
DB_NAME="aurweb"
DB_USER="aur"
DB_PASS="aur"
# Setup a config for our mysql db.
cp -vf conf/config.dev conf/config
sed -i "s;YOUR_AUR_ROOT;$(pwd);g" conf/config
sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" conf/config
sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" conf/config
sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" conf/config
sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" conf/config
python -m aurweb.initdb 2>/dev/null || /bin/true
exec "$@"

View file

@ -1,11 +1,21 @@
#!/bin/bash #!/bin/bash
set -eou pipefail set -eou pipefail
dir="$(dirname $0)"
bash $dir/test-mysql-entrypoint.sh [[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1
DB_NAME="aurweb"
DB_USER="aur"
DB_PASS="aur"
# Setup a config for our mysql db.
cp -vf conf/config.dev conf/config
sed -i "s;YOUR_AUR_ROOT;$(pwd);g" conf/config
sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" conf/config
sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" conf/config
sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" conf/config
sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" conf/config
sed -ri "s;^(aur_location) = .+;\1 = https://localhost:8443;" conf/config sed -ri "s;^(aur_location) = .+;\1 = https://localhost:8443;" conf/config
sed -ri 's/^(name) = .+/\1 = aurweb/' conf/config
# Enable memcached. # Enable memcached.
sed -ri 's/^(cache) = .+$/\1 = memcache/' conf/config sed -ri 's/^(cache) = .+$/\1 = memcache/' conf/config
@ -27,6 +37,4 @@ sed -ri 's/^;?(open_basedir).*$/\1 = \//' /etc/php/php.ini
# Use the sqlite3 extension line for memcached. # Use the sqlite3 extension line for memcached.
sed -ri 's/^;(extension)=sqlite3$/\1=memcached/' /etc/php/php.ini sed -ri 's/^;(extension)=sqlite3$/\1=memcached/' /etc/php/php.ini
python -m aurweb.initdb 2>/dev/null || /bin/true
exec "$@" exec "$@"