From e1f3f442a7c73ef50aac764a91100a5f1ec6ca75 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 15 Aug 2018 10:50:56 +0200 Subject: [PATCH] Adding bin/check-puppet-env and pp_lib/check_puppet_env_app.py --- bin/check-puppet-env | 42 ++++++++ pp_lib/check_puppet_env_app.py | 184 +++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100755 bin/check-puppet-env create mode 100644 pp_lib/check_puppet_env_app.py diff --git a/bin/check-puppet-env b/bin/check-puppet-env new file mode 100755 index 0000000..641a3c0 --- /dev/null +++ b/bin/check-puppet-env @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# Standard modules +import sys +import os +import logging +import locale + +# own modules: +cur_dir = os.getcwd() +base_dir = cur_dir + +if sys.argv[0] != '' and sys.argv[0] != '-c': + bin_dir = os.path.dirname(os.path.realpath(sys.argv[0])) +else: + bin_dir = os.path.dirname(os.path.realpath(__file__)) +base_dir = os.path.abspath(os.path.join(bin_dir, '..')) +module_dir = os.path.join(base_dir, 'pp_lib') +if os.path.exists(module_dir): + sys.path.insert(0, base_dir) + +from pp_lib.check_puppet_env_app import CheckPuppetEnvApp + +log = logging.getLogger(__name__) + +__author__ = 'Frank Brehm ' +__copyright__ = '(C) 2018 by Frank Brehm, Pixelpark GmbH, Berlin' + +appname = os.path.basename(sys.argv[0]) + +locale.setlocale(locale.LC_ALL, '') + +app = CheckPuppetEnvApp(appname=appname, base_dir=base_dir) + +if app.verbose > 2: + print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) + +app() + +sys.exit(0) + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list diff --git a/pp_lib/check_puppet_env_app.py b/pp_lib/check_puppet_env_app.py new file mode 100644 index 0000000..9c8e495 --- /dev/null +++ b/pp_lib/check_puppet_env_app.py @@ -0,0 +1,184 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2018 by Frank Brehm, Berlin +@summary: The module the check-puppet-env application +""" +from __future__ import absolute_import + +# Standard modules +import os +import logging +import logging.config +import re +import copy +import json +import socket +import pwd +import pipes +import codecs +import ipaddress + +# Third party modules +import six + +from six import StringIO +from six.moves import configparser + +from configparser import Error as ConfigParseError + +# Own modules +from .global_version import __version__ as __global_version__ + +from .errors import PpAppError + +from .common import pp, to_bool, RE_DOT_AT_END + +from .merge import merge_structure + +from .app import PpApplication + +__version__ = '0.1.0' +LOG = logging.getLogger(__name__) + + +# ============================================================================= +class CheckPuppetEnvError(PpAppError): + """Base error class for all exceptions happened during + execution this application""" + + pass + + +# ============================================================================= +class CheckPuppetEnvApp(PpApplication): + """ + Class for the check-puppet-env application objects. + """ + + default_puppet_root_env_dir = os.sep + os.path.join('etc', 'puppetlabs', 'code', 'environments') + + # ------------------------------------------------------------------------- + def __init__( + self, appname=None, verbose=0, version=__version__, base_dir=None, + initialized=None, usage=None, description=None, + argparse_epilog=None, argparse_prefix_chars='-', env_prefix=None, + puppet_root_env_dir=None, out_dir=None, + ): + + self.puppet_root_env_dir = puppet_root_env_dir + if not self.puppet_root_env_dir: + self.puppet_root_env_dir = self.default_puppet_root_env_dir + self.out_dir = None + self.environments = [] + self.env_name = None + + super(CheckPuppetEnvApp, self).__init__( + appname=appname, verbose=verbose, version=version, base_dir=base_dir, + initialized=False, usage=usage, description=description, + argparse_epilog=argparse_epilog, argparse_prefix_chars=argparse_prefix_chars, + env_prefix=env_prefix, + ) + + self.initialized = False + + if out_dir: + self.out_dir = out_dir + else: + self.out_dir = os.path.join(self.base_dir, 'tmp') + + self.post_init() + +# # ------------------------------------------------------------------------- +# def as_dict(self, short=True): +# """ +# Transforms the elements of the object into a dict +# +# @param short: don't include local properties in resulting dict. +# @type short: bool +# +# @return: structure as dict +# @rtype: dict +# """ +# +# res = super(CheckPuppetEnvApp, self).as_dict(short=short) +# res['cfg_dir'] = self.cfg_dir +# +# return res +# + # ------------------------------------------------------------------------- + def init_arg_parser(self): + """ + Method to initiate the argument parser. + + This method should be explicitely called by all init_arg_parser() + methods in descendant classes. + """ + + self.arg_parser.add_argument( + '-D', '--env-dir', metavar="DIRECTORY", dest="env_dir", + help="Parent directory of all puppet environments, default: {!r}".format( + self.puppet_root_env_dir) + ) + + self.arg_parser.add_argument( + '-E', '--env', '--environment', + dest="env", required=True, metavar="ENVIRONMENT", + help="The Puppet environment to analyze." + ) + + self.arg_parser.add_argument( + '-O', '--out', '--output-dir', + metavar="DIRECTORY", dest="out_dir", + help="Output directory of all analyzing results, default: {!r}".format( + os.path.join(self.base_dir, 'tmp')) + ) + + # ------------------------------------------------------------------------- + def perform_arg_parser(self): + + if self.args.env_dir: + self.puppet_root_env_dir = self.args.env_dir + + self.env_name = self.args.env + + if self.args.out_dir: + self.out_dir = self.args.out_dir + + # ------------------------------------------------------------------------- + def post_init(self): + """ + Method to execute before calling run(). Here could be done some + finishing actions after reading in commandline parameters, + configuration a.s.o. + + This method could be overwritten by descendant classes, these + methhods should allways include a call to post_init() of the + parent class. + + """ + + self.perform_arg_parser() + self.init_logging() + self.initialized = True + + # ------------------------------------------------------------------------- + def _run(self): + """ + Main application routine. + """ + + LOG.info("I'm walkin, yes indeed, I'm walkin ...") + + +# ============================================================================= + +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5