From 9128862b3b76edff11033b370ce2ae3552b16120 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Wed, 16 Oct 2013 09:51:08 -0400 Subject: [PATCH] Fixes #4301 Use module_common functions for mysqldump and return better errors --- database/mysql_db | 48 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/database/mysql_db b/database/mysql_db index e33dba4caac..f8ab4bd3b82 100644 --- a/database/mysql_db +++ b/database/mysql_db @@ -116,17 +116,25 @@ def db_delete(cursor, db): cursor.execute(query) return True -def db_dump(host, user, password, db_name, target): - res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" " - +db_name+" > " - +target) - return (res == 0) +def db_dump(module, host, user, password, db_name, target, socket=None): + cmd = module.get_bin_path('mysqldump', True) + cmd += " --quick --host=%s --user=%s --password=%s" %(host, user, password) + if socket is not None: + cmd += " --socket=%s" % socket + cmd += " %s" % db_name + cmd += " > %s" % target + rc, stdout, stderr = module.run_command(cmd) + return rc, stdout, stderr -def db_import(host, user, password, db_name, target): - res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" " - +db_name+" < " - +target) - return (res == 0) +def db_import(module, host, user, password, db_name, target, socket=None): + cmd = module.get_bin_path('mysqldump', True) + cmd += " --host=%s --user=%s --password=%s" %(host, user, password) + if socket is not None: + cmd += " --socket=%s" % socket + cmd += " %s" % db_name + cmd += " < %s" % target + rc, stdout, stderr = module.run_command(cmd) + return rc, stdout, stderr def db_create(cursor, db, encoding, collation): if encoding: @@ -258,13 +266,21 @@ def main(): if state == "absent": changed = db_delete(cursor, db) elif state == "dump": - changed = db_dump(login_host, login_user, login_password, db, target) - if not changed: - module.fail_json(msg="dump failed!") + rc, stdout, stderr = db_dump(module, login_host, login_user, + login_password, db, target, + socket=module.params['login_unix_socket']) + if rc != 0: + module.fail_json(msg="%s" % stderr) + else: + module.exit_json(changed=True, db=db, msg=stdout) elif state == "import": - changed = db_import(login_host, login_user, login_password, db, target) - if not changed: - module.fail_json(msg="import failed!") + rc, stdout, stderr = db_import(module, login_host, login_user, + login_password, db, target, + socket=module.params['login_unix_socket']) + if rc != 0: + module.fail_json(msg="%s" % stderr) + else: + module.exit_json(changed=True, db=db, msg=stdout) else: if state == "present": changed = db_create(cursor, db, encoding, collation)