__cdist_log_level=<log level int value>; __cdist_log_level_name=<log level name> (#574)

This commit is contained in:
Darko Poljak 2017-09-11 09:06:47 +02:00 committed by GitHub
parent 1ae5b1732e
commit f0dc21ec0c
20 changed files with 192 additions and 40 deletions

View File

@ -22,16 +22,14 @@ uri="$(cat "$__object/parameter/uri" 2>/dev/null \
|| echo "$__object_id")" || echo "$__object_id")"
target="$(cat "$__object/parameter/target")" target="$(cat "$__object/parameter/target")"
case "$__cdist_log_level" in if [ "$__cdist_log_level" -le "10" ]
DEBUG|TRACE) then
curl="curl" curl="curl"
tar="tar -xvzp" tar="tar -xvzp"
;; else
*) curl="curl -s"
curl="curl -s" tar="tar -xzp"
tar="tar -xzp" fi
;;
esac
if [ -f "$__object/parameter/insecure" ] ; then if [ -f "$__object/parameter/insecure" ] ; then
curl="$curl -k" curl="$curl -k"

View File

@ -26,6 +26,7 @@ import cdist
import cdist.argparse import cdist.argparse
import re import re
import multiprocessing import multiprocessing
import logging
class Singleton(type): class Singleton(type):
@ -215,6 +216,24 @@ class ArchivingOption(SelectOption):
return val 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 = { _ARG_OPTION_MAPPING = {
'beta': 'beta', 'beta': 'beta',
'cache_path_pattern': 'cache_path_pattern', 'cache_path_pattern': 'cache_path_pattern',
@ -281,6 +300,9 @@ class Configuration(metaclass=Singleton):
'__cdist_log_level': 'verbosity', '__cdist_log_level': 'verbosity',
} }
ENV_VAR_BOOLEAN_OPTIONS = ('CDIST_BETA', ) ENV_VAR_BOOLEAN_OPTIONS = ('CDIST_BETA', )
ENV_VAR_OPTIONS = {
'__cdist_log_level': LogLevelOption(),
}
ARG_OPTION_MAPPING = _ARG_OPTION_MAPPING ARG_OPTION_MAPPING = _ARG_OPTION_MAPPING
ADJUST_ARG_OPTION_MAPPING = { ADJUST_ARG_OPTION_MAPPING = {
@ -366,8 +388,11 @@ class Configuration(metaclass=Singleton):
if option in self.ENV_VAR_BOOLEAN_OPTIONS: if option in self.ENV_VAR_BOOLEAN_OPTIONS:
d[dst_opt] = True d[dst_opt] = True
else: else:
option_object = self.CONFIG_FILE_OPTIONS[section][dst_opt] if option in self.ENV_VAR_OPTIONS:
converter = option_object.converter() opt = self.ENV_VAR_OPTIONS[option]
else:
opt = self.CONFIG_FILE_OPTIONS[section][dst_opt]
converter = opt.converter()
val = env[option] val = env[option]
newval = converter(val) newval = converter(val)
d[dst_opt] = newval d[dst_opt] = newval

View File

@ -28,4 +28,4 @@ from cdist.core.explorer import Explorer
from cdist.core.manifest import Manifest from cdist.core.manifest import Manifest
from cdist.core.code import Code from cdist.core.code import Code
from cdist.core.util import listdir 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

View File

@ -109,6 +109,8 @@ class Code(object):
'__files': self.local.files_path, '__files': self.local.files_path,
'__target_host_tags': self.local.target_host_tags, '__target_host_tags': self.local.target_host_tags,
'__cdist_log_level': util.log_level_env_var_val(local.log), '__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): def _run_gencode(self, cdist_object, which):

View File

@ -80,6 +80,8 @@ class Explorer(object):
'__explorer': self.remote.global_explorer_path, '__explorer': self.remote.global_explorer_path,
'__target_host_tags': self.local.target_host_tags, '__target_host_tags': self.local.target_host_tags,
'__cdist_log_level': util.log_level_env_var_val(self.log), '__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._type_explorers_transferred = []
self.jobs = jobs self.jobs = jobs

View File

@ -113,6 +113,8 @@ class Manifest(object):
'__files': self.local.files_path, '__files': self.local.files_path,
'__target_host_tags': self.local.target_host_tags, '__target_host_tags': self.local.target_host_tags,
'__cdist_log_level': util.log_level_env_var_val(self.log), '__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): def _open_logger(self):

View File

@ -38,4 +38,8 @@ def _ishidden(path):
def log_level_env_var_val(log): def log_level_env_var_val(log):
return str(log.getEffectiveLevel())
def log_level_name_env_var_val(log):
return logging.getLevelName(log.getEffectiveLevel()) return logging.getLevelName(log.getEffectiveLevel())

View File

@ -112,8 +112,7 @@ class Emulator(object):
if '__cdist_log_level' in self.env: if '__cdist_log_level' in self.env:
try: try:
loglevel = self.env['__cdist_log_level'] loglevel = self.env['__cdist_log_level']
# For a text level it returns its numerical value. level = int(loglevel)
level = logging.getLevelName(loglevel)
except ValueError: except ValueError:
level = logging.WARNING level = logging.WARNING
else: else:

View File

@ -101,7 +101,9 @@ class CodeTestCase(test.CdistTestCase):
self.assertEqual(output_dict['__files'], self.local.files_path) self.assertEqual(output_dict['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'], self.assertEqual(output_dict['__target_host_tags'],
self.local.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): def test_run_gencode_remote_environment(self):
output_string = self.code.run_gencode_remote(self.cdist_object) 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['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'], self.assertEqual(output_dict['__target_host_tags'],
self.local.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): def test_transfer_code_remote(self):
self.cdist_object.code_remote = self.code.run_gencode_remote( self.cdist_object.code_remote = self.code.run_gencode_remote(

View File

@ -11,3 +11,4 @@ echo "echo __object_name: $__object_name"
echo "echo __files: $__files" echo "echo __files: $__files"
echo "echo __target_host_tags: $__target_host_tags" echo "echo __target_host_tags: $__target_host_tags"
echo "echo __cdist_log_level: $__cdist_log_level" echo "echo __cdist_log_level: $__cdist_log_level"
echo "echo __cdist_log_level_name: $__cdist_log_level_name"

View File

@ -27,7 +27,7 @@ import os.path as op
import argparse import argparse
from cdist import test from cdist import test
import cdist.argparse as cap import cdist.argparse as cap
import logging
my_dir = op.abspath(op.dirname(__file__)) my_dir = op.abspath(op.dirname(__file__))
fixtures = op.join(my_dir, 'fixtures') fixtures = op.join(my_dir, 'fixtures')
@ -124,6 +124,18 @@ class ConfigurationOptionsTestCase(test.CdistTestCase):
self.assertEqual(converter(value), ['spam', 'eggs', 'ham', ]) self.assertEqual(converter(value), ['spam', 'eggs', 'ham', ])
self.assertIsNone(converter('')) 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): class ConfigurationTestCase(test.CdistTestCase):
@ -234,17 +246,17 @@ class ConfigurationTestCase(test.CdistTestCase):
os.remove(custom_config_file) os.remove(custom_config_file)
def test_singleton(self): def test_singleton(self):
x = cc.Configuration(None) x = cc.Configuration(None, env={}, config_files=())
args = argparse.Namespace() args = argparse.Namespace()
args.a = 'a' args.a = 'a'
y = cc.Configuration(args) y = cc.Configuration(args, env={}, config_files=())
self.assertIs(x, y) self.assertIs(x, y)
def test_non_singleton(self): def test_non_singleton(self):
x = cc.Configuration(None, singleton=False) x = cc.Configuration(None, env={}, config_files=(), singleton=False)
args = argparse.Namespace() args = argparse.Namespace()
args.a = 'a' args.a = 'a'
y = cc.Configuration(args, singleton=False) y = cc.Configuration(args, env={}, config_files=(), singleton=False)
self.assertIsNot(x, y) self.assertIsNot(x, y)
def test_read_config_file(self): def test_read_config_file(self):
@ -1122,6 +1134,39 @@ class ConfigurationTestCase(test.CdistTestCase):
config_files=config_files) config_files=config_files)
self.assertEqual(configuration.config, expected_config_dict) 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__": if __name__ == "__main__":
import unittest import unittest

View File

@ -124,7 +124,7 @@ class EmulatorTestCase(test.CdistTestCase):
emu = emulator.Emulator(argv, env=self.env) emu = emulator.Emulator(argv, env=self.env)
emu_loglevel = emu.log.getEffectiveLevel() emu_loglevel = emu.log.getEffectiveLevel()
self.assertEqual(emu_loglevel, logging.WARNING) 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 = emulator.Emulator(argv, env=self.env)
emu_loglevel = emu.log.getEffectiveLevel() emu_loglevel = emu.log.getEffectiveLevel()
self.assertEqual(emu_loglevel, logging.DEBUG) self.assertEqual(emu_loglevel, logging.DEBUG)

View File

@ -31,6 +31,7 @@ from cdist import test
from cdist.exec import local from cdist.exec import local
from cdist.exec import remote from cdist.exec import remote
from cdist.core import explorer from cdist.core import explorer
import logging
import os.path as op import os.path as op
my_dir = op.abspath(op.dirname(__file__)) my_dir = op.abspath(op.dirname(__file__))
@ -233,7 +234,9 @@ class ExplorerClassTestCase(test.CdistTestCase):
self.remote.global_explorer_path) self.remote.global_explorer_path)
self.assertEqual(output_dict['__target_host_tags'], self.assertEqual(output_dict['__target_host_tags'],
self.local.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__': if __name__ == '__main__':

View File

@ -6,3 +6,4 @@ echo "__target_fqdn: $__target_fqdn"
echo "__explorer: $__explorer" echo "__explorer: $__explorer"
echo "__target_host_tags: $__target_host_tags" echo "__target_host_tags: $__target_host_tags"
echo "__cdist_log_level: $__cdist_log_level" echo "__cdist_log_level: $__cdist_log_level"
echo "__cdist_log_level_name: $__cdist_log_level_name"

View File

@ -99,7 +99,9 @@ class ManifestTestCase(test.CdistTestCase):
self.assertEqual(output_dict['__files'], self.local.files_path) self.assertEqual(output_dict['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'], self.assertEqual(output_dict['__target_host_tags'],
self.local.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) self.log.setLevel(old_loglevel)
def test_type_manifest_environment(self): 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['__files'], self.local.files_path)
self.assertEqual(output_dict['__target_host_tags'], self.assertEqual(output_dict['__target_host_tags'],
self.local.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) self.log.setLevel(old_loglevel)
def test_loglevel_env_setup(self): def test_loglevel_env_setup(self):
@ -147,7 +151,10 @@ class ManifestTestCase(test.CdistTestCase):
self.log.setLevel(logging.DEBUG) self.log.setLevel(logging.DEBUG)
manifest = cdist.core.manifest.Manifest(self.target_host, self.local) manifest = cdist.core.manifest.Manifest(self.target_host, self.local)
self.assertTrue("__cdist_log_level" in manifest.env) 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) self.log.setLevel(current_level)

View File

@ -11,4 +11,5 @@ __manifest: $__manifest
__files: $__files __files: $__files
__target_host_tags: $__target_host_tags __target_host_tags: $__target_host_tags
__cdist_log_level: $__cdist_log_level __cdist_log_level: $__cdist_log_level
__cdist_log_level_name: $__cdist_log_level_name
DONE DONE

View File

@ -15,4 +15,5 @@ __object_name: $__object_name
__files: $__files __files: $__files
__target_host_tags: $__target_host_tags __target_host_tags: $__target_host_tags
__cdist_log_level: $__cdist_log_level __cdist_log_level: $__cdist_log_level
__cdist_log_level_name: $__cdist_log_level_name
DONE DONE

View File

@ -10,12 +10,13 @@ next:
* Type __daemontools: Improve it on FreeBSD (Kamila Součková) * Type __daemontools: Improve it on FreeBSD (Kamila Součková)
* Type __package_pkg_openbsd: Fix use of --name (Philippe Gregoire) * Type __package_pkg_openbsd: Fix use of --name (Philippe Gregoire)
* Type __package_pkg_openbsd: Fix pkg_version explorer (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á) * 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 4.6.1: 2017-08-30
* Type __user: Explore with /etc files (passwd, group, shadow) (Philippe Gregoire) * Type __user: Explore with /etc files (passwd, group, shadow) (Philippe Gregoire)

View File

@ -198,9 +198,27 @@ Environment variables (for reading)
----------------------------------- -----------------------------------
The following environment variables are exported by cdist: The following environment variables are exported by cdist:
__cdist_log_level __cdist_log_level, __cdist_log_level_name
String value of cdist log level. One of OFF, ERROR, WARNING, INFO, cdist log level value and cdist log level name. One of:
VERBOSE, DEBUG and TRACE.
+----------------+-----------------+
| 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, Available for: initial manifest, explorer, type manifest, type explorer,
type gencode. type gencode.
__explorer __explorer
@ -266,8 +284,27 @@ require
Setup dependencies between objects (see \`cdist manifest <cdist-manifest.html>\`_). Setup dependencies between objects (see \`cdist manifest <cdist-manifest.html>\`_).
__cdist_log_level __cdist_log_level
String value of cdist log level. One of OFF, ERROR, WARNING, INFO, cdist log level value. One of:
VERBOSE, DEBUG and TRACE. If set cdist will set this log level in
+----------------+-----------------+
| 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 accordance with configuration rules. If cdist invokation is used
in types then nested cdist will honor this specified log level if in types then nested cdist will honor this specified log level if
not specified otherwise while invoking it. not specified otherwise while invoking it.

View File

@ -333,9 +333,28 @@ So when you generate a script with the following content, it will work:
Log level in types Log level in types
------------------ ------------------
cdist log level can be accessed from __cdist_log_level variable. cdist log level can be accessed from __cdist_log_level variable.One of:
Value is a string, one of OFF, ERROR, WARNING, INFO, VERBOSE, DEBUG and
TRACE. It is available for initial manifest, explorer, type manifest, +----------------+-----------------+
| 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. type explorer, type gencode.