]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Adding more config options to lib/dpx_puppettools/config.py and testing them
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Feb 2023 16:52:19 +0000 (17:52 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Feb 2023 16:52:19 +0000 (17:52 +0100)
lib/dpx_puppettools/config.py
test/test-config/puppet-tools.yaml [new file with mode: 0644]
test/test_10_config.py

index 75a198dd80f1c2efc7f681f26db2754e6717d04b..23b1950491f0c8aee4211f0e8e61d796fd38abac 100644 (file)
@@ -16,6 +16,8 @@ import os
 import copy
 import re
 
+from pathlib import Path
+
 # Third party modules
 
 from fb_tools.common import is_sequence, to_bool
@@ -27,11 +29,12 @@ from fb_tools import MailAddress
 # Own modules
 
 from . import __version__ as GLOBAL_VERSION
+from . import pp
 from .errors import PuppetToolsError
 from .xlate import XLATOR
 
 CONFIG_DIR = 'pixelpark'
-__version__ = '0.2.1'
+__version__ = '0.3.0'
 LOG = logging.getLogger(__name__)
 VALID_MAIL_METHODS = ('smtp', 'sendmail')
 DEFAULT_DOMAIN = 'pixelpark.com'
@@ -87,6 +90,10 @@ class DpxPuppetConfig(BaseMultiConfig):
 
     re_whitespace = re.compile(r'(?:[,;]+|\s*[,;]*\s+)+')
 
+    default_log_dir = Path('/var/log/dpx-puppet-tools')
+    default_var_dir = Path('/var/lib/dpx-puppet-tools')
+    default_deploy_root_dir = Path('/etc/puppetlabs/code')
+
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
@@ -102,6 +109,10 @@ class DpxPuppetConfig(BaseMultiConfig):
         self.mail_server = self.default_mail_server
         self.smtp_port = self.default_smtp_port
 
+        self.log_dir = copy.copy(self.default_log_dir)
+        self.var_dir = copy.copy(self.default_var_dir)
+        self.deploy_root_dir = copy.copy(self.default_deploy_root_dir)
+
         add_stems = []
         if additional_stems:
             if is_sequence(additional_stems):
@@ -156,7 +167,9 @@ class DpxPuppetConfig(BaseMultiConfig):
 
         res['current_user_gecos'] = self.current_user_gecos
         res['current_user_name'] = self.current_user_name
+        res['default_deploy_root_dir'] = self.default_deploy_root_dir
         res['default_domain'] = self.default_domain
+        res['default_log_dir'] = self.default_log_dir
         res['default_mail_cc'] = self.default_mail_cc
         res['default_mail_method'] = self.default_mail_method
         res['default_mail_recipients'] = self.default_mail_recipients
@@ -165,6 +178,7 @@ class DpxPuppetConfig(BaseMultiConfig):
         res['default_mail_server'] = self.default_mail_server
         res['default_reply_to'] = self.default_reply_to
         res['default_smtp_port'] = self.default_smtp_port
+        res['default_var_dir'] = self.default_var_dir
         res['mail_cc_configured'] = self.mail_cc_configured
         res['valid_mail_methods'] = self.valid_mail_methods
 
@@ -194,6 +208,72 @@ class DpxPuppetConfig(BaseMultiConfig):
             section = self.cfg[section_name]
             return self._eval_mail(section_name, section)
 
+        if sn == 'puppet':
+            section = self.cfg[section_name]
+            return self._eval_puppet(section_name, section)
+
+    # -------------------------------------------------------------------------
+    def eval_global_section(self, section_name):
+
+        super(DpxPuppetConfig, self).eval_global_section(section_name)
+
+        if self.verbose > 1:
+            LOG.debug(_("Evaluating config section {!r} again ...").format(section_name))
+
+        config = self.cfg[section_name]
+        if self.verbose > 2:
+            LOG.debug(_("Evaluating config section {!r}:").format(section_name) + '\n' + pp(config))
+
+        re_log_dir = re.compile(r'^\s*log(?:ging)?[_-]*dir\s*$', re.IGNORECASE)
+        re_var_dir = re.compile(r'^\s*var[_-]*dir\s*$', re.IGNORECASE)
+
+        for key in config.keys():
+            value = config[key].strip()
+
+            if re_log_dir.match(key):
+                log_dir = Path(value)
+                if log_dir.is_absolute():
+                    self.log_dir = log_dir
+                else:
+                    msg = _("The path to the logging directory {!r} must be absolute.").format(
+                        str(log_dir))
+                    LOG.warn(msg)
+                continue
+
+            if re_var_dir.match(key):
+                var_dir = Path(value)
+                if var_dir.is_absolute():
+                    self.var_dir = var_dir
+                else:
+                    msg = _("The path to the var directory {!r} must be absolute.").format(
+                        str(var_dir))
+                    LOG.warn(msg)
+                continue
+
+    # -------------------------------------------------------------------------
+    def _eval_puppet(self, section_name, section):
+
+        if self.verbose > 2:
+            msg = _("Evaluating config section {!r}:").format(section_name)
+            LOG.debug(msg + '\n' + pp(section))
+
+        re_deploy_root_dir = re.compile(
+            r'^\s*deploy[_-]*root(?:[_-]*dir)?\s*$', re.IGNORECASE)
+
+        for key in section.keys():
+            value = section[key].strip()
+
+            if re_deploy_root_dir.match(key):
+                deploy_root_dir = Path(value)
+                if deploy_root_dir.is_absolute():
+                    self.deploy_root_dir = deploy_root_dir
+                else:
+                    msg = _(
+                        "The path to the root deployment directory {!r} "
+                        "must be absolute.").format(str(deploy_root_dir))
+                    LOG.warn(msg)
+                continue
+
     # -------------------------------------------------------------------------
     def _eval_mail(self, section_name, section):
 
diff --git a/test/test-config/puppet-tools.yaml b/test/test-config/puppet-tools.yaml
new file mode 100644 (file)
index 0000000..c0afc30
--- /dev/null
@@ -0,0 +1,11 @@
+---
+section1:
+  uhu: banane
+
+common:
+  logdir: /var/log/puppet-tools
+  var_dir: /var/lib/webhooks
+
+puppet:
+  deploy_root: /etc/puppetlabs/filesdir
+
index b7b87a9cbde3e688918c7c65418fbb4d7e74d30e..c3204d1a96c18cf34a91194fa5aca935f9ad9d85 100755 (executable)
@@ -27,6 +27,7 @@ sys.path.insert(0, libdir)
 from general import DpxPuppetToolsTestcase, get_arg_verbose, init_root_logger
 
 # from fb_tools.common import pp, to_str, is_sequence
+from fb_tools.common import pp
 
 LOG = logging.getLogger('test_config')
 
@@ -74,7 +75,47 @@ class TestConfig(DpxPuppetToolsTestcase):
             verbose=self.verbose,
         )
         LOG.debug("DpxPuppetConfig %%r: %r", cfg)
