summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <toshio@fedoraproject.org>2015-05-27 20:30:40 -0700
committerToshio Kuratomi <toshio@fedoraproject.org>2015-05-27 20:30:40 -0700
commitc4b80afaa75ad0e25a2294137b7e3cd58b6bec3c (patch)
tree710cc4a1ed9ed6fbfa0c7b0c074e070c385f4421
parent2b5e932cfb4df42f46812aee2476fdf5aabab172 (diff)
downloadansible-modules-core-c4b80afaa75ad0e25a2294137b7e3cd58b6bec3c.tar.gz
Back to the known good
-rw-r--r--database/mysql/mysql_db.py100
-rw-r--r--database/mysql/mysql_user.py19
2 files changed, 70 insertions, 49 deletions
diff --git a/database/mysql/mysql_db.py b/database/mysql/mysql_db.py
index a76c4526..16ddf93e 100644
--- a/database/mysql/mysql_db.py
+++ b/database/mysql/mysql_db.py
@@ -111,7 +111,6 @@ import ConfigParser
import os
import pipes
import stat
-import subprocess
try:
import MySQLdb
except ImportError:
@@ -143,20 +142,14 @@ def db_dump(module, host, user, password, db_name, target, all_databases, port,
cmd += " --all-databases"
else:
cmd += " %s" % pipes.quote(db_name)
-
- path = None
if os.path.splitext(target)[-1] == '.gz':
- path = module.get_bin_path('gzip', True)
+ cmd = cmd + ' | gzip > ' + pipes.quote(target)
elif os.path.splitext(target)[-1] == '.bz2':
- path = module.get_bin_path('bzip2', True)
+ cmd = cmd + ' | bzip2 > ' + pipes.quote(target)
elif os.path.splitext(target)[-1] == '.xz':
- path = module.get_bin_path('xz', True)
-
- if path:
- cmd = '%s | %s > %s' % (cmd, path, pipes.quote(target))
+ cmd = cmd + ' | xz > ' + pipes.quote(target)
else:
cmd += " > %s" % pipes.quote(target)
-
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
return rc, stdout, stderr
@@ -164,44 +157,69 @@ def db_import(module, host, user, password, db_name, target, all_databases, port
if not os.path.exists(target):
return module.fail_json(msg="target %s does not exist on the host" % target)
- cmd = [module.get_bin_path('mysql', True)]
- if user:
- cmd.append("--user=%s" % pipes.quote(user))
- if password:
- cmd.append("--password=%s" % pipes.quote(password))
+ cmd = module.get_bin_path('mysql', True)
+ cmd += " --user=%s --password=%s" % (pipes.quote(user), pipes.quote(password))
if socket is not None:
- cmd.append("--socket=%s" % pipes.quote(socket))
+ cmd += " --socket=%s" % pipes.quote(socket)
else:
- cmd.append("--host=%s" % pipes.quote(host))
- cmd.append("--port=%i" % port)
+ cmd += " --host=%s --port=%i" % (pipes.quote(host), port)
if not all_databases:
- cmd.append("-D")
- cmd.append(pipes.quote(db_name))
-
- comp_prog_path = None
+ cmd += " -D %s" % pipes.quote(db_name)
if os.path.splitext(target)[-1] == '.gz':
- comp_prog_path = module.get_bin_path('gzip', required=True)
+ gzip_path = module.get_bin_path('gzip')
+ if not gzip_path:
+ module.fail_json(msg="gzip command not found")
+ #gzip -d file (uncompress)
+ rc, stdout, stderr = module.run_command('%s -d %s' % (gzip_path, target))
+ if rc != 0:
+ return rc, stdout, stderr
+ #Import sql
+ cmd += " < %s" % pipes.quote(os.path.splitext(target)[0])
+ try:
+ rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
+ if rc != 0:
+ return rc, stdout, stderr
+ finally:
+ #gzip file back up
+ module.run_command('%s %s' % (gzip_path, os.path.splitext(target)[0]))
elif os.path.splitext(target)[-1] == '.bz2':
- comp_prog_path = module.get_bin_path('bzip2', required=True)
+ bzip2_path = module.get_bin_path('bzip2')
+ if not bzip2_path:
+ module.fail_json(msg="bzip2 command not found")
+ #bzip2 -d file (uncompress)
+ rc, stdout, stderr = module.run_command('%s -d %s' % (bzip2_path, target))
+ if rc != 0:
+ return rc, stdout, stderr
+ #Import sql
+ cmd += " < %s" % pipes.quote(os.path.splitext(target)[0])
+ try:
+ rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
+ if rc != 0:
+ return rc, stdout, stderr
+ finally:
+ #bzip2 file back up
+ rc, stdout, stderr = module.run_command('%s %s' % (bzip2_path, os.path.splitext(target)[0]))
elif os.path.splitext(target)[-1] == '.xz':
- comp_prog_path = module.get_bin_path('xz', required=True)
-
- if comp_prog_path:
- p1 = subprocess.Popen([comp_prog_path, '-dc', target], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- p2 = subprocess.Popen(cmd, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- (stdout2, stderr2) = p2.communicate()
- p1.stdout.close()
- p1.wait()
- if p1.returncode != 0:
- stderr1 = p1.stderr.read()
- return p1.returncode, '', stderr1
- else:
- return p2.returncode, stdout2, stderr2
+ xz_path = module.get_bin_path('xz')
+ if not xz_path:
+ module.fail_json(msg="xz command not found")
+ #xz -d file (uncompress)
+ rc, stdout, stderr = module.run_command('%s -d %s' % (xz_path, target))
+ if rc != 0:
+ return rc, stdout, stderr
+ #Import sql
+ cmd += " < %s" % pipes.quote(os.path.splitext(target)[0])
+ try:
+ rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
+ if rc != 0:
+ return rc, stdout, stderr
+ finally:
+ #xz file back up
+ rc, stdout, stderr = module.run_command('%s %s' % (xz_path, os.path.splitext(target)[0]))
else:
- cmd = ' '.join(cmd)
cmd += " < %s" % pipes.quote(target)
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
- return rc, stdout, stderr
+ return rc, stdout, stderr
def db_create(cursor, db, encoding, collation):
query_params = dict(enc=encoding, collate=collation)
@@ -352,11 +370,11 @@ def main():
db_connection = MySQLdb.connect(host=module.params["login_host"], port=login_port, user=login_user, passwd=login_password, db=connect_to_db)
cursor = db_connection.cursor()
except Exception, e:
- errno, errstr = e.args
if "Unknown database" in str(e):
+ errno, errstr = e.args
module.fail_json(msg="ERROR: %s %s" % (errno, errstr))
else:
- module.fail_json(msg="unable to connect, check login credentials (login_user, and login_password, which can be defined in ~/.my.cnf), check that mysql socket exists and mysql server is running (ERROR: %s %s)" % (errno, errstr))
+ module.fail_json(msg="unable to connect, check login credentials (login_user, and login_password, which can be defined in ~/.my.cnf), check that mysql socket exists and mysql server is running")
changed = False
if db_exists(cursor, db):
diff --git a/database/mysql/mysql_user.py b/database/mysql/mysql_user.py
index a3e24261..ba5b6370 100644
--- a/database/mysql/mysql_user.py
+++ b/database/mysql/mysql_user.py
@@ -245,7 +245,7 @@ def user_mod(cursor, user, host, password, new_priv, append_privs):
grant_option = True
if db_table not in new_priv:
if user != "root" and "PROXY" not in priv and not append_privs:
- privileges_revoke(cursor, user,host,db_table,priv,grant_option)
+ privileges_revoke(cursor, user,host,db_table,grant_option)
changed = True
# If the user doesn't currently have any privileges on a db.table, then
@@ -262,7 +262,7 @@ def user_mod(cursor, user, host, password, new_priv, append_privs):
priv_diff = set(new_priv[db_table]) ^ set(curr_priv[db_table])
if (len(priv_diff) > 0):
if not append_privs:
- privileges_revoke(cursor, user,host,db_table,curr_priv[db_table],grant_option)
+ privileges_revoke(cursor, user,host,db_table,grant_option)
privileges_grant(cursor, user,host,db_table,new_priv[db_table])
changed = True
@@ -320,8 +320,12 @@ def privileges_unpack(priv):
output = {}
for item in priv.strip().split('/'):
pieces = item.strip().split(':')
- dbpriv = pieces[0].rsplit(".", 1)
- pieces[0] = "`%s`.%s" % (dbpriv[0].strip('`'), dbpriv[1])
+ if '.' in pieces[0]:
+ pieces[0] = pieces[0].split('.')
+ for idx, piece in enumerate(pieces):
+ if pieces[0][idx] != "*":
+ pieces[0][idx] = "`" + pieces[0][idx] + "`"
+ pieces[0] = '.'.join(pieces[0])
output[pieces[0]] = pieces[1].upper().split(',')
new_privs = frozenset(output[pieces[0]])
@@ -338,7 +342,7 @@ def privileges_unpack(priv):
return output
-def privileges_revoke(cursor, user,host,db_table,priv,grant_option):
+def privileges_revoke(cursor, user,host,db_table,grant_option):
# Escape '%' since mysql db.execute() uses a format string
db_table = db_table.replace('%', '%%')
if grant_option:
@@ -346,8 +350,7 @@ def privileges_revoke(cursor, user,host,db_table,priv,grant_option):
query.append("FROM %s@%s")
query = ' '.join(query)
cursor.execute(query, (user, host))
- priv_string = ",".join([p for p in priv if p not in ('GRANT', 'REQUIRESSL')])
- query = ["REVOKE %s ON %s" % (priv_string, mysql_quote_identifier(db_table, 'table'))]
+ query = ["REVOKE ALL PRIVILEGES ON %s" % mysql_quote_identifier(db_table, 'table')]
query.append("FROM %s@%s")
query = ' '.join(query)
cursor.execute(query, (user, host))
@@ -356,7 +359,7 @@ def privileges_grant(cursor, user,host,db_table,priv):
# Escape '%' since mysql db.execute uses a format string and the
# specification of db and table often use a % (SQL wildcard)
db_table = db_table.replace('%', '%%')
- priv_string = ",".join([p for p in priv if p not in ('GRANT', 'REQUIRESSL')])
+ priv_string = ",".join(filter(lambda x: x not in [ 'GRANT', 'REQUIRESSL' ], priv))
query = ["GRANT %s ON %s" % (priv_string, mysql_quote_identifier(db_table, 'table'))]
query.append("TO %s@%s")
if 'GRANT' in priv: