From: Frank Brehm Date: Mon, 3 Sep 2018 16:17:40 +0000 (+0200) Subject: Adding and using lib/webhooks/puppetfile.py X-Git-Tag: 0.11.4^2~7 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=4aeebd4504c672f7d56d0b5e908278487f21b1b4;p=pixelpark%2Fpuppetmaster-webhooks.git Adding and using lib/webhooks/puppetfile.py --- diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index ccbf230..189da34 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -39,6 +39,8 @@ from .base_app import BaseHookError, BaseHookApp from .module_info import ModuleInfo +from .puppetfile import Puppetfile + LOG = logging.getLogger(__name__) @@ -277,6 +279,15 @@ class GetForgeModulesApp(BaseHookApp): # ------------------------------------------------------------------------- def read_puppetfile(self, env): + pfile = Puppetfile( + env_root_dir=self.puppet_root_env_dir, environment=env, + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, + initialized=True) + + if self.verbose > 0: + LOG.debug("Got Puppetfile:\n{}".format(pp(pfile.as_dict()))) + + puppetfile = os.path.join(self.puppet_root_env_dir, env, 'Puppetfile') if self.verbose > 1: LOG.debug("Searching {!r} ...".format(puppetfile)) diff --git a/lib/webhooks/module_info.py b/lib/webhooks/module_info.py index 891d199..2914df6 100644 --- a/lib/webhooks/module_info.py +++ b/lib/webhooks/module_info.py @@ -26,7 +26,7 @@ from .common import pp, to_str, to_bool, is_sequence from .obj import BaseObjectError from .obj import BaseObject -__version__ = '0.6.2' +__version__ = '0.6.3' LOG = logging.getLogger(__name__) @@ -263,7 +263,7 @@ class ModuleInfo(BaseObject): @rtype: dict """ - res = super(ModuleInfo, self).as_dict() + res = super(ModuleInfo, self).as_dict(short=short) res['name'] = self.name res['vendor'] = self.vendor diff --git a/lib/webhooks/puppetfile.py b/lib/webhooks/puppetfile.py new file mode 100644 index 0000000..5b9781e --- /dev/null +++ b/lib/webhooks/puppetfile.py @@ -0,0 +1,210 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2018 by Frank Brehm, Publicies Pixelpark GmbH, Berlin +@summary: A module for encapsulating all information about a Puppetfile + of a r10k environment +""" +from __future__ import absolute_import + +# Standard modules +import logging +import os +import re +import copy +import warnings +import time +import datetime +import pwd +import grp + +# Third party modules + +# Own modules +from .common import pp, to_str, to_bool, is_sequence + +from .obj import BaseObjectError +from .obj import BaseObject + +__version__ = '0.1.1' + +LOG = logging.getLogger(__name__) + + +# ============================================================================= +class PuppetfileError(BaseObjectError): + + pass + + + +# ============================================================================= +class Puppetfile(BaseObject): + """Class for encapsulating a Puppetfile of a r10k environment.""" + + default_environment = 'production' + default_env_root_dir = os.sep + os.path.join("etc", "puppetlabs", "code", "environments") + + # ------------------------------------------------------------------------- + def __init__( + self, env_root_dir=None, environment=None, + appname=None, verbose=0, version=__version__, base_dir=None, initialized=None): + + self._env_root_dir = self.default_env_root_dir + self._environment = self.default_environment + self._stat = None + + super(Puppetfile, self).__init__( + appname=appname, verbose=verbose, version=version, + base_dir=base_dir, initialized=False, + ) + + if initialized is not None: + self.initialized = initialized + + # ------------------------------------------------------------------------- + @property + def env_root_dir(self): + """The root directory of all puppet environments.""" + return self._env_root_dir + + @env_root_dir.setter + def env_root_dir(self, value): + if value is None: + msg = "The root directory of all puppet environments may not be None." + raise TypeError(msg) + + val = str(value) + if not os.path.isabs(val): + msg = ( + "Path {!r} for the root directory of all puppet environments must " + "be an absolute path.").format(value) + raise ValueError(msg) + self._env_root_dir = val + + # ------------------------------------------------------------------------- + @property + def environment(self): + """The r10k environment of this Puppetfile.""" + return self._environment + + @environment.setter + def environment(self, value): + if value is None: + msg = "The Puppet environment may not be None." + raise TypeError(msg) + + val = str(value).strip() + if val == '': + msg = "The Puppet environment may not be empty." + raise ValueError(msg) + + self._environment = val + + # ------------------------------------------------------------------------- + @property + def env_dir(self): + """The directory containing the r10k environmenmt.""" + edir = os.path.normpath(os.path.join(self.env_root_dir, self.environment)) + if os.path.exists(edir): + return os.path.abspath(edir) + return edir + + # ------------------------------------------------------------------------- + @property + def filename(self): + """The filename of the Puppetfile.""" + return os.path.join(self.env_dir, 'Puppetfile') + + # ------------------------------------------------------------------------- + def get_file_stat(self, forced=False): + + if self._stat is not None and not forced: + return + + if self.verbose > 2: + LOG.debug("Getting file status of {!r}.".format(self.filename)) + if not self.exists: + self._stat = None + return + + self._stat = os.stat(self.filename) + + # ------------------------------------------------------------------------- + @property + def exists(self): + """A flag, whether the Puppetfile exists.""" + return os.path.exists(self.filename) + + # ------------------------------------------------------------------------- + @property + def stat(self): + """The file status of the Puppetfile.""" + self.get_file_stat() + return self._stat + + # ------------------------------------------------------------------------- + @property + def owner(self): + """The owner of the Puppetfile as a string.""" + if self.stat is None: + return None + + owner_name = None + try: + owner_name = pwd.getpwuid(self.stat.st_uid).pw_name + except KeyError: + owner_name = "{}".format(self.stat.st_uid) + return owner_name + + # ------------------------------------------------------------------------- + @property + def group(self): + """The owning group of the Puppetfile as a string.""" + if self.stat is None: + return None + + group_name = None + try: + group_name = grp.getgrgid(self.stat.st_gid).gr_name + except KeyError: + group_name = "{}".format(self.stat.st_gid) + return group_name + + # ------------------------------------------------------------------------- + def as_dict(self, short=True): + """ + Transforms the elements of the object into a dict + + @return: structure as dict + @rtype: dict + """ + + res = super(Puppetfile, self).as_dict(short=short) + + res['default_environment'] = self.default_environment + res['default_env_root_dir'] = self.default_env_root_dir + res['env_root_dir'] = self.env_root_dir + res['environment'] = self.environment + res['env_dir'] = self.env_dir + res['filename'] = self.filename + res['exists'] = self.exists + res['stat'] = self.stat + res['owner'] = self.owner + res['group'] = self.group + + return res + + + +# ============================================================================= + +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list