Convert all databases modules to python3 and 2.4 syntax (#3688)
This commit is contained in:
parent
f7b29ba8fd
commit
09066f1518
6 changed files with 54 additions and 28 deletions
|
@ -289,7 +289,8 @@ def main():
|
||||||
try:
|
try:
|
||||||
cursor = mysql_connect(module, login_user, login_password, config_file, ssl_cert, ssl_key, ssl_ca,
|
cursor = mysql_connect(module, login_user, login_password, config_file, ssl_cert, ssl_key, ssl_ca,
|
||||||
connect_timeout=connect_timeout)
|
connect_timeout=connect_timeout)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
if os.path.exists(config_file):
|
if os.path.exists(config_file):
|
||||||
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. Exception message: %s" % (config_file, e))
|
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. Exception message: %s" % (config_file, e))
|
||||||
else:
|
else:
|
||||||
|
@ -305,7 +306,8 @@ def main():
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
changed = db_delete(cursor, db)
|
changed = db_delete(cursor, db)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg="error deleting database: " + str(e))
|
module.fail_json(msg="error deleting database: " + str(e))
|
||||||
elif state == "dump":
|
elif state == "dump":
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
|
@ -336,7 +338,8 @@ def main():
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
changed = db_create(cursor, db, encoding, collation)
|
changed = db_create(cursor, db, encoding, collation)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg="error creating database: " + str(e))
|
module.fail_json(msg="error creating database: " + str(e))
|
||||||
|
|
||||||
module.exit_json(changed=changed, db=db)
|
module.exit_json(changed=changed, db=db)
|
||||||
|
|
|
@ -541,7 +541,8 @@ def main():
|
||||||
if not cursor:
|
if not cursor:
|
||||||
cursor = mysql_connect(module, login_user, login_password, config_file, ssl_cert, ssl_key, ssl_ca, db,
|
cursor = mysql_connect(module, login_user, login_password, config_file, ssl_cert, ssl_key, ssl_ca, db,
|
||||||
connect_timeout=connect_timeout)
|
connect_timeout=connect_timeout)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. Exception message: %s" % (config_file, e))
|
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. Exception message: %s" % (config_file, e))
|
||||||
|
|
||||||
if not sql_log_bin:
|
if not sql_log_bin:
|
||||||
|
@ -550,11 +551,13 @@ def main():
|
||||||
if priv is not None:
|
if priv is not None:
|
||||||
try:
|
try:
|
||||||
mode = get_mode(cursor)
|
mode = get_mode(cursor)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
try:
|
try:
|
||||||
priv = privileges_unpack(priv, mode)
|
priv = privileges_unpack(priv, mode)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg="invalid privileges string: %s" % str(e))
|
module.fail_json(msg="invalid privileges string: %s" % str(e))
|
||||||
|
|
||||||
if state == "present":
|
if state == "present":
|
||||||
|
@ -565,14 +568,16 @@ def main():
|
||||||
else:
|
else:
|
||||||
changed = user_mod(cursor, user, host, host_all, None, encrypted, priv, append_privs, module)
|
changed = user_mod(cursor, user, host, host_all, None, encrypted, priv, append_privs, module)
|
||||||
|
|
||||||
except (SQLParseError, InvalidPrivsError, MySQLdb.Error), e:
|
except (SQLParseError, InvalidPrivsError, MySQLdb.Error):
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
else:
|
else:
|
||||||
if host_all:
|
if host_all:
|
||||||
module.fail_json(msg="host_all parameter cannot be used when adding a user")
|
module.fail_json(msg="host_all parameter cannot be used when adding a user")
|
||||||
try:
|
try:
|
||||||
changed = user_add(cursor, user, host, host_all, password, encrypted, priv, module.check_mode)
|
changed = user_add(cursor, user, host, host_all, password, encrypted, priv, module.check_mode)
|
||||||
except (SQLParseError, InvalidPrivsError, MySQLdb.Error), e:
|
except (SQLParseError, InvalidPrivsError, MySQLdb.Error):
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
elif state == "absent":
|
elif state == "absent":
|
||||||
if user_exists(cursor, user, host, host_all):
|
if user_exists(cursor, user, host, host_all):
|
||||||
|
|
|
@ -109,7 +109,8 @@ def setvariable(cursor, mysqlvar, value):
|
||||||
cursor.execute(query + "%s", (value,))
|
cursor.execute(query + "%s", (value,))
|
||||||
cursor.fetchall()
|
cursor.fetchall()
|
||||||
result = True
|
result = True
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
result = str(e)
|
result = str(e)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -153,7 +154,8 @@ def main():
|
||||||
try:
|
try:
|
||||||
cursor = mysql_connect(module, user, password, config_file, ssl_cert, ssl_key, ssl_ca, db,
|
cursor = mysql_connect(module, user, password, config_file, ssl_cert, ssl_key, ssl_ca, db,
|
||||||
connect_timeout=connect_timeout)
|
connect_timeout=connect_timeout)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
if os.path.exists(config_file):
|
if os.path.exists(config_file):
|
||||||
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. Exception message: %s" % (config_file, e))
|
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. Exception message: %s" % (config_file, e))
|
||||||
else:
|
else:
|
||||||
|
@ -172,7 +174,8 @@ def main():
|
||||||
module.exit_json(msg="Variable already set to requested value", changed=False)
|
module.exit_json(msg="Variable already set to requested value", changed=False)
|
||||||
try:
|
try:
|
||||||
result = setvariable(cursor, mysqlvar, value_wanted)
|
result = setvariable(cursor, mysqlvar, value_wanted)
|
||||||
except SQLParseError, e:
|
except SQLParseError:
|
||||||
|
e = get_exception()
|
||||||
result = str(e)
|
result = str(e)
|
||||||
if result is True:
|
if result is True:
|
||||||
module.exit_json(msg="Variable change succeeded prev_value=%s" % value_actual, changed=True)
|
module.exit_json(msg="Variable change succeeded prev_value=%s" % value_actual, changed=True)
|
||||||
|
|
|
@ -280,7 +280,8 @@ def main():
|
||||||
.ISOLATION_LEVEL_AUTOCOMMIT)
|
.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||||
cursor = db_connection.cursor(
|
cursor = db_connection.cursor(
|
||||||
cursor_factory=psycopg2.extras.DictCursor)
|
cursor_factory=psycopg2.extras.DictCursor)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg="unable to connect to database: %s" % e)
|
module.fail_json(msg="unable to connect to database: %s" % e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -295,21 +296,25 @@ def main():
|
||||||
if state == "absent":
|
if state == "absent":
|
||||||
try:
|
try:
|
||||||
changed = db_delete(cursor, db)
|
changed = db_delete(cursor, db)
|
||||||
except SQLParseError, e:
|
except SQLParseError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
elif state == "present":
|
elif state == "present":
|
||||||
try:
|
try:
|
||||||
changed = db_create(cursor, db, owner, template, encoding,
|
changed = db_create(cursor, db, owner, template, encoding,
|
||||||
lc_collate, lc_ctype)
|
lc_collate, lc_ctype)
|
||||||
except SQLParseError, e:
|
except SQLParseError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
except NotSupportedError, e:
|
except NotSupportedError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
except SystemExit:
|
except SystemExit:
|
||||||
# Avoid catching this on Python 2.4
|
# Avoid catching this on Python 2.4
|
||||||
raise
|
raise
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg="Database query failed: %s" % e)
|
module.fail_json(msg="Database query failed: %s" % e)
|
||||||
|
|
||||||
module.exit_json(changed=changed, db=db)
|
module.exit_json(changed=changed, db=db)
|
||||||
|
|
|
@ -573,7 +573,8 @@ def main():
|
||||||
module.fail_json(msg='Python module "psycopg2" must be installed.')
|
module.fail_json(msg='Python module "psycopg2" must be installed.')
|
||||||
try:
|
try:
|
||||||
conn = Connection(p)
|
conn = Connection(p)
|
||||||
except psycopg2.Error, e:
|
except psycopg2.Error:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg='Could not connect to database: %s' % e)
|
module.fail_json(msg='Could not connect to database: %s' % e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -613,11 +614,13 @@ def main():
|
||||||
schema_qualifier=p.schema
|
schema_qualifier=p.schema
|
||||||
)
|
)
|
||||||
|
|
||||||
except Error, e:
|
except Error:
|
||||||
|
e = get_exception()
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
module.fail_json(msg=e.message)
|
module.fail_json(msg=e.message)
|
||||||
|
|
||||||
except psycopg2.Error, e:
|
except psycopg2.Error:
|
||||||
|
e = get_exception()
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
# psycopg2 errors come in connection encoding, reencode
|
# psycopg2 errors come in connection encoding, reencode
|
||||||
msg = e.message.decode(conn.encoding).encode(sys.getdefaultencoding(),
|
msg = e.message.decode(conn.encoding).encode(sys.getdefaultencoding(),
|
||||||
|
|
|
@ -290,7 +290,8 @@ def user_alter(cursor, module, user, password, role_attr_flags, encrypted, expir
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cursor.execute(' '.join(alter), query_password_data)
|
cursor.execute(' '.join(alter), query_password_data)
|
||||||
except psycopg2.InternalError, e:
|
except psycopg2.InternalError:
|
||||||
|
e = get_exception()
|
||||||
if e.pgcode == '25006':
|
if e.pgcode == '25006':
|
||||||
# Handle errors due to read-only transactions indicated by pgcode 25006
|
# Handle errors due to read-only transactions indicated by pgcode 25006
|
||||||
# ERROR: cannot execute ALTER ROLE in a read-only transaction
|
# ERROR: cannot execute ALTER ROLE in a read-only transaction
|
||||||
|
@ -298,7 +299,7 @@ def user_alter(cursor, module, user, password, role_attr_flags, encrypted, expir
|
||||||
module.fail_json(msg=e.pgerror)
|
module.fail_json(msg=e.pgerror)
|
||||||
return changed
|
return changed
|
||||||
else:
|
else:
|
||||||
raise psycopg2.InternalError, e
|
raise psycopg2.InternalError(e)
|
||||||
|
|
||||||
# Grab new role attributes.
|
# Grab new role attributes.
|
||||||
cursor.execute(select, {"user": user})
|
cursor.execute(select, {"user": user})
|
||||||
|
@ -551,7 +552,8 @@ def main():
|
||||||
no_password_changes = module.params["no_password_changes"]
|
no_password_changes = module.params["no_password_changes"]
|
||||||
try:
|
try:
|
||||||
role_attr_flags = parse_role_attrs(module.params["role_attr_flags"])
|
role_attr_flags = parse_role_attrs(module.params["role_attr_flags"])
|
||||||
except InvalidFlagsError, e:
|
except InvalidFlagsError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
if module.params["encrypted"]:
|
if module.params["encrypted"]:
|
||||||
encrypted = "ENCRYPTED"
|
encrypted = "ENCRYPTED"
|
||||||
|
@ -583,7 +585,8 @@ def main():
|
||||||
try:
|
try:
|
||||||
db_connection = psycopg2.connect(**kw)
|
db_connection = psycopg2.connect(**kw)
|
||||||
cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg="unable to connect to database: %s" % e)
|
module.fail_json(msg="unable to connect to database: %s" % e)
|
||||||
|
|
||||||
kw = dict(user=user)
|
kw = dict(user=user)
|
||||||
|
@ -594,16 +597,19 @@ def main():
|
||||||
if user_exists(cursor, user):
|
if user_exists(cursor, user):
|
||||||
try:
|
try:
|
||||||
changed = user_alter(cursor, module, user, password, role_attr_flags, encrypted, expires, no_password_changes)
|
changed = user_alter(cursor, module, user, password, role_attr_flags, encrypted, expires, no_password_changes)
|
||||||
except SQLParseError, e:
|
except SQLParseError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
changed = user_add(cursor, user, password, role_attr_flags, encrypted, expires)
|
changed = user_add(cursor, user, password, role_attr_flags, encrypted, expires)
|
||||||
except SQLParseError, e:
|
except SQLParseError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
try:
|
try:
|
||||||
changed = grant_privileges(cursor, user, privs) or changed
|
changed = grant_privileges(cursor, user, privs) or changed
|
||||||
except SQLParseError, e:
|
except SQLParseError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
else:
|
else:
|
||||||
if user_exists(cursor, user):
|
if user_exists(cursor, user):
|
||||||
|
@ -614,7 +620,8 @@ def main():
|
||||||
try:
|
try:
|
||||||
changed = revoke_privileges(cursor, user, privs)
|
changed = revoke_privileges(cursor, user, privs)
|
||||||
user_removed = user_delete(cursor, user)
|
user_removed = user_delete(cursor, user)
|
||||||
except SQLParseError, e:
|
except SQLParseError:
|
||||||
|
e = get_exception()
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
changed = changed or user_removed
|
changed = changed or user_removed
|
||||||
if fail_on_user and not user_removed:
|
if fail_on_user and not user_removed:
|
||||||
|
|
Loading…
Reference in a new issue