#!/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 <