From f0dc21ec0c72aeec58dc0f47edf74d4ca223f03d Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 11 Sep 2017 09:06:47 +0200 Subject: [PATCH] __cdist_log_level=; __cdist_log_level_name= (#574) --- .../conf/type/__install_stage/gencode-remote | 18 +++--- cdist/configuration.py | 29 +++++++++- cdist/core/__init__.py | 2 +- cdist/core/code.py | 2 + cdist/core/explorer.py | 2 + cdist/core/manifest.py | 2 + cdist/core/util.py | 4 ++ cdist/emulator.py | 3 +- cdist/test/code/__init__.py | 8 ++- .../type/__dump_environment/gencode-local | 1 + cdist/test/configuration/__init__.py | 55 +++++++++++++++++-- cdist/test/emulator/__init__.py | 2 +- cdist/test/explorer/__init__.py | 5 +- .../conf/type/__dump_env/explorer/dump | 1 + cdist/test/manifest/__init__.py | 13 ++++- .../fixtures/conf/manifest/dump_environment | 1 + .../conf/type/__dump_environment/manifest | 1 + docs/changelog | 11 ++-- docs/src/cdist-reference.rst.sh | 47 ++++++++++++++-- docs/src/cdist-type.rst | 25 ++++++++- 20 files changed, 192 insertions(+), 40 deletions(-) diff --git a/cdist/conf/type/__install_stage/gencode-remote b/cdist/conf/type/__install_stage/gencode-remote index 4d013e3d..776e9fd5 100755 --- a/cdist/conf/type/__install_stage/gencode-remote +++ b/cdist/conf/type/__install_stage/gencode-remote @@ -22,16 +22,14 @@ uri="$(cat "$__object/parameter/uri" 2>/dev/null \ || echo "$__object_id")" target="$(cat "$__object/parameter/target")" -case "$__cdist_log_level" in - DEBUG|TRACE) - curl="curl" - tar="tar -xvzp" - ;; - *) - curl="curl -s" - tar="tar -xzp" - ;; -esac +if [ "$__cdist_log_level" -le "10" ] +then + curl="curl" + tar="tar -xvzp" +else + curl="curl -s" + tar="tar -xzp" +fi if [ -f "$__object/parameter/insecure" ] ; then curl="$curl -k" diff --git a/cdist/configuration.py b/cdist/configuration.py index 30176200..4a8274d8 100644 --- a/cdist/configuration.py +++ b/cdist/configuration.py @@ -26,6 +26,7 @@ import cdist import cdist.argparse import re import multiprocessing +import logging class Singleton(type): @@ -215,6 +216,24 @@ class ArchivingOption(SelectOption): return val +class LogLevelOption(OptionBase): + def __init__(self): + super().__init__('__cdist_log_level') + + def converter(self): + def log_level_converter(val): + try: + val = logging.getLevelName(int(val)) + return self.translate(val) + except (ValueError, AttributeError): + raise ValueError("Invalid {} value: {}.".format( + self.name, val)) + return log_level_converter + + def translate(self, val): + return VerbosityOption().translate(val) + + _ARG_OPTION_MAPPING = { 'beta': 'beta', 'cache_path_pattern': 'cache_path_pattern', @@ -281,6 +300,9 @@ class Configuration(metaclass=Singleton): '__cdist_log_level': 'verbosity', } ENV_VAR_BOOLEAN_OPTIONS = ('CDIST_BETA', ) + ENV_VAR_OPTIONS = { + '__cdist_log_level': LogLevelOption(), + } ARG_OPTION_MAPPING = _ARG_OPTION_MAPPING ADJUST_ARG_OPTION_MAPPING = { @@ -366,8 +388,11 @@ class Configuration(metaclass=Singleton): if option in self.ENV_VAR_BOOLEAN_OPTIONS: d[dst_opt] = True else: - option_object = self.CONFIG_FILE_OPTIONS[section][dst_opt] - converter = option_object.converter() + if option in self.ENV_VAR_OPTIONS: + opt = self.ENV_VAR_OPTIONS[option] + else: + opt = self.CONFIG_FILE_OPTIONS[section][dst_opt] + converter = opt.converter() val = env[option] newval = converter(val) d[dst_opt] = newval diff --git a/cdist/core/__init__.py b/cdist/core/__init__.py index adec60b2..179c1a29 100644 --- a/cdist/core/__init__.py +++ b/cdist/core/__init__.py @@ -28,4 +28,4 @@ from cdist.core.explorer import Explorer from cdist.core.manifest import Manifest from cdist.core.code import Code from cdist.core.util import listdir -from cdist.core.util import log_level_env_var_val +from cdist.core.util import log_level_env_var_val, log_level_name_env_var_val diff --git a/cdist/core/code.py b/cdist/core/code.py index 5ada4c14..ad02e199 100644 --- a/cdist/core/code.py +++ b/cdist/core/code.py @@ -109,6 +109,8 @@ class Code(object): '__files': self.local.files_path, '__target_host_tags': self.local.target_host_tags, '__cdist_log_level': util.log_level_env_var_val(local.log), + '__cdist_log_level_name': util.log_level_name_env_var_val( + local.log), } def _run_gencode(self, cdist_object, which): diff --git a/cdist/core/explorer.py b/cdist/core/explorer.py index 8d7cd593..c4708eac 100644 --- a/cdist/core/explorer.py +++ b/cdist/core/explorer.py @@ -80,6 +80,8 @@ class Explorer(object): '__explorer': self.remote.global_explorer_path, '__target_host_tags': self.local.target_host_tags, '__cdist_log_level': util.log_level_env_var_val(self.log), + '__cdist_log_level_name': util.log_level_name_env_var_val( + self.log), } self._type_explorers_transferred = [] self.jobs = jobs diff --git a/cdist/core/manifest.py b/cdist/core/manifest.py index e69db63f..82e9f780 100644 --- a/cdist/core/manifest.py +++ b/cdist/core/manifest.py @@ -113,6 +113,8 @@ class Manifest(object): '__files': self.local.files_path, '__target_host_tags': self.local.target_host_tags, '__cdist_log_level': util.log_level_env_var_val(self.log), + '__cdist_log_level_name': util.log_level_name_env_var_val( + self.log), } def _open_logger(self): diff --git a/cdist/core/util.py b/cdist/core/util.py index cce608bc..64570d34 100644 --- a/cdist/core/util.py +++ b/cdist/core/util.py @@ -38,4 +38,8 @@ def _ishidden(path): def log_level_env_var_val(log): + return str(log.getEffectiveLevel()) + + +def log_level_name_env_var_val(log): return logging.getLevelName(log.getEffectiveLevel()) diff --git a/cdist/emulator.py b/cdist/emulator.py index 0655fe9c..40b19e4b 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -112,8 +112,7 @@ class Emulator(object): if '__cdist_log_level' in self.env: try: loglevel = self.env['__cdist_log_level'] - # For a text level it returns its numerical value. - level = logging.getLevelName(loglevel) + level = int(loglevel) except ValueError: level = logging.WARNING else: diff --git a/cdist/test/code/__init__.py b/cdist/test/code/__init__.py index 4d0427aa..c1d19d33 100644 --- a/cdist/test/code/__init__.py +++ b/cdist/test/code/__init__.py @@ -101,7 +101,9 @@ class CodeTestCase(test.CdistTestCase): self.assertEqual(output_dict['__files'], self.local.files_path) self.assertEqual(output_dict['__target_host_tags'], self.local.target_host_tags) - self.assertEqual(output_dict['__cdist_log_level'], 'WARNING') + self.assertEqual(output_dict['__cdist_log_level'], + str(logging.WARNING)) + self.assertEqual(output_dict['__cdist_log_level_name'], 'WARNING') def test_run_gencode_remote_environment(self): output_string = self.code.run_gencode_remote(self.cdist_object) @@ -127,7 +129,9 @@ class CodeTestCase(test.CdistTestCase): self.assertEqual(output_dict['__files'], self.local.files_path) self.assertEqual(output_dict['__target_host_tags'], self.local.target_host_tags) - self.assertEqual(output_dict['__cdist_log_level'], 'WARNING') + self.assertEqual(output_dict['__cdist_log_level'], + str(logging.WARNING)) + self.assertEqual(output_dict['__cdist_log_level_name'], 'WARNING') def test_transfer_code_remote(self): self.cdist_object.code_remote = self.code.run_gencode_remote( diff --git a/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local b/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local index 35120f10..2829d633 100755 --- a/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local +++ b/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local @@ -11,3 +11,4 @@ echo "echo __object_name: $__object_name" echo "echo __files: $__files" echo "echo __target_host_tags: $__target_host_tags" echo "echo __cdist_log_level: $__cdist_log_level" +echo "echo __cdist_log_level_name: $__cdist_log_level_name" diff --git a/cdist/test/configuration/__init__.py b/cdist/test/configuration/__init__.py index 3630fb95..c6135317 100644 --- a/cdist/test/configuration/__init__.py +++ b/cdist/test/configuration/__init__.py @@ -27,7 +27,7 @@ import os.path as op import argparse from cdist import test import cdist.argparse as cap - +import logging my_dir = op.abspath(op.dirname(__file__)) fixtures = op.join(my_dir, 'fixtures') @@ -124,6 +124,18 @@ class ConfigurationOptionsTestCase(test.CdistTestCase): self.assertEqual(converter(value), ['spam', 'eggs', 'ham', ]) self.assertIsNone(converter('')) + def test_LogLevelOption(self): + option = cc.LogLevelOption() + converter = option.converter() + value = str(logging.DEBUG) + conv_val = converter(value) + self.assertEqual(conv_val, cap.VERBOSE_DEBUG) + value = str(logging.INFO) + conv_val = converter(value) + self.assertEqual(conv_val, cap.VERBOSE_INFO) + for value in ('11', '80', 'a'): + self.assertRaises(ValueError, converter, value) + class ConfigurationTestCase(test.CdistTestCase): @@ -234,17 +246,17 @@ class ConfigurationTestCase(test.CdistTestCase): os.remove(custom_config_file) def test_singleton(self): - x = cc.Configuration(None) + x = cc.Configuration(None, env={}, config_files=()) args = argparse.Namespace() args.a = 'a' - y = cc.Configuration(args) + y = cc.Configuration(args, env={}, config_files=()) self.assertIs(x, y) def test_non_singleton(self): - x = cc.Configuration(None, singleton=False) + x = cc.Configuration(None, env={}, config_files=(), singleton=False) args = argparse.Namespace() args.a = 'a' - y = cc.Configuration(args, singleton=False) + y = cc.Configuration(args, env={}, config_files=(), singleton=False) self.assertIsNot(x, y) def test_read_config_file(self): @@ -1122,6 +1134,39 @@ class ConfigurationTestCase(test.CdistTestCase): config_files=config_files) self.assertEqual(configuration.config, expected_config_dict) + def test_configuration_cdist_log_level_env_var(self): + env = { + '__cdist_log_level': str(logging.DEBUG), + } + args = argparse.Namespace() + + expected_config_dict = { + 'GLOBAL': { + 'verbosity': cap.VERBOSE_DEBUG, + }, + } + + # bypass singleton so we can test further + cc.Configuration.instance = None + + configuration = cc.Configuration(args, env=env, + config_files=()) + self.assertEqual(configuration.config, expected_config_dict) + + # bypass singleton so we can test further + cc.Configuration.instance = None + env['__cdist_log_level'] = '80' + with self.assertRaises(ValueError): + configuration = cc.Configuration(args, env=env, + config_files=()) + + # bypass singleton so we can test further + cc.Configuration.instance = None + env['__cdist_log_level'] = 'x' + with self.assertRaises(ValueError): + configuration = cc.Configuration(args, env=env, + config_files=()) + if __name__ == "__main__": import unittest diff --git a/cdist/test/emulator/__init__.py b/cdist/test/emulator/__init__.py index 48c5362e..6bc80e96 100644 --- a/cdist/test/emulator/__init__.py +++ b/cdist/test/emulator/__init__.py @@ -124,7 +124,7 @@ class EmulatorTestCase(test.CdistTestCase): emu = emulator.Emulator(argv, env=self.env) emu_loglevel = emu.log.getEffectiveLevel() self.assertEqual(emu_loglevel, logging.WARNING) - self.env['__cdist_log_level'] = logging.getLevelName(logging.DEBUG) + self.env['__cdist_log_level'] = str(logging.DEBUG) emu = emulator.Emulator(argv, env=self.env) emu_loglevel = emu.log.getEffectiveLevel() self.assertEqual(emu_loglevel, logging.DEBUG) diff --git a/cdist/test/explorer/__init__.py b/cdist/test/explorer/__init__.py index 48b54cd7..dcdd91f1 100644 --- a/cdist/test/explorer/__init__.py +++ b/cdist/test/explorer/__init__.py @@ -31,6 +31,7 @@ from cdist import test from cdist.exec import local from cdist.exec import remote from cdist.core import explorer +import logging import os.path as op my_dir = op.abspath(op.dirname(__file__)) @@ -233,7 +234,9 @@ class ExplorerClassTestCase(test.CdistTestCase): self.remote.global_explorer_path) self.assertEqual(output_dict['__target_host_tags'], self.local.target_host_tags) - self.assertEqual(output_dict['__cdist_log_level'], 'WARNING') + self.assertEqual(output_dict['__cdist_log_level'], + str(logging.WARNING)) + self.assertEqual(output_dict['__cdist_log_level_name'], 'WARNING') if __name__ == '__main__': diff --git a/cdist/test/explorer/fixtures/conf/type/__dump_env/explorer/dump b/cdist/test/explorer/fixtures/conf/type/__dump_env/explorer/dump index ae477cd0..3682816b 100755 --- a/cdist/test/explorer/fixtures/conf/type/__dump_env/explorer/dump +++ b/cdist/test/explorer/fixtures/conf/type/__dump_env/explorer/dump @@ -6,3 +6,4 @@ echo "__target_fqdn: $__target_fqdn" echo "__explorer: $__explorer" echo "__target_host_tags: $__target_host_tags" echo "__cdist_log_level: $__cdist_log_level" +echo "__cdist_log_level_name: $__cdist_log_level_name" diff --git a/cdist/test/manifest/__init__.py b/cdist/test/manifest/__init__.py index 5b372f35..fffa77b8 100644 --- a/cdist/test/manifest/__init__.py +++ b/cdist/test/manifest/__init__.py @@ -99,7 +99,9 @@ class ManifestTestCase(test.CdistTestCase): self.assertEqual(output_dict['__files'], self.local.files_path) self.assertEqual(output_dict['__target_host_tags'], self.local.target_host_tags) - self.assertEqual(output_dict['__cdist_log_level'], 'VERBOSE') + self.assertEqual(output_dict['__cdist_log_level'], + str(logging.VERBOSE)) + self.assertEqual(output_dict['__cdist_log_level_name'], 'VERBOSE') self.log.setLevel(old_loglevel) def test_type_manifest_environment(self): @@ -139,7 +141,9 @@ class ManifestTestCase(test.CdistTestCase): self.assertEqual(output_dict['__files'], self.local.files_path) self.assertEqual(output_dict['__target_host_tags'], self.local.target_host_tags) - self.assertEqual(output_dict['__cdist_log_level'], 'VERBOSE') + self.assertEqual(output_dict['__cdist_log_level'], + str(logging.VERBOSE)) + self.assertEqual(output_dict['__cdist_log_level_name'], 'VERBOSE') self.log.setLevel(old_loglevel) def test_loglevel_env_setup(self): @@ -147,7 +151,10 @@ class ManifestTestCase(test.CdistTestCase): self.log.setLevel(logging.DEBUG) manifest = cdist.core.manifest.Manifest(self.target_host, self.local) self.assertTrue("__cdist_log_level" in manifest.env) - self.assertEqual(manifest.env["__cdist_log_level"], 'DEBUG') + self.assertTrue("__cdist_log_level_name" in manifest.env) + self.assertEqual(manifest.env["__cdist_log_level"], + str(logging.DEBUG)) + self.assertEqual(manifest.env["__cdist_log_level_name"], 'DEBUG') self.log.setLevel(current_level) diff --git a/cdist/test/manifest/fixtures/conf/manifest/dump_environment b/cdist/test/manifest/fixtures/conf/manifest/dump_environment index adf3c3e0..8057ca42 100755 --- a/cdist/test/manifest/fixtures/conf/manifest/dump_environment +++ b/cdist/test/manifest/fixtures/conf/manifest/dump_environment @@ -11,4 +11,5 @@ __manifest: $__manifest __files: $__files __target_host_tags: $__target_host_tags __cdist_log_level: $__cdist_log_level +__cdist_log_level_name: $__cdist_log_level_name DONE diff --git a/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest b/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest index f2ae74e2..a38050f9 100755 --- a/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest +++ b/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest @@ -15,4 +15,5 @@ __object_name: $__object_name __files: $__files __target_host_tags: $__target_host_tags __cdist_log_level: $__cdist_log_level +__cdist_log_level_name: $__cdist_log_level_name DONE diff --git a/docs/changelog b/docs/changelog index 9ca5e2f2..ddf4a685 100644 --- a/docs/changelog +++ b/docs/changelog @@ -10,12 +10,13 @@ next: * Type __daemontools: Improve it on FreeBSD (Kamila Součková) * Type __package_pkg_openbsd: Fix use of --name (Philippe Gregoire) * Type __package_pkg_openbsd: Fix pkg_version explorer (Philippe Gregoire) - * Documentation: Document __cdist_loglevel type variable (Darko Poljak) - * Type __install_stage: Fix __debug -> __cdist_loglevel (Darko Poljak) - * Core, types: Make __cdist_loglevel value more expressive (Darko Poljak) - * Core: Add -l/--log-level option (Darko Poljak) - * Core: __cdist_loglevel -> __cdist_log_level and make cdist honor __cdist_log_level env var (Darko Poljak) * Type __prometheus_exporter: Fixes + go version bump (Kamila Součková) + * Core, types: __cdist_loglevel -> __cdist_log_level (Darko Poljak) + * Core, types: Add __cdist_log_level_name env var with vlaue of log level name (Darko Poljak) + * Core: Make cdist honor __cdist_log_level env var (Darko Poljak) + * Core: Add -l/--log-level option (Darko Poljak) + * Type __install_stage: Fix __debug -> __cdist_log_level (Darko Poljak) + * Documentation: Document __cdist_log_level (Darko Poljak) 4.6.1: 2017-08-30 * Type __user: Explore with /etc files (passwd, group, shadow) (Philippe Gregoire) diff --git a/docs/src/cdist-reference.rst.sh b/docs/src/cdist-reference.rst.sh index ec3216c2..59ce018b 100755 --- a/docs/src/cdist-reference.rst.sh +++ b/docs/src/cdist-reference.rst.sh @@ -198,9 +198,27 @@ Environment variables (for reading) ----------------------------------- The following environment variables are exported by cdist: -__cdist_log_level - String value of cdist log level. One of OFF, ERROR, WARNING, INFO, - VERBOSE, DEBUG and TRACE. +__cdist_log_level, __cdist_log_level_name + cdist log level value and cdist log level name. One of: + + +----------------+-----------------+ + | Log level name | Log level value | + +================+=================+ + | OFF | 60 | + +----------------+-----------------+ + | ERROR | 40 | + +----------------+-----------------+ + | WARNING | 30 | + +----------------+-----------------+ + | INFO | 20 | + +----------------+-----------------+ + | VERBOSE | 15 | + +----------------+-----------------+ + | DEBUG | 10 | + +----------------+-----------------+ + | TRACE | 5 | + +----------------+-----------------+ + Available for: initial manifest, explorer, type manifest, type explorer, type gencode. __explorer @@ -266,8 +284,27 @@ require Setup dependencies between objects (see \`cdist manifest \`_). __cdist_log_level - String value of cdist log level. One of OFF, ERROR, WARNING, INFO, - VERBOSE, DEBUG and TRACE. If set cdist will set this log level in + cdist log level value. One of: + + +----------------+-----------------+ + | Log level | Log level value | + +================+=================+ + | OFF | 60 | + +----------------+-----------------+ + | ERROR | 40 | + +----------------+-----------------+ + | WARNING | 30 | + +----------------+-----------------+ + | INFO | 20 | + +----------------+-----------------+ + | VERBOSE | 15 | + +----------------+-----------------+ + | DEBUG | 10 | + +----------------+-----------------+ + | TRACE | 5 | + +----------------+-----------------+ + + If set cdist will set this log level in accordance with configuration rules. If cdist invokation is used in types then nested cdist will honor this specified log level if not specified otherwise while invoking it. diff --git a/docs/src/cdist-type.rst b/docs/src/cdist-type.rst index dbecc409..a6587328 100644 --- a/docs/src/cdist-type.rst +++ b/docs/src/cdist-type.rst @@ -333,9 +333,28 @@ So when you generate a script with the following content, it will work: Log level in types ------------------ -cdist log level can be accessed from __cdist_log_level variable. -Value is a string, one of OFF, ERROR, WARNING, INFO, VERBOSE, DEBUG and -TRACE. It is available for initial manifest, explorer, type manifest, +cdist log level can be accessed from __cdist_log_level variable.One of: + + +----------------+-----------------+ + | Log level | Log level value | + +================+=================+ + | OFF | 60 | + +----------------+-----------------+ + | ERROR | 40 | + +----------------+-----------------+ + | WARNING | 30 | + +----------------+-----------------+ + | INFO | 20 | + +----------------+-----------------+ + | VERBOSE | 15 | + +----------------+-----------------+ + | DEBUG | 10 | + +----------------+-----------------+ + | TRACE | 5 | + +----------------+-----------------+ + + +It is available for initial manifest, explorer, type manifest, type explorer, type gencode.