Fixes #4301 Use module_common functions for mysqldump and return better errors

This commit is contained in:
James Tanner 2013-10-16 09:51:08 -04:00
parent efe2a9fb1d
commit 84a692bcf7

View file

@ -116,17 +116,25 @@ def db_delete(cursor, db):
cursor.execute(query) cursor.execute(query)
return True return True
def db_dump(host, user, password, db_name, target): def db_dump(module, host, user, password, db_name, target, socket=None):
res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" " cmd = module.get_bin_path('mysqldump', True)
+db_name+" > " cmd += " --quick --host=%s --user=%s --password=%s" %(host, user, password)
+target) if socket is not None:
return (res == 0) 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): def db_import(module, host, user, password, db_name, target, socket=None):
res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" " cmd = module.get_bin_path('mysqldump', True)
+db_name+" < " cmd += " --host=%s --user=%s --password=%s" %(host, user, password)
+target) if socket is not None:
return (res == 0) 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): def db_create(cursor, db, encoding, collation):
if encoding: if encoding:
@ -258,13 +266,21 @@ def main():
if state == "absent": if state == "absent":
changed = db_delete(cursor, db) changed = db_delete(cursor, db)
elif state == "dump": elif state == "dump":
changed = db_dump(login_host, login_user, login_password, db, target) rc, stdout, stderr = db_dump(module, login_host, login_user,
if not changed: login_password, db, target,
module.fail_json(msg="dump failed!") 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": elif state == "import":
changed = db_import(login_host, login_user, login_password, db, target) rc, stdout, stderr = db_import(module, login_host, login_user,
if not changed: login_password, db, target,
module.fail_json(msg="import failed!") 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: else:
if state == "present": if state == "present":
changed = db_create(cursor, db, encoding, collation) changed = db_create(cursor, db, encoding, collation)