This commit is contained in:
Jonas Forsberg
2021-09-25 14:09:55 +02:00
parent 283c7afd62
commit 0a1dbb5971
11 changed files with 283 additions and 6 deletions

View File

@@ -56,8 +56,7 @@ def check_credentials(username, password, host="127.0.0.1", port="3306"):
salt '*' mysql.check_credentials root secret
"""
cmd = f"mysql --host={host} --port={port} --user={username} --password={password} --execute=;"
return _execute(cmd)["result"]
return execute(username, password, ";" , host=host, port=port)["result"]
def set_root_password(password):
@@ -70,6 +69,65 @@ def set_root_password(password):
salt '*' mysql.set_root_password secret
"""
ret= {}
cmd = f"mysqladmin password {password}"
return _execute(cmd)
LOG.debug(f"Executing: {cmd}")
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if stdout:
ret["out"] = stdout
if stderr:
ret["err"] = stderr
LOG.debug(f"Exit code: { process.returncode }")
if process.returncode == 0:
ret["result"] = True
else:
ret["result"] = False
return ret
def execute(user, password, sql, host='127.0.0.1', port='3306'):
"""
Execute an SQL statement
:param str user: The user to connect with
:param str password: The user password
:param str host: the mysql server address
:param str sql: The SQL statement to be executed
:param str port: the mysql server port
CLI Example
salt '*' mysql.execute root secret "SHOW DATABASES;"
"""
ret = {}
cmd = [ "mysql",
"--skip-column-names",
"--silent",
f"--user={user}",
f"--password={password}",
f"--host={host}",
f"--port={port}",
f"--execute={sql}"
]
LOG.debug(f"Executing: {' '.join(cmd)}")
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if stdout:
ret["out"] = stdout
if stderr:
ret["err"] = stderr
LOG.debug(f"Exit code: { process.returncode }")
if process.returncode == 0:
ret["result"] = True
else:
ret["result"] = False
return ret