import json
import re
import glob
+import warnings
+import datetime
# Third party modules
import six
def run(self):
"""Main routine."""
- msg = "Here I go again ..."
- LOG.info(msg)
+ dt = datetime.datetime.now().isoformat(' ')
+ msg = "[{}]: Here I go again ...".format(dt)
self.print_out(msg)
+ print()
self.check_data_dir()
self.init_puppet_environments()
self.collect_local_modules()
+ print()
+ dt = datetime.datetime.now().isoformat(' ')
+ msg = "[{}]: Finished.".format(dt)
+ self.print_out(msg)
+
# -------------------------------------------------------------------------
def collect_local_modules(self):
self.read_puppetfile(env)
self.read_metadata_files(env)
+ self.get_upstream_versions()
+
if self.verbose > 2:
LOG.debug("Found modules:\n{}".format(pp(self.modules)))
+ # -------------------------------------------------------------------------
+ def get_upstream_versions(self):
+
+ print()
+ msg = "Collecting module information from upstream ..."
+ self.print_out(msg)
+
+ for full_name in self.modules.keys():
+ self.get_upstream_version(full_name)
+ if not self.verbose:
+ if self.modules[full_name].version_upstream:
+ print('.', end='', flush=True)
+ else:
+ print('~', end='', flush=True)
+ if not self.verbose:
+ print()
+
+ # -------------------------------------------------------------------------
+ def get_upstream_version(self, full_name):
+
+ module_info = self.modules[full_name]
+ url = "{url}/{name}".format(url=self.forge_uri, name=full_name)
+
+ LOG.debug(
+ "Trying to get current version of module {} from Puppet forge.".format(full_name))
+ if self.verbose > 1:
+ 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 not response.ok:
+ LOG.debug("Did not found module {} on Puppet forge.".format(full_name))
+ return
+
+ if not response.text:
+ LOG.warn("No output for URL {!r}".format(url))
+ return None
+ if self.verbose > 2:
+ msg = "Output:\n{}".format(response.text)
+ LOG.debug(msg)
+
+ version = None
+
+ js_info = response.json()
+ if 'current_release' in js_info and js_info['current_release']:
+ if 'version' in js_info['current_release']:
+ version = js_info['current_release']['version']
+ else:
+ msg = "Did not found version of current_release of module {}.".format(full_name)
+ LOG.warn(msg)
+ else:
+ msg = "Did not found current_release of module {}.".format(full_name)
+ LOG.warn(msg)
+
+ LOG.debug("Current version of module {name} is {version}.".format(
+ name=full_name, version=version))
+
+ if version:
+ module_info._version_upstream = version
+
# -------------------------------------------------------------------------
def read_metadata_files(self, env):
+ LOG.info("Reading metadata files of environment {!r} ...".format(env))
+
pattern = os.path.join(self.puppet_root_env_dir, env, 'modules', '*')
if self.verbose > 2:
LOG.debug("Globbing pattern for module directories: {!r}".format(pattern))