From: Frank Brehm Date: Tue, 21 Aug 2018 13:29:55 +0000 (+0200) Subject: Starting with requesting Puppetforge for latest version of a module X-Git-Tag: 0.1.2~6^2~10^2^2~9 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=79b6c3a2a74660d1759280f4cc8f7d80fa7026c0;p=pixelpark%2Fadmin-tools.git Starting with requesting Puppetforge for latest version of a module --- diff --git a/pp_lib/check_puppet_env_app.py b/pp_lib/check_puppet_env_app.py index 6c8ebb0..3fed85b 100644 --- a/pp_lib/check_puppet_env_app.py +++ b/pp_lib/check_puppet_env_app.py @@ -20,10 +20,12 @@ import pwd import sys import glob import datetime +import warnings # Third party modules import six import yaml +import requests from six import StringIO from six.moves import configparser @@ -41,7 +43,7 @@ from .merge import merge_structure from .app import PpApplication -__version__ = '0.5.2' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) @@ -70,6 +72,9 @@ class CheckPuppetEnvApp(PpApplication): dev_null = os.sep + os.path.join('dev', 'null') + default_forge_uri = 'https://forgeapi.puppet.com/v3/modules' + default_http_timeout = 30 + # ------------------------------------------------------------------------- def __init__( self, appname=None, verbose=0, version=__version__, base_dir=None, @@ -90,6 +95,8 @@ class CheckPuppetEnvApp(PpApplication): self.dependencies = [] self.rev_dep = {} self.no_write = False + self.forge_uri = self.default_forge_uri + self.http_timeout = self.default_http_timeout super(CheckPuppetEnvApp, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, @@ -158,6 +165,20 @@ class CheckPuppetEnvApp(PpApplication): help="Do not generate output files.", ) + self.arg_parser.add_argument( + '--forge-uri', metavar="URL", dest='forge_uri', + help="URL of the Puppetforge-API-Server, default: {!r}".format( + self.default_forge_uri) + ) + + self.arg_parser.add_argument( + '-T', '--timeout', '--http-timeout', + metavar="SECS", dest='http_timeout', type=int, + help=( + "Timeout for requesting current version of a module from Puppetforge, " + "default: {} seconds.").format(self.default_http_timeout) + ) + # ------------------------------------------------------------------------- def perform_arg_parser(self): @@ -188,6 +209,11 @@ class CheckPuppetEnvApp(PpApplication): self.puppet_root_env_dir = puppet_root_env_dir + if self.args.forge_uri: + self.forge_uri = self.args.forge_uri + if self.args.http_timeout: + self.http_timeout = self.args.http_timeout + self._init_puppet_environments() self.env_name = self.args.env self.env_dir = os.path.join(self.puppet_root_env_dir, self.env_name) @@ -572,10 +598,50 @@ class CheckPuppetEnvApp(PpApplication): if module_info: base_name = module_info['base_name'] self.modules[base_name] = module_info + upstream_version = self.get_upstream_version(module_info) if self.verbose > 2: LOG.debug("Found module information:\n{}".format(pp(self.modules))) + # ------------------------------------------------------------------------- + def get_upstream_version(self, module_info): + + version = None + + url = "{url}/{user}-{module}".format( + url=self.forge_uri, user=module_info['vendor'], module=module_info['base_name']) + + LOG.debug(( + "Trying to get current version of module {user}-{module} from Puppet forge.").format( + user=module_info['vendor'], module=module_info['base_name'])) + if self.verbose > 2: + LOG.debug("URL to request: {}".format(url)) + + session = requests.Session() + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + response = session.request('GET', url, timeout=self.http_timeout) + if w: + warn_class = w[-1].category.__name__ + warn_msg = '{}: {}'.format( + warn_class, w[-1].message) + if warn_class == 'SubjectAltNameWarning': + LOG.debug(warn_msg) + else: + LOG.warn(warn_msg) + + LOG.debug("Got status code: {}.".format(response.status_code)) + if response.ok: + if response.text: + msg = "Output:\n{}".format(response.text) + else: + msg = "No output." + LOG.info(msg) + #self.print_out(msg) + return version + + return version + # ------------------------------------------------------------------------- def get_module_info(self, module_dir):