Pep8 fixes for mysql module (#23923)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-04-26 17:26:35 +05:30 committed by John R Barker
parent 9d5c399313
commit 9fbbb5e10f
5 changed files with 63 additions and 42 deletions

View file

@ -128,15 +128,18 @@ else:
# MySQL module specific support methods. # MySQL module specific support methods.
# #
def db_exists(cursor, db): def db_exists(cursor, db):
res = cursor.execute("SHOW DATABASES LIKE %s", (db.replace("_","\_"),)) res = cursor.execute("SHOW DATABASES LIKE %s", (db.replace("_", "\_"),))
return bool(res) return bool(res)
def db_delete(cursor, db): def db_delete(cursor, db):
query = "DROP DATABASE %s" % mysql_quote_identifier(db, 'database') query = "DROP DATABASE %s" % mysql_quote_identifier(db, 'database')
cursor.execute(query) cursor.execute(query)
return True return True
def db_dump(module, host, user, password, db_name, target, all_databases, port, config_file, socket=None, ssl_cert=None, ssl_key=None, ssl_ca=None, def db_dump(module, host, user, password, db_name, target, all_databases, port, config_file, socket=None, ssl_cert=None, ssl_key=None, ssl_ca=None,
single_transaction=None, quick=None): single_transaction=None, quick=None):
cmd = module.get_bin_path('mysqldump', True) cmd = module.get_bin_path('mysqldump', True)
@ -182,6 +185,7 @@ def db_dump(module, host, user, password, db_name, target, all_databases, port,
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 return rc, stdout, stderr
def db_import(module, host, user, password, db_name, target, all_databases, port, config_file, socket=None, ssl_cert=None, ssl_key=None, ssl_ca=None): def db_import(module, host, user, password, db_name, target, all_databases, port, config_file, socket=None, ssl_cert=None, ssl_key=None, ssl_ca=None):
if not os.path.exists(target): if not os.path.exists(target):
return module.fail_json(msg="target %s does not exist on the host" % target) return module.fail_json(msg="target %s does not exist on the host" % target)
@ -234,6 +238,7 @@ def db_import(module, host, user, password, db_name, target, all_databases, port
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 return rc, stdout, stderr
def db_create(cursor, db, encoding, collation): def db_create(cursor, db, encoding, collation):
query_params = dict(enc=encoding, collate=collation) query_params = dict(enc=encoding, collate=collation)
query = ['CREATE DATABASE %s' % mysql_quote_identifier(db, 'database')] query = ['CREATE DATABASE %s' % mysql_quote_identifier(db, 'database')]
@ -242,16 +247,17 @@ def db_create(cursor, db, encoding, collation):
if collation: if collation:
query.append("COLLATE %(collate)s") query.append("COLLATE %(collate)s")
query = ' '.join(query) query = ' '.join(query)
res = cursor.execute(query, query_params) cursor.execute(query, query_params)
return True return True
# =========================================== # ===========================================
# Module execution. # Module execution.
# #
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
login_user=dict(default=None), login_user=dict(default=None),
login_password=dict(default=None, no_log=True), login_password=dict(default=None, no_log=True),
login_host=dict(default="localhost"), login_host=dict(default="localhost"),
@ -261,7 +267,7 @@ def main():
encoding=dict(default=""), encoding=dict(default=""),
collation=dict(default=""), collation=dict(default=""),
target=dict(default=None, type='path'), target=dict(default=None, type='path'),
state=dict(default="present", choices=["absent", "present","dump", "import"]), state=dict(default="present", choices=["absent", "present", "dump", "import"]),
ssl_cert=dict(default=None, type='path'), ssl_cert=dict(default=None, type='path'),
ssl_key=dict(default=None, type='path'), ssl_key=dict(default=None, type='path'),
ssl_ca=dict(default=None, type='path'), ssl_ca=dict(default=None, type='path'),
@ -296,9 +302,9 @@ def main():
single_transaction = module.params["single_transaction"] single_transaction = module.params["single_transaction"]
quick = module.params["quick"] quick = module.params["quick"]
if state in ['dump','import']: if state in ['dump', 'import']:
if target is None: if target is None:
module.fail_json(msg="with state=%s target is required" % (state)) module.fail_json(msg="with state=%s target is required" % state)
if db == 'all': if db == 'all':
db = 'mysql' db = 'mysql'
all_databases = True all_databases = True
@ -338,8 +344,9 @@ def main():
module.exit_json(changed=True, db=db) module.exit_json(changed=True, db=db)
else: else:
rc, stdout, stderr = db_dump(module, login_host, login_user, rc, stdout, stderr = db_dump(module, login_host, login_user,
login_password, db, target, all_databases, login_password, db, target, all_databases,
login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca, single_transaction, quick) login_port, config_file, socket, ssl_cert, ssl_key,
ssl_ca, single_transaction, quick)
if rc != 0: if rc != 0:
module.fail_json(msg="%s" % stderr) module.fail_json(msg="%s" % stderr)
else: else:
@ -350,8 +357,10 @@ def main():
module.exit_json(changed=True, db=db) module.exit_json(changed=True, db=db)
else: else:
rc, stdout, stderr = db_import(module, login_host, login_user, rc, stdout, stderr = db_import(module, login_host, login_user,
login_password, db, target, all_databases, login_password, db, target,
login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca) all_databases,
login_port, config_file,
socket, ssl_cert, ssl_key, ssl_ca)
if rc != 0: if rc != 0:
module.fail_json(msg="%s" % stderr) module.fail_json(msg="%s" % stderr)
else: else:
@ -382,8 +391,8 @@ def main():
changed = db_create(cursor, db, encoding, collation) changed = db_create(cursor, db, encoding, collation)
if changed: if changed:
rc, stdout, stderr = db_import(module, login_host, login_user, rc, stdout, stderr = db_import(module, login_host, login_user,
login_password, db, target, all_databases, login_password, db, target, all_databases,
login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca) login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca)
if rc != 0: if rc != 0:
module.fail_json(msg="%s" % stderr) module.fail_json(msg="%s" % stderr)
else: else:

View file

@ -201,7 +201,7 @@ def changemaster(cursor, chm, chm_params):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
login_user=dict(default=None), login_user=dict(default=None),
login_password=dict(default=None, no_log=True), login_password=dict(default=None, no_log=True),
login_host=dict(default="localhost"), login_host=dict(default="localhost"),
@ -290,7 +290,7 @@ def main():
module.exit_json(**status) module.exit_json(**status)
elif mode in "changemaster": elif mode in "changemaster":
chm=[] chm = []
chm_params = {} chm_params = {}
result = {} result = {}
if master_host: if master_host:
@ -347,7 +347,7 @@ def main():
except Exception: except Exception:
e = get_exception() e = get_exception()
module.fail_json(msg='%s. Query == CHANGE MASTER TO %s' % (e, chm)) module.fail_json(msg='%s. Query == CHANGE MASTER TO %s' % (e, chm))
result['changed']=True result['changed'] = True
module.exit_json(**result) module.exit_json(**result)
elif mode in "startslave": elif mode in "startslave":
started = start_slave(cursor) started = start_slave(cursor)

View file

@ -214,8 +214,7 @@ EXAMPLES = """
# password=n<_665{vS43y # password=n<_665{vS43y
""" """
import getpass
import tempfile
import re import re
import string import string
try: try:
@ -236,6 +235,7 @@ VALID_PRIVS = frozenset(('CREATE', 'DROP', 'GRANT', 'GRANT OPTION',
'REPLICATION SLAVE', 'SHOW DATABASES', 'SHUTDOWN', 'REPLICATION SLAVE', 'SHOW DATABASES', 'SHUTDOWN',
'SUPER', 'ALL', 'ALL PRIVILEGES', 'USAGE', 'REQUIRESSL')) 'SUPER', 'ALL', 'ALL PRIVILEGES', 'USAGE', 'REQUIRESSL'))
class InvalidPrivsError(Exception): class InvalidPrivsError(Exception):
pass pass
@ -243,6 +243,7 @@ class InvalidPrivsError(Exception):
# MySQL module specific support methods. # MySQL module specific support methods.
# #
# User Authentication Management was change in MySQL 5.7 # User Authentication Management was change in MySQL 5.7
# This is a generic check for if the server version is less than version 5.7 # This is a generic check for if the server version is less than version 5.7
def server_version_check(cursor): def server_version_check(cursor):
@ -255,11 +256,12 @@ def server_version_check(cursor):
# mariadb and the old-style update continues to work # mariadb and the old-style update continues to work
if 'mariadb' in version_str.lower(): if 'mariadb' in version_str.lower():
return True return True
if (int(version[0]) <= 5 and int(version[1]) < 7): if int(version[0]) <= 5 and int(version[1]) < 7:
return True return True
else: else:
return False return False
def get_mode(cursor): def get_mode(cursor):
cursor.execute('SELECT @@GLOBAL.sql_mode') cursor.execute('SELECT @@GLOBAL.sql_mode')
result = cursor.fetchone() result = cursor.fetchone()
@ -270,15 +272,17 @@ def get_mode(cursor):
mode = 'NOTANSI' mode = 'NOTANSI'
return mode return mode
def user_exists(cursor, user, host, host_all): def user_exists(cursor, user, host, host_all):
if host_all: if host_all:
cursor.execute("SELECT count(*) FROM user WHERE user = %s", ([user])) cursor.execute("SELECT count(*) FROM user WHERE user = %s", ([user]))
else: else:
cursor.execute("SELECT count(*) FROM user WHERE user = %s AND host = %s", (user,host)) cursor.execute("SELECT count(*) FROM user WHERE user = %s AND host = %s", (user, host))
count = cursor.fetchone() count = cursor.fetchone()
return count[0] > 0 return count[0] > 0
def user_add(cursor, user, host, host_all, password, encrypted, new_priv, check_mode): def user_add(cursor, user, host, host_all, password, encrypted, new_priv, check_mode):
# we cannot create users without a proper hostname # we cannot create users without a proper hostname
if host_all: if host_all:
@ -288,16 +292,17 @@ def user_add(cursor, user, host, host_all, password, encrypted, new_priv, check_
return True return True
if password and encrypted: if password and encrypted:
cursor.execute("CREATE USER %s@%s IDENTIFIED BY PASSWORD %s", (user,host,password)) cursor.execute("CREATE USER %s@%s IDENTIFIED BY PASSWORD %s", (user, host, password))
elif password and not encrypted: elif password and not encrypted:
cursor.execute("CREATE USER %s@%s IDENTIFIED BY %s", (user,host,password)) cursor.execute("CREATE USER %s@%s IDENTIFIED BY %s", (user, host, password))
else: else:
cursor.execute("CREATE USER %s@%s", (user,host)) cursor.execute("CREATE USER %s@%s", (user, host))
if new_priv is not None: if new_priv is not None:
for db_table, priv in iteritems(new_priv): for db_table, priv in iteritems(new_priv):
privileges_grant(cursor, user,host,db_table,priv) privileges_grant(cursor, user, host, db_table, priv)
return True return True
def is_hash(password): def is_hash(password):
ishash = False ishash = False
if len(password) == 41 and password[0] == '*': if len(password) == 41 and password[0] == '*':
@ -305,6 +310,7 @@ def is_hash(password):
ishash = True ishash = True
return ishash return ishash
def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append_privs, module): def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append_privs, module):
changed = False changed = False
grant_option = False grant_option = False
@ -321,9 +327,9 @@ def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append
old_user_mgmt = server_version_check(cursor) old_user_mgmt = server_version_check(cursor)
if old_user_mgmt: if old_user_mgmt:
cursor.execute("SELECT password FROM user WHERE user = %s AND host = %s", (user,host)) cursor.execute("SELECT password FROM user WHERE user = %s AND host = %s", (user, host))
else: else:
cursor.execute("SELECT authentication_string FROM user WHERE user = %s AND host = %s", (user,host)) cursor.execute("SELECT authentication_string FROM user WHERE user = %s AND host = %s", (user, host))
current_pass_hash = cursor.fetchone() current_pass_hash = cursor.fetchone()
if encrypted: if encrypted:
@ -356,7 +362,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append
# Handle privileges # Handle privileges
if new_priv is not None: if new_priv is not None:
curr_priv = privileges_get(cursor, user,host) curr_priv = privileges_get(cursor, user, host)
# If the user has privileges on a db.table that doesn't appear at all in # If the user has privileges on a db.table that doesn't appear at all in
# the new specification, then revoke all privileges on it. # the new specification, then revoke all privileges on it.
@ -368,7 +374,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append
if user != "root" and "PROXY" not in priv and not append_privs: if user != "root" and "PROXY" not in priv and not append_privs:
if module.check_mode: if module.check_mode:
return True return True
privileges_revoke(cursor, user,host,db_table,priv,grant_option) privileges_revoke(cursor, user, host, db_table, priv, grant_option)
changed = True changed = True
# If the user doesn't currently have any privileges on a db.table, then # If the user doesn't currently have any privileges on a db.table, then
@ -377,7 +383,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append
if db_table not in curr_priv: if db_table not in curr_priv:
if module.check_mode: if module.check_mode:
return True return True
privileges_grant(cursor, user,host,db_table,priv) privileges_grant(cursor, user, host, db_table, priv)
changed = True changed = True
# If the db.table specification exists in both the user's current privileges # If the db.table specification exists in both the user's current privileges
@ -385,16 +391,17 @@ def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append
db_table_intersect = set(new_priv.keys()) & set(curr_priv.keys()) db_table_intersect = set(new_priv.keys()) & set(curr_priv.keys())
for db_table in db_table_intersect: for db_table in db_table_intersect:
priv_diff = set(new_priv[db_table]) ^ set(curr_priv[db_table]) priv_diff = set(new_priv[db_table]) ^ set(curr_priv[db_table])
if (len(priv_diff) > 0): if len(priv_diff) > 0:
if module.check_mode: if module.check_mode:
return True return True
if not append_privs: if not append_privs:
privileges_revoke(cursor, user,host,db_table,curr_priv[db_table],grant_option) privileges_revoke(cursor, user, host, db_table, curr_priv[db_table], grant_option)
privileges_grant(cursor, user,host,db_table,new_priv[db_table]) privileges_grant(cursor, user, host, db_table, new_priv[db_table])
changed = True changed = True
return changed return changed
def user_delete(cursor, user, host, host_all, check_mode): def user_delete(cursor, user, host, host_all, check_mode):
if check_mode: if check_mode:
return True return True
@ -409,6 +416,7 @@ def user_delete(cursor, user, host, host_all, check_mode):
return True return True
def user_get_hostnames(cursor, user): def user_get_hostnames(cursor, user):
cursor.execute("SELECT Host FROM mysql.user WHERE user = %s", user) cursor.execute("SELECT Host FROM mysql.user WHERE user = %s", user)
hostnames_raw = cursor.fetchall() hostnames_raw = cursor.fetchall()
@ -419,7 +427,8 @@ def user_get_hostnames(cursor, user):
return hostnames return hostnames
def privileges_get(cursor, user,host):
def privileges_get(cursor, user, host):
""" MySQL doesn't have a better method of getting privileges aside from the """ MySQL doesn't have a better method of getting privileges aside from the
SHOW GRANTS query syntax, which requires us to then parse the returned string. SHOW GRANTS query syntax, which requires us to then parse the returned string.
Here's an example of the string that is returned from MySQL: Here's an example of the string that is returned from MySQL:
@ -444,7 +453,7 @@ def privileges_get(cursor, user,host):
if res is None: if res is None:
raise InvalidPrivsError('unable to parse the MySQL grant string: %s' % grant[0]) raise InvalidPrivsError('unable to parse the MySQL grant string: %s' % grant[0])
privileges = res.group(1).split(", ") privileges = res.group(1).split(", ")
privileges = [ pick(x) for x in privileges] privileges = [pick(x) for x in privileges]
if "WITH GRANT OPTION" in res.group(4): if "WITH GRANT OPTION" in res.group(4):
privileges.append('GRANT') privileges.append('GRANT')
if "REQUIRE SSL" in res.group(4): if "REQUIRE SSL" in res.group(4):
@ -453,6 +462,7 @@ def privileges_get(cursor, user,host):
output[db] = privileges output[db] = privileges
return output return output
def privileges_unpack(priv, mode): def privileges_unpack(priv, mode):
""" Take a privileges string, typically passed as a parameter, and unserialize """ Take a privileges string, typically passed as a parameter, and unserialize
it into a dictionary, the same format as privileges_get() above. We have this it into a dictionary, the same format as privileges_get() above. We have this
@ -483,7 +493,7 @@ def privileges_unpack(priv, mode):
if '(' in pieces[1]: if '(' in pieces[1]:
output[pieces[0]] = re.split(r',\s*(?=[^)]*(?:\(|$))', pieces[1].upper()) output[pieces[0]] = re.split(r',\s*(?=[^)]*(?:\(|$))', pieces[1].upper())
for i in output[pieces[0]]: for i in output[pieces[0]]:
privs.append(re.sub(r'\s*\(.*\)','',i)) privs.append(re.sub(r'\s*\(.*\)', '', i))
else: else:
output[pieces[0]] = pieces[1].upper().split(',') output[pieces[0]] = pieces[1].upper().split(',')
privs = output[pieces[0]] privs = output[pieces[0]]
@ -501,7 +511,8 @@ def privileges_unpack(priv, mode):
return output return output
def privileges_revoke(cursor, user,host,db_table,priv,grant_option):
def privileges_revoke(cursor, user, host, db_table, priv, grant_option):
# Escape '%' since mysql db.execute() uses a format string # Escape '%' since mysql db.execute() uses a format string
db_table = db_table.replace('%', '%%') db_table = db_table.replace('%', '%%')
if grant_option: if grant_option:
@ -515,7 +526,8 @@ def privileges_revoke(cursor, user,host,db_table,priv,grant_option):
query = ' '.join(query) query = ' '.join(query)
cursor.execute(query, (user, host)) cursor.execute(query, (user, host))
def privileges_grant(cursor, user,host,db_table,priv):
def privileges_grant(cursor, user, host, db_table, priv):
# Escape '%' since mysql db.execute uses a format string and the # Escape '%' since mysql db.execute uses a format string and the
# specification of db and table often use a % (SQL wildcard) # specification of db and table often use a % (SQL wildcard)
db_table = db_table.replace('%', '%%') db_table = db_table.replace('%', '%%')
@ -533,9 +545,10 @@ def privileges_grant(cursor, user,host,db_table,priv):
# Module execution. # Module execution.
# #
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
login_user=dict(default=None), login_user=dict(default=None),
login_password=dict(default=None, no_log=True), login_password=dict(default=None, no_log=True),
login_host=dict(default="localhost"), login_host=dict(default="localhost"),
@ -645,5 +658,6 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.database import * from ansible.module_utils.database import *
from ansible.module_utils.mysql import * from ansible.module_utils.mysql import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -104,6 +104,7 @@ def getvariable(cursor, mysqlvar):
else: else:
return None return None
def setvariable(cursor, mysqlvar, value): def setvariable(cursor, mysqlvar, value):
""" Set a global mysql variable to a given value """ Set a global mysql variable to a given value
@ -122,9 +123,10 @@ def setvariable(cursor, mysqlvar, value):
result = str(e) result = str(e)
return result return result
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
login_user=dict(default=None), login_user=dict(default=None),
login_password=dict(default=None, no_log=True), login_password=dict(default=None, no_log=True),
login_host=dict(default="localhost"), login_host=dict(default="localhost"),

View file

@ -394,10 +394,6 @@ lib/ansible/modules/database/misc/riak.py
lib/ansible/modules/database/mongodb/mongodb_parameter.py lib/ansible/modules/database/mongodb/mongodb_parameter.py
lib/ansible/modules/database/mongodb/mongodb_user.py lib/ansible/modules/database/mongodb/mongodb_user.py
lib/ansible/modules/database/mssql/mssql_db.py lib/ansible/modules/database/mssql/mssql_db.py
lib/ansible/modules/database/mysql/mysql_db.py
lib/ansible/modules/database/mysql/mysql_replication.py
lib/ansible/modules/database/mysql/mysql_user.py
lib/ansible/modules/database/mysql/mysql_variables.py
lib/ansible/modules/database/postgresql/postgresql_db.py lib/ansible/modules/database/postgresql/postgresql_db.py
lib/ansible/modules/database/postgresql/postgresql_ext.py lib/ansible/modules/database/postgresql/postgresql_ext.py
lib/ansible/modules/database/postgresql/postgresql_lang.py lib/ansible/modules/database/postgresql/postgresql_lang.py