__cdist_log_level=<log level int value>; __cdist_log_level_name=<log level name> (#574)
This commit is contained in:
		
					parent
					
						
							
								1ae5b1732e
							
						
					
				
			
			
				commit
				
					
						f0dc21ec0c
					
				
			
		
					 20 changed files with 192 additions and 40 deletions
				
			
		|  | @ -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) | ||||
| if [ "$__cdist_log_level" -le "10" ] | ||||
| then | ||||
|     curl="curl" | ||||
|     tar="tar -xvzp" | ||||
|     ;; | ||||
|     *) | ||||
| else | ||||
|     curl="curl -s" | ||||
|     tar="tar -xzp" | ||||
|     ;; | ||||
| esac | ||||
| fi | ||||
| 
 | ||||
| if [ -f "$__object/parameter/insecure" ] ; then | ||||
|    curl="$curl -k" | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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): | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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): | ||||
|  |  | |||
|  | @ -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()) | ||||
|  |  | |||
|  | @ -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: | ||||
|  |  | |||
|  | @ -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( | ||||
|  |  | |||
|  | @ -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" | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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__': | ||||
|  |  | |||
|  | @ -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" | ||||
|  |  | |||
|  | @ -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) | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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-manifest.html>\`_). | ||||
| 
 | ||||
| __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. | ||||
|  |  | |||
|  | @ -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. | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue