From d6b109da01ea11116719007242ed238776e8dc18 Mon Sep 17 00:00:00 2001 From: cronekorkn Date: Sun, 4 Feb 2024 10:50:49 +0100 Subject: [PATCH] wip --- bundles/mariadb/items.py | 22 ++++++++++++++++++++-- bundles/wordpress/metadata.py | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/bundles/mariadb/items.py b/bundles/mariadb/items.py index 7eeb8f5..e7868af 100644 --- a/bundles/mariadb/items.py +++ b/bundles/mariadb/items.py @@ -1,5 +1,8 @@ from shlex import quote +def mariadb(sql): + return f"mariadb -Bsr --execute {quote(sql)}" + directories = { '/var/lib/mysql': { 'owner': 'mysql', @@ -32,9 +35,24 @@ svc_systemd = { for db, conf in node.metadata.get('mariadb/databases', {}).items(): actions[f'mariadb_create_database_{db}'] = { - 'command': 'mariadb -Bsr --execute ' + quote(f"CREATE DATABASE {db}"), - 'unless': '! mariadb -Bsr --execute ' + quote(f"SHOW DATABASES LIKE '{db}'") + ' | grep -q ^db$', + 'command': mariadb(f"CREATE DATABASE {db}"), + 'unless': mariadb(f"SHOW DATABASES LIKE '{db}'") + f' | grep -q ^{db}$', 'needs': [ 'svc_systemd:mariadb.service', ], } + actions[f'mariadb_user_{db}_create'] = { + 'command': mariadb(f"CREATE USER {db}"), + 'unless': mariadb(f"SELECT User FROM mysql.user WHERE User = '{db}'") + f' | grep -q ^{db}$', + 'needs': [ + f'action:mariadb_create_database_{db}', + ], + } + pw = conf['password'] + actions[f'mariadb_user_{db}_password'] = { + 'command': mariadb(f"SET PASSWORD FOR {db} = PASSWORD('{conf['password']}')"), + 'unless': f'echo {quote(pw)} | mariadb -u {db} -e quit -p', + 'needs': [ + f'action:mariadb_user_{db}_create', + ], + } diff --git a/bundles/wordpress/metadata.py b/bundles/wordpress/metadata.py index 3443120..6e1219d 100644 --- a/bundles/wordpress/metadata.py +++ b/bundles/wordpress/metadata.py @@ -8,7 +8,7 @@ def wordpress(metadata): return { 'wordpress': { site: { - 'db_password': repo.vault.password_for(f"wordpress {site} db"), + 'db_password': repo.vault.password_for(f"wordpress {site} db").value, } for site in metadata.get('wordpress', {}) },