-        LOG.debug("DpxPuppetConfig %%s: %s", str(cfg))
+        LOG.debug("DpxPuppetConfig %%s:\n%s", str(cfg))
+
+    # -------------------------------------------------------------------------
+    def test_read_config(self):
+
+        LOG.info("Testing reading config with a DpxPuppetConfig object.")
+
+        from dpx_puppettools.config import DpxPuppetConfig
+
+        cfg = DpxPuppetConfig(
+            appname=self.appname,
+            additional_cfgdirs=self.test_cfg_dir, additional_stems='test',
+            verbose=self.verbose,
+        )
+        if self.verbose >= 2:
+            LOG.debug("Current configuration directories:\n{}".format(pp(cfg.config_dirs)))
+        cfg.read()
+
+        if self.verbose >= 2:
+            LOG.debug("Current configuration files:\n{}".format(pp(cfg.config_files)))
+        LOG.info('Read config:\n' + pp(cfg.cfg))
+
+        if self.verbose > 1:
+            LOG.debug("Read raw configs:\n" + pp(cfg.configs_raw))
+
+        cfg.eval()
+
+        test_dir_pairs = (
+            ('log_dir', '/var/log/puppet-tools'),
+            ('var_dir', '/var/lib/webhooks'),
+            ('deploy_root_dir', '/etc/puppetlabs/filesdir'),
+        )
+
+        for pair in test_dir_pairs:
+            prop_name = pair[0]
+            expected = Path(pair[1])
+            LOG.debug("Expecting cfg.{k}: {exp!r}.".format(k=prop_name, exp=expected))
+            value_got = getattr(cfg, prop_name)
+            LOG.debug("Got cfg.{k}: {val!r}.".format(k=prop_name, val=value_got))
+            self.assertIsInstance(value_got, Path)
+            self.assertEqual(value_got, expected)
 
 
 # =============================================================================
@@ -91,6 +132,7 @@ if __name__ == '__main__':
 
     suite.addTest(TestConfig('test_import', verbose))
     suite.addTest(TestConfig('test_object', verbose))
+    suite.addTest(TestConfig('test_read_config', verbose))
 
     runner = unittest.TextTestRunner(verbosity=verbose)