From f0b2616bd84a3797465460c9003b7c84b4c0c545 Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Sun, 30 Mar 2014 13:39:00 -0400 Subject: [PATCH 1/2] mysql_db module now correctly fails on import if bz2 or gz fails. --- database/mysql_db | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/database/mysql_db b/database/mysql_db index c9fd5b4e087..aa852ea78c1 100644 --- a/database/mysql_db +++ b/database/mysql_db @@ -148,12 +148,42 @@ def db_import(module, host, user, password, db_name, target, port, socket=None): cmd += " --host=%s --port=%s" % (pipes.quote(host), pipes.quote(port)) cmd += " -D %s" % pipes.quote(db_name) if os.path.splitext(target)[-1] == '.gz': - cmd = 'gunzip < ' + pipes.quote(target) + ' | ' + cmd + gunzip_path = module.get_bin_path('gunzip') + if gunzip_path: + rc, stdout, stderr = module.run_command('%s %s' % (gunzip_path, target)) + if rc != 0: + return rc, stdout, stderr + cmd += " < %s" % pipes.quote(target[0:-3]) + rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True) + if rc != 0: + return rc, stdout, stderr + gzip_path = module.get_bin_path('gzip') + if gzip_path: + rc, stdout, stderr = module.run_command('%s %s' % (gzip_path, target[0:-3])) + else: + module.fail_json(msg="gzip command not found") + else: + module.fail_json(msg="gunzip command not found") elif os.path.splitext(target)[-1] == '.bz2': - cmd = 'bunzip2 < ' + pipes.quote(target) + ' | ' + cmd + bunzip2_path = module.get_bin_path('bunzip2') + if bunzip2_path: + rc, stdout, stderr = module.run_command('%s %s' % (bunzip2_path, target)) + if rc != 0: + return rc, stdout, stderr + cmd += " < %s" % pipes.quote(target[0:-4]) + rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True) + if rc != 0: + return rc, stdout, stderr + bzip2_path = module.get_bin_path('bzip2') + if bzip2_path: + rc, stdout, stderr = module.run_command('%s %s' % (bzip2_path, target[0:-4])) + else: + module.fail_json(msg="bzip2 command not found") + else: + module.fail_json(msg="bunzip2 command not found") else: cmd += " < %s" % pipes.quote(target) - rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True) + rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True) return rc, stdout, stderr def db_create(cursor, db, encoding, collation): From 17fa8e3c84d3e2eb1e4839c938f58bd7dd452089 Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Mon, 31 Mar 2014 11:09:00 -0400 Subject: [PATCH 2/2] Added mpdehaan suggestion to use os.path.splitext. Wise suggestion, does look clearner --- database/mysql_db | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/database/mysql_db b/database/mysql_db index aa852ea78c1..97655fbc006 100644 --- a/database/mysql_db +++ b/database/mysql_db @@ -153,13 +153,13 @@ def db_import(module, host, user, password, db_name, target, port, socket=None): rc, stdout, stderr = module.run_command('%s %s' % (gunzip_path, target)) if rc != 0: return rc, stdout, stderr - cmd += " < %s" % pipes.quote(target[0:-3]) + cmd += " < %s" % pipes.quote(os.path.splitext(target)[0]) rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True) if rc != 0: return rc, stdout, stderr gzip_path = module.get_bin_path('gzip') if gzip_path: - rc, stdout, stderr = module.run_command('%s %s' % (gzip_path, target[0:-3])) + rc, stdout, stderr = module.run_command('%s %s' % (gzip_path, os.path.splitext(target)[0])) else: module.fail_json(msg="gzip command not found") else: @@ -170,13 +170,13 @@ def db_import(module, host, user, password, db_name, target, port, socket=None): rc, stdout, stderr = module.run_command('%s %s' % (bunzip2_path, target)) if rc != 0: return rc, stdout, stderr - cmd += " < %s" % pipes.quote(target[0:-4]) + cmd += " < %s" % pipes.quote(os.path.splitext(target)[0]) rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True) if rc != 0: return rc, stdout, stderr bzip2_path = module.get_bin_path('bzip2') if bzip2_path: - rc, stdout, stderr = module.run_command('%s %s' % (bzip2_path, target[0:-4])) + rc, stdout, stderr = module.run_command('%s %s' % (bzip2_path, os.path.splitext(target)[0])) else: module.fail_json(msg="bzip2 command not found") else: