diff --git a/atomicapp/cli/main.py b/atomicapp/cli/main.py index 5185b0c8..b5a51c3c 100644 --- a/atomicapp/cli/main.py +++ b/atomicapp/cli/main.py @@ -32,7 +32,8 @@ ANSWERS_FILE_SAMPLE_FORMAT, HOST_DIR, LOCK_FILE, - PROVIDERS) + PROVIDERS, + LOG_LEVELS) from atomicapp.nulecule import NuleculeManager from atomicapp.nulecule.exceptions import NuleculeException from atomicapp.utils import Utils @@ -149,9 +150,12 @@ def set_arguments(self): self.parser.add_argument( "--logging-output", - dest="logging-output", - choices=['cockpit'], - help="Override the default logging output.") + dest="logging_output", + choices=['cockpit', 'stdout'], + help="Override the default logging output." + "stdout: We will only log to stdout/stderr" + "cockpit: Used with cockpit integration" + "none: atomicapp will disable any logging") self.parser.add_argument( "--answers-format", @@ -256,6 +260,7 @@ def set_arguments(self): def run(self): self.set_arguments() args = self.parser.parse_args() + if args.verbose: set_logging(level=logging.DEBUG) elif args.quiet: @@ -263,6 +268,11 @@ def run(self): else: set_logging(level=logging.INFO) + # Let's check to see if any of our choices match the LOG_LEVELS constant! + for k in LOG_LEVELS: + if args.logging_output == k: + set_logging(level=LOG_LEVELS[args.logging_output]) + lock = LockFile(os.path.join(Utils.getRoot(), LOCK_FILE)) try: lock.acquire(timeout=-1) diff --git a/atomicapp/constants.py b/atomicapp/constants.py index a1b32a94..54b9735e 100644 --- a/atomicapp/constants.py +++ b/atomicapp/constants.py @@ -49,6 +49,7 @@ LOCK_FILE = "/run/lock/atomicapp.lock" LOG_FILE = "/var/log/atomicapp.log" LOG_NAME = "atomicapp" +LOG_LEVELS = {"cockpit": 91, "stdout": 92} HOST_DIR = "/host" DEFAULT_PROVIDER = "kubernetes" diff --git a/atomicapp/display.py b/atomicapp/display.py index 71a8c0a7..d475df5d 100644 --- a/atomicapp/display.py +++ b/atomicapp/display.py @@ -9,12 +9,12 @@ class Display: ''' Logging level codes - CRITICAL 50 - ERROR 40 - WARNING 30 - INFO 20 - DEBUG 10 NOTSET 0 + DEBUG 10 + INFO 20 + WARNING 30 + ERROR 40 + CRITICAL 50 ''' # Console colour codes @@ -34,35 +34,41 @@ def __init__(self): self.logger = logging.getLogger(LOG_NAME) self.verbose_level = self.logger.getEffectiveLevel() - def display(self, msg, color='white', stderr=False): - msg = self._colorize(self._make_unicode(msg), color) - print(msg) - if stderr: - sys.stderr.flush() + def display(self, msg, code, color='white', stderr=False): + + if self.verbose_level == 91: + print "cockpit test" else: - sys.stdout.flush() + if code == 10: + self.logger.info(msg) + elif code == 20: + self.logger.info(msg) + elif code == 30: + self.logger.warning(msg) + elif code == 40: + self.logger.error(msg) + + if stderr: + sys.stderr.flush() + else: + sys.stdout.flush() + + print(self._colorize(self._make_unicode(msg), color)) def debug(self, msg, *args): - self.logger.debug(msg) - if self.verbose_level is 10: - self.display("[DEBUG] %6d %0.2f %s" % (os.getpid(), time.time(), msg), 'cyan') + self.display("[DEBUG] %6d %0.2f %s" % (os.getpid(), time.time(), msg), 10, color='cyan') def verbose(self, msg, *args): - self.logger.info(msg) - if self.verbose_level is 10: - self.display("[VERBOSE] %s" % msg, 'cyan') + self.display("[VERBOSE] %s" % msg, 10, color='cyan') def info(self, msg, *args): - self.logger.info(msg) - self.display("[OK] %s" % msg, 'green') + self.display("[OK] %s" % msg, 20, color='green',) def warning(self, msg, *args): - self.logger.warning(msg) - self.display("[WARNING] %s" % msg, 'yellow', stderr=True) + self.display("[WARNING] %s" % msg, 30, color='yellow', stderr=True) def error(self, msg, *args): - self.logger.error(msg) - self.display("[ERROR] %s" % msg, 'red', stderr=True) + self.display("[ERROR] %s" % msg, 40, color='red', stderr=True) # Colors! def _colorize(self, text, color): diff --git a/atomicapp/nulecule/lib.py b/atomicapp/nulecule/lib.py index 769ecf3c..86a16b07 100644 --- a/atomicapp/nulecule/lib.py +++ b/atomicapp/nulecule/lib.py @@ -5,9 +5,6 @@ from atomicapp.utils import Utils from atomicapp.plugin import Plugin -plugin = Plugin() -plugin.load_plugins() - class NuleculeBase(object): """ @@ -15,6 +12,8 @@ class NuleculeBase(object): atomicapp.nulecule.base. """ def __init__(self, basepath, params, namespace): + self.plugin = Plugin() + self.plugin.load_plugins() self.basepath = basepath self.params = params or [] self.namespace = namespace @@ -93,7 +92,7 @@ def get_provider(self, provider_key=None, dry=False): if provider_key is None: provider_key = self.config.get(GLOBAL_CONF, {}).get( PROVIDER_KEY, DEFAULT_PROVIDER) - provider_class = plugin.getProvider(provider_key) + provider_class = self.plugin.getProvider(provider_key) return provider_key, provider_class( self.get_context(), self.basepath, dry)