MariaDB: update password cleanly

This PS updates the password update logic to operate cleanly under all
senarios.

Change-Id: I675a344b00984c63cec34919f84a5e9dd8aa5b4e
Signed-off-by: Pete Birley <pete@port.direct>
This commit is contained in:
Pete Birley 2019-04-03 13:05:28 -05:00 committed by Pete Birley
parent 8863bcfc11
commit db209e0bb5

@ -238,7 +238,7 @@ def mysqld_write_cluster_conf(mode='run'):
# Function to setup mysqld # Function to setup mysqld
def mysqld_bootstrap(): def mysqld_bootstrap():
"""Boostrap the db if no data found in the 'bootstrap_test_dir'""" """Bootstrap the db if no data found in the 'bootstrap_test_dir'"""
logger.info("Boostrapping Mariadb") logger.info("Boostrapping Mariadb")
mysql_data_dir = '/var/lib/mysql' mysql_data_dir = '/var/lib/mysql'
bootstrap_test_dir = "{0}/mysql".format(mysql_data_dir) bootstrap_test_dir = "{0}/mysql".format(mysql_data_dir)
@ -726,26 +726,33 @@ def run_mysqld(cluster='existing'):
'--defaults-file=/etc/mysql/admin_user.cnf' '--defaults-file=/etc/mysql/admin_user.cnf'
], logger) ], logger)
logger.info("Setting the root password to the current value") mysql_data_dir = '/var/lib/mysql'
template = ( db_test_dir = "{0}/mysql".format(mysql_data_dir)
"CREATE OR REPLACE USER '{0}'@'%' IDENTIFIED BY \'{1}\' ;\n" if os.path.isdir(db_test_dir):
"GRANT ALL ON *.* TO '{0}'@'%' WITH GRANT OPTION ;\n" logger.info("Setting the admin passwords to the current value")
"CREATE OR REPLACE USER '{2}'@'127.0.0.1' IDENTIFIED BY '{3}' ;\n" template = (
"GRANT PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '{2}'@'127.0.0.1' ;\n" "CREATE OR REPLACE USER '{0}'@'%' IDENTIFIED BY \'{1}\' ;\n"
"FLUSH PRIVILEGES ;\n" "GRANT ALL ON *.* TO '{0}'@'%' WITH GRANT OPTION ;\n"
"SHUTDOWN ;".format(mysql_dbadmin_username, mysql_dbadmin_password, "CREATE OR REPLACE USER '{2}'@'127.0.0.1' IDENTIFIED BY '{3}' ;\n"
mysql_dbsst_username, mysql_dbsst_password)) "GRANT PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '{2}'@'127.0.0.1' ;\n"
bootstrap_sql_file = tempfile.NamedTemporaryFile(suffix='.sql').name "FLUSH PRIVILEGES ;\n"
with open(bootstrap_sql_file, 'w') as f: "SHUTDOWN ;".format(mysql_dbadmin_username, mysql_dbadmin_password,
f.write(template) mysql_dbsst_username, mysql_dbsst_password))
f.close() bootstrap_sql_file = tempfile.NamedTemporaryFile(suffix='.sql').name
run_cmd_with_logging([ with open(bootstrap_sql_file, 'w') as f:
'mysqld', '--bind-address=127.0.0.1', f.write(template)
'--wsrep_cluster_address=gcomm://', f.close()
"--init-file={0}".format(bootstrap_sql_file) run_cmd_with_logging([
], logger) 'mysqld', '--bind-address=127.0.0.1', '--wsrep-on=false',
os.remove(bootstrap_sql_file) "--init-file={0}".format(bootstrap_sql_file)
], logger)
os.remove(bootstrap_sql_file)
else:
logger.info(
"This is a fresh node joining the cluster for the 1st time, not attempting to set admin passwords"
)
logger.info("Launching MariaDB")
run_cmd_with_logging(mysqld_cmd, logger) run_cmd_with_logging(mysqld_cmd, logger)