Add script to initialize database user in Docker Swarm

This commit is contained in:
Colin 2025-10-13 00:43:01 -04:00
parent 102c6b255e
commit 18c651c83f
Signed by: colin
SSH Key Fingerprint: SHA256:nRPCQTeMFLdGytxRQmPVK9VXY3/ePKQ5lGRyJhT5DY8
1 changed files with 77 additions and 0 deletions

77
init_database_swarm.sh Executable file
View File

@ -0,0 +1,77 @@
#!/bin/bash
# Script to initialize the database with the proper user permissions in Docker Swarm
echo "===== FINDING DATABASE CONTAINER ====="
# Find the node running the database
DB_NODE=$(docker service ps --format "{{.Node}}" woodpecker_db | head -1)
echo "Database is running on node: $DB_NODE"
# Find the container ID on that node
if [ "$DB_NODE" = "$(hostname)" ]; then
# We're on the same node
DB_CONTAINER=$(docker ps --filter name=woodpecker_db -q | head -1)
else
# We need to SSH to the node
DB_CONTAINER=$(ssh $DB_NODE "docker ps --filter name=woodpecker_db -q | head -1")
fi
if [ -z "$DB_CONTAINER" ]; then
echo "ERROR: No database container found!"
exit 1
fi
echo "Found database container: $DB_CONTAINER on node $DB_NODE"
echo "===== CREATING DATABASE USER ====="
if [ "$DB_NODE" = "$(hostname)" ]; then
# We're on the same node
# First, find the root password
ROOT_PASSWORD=$(docker exec $DB_CONTAINER cat /var/lib/mysql/mysql_root_password 2>/dev/null || echo "")
if [ -z "$ROOT_PASSWORD" ]; then
echo "No root password file found, using MYSQL_ROOT_PASSWORD environment variable"
ROOT_PASSWORD=$(docker inspect $DB_CONTAINER --format '{{range .Config.Env}}{{if eq (index (split . "=") 0) "MYSQL_ROOT_PASSWORD"}}{{index (split . "=") 1}}{{end}}{{end}}')
fi
if [ -z "$ROOT_PASSWORD" ]; then
echo "WARNING: Could not determine root password, trying without password"
docker exec -i $DB_CONTAINER mysql -u root <<EOF
CREATE DATABASE IF NOT EXISTS woodpecker;
CREATE USER IF NOT EXISTS 'woodpecker'@'%' IDENTIFIED BY 'woodpecker';
GRANT ALL PRIVILEGES ON woodpecker.* TO 'woodpecker'@'%';
FLUSH PRIVILEGES;
EOF
else
echo "Using root password from container"
docker exec -i $DB_CONTAINER mysql -u root -p"$ROOT_PASSWORD" <<EOF
CREATE DATABASE IF NOT EXISTS woodpecker;
CREATE USER IF NOT EXISTS 'woodpecker'@'%' IDENTIFIED BY 'woodpecker';
GRANT ALL PRIVILEGES ON woodpecker.* TO 'woodpecker'@'%';
FLUSH PRIVILEGES;
EOF
fi
else
# We need to SSH to the node
echo "Executing commands on remote node $DB_NODE"
ssh $DB_NODE "docker exec -i $DB_CONTAINER mysql -u root <<EOF
CREATE DATABASE IF NOT EXISTS woodpecker;
CREATE USER IF NOT EXISTS 'woodpecker'@'%' IDENTIFIED BY 'woodpecker';
GRANT ALL PRIVILEGES ON woodpecker.* TO 'woodpecker'@'%';
FLUSH PRIVILEGES;
EOF"
fi
echo "===== VERIFYING DATABASE USER ====="
if [ "$DB_NODE" = "$(hostname)" ]; then
# We're on the same node
docker exec -i $DB_CONTAINER mysql -u woodpecker -pwoodpecker -e "SHOW DATABASES;"
else
# We need to SSH to the node
ssh $DB_NODE "docker exec -i $DB_CONTAINER mysql -u woodpecker -pwoodpecker -e 'SHOW DATABASES;'"
fi
echo "===== RESTARTING SERVER SERVICE ====="
docker service update --force woodpecker_server
echo "===== DONE ====="
echo "Database user created and server restarted."