diff --git a/plugins/module_utils/tripleo_repos/get_hash/__main__.py b/plugins/module_utils/tripleo_repos/get_hash/__main__.py index e99033f..4ae61a1 100644 --- a/plugins/module_utils/tripleo_repos/get_hash/__main__.py +++ b/plugins/module_utils/tripleo_repos/get_hash/__main__.py @@ -17,6 +17,7 @@ import argparse import logging import sys +from tripleo_repos.utils import load_logging from tripleo_repos.get_hash.tripleo_hash_info import TripleOHashInfo import tripleo_repos.get_hash.exceptions as exc @@ -31,7 +32,7 @@ def _validate_args(parsed_args): def main(): - TripleOHashInfo.load_logging() + load_logging() config = TripleOHashInfo.load_config() parser = argparse.ArgumentParser(description='tripleo-get-hash.py') parser.add_argument( diff --git a/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py b/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py index af4cbd7..71edb2c 100644 --- a/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py +++ b/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py @@ -16,47 +16,17 @@ from __future__ import (absolute_import, division, print_function) import logging -import sys import os from .constants import CONFIG_PATH, CONFIG_KEYS, DEFAULT_CONFIG from .exceptions import ( TripleOHashInvalidConfig, TripleOHashInvalidDLRNResponse ) -# portable http_get that uses either ansible recommended way or python native -# urllib. Also deals with python2 vs python3 for centos7 train jobs. -py_version = sys.version_info.major -if py_version < 3: - import urllib2 +try: + from tripleo_repos.utils import http_get +except ImportError: + from ansible_collections.tripleo.repos.plugins.module_utils. \ + tripleo_repos.utils import http_get - def http_get(url): - try: - response = urllib2.urlopen(url) - return ( - response.read().decode('utf-8'), - int(response.code)) - except Exception as e: - return (str(e), -1) -else: - try: - from ansible.module_utils.urls import open_url - - def http_get(url): - try: - response = open_url(url, method='GET') - return (response.read(), response.status) - except Exception as e: - return (str(e), -1) - except ImportError: - from urllib.request import urlopen - - def http_get(url): - try: - response = urlopen(url) - return ( - response.read().decode('utf-8'), - int(response.status)) - except Exception as e: - return (str(e), -1) __metaclass__ = type @@ -77,37 +47,6 @@ class TripleOHashInfo: import yaml return yaml.safe_load(filename) - @classmethod - def load_logging(cls): - """ - This is a class method since we call it from the CLI entrypoint - before the TripleOHashInfo object is created. Default is to add - logging.INFO level logging. - """ - logger = logging.getLogger() - # Only add logger once to avoid duplicated streams in tests - if not logger.handlers: - stdout_handlers = [ - _handler - for _handler in logger.handlers - if - ( - hasattr(_handler, 'stream') and 'stdout' in - _handler.stream.name - ) - ] - if stdout_handlers == []: - formatter = logging.Formatter( - ( - "%(asctime)s - tripleo-get-hash - %(levelname)s - " - "%(message)s" - ) - ) - handler = logging.StreamHandler(sys.stdout) - handler.setFormatter(formatter) - logger.addHandler(handler) - logger.setLevel(logging.INFO) - @classmethod def _resolve_local_config_path(cls): """ Load local config from disk from expected locations. """ diff --git a/plugins/module_utils/tripleo_repos/utils.py b/plugins/module_utils/tripleo_repos/utils.py new file mode 100644 index 0000000..8226929 --- /dev/null +++ b/plugins/module_utils/tripleo_repos/utils.py @@ -0,0 +1,83 @@ +# Copyright 2021 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +from __future__ import (absolute_import, division, print_function) + +import logging +import sys + +__metaclass__ = type + +# portable http_get that uses either ansible recommended way or python native +# urllib. Also deals with python2 vs python3 for centos7 train jobs. +py_version = sys.version_info.major +if py_version < 3: + import urllib2 + + def http_get(url): + try: + response = urllib2.urlopen(url) + return ( + response.read().decode('utf-8'), + int(response.code)) + except Exception as e: + return (str(e), -1) +else: + try: + from ansible.module_utils.urls import open_url + + def http_get(url): + try: + response = open_url(url, method='GET') + return (response.read(), response.status) + except Exception as e: + return (str(e), -1) + except ImportError: + from urllib.request import urlopen + + def http_get(url): + try: + response = urlopen(url) + return ( + response.read().decode('utf-8'), + int(response.status)) + except Exception as e: + return (str(e), -1) + + +def load_logging(level=logging.INFO): + """Load and set logging level. Default is set to logging.INFO level.""" + logger = logging.getLogger() + # Only add logger once to avoid duplicated streams in tests + if not logger.handlers: + stdout_handlers = [ + _handler + for _handler in logger.handlers + if + ( + hasattr(_handler, 'stream') and 'stdout' in + _handler.stream.name + ) + ] + if stdout_handlers == []: + formatter = logging.Formatter( + ( + "%(asctime)s - tripleo-get-hash - %(levelname)s - " + "%(message)s" + ) + ) + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(formatter) + logger.addHandler(handler) + logger.setLevel(level) diff --git a/plugins/module_utils/tripleo_repos/yum_config/__main__.py b/plugins/module_utils/tripleo_repos/yum_config/__main__.py index 01b1131..4e87634 100644 --- a/plugins/module_utils/tripleo_repos/yum_config/__main__.py +++ b/plugins/module_utils/tripleo_repos/yum_config/__main__.py @@ -17,6 +17,7 @@ import argparse import logging import sys +from tripleo_repos.utils import load_logging import tripleo_repos.yum_config.constants as const import tripleo_repos.yum_config.yum_config as cfg import tripleo_repos.yum_config.utils as utils @@ -37,7 +38,7 @@ def options_to_dict(options): def main(): - cfg.TripleOYumConfig.load_logging() + load_logging() # Get release model and version distro, major_version, __ = utils.get_distro_info() py_version = sys.version_info.major diff --git a/plugins/module_utils/tripleo_repos/yum_config/yum_config.py b/plugins/module_utils/tripleo_repos/yum_config/yum_config.py index 675d0c4..aa1bf90 100644 --- a/plugins/module_utils/tripleo_repos/yum_config/yum_config.py +++ b/plugins/module_utils/tripleo_repos/yum_config/yum_config.py @@ -114,34 +114,6 @@ class TripleOYumConfig: provided to the class constructor. """ - @classmethod - def load_logging(cls): - """ - This is a class method since we call it from the CLI entrypoint - before any object is created. Default is to add logging.INFO level - logging. - """ - logger = logging.getLogger() - # Only add logger once to avoid duplicated streams in tests - if not logger.handlers: - stdout_handlers = [ - _handler - for _handler in logger.handlers - if (hasattr(_handler, 'stream') and 'stdout' - in _handler.stream.name) - ] - if not stdout_handlers: - formatter = logging.Formatter( - ( - "%(asctime)s - tripleo-yum-config - %(levelname)s - " - "%(message)s" - ) - ) - handler = logging.StreamHandler(sys.stdout) - handler.setFormatter(formatter) - logger.addHandler(handler) - logger.setLevel(logging.INFO) - def __init__(self, valid_options=None, dir_path=None, file_extension=None, environment_file=None): """ diff --git a/tests/sanity/ignore.txt b/tests/sanity/ignore.txt index 4607a25..c1897b3 100644 --- a/tests/sanity/ignore.txt +++ b/tests/sanity/ignore.txt @@ -1,3 +1,3 @@ -plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py replace-urlopen -plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py pylint:ansible-bad-import +plugins/module_utils/tripleo_repos/utils.py replace-urlopen +plugins/module_utils/tripleo_repos/utils.py pylint:ansible-bad-import plugins/module_utils/tripleo_repos/yum_config/compose_repos.py replace-urlopen