From ecef18d7441acd293fbc79fffb688654f280ac3a Mon Sep 17 00:00:00 2001 From: Andrey Klychkov Date: Wed, 22 May 2019 16:46:48 +0300 Subject: [PATCH] Backport/2.8/55514 (#56686) * postgresql modules: move connect_to_db and get_pg_version to module_utils/postgres.py (#55514) * postgresql modules: move connect_to_db, get_pg_version to module_utils * postgresql modules: move connect_to_db, get_pg_version to module_utils 2 * postgresql modules: move connect_to_db, get_pg_version to module_utils 3 (cherry picked from commit 316d1db29fafc5f6c040463e153d2dcc188805b1) * postgresql modules: move connect_to_db and get_pg_version to module_utils/postgres.py Backport/2.8/55514 --- .../55514-postgres_move_connect_to_db.yml | 3 ++ lib/ansible/module_utils/postgres.py | 29 +++++++++++++++++++ .../database/postgresql/postgresql_ext.py | 23 ++------------- .../database/postgresql/postgresql_idx.py | 20 ++----------- .../database/postgresql/postgresql_lang.py | 15 ++-------- .../postgresql/postgresql_membership.py | 24 +-------------- .../database/postgresql/postgresql_owner.py | 24 +-------------- .../database/postgresql/postgresql_query.py | 23 +-------------- .../database/postgresql/postgresql_schema.py | 24 ++------------- .../database/postgresql/postgresql_set.py | 20 ++----------- .../database/postgresql/postgresql_slot.py | 29 +------------------ .../database/postgresql/postgresql_table.py | 13 ++------- .../postgresql/postgresql_tablespace.py | 24 +-------------- 13 files changed, 54 insertions(+), 217 deletions(-) create mode 100644 changelogs/fragments/55514-postgres_move_connect_to_db.yml diff --git a/changelogs/fragments/55514-postgres_move_connect_to_db.yml b/changelogs/fragments/55514-postgres_move_connect_to_db.yml new file mode 100644 index 0000000000..582a8339ca --- /dev/null +++ b/changelogs/fragments/55514-postgres_move_connect_to_db.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - postgresql - move connect_to_db and get_pg_version into module_utils/postgres.py (https://github.com/ansible/ansible/pull/55514) diff --git a/lib/ansible/module_utils/postgres.py b/lib/ansible/module_utils/postgres.py index 1066b5693f..87ec25806c 100644 --- a/lib/ansible/module_utils/postgres.py +++ b/lib/ansible/module_utils/postgres.py @@ -34,6 +34,8 @@ try: except ImportError: HAS_PSYCOPG2 = False +from ansible.module_utils._text import to_native + class LibraryError(Exception): pass @@ -59,3 +61,30 @@ def postgres_common_argument_spec(): ssl_mode=dict(default='prefer', choices=['allow', 'disable', 'prefer', 'require', 'verify-ca', 'verify-full']), ca_cert=dict(aliases=['ssl_rootcert']), ) + + +def connect_to_db(module, kw, autocommit=False): + try: + db_connection = psycopg2.connect(**kw) + if autocommit: + if psycopg2.__version__ >= '2.4.2': + db_connection.set_session(autocommit=True) + else: + db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) + + except TypeError as e: + if 'sslrootcert' in e.args[0]: + module.fail_json(msg='Postgresql server must be at least ' + 'version 8.4 to support sslrootcert') + + module.fail_json(msg="unable to connect to database: %s" % to_native(e)) + + except Exception as e: + module.fail_json(msg="unable to connect to database: %s" % to_native(e)) + + return db_connection + + +def get_pg_version(cursor): + cursor.execute("select current_setting('server_version_num')") + return int(cursor.fetchone()[0]) diff --git a/lib/ansible/modules/database/postgresql/postgresql_ext.py b/lib/ansible/modules/database/postgresql/postgresql_ext.py index 6015cc0deb..ca3dabe7cd 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_ext.py +++ b/lib/ansible/modules/database/postgresql/postgresql_ext.py @@ -145,7 +145,7 @@ except ImportError: HAS_PSYCOPG2 = False from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native from ansible.module_utils.database import pg_quote_identifier @@ -248,25 +248,8 @@ def main(): if psycopg2.__version__ < '2.4.3' and sslrootcert is not None: module.fail_json(msg='psycopg2 must be at least 2.4.3 in order to user the ca_cert parameter') - try: - db_connection = psycopg2.connect(**kw) - # Enable autocommit so we can create databases - if psycopg2.__version__ >= '2.4.2': - db_connection.autocommit = True - else: - db_connection.set_isolation_level(psycopg2 - .extensions - .ISOLATION_LEVEL_AUTOCOMMIT) - cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json( - msg='Postgresql server must be at least version 8.4 to support sslrootcert') - module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) + db_connection = connect_to_db(module, kw, autocommit=True) + cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) if session_role: try: diff --git a/lib/ansible/modules/database/postgresql/postgresql_idx.py b/lib/ansible/modules/database/postgresql/postgresql_idx.py index 9e551f39f5..75601e5087 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_idx.py +++ b/lib/ansible/modules/database/postgresql/postgresql_idx.py @@ -241,7 +241,7 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils._text import to_native from ansible.module_utils.six import iteritems @@ -485,22 +485,8 @@ def main(): if psycopg2.__version__ < '2.4.3' and sslrootcert is not None: module.fail_json(msg='psycopg2 must be at least 2.4.3 in order to user the ca_cert parameter') - try: - db_connection = psycopg2.connect(**kw) - if concurrent: - if psycopg2.__version__ >= '2.4.2': - db_connection.set_session(autocommit=True) - else: - db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - - cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least version 8.4 to support sslrootcert') - - module.fail_json(msg="Unable to connect to database: %s" % to_native(e)) - except Exception as e: - module.fail_json(msg="Unable to connect to database: %s" % to_native(e)) + db_connection = connect_to_db(module, kw, autocommit=True) + cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) if session_role: try: diff --git a/lib/ansible/modules/database/postgresql/postgresql_lang.py b/lib/ansible/modules/database/postgresql/postgresql_lang.py index d7d1a6f56e..c56af11531 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_lang.py +++ b/lib/ansible/modules/database/postgresql/postgresql_lang.py @@ -180,7 +180,7 @@ except ImportError: HAS_PSYCOPG2 = False from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native from ansible.module_utils.database import pg_quote_identifier @@ -293,17 +293,8 @@ def main(): if psycopg2.__version__ < '2.4.3' and sslrootcert is not None: module.fail_json(msg='psycopg2 must be at least 2.4.3 in order to user the ca_cert parameter') - try: - db_connection = psycopg2.connect(**kw) - cursor = db_connection.cursor() - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least version 8.4 to support sslrootcert') - module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) + db_connection = connect_to_db(module, kw, autocommit=False) + cursor = db_connection.cursor() if session_role: try: diff --git a/lib/ansible/modules/database/postgresql/postgresql_membership.py b/lib/ansible/modules/database/postgresql/postgresql_membership.py index 34ae32a4ed..ce7f2292ea 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_membership.py +++ b/lib/ansible/modules/database/postgresql/postgresql_membership.py @@ -143,33 +143,11 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError, pg_quote_identifier -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils._text import to_native from ansible.module_utils.six import iteritems -def connect_to_db(module, kw, autocommit=False): - try: - db_connection = psycopg2.connect(**kw) - if autocommit: - if psycopg2.__version__ >= '2.4.2': - db_connection.set_session(autocommit=True) - else: - db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least ' - 'version 8.4 to support sslrootcert') - - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - return db_connection - - class PgMembership(object): def __init__(self, module, cursor, groups, target_roles, fail_on_role): self.module = module diff --git a/lib/ansible/modules/database/postgresql/postgresql_owner.py b/lib/ansible/modules/database/postgresql/postgresql_owner.py index 4f8130e019..dea01e0895 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_owner.py +++ b/lib/ansible/modules/database/postgresql/postgresql_owner.py @@ -157,33 +157,11 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError, pg_quote_identifier -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils._text import to_native from ansible.module_utils.six import iteritems -def connect_to_db(module, kw, autocommit=False): - try: - db_connection = psycopg2.connect(**kw) - if autocommit: - if psycopg2.__version__ >= '2.4.2': - db_connection.set_session(autocommit=True) - else: - db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least ' - 'version 8.4 to support sslrootcert') - - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - return db_connection - - class PgOwnership(object): def __init__(self, module, cursor, role): self.module = module diff --git a/lib/ansible/modules/database/postgresql/postgresql_query.py b/lib/ansible/modules/database/postgresql/postgresql_query.py index d5d0234964..88e6522b6f 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_query.py +++ b/lib/ansible/modules/database/postgresql/postgresql_query.py @@ -147,32 +147,11 @@ except ImportError: import ansible.module_utils.postgres as pgutils from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils._text import to_native from ansible.module_utils.six import iteritems -def connect_to_db(module, kw, autocommit=False): - try: - db_connection = psycopg2.connect(**kw) - if autocommit: - if psycopg2.__version__ >= '2.4.2': - db_connection.set_session(autocommit=True) - else: - db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least ' - 'version 8.4 to support sslrootcert') - - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - return db_connection - # =========================================== # Module execution. # diff --git a/lib/ansible/modules/database/postgresql/postgresql_schema.py b/lib/ansible/modules/database/postgresql/postgresql_schema.py index 88fcc17a7b..f670fd4f5d 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_schema.py +++ b/lib/ansible/modules/database/postgresql/postgresql_schema.py @@ -131,7 +131,7 @@ except ImportError: HAS_PSYCOPG2 = False from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils.database import SQLParseError, pg_quote_identifier from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -266,26 +266,8 @@ def main(): module.fail_json( msg='psycopg2 must be at least 2.4.3 in order to user the ca_cert parameter') - try: - db_connection = psycopg2.connect(**kw) - # Enable autocommit so we can create databases - if psycopg2.__version__ >= '2.4.2': - db_connection.autocommit = True - else: - db_connection.set_isolation_level(psycopg2 - .extensions - .ISOLATION_LEVEL_AUTOCOMMIT) - cursor = db_connection.cursor( - cursor_factory=psycopg2.extras.DictCursor) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json( - msg='Postgresql server must be at least version 8.4 to support sslrootcert') - module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) + db_connection = connect_to_db(module, kw, autocommit=True) + cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) if session_role: try: diff --git a/lib/ansible/modules/database/postgresql/postgresql_set.py b/lib/ansible/modules/database/postgresql/postgresql_set.py index 278c5ff47b..b3cd4faed6 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_set.py +++ b/lib/ansible/modules/database/postgresql/postgresql_set.py @@ -167,7 +167,7 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, get_pg_version, postgres_common_argument_spec from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -271,21 +271,6 @@ def param_set(cursor, module, name, value, context): return True -def connect_to_db(module, kw, autocommit=False): - try: - db_connection = psycopg2.connect(**kw) - if autocommit: - db_connection.set_session(autocommit=True) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least version 8.4 to support sslrootcert') - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - return db_connection - # =========================================== # Module execution. # @@ -357,8 +342,7 @@ def main(): cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) # Check server version (needs 9.4 or later): - cursor.execute("select current_setting('server_version_num')") - ver = int(cursor.fetchone()[0]) + ver = get_pg_version(cursor) if ver < PG_REQ_VER: module.warn("PostgreSQL is %s version but %s or later is required" % (ver, PG_REQ_VER)) kw = dict( diff --git a/lib/ansible/modules/database/postgresql/postgresql_slot.py b/lib/ansible/modules/database/postgresql/postgresql_slot.py index 7ba6ab26f6..30b6c4cdf0 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_slot.py +++ b/lib/ansible/modules/database/postgresql/postgresql_slot.py @@ -149,37 +149,10 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, get_pg_version, postgres_common_argument_spec from ansible.module_utils._text import to_native -def connect_to_db(module, kw, autocommit=False): - try: - db_connection = psycopg2.connect(**kw) - if autocommit: - if psycopg2.__version__ >= '2.4.2': - db_connection.set_session(autocommit=True) - else: - db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least ' - 'version 8.4 to support sslrootcert') - - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - return db_connection - - -def get_pg_version(cursor): - cursor.execute("select current_setting('server_version_num')") - return int(cursor.fetchone()[0]) - - # =========================================== # PostgreSQL module specific support methods. # diff --git a/lib/ansible/modules/database/postgresql/postgresql_table.py b/lib/ansible/modules/database/postgresql/postgresql_table.py index 70edaa3503..dab7b16d3b 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_table.py +++ b/lib/ansible/modules/database/postgresql/postgresql_table.py @@ -218,7 +218,7 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError, pg_quote_identifier -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils._text import to_native from ansible.module_utils.six import iteritems @@ -525,15 +525,8 @@ def main(): if psycopg2.__version__ < '2.4.3' and sslrootcert is not None: module.fail_json(msg='psycopg2 must be at least 2.4.3 in order to user the ca_cert parameter') - try: - db_connection = psycopg2.connect(**kw) - cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least version 8.4 to support sslrootcert') - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) + db_connection = connect_to_db(module, kw, autocommit=False) + cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor) if session_role: try: diff --git a/lib/ansible/modules/database/postgresql/postgresql_tablespace.py b/lib/ansible/modules/database/postgresql/postgresql_tablespace.py index 30cd23a2eb..b771967bad 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_tablespace.py +++ b/lib/ansible/modules/database/postgresql/postgresql_tablespace.py @@ -177,33 +177,11 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.database import SQLParseError, pg_quote_identifier -from ansible.module_utils.postgres import postgres_common_argument_spec +from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec from ansible.module_utils._text import to_native from ansible.module_utils.six import iteritems -def connect_to_db(module, kw, autocommit=False): - try: - db_connection = psycopg2.connect(**kw) - if autocommit: - if psycopg2.__version__ >= '2.4.2': - db_connection.set_session(autocommit=True) - else: - db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - - except TypeError as e: - if 'sslrootcert' in e.args[0]: - module.fail_json(msg='Postgresql server must be at least ' - 'version 8.4 to support sslrootcert') - - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) - - return db_connection - - class PgTablespace(object): def __init__(self, module, cursor, name): self.module = module