import copy
import re
+from pathlib import Path
+
# Third party modules
from fb_tools.common import is_sequence, to_bool
# 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'
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,
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):
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
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
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):
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')
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)
# =============================================================================
suite.addTest(TestConfig('test_import', verbose))
suite.addTest(TestConfig('test_object', verbose))
+ suite.addTest(TestConfig('test_read_config', verbose))
runner = unittest.TextTestRunner(verbosity=verbose)