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 56ffd4edbb
commit 9128862b3b

View file

@ -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)