Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrage committed Nov 24, 2015
1 parent 19e2aaf commit f0f21e2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
18 changes: 14 additions & 4 deletions atomicapp/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -256,13 +260,19 @@ 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:
set_logging(level=logging.WARNING)
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)
Expand Down
1 change: 1 addition & 0 deletions atomicapp/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 29 additions & 23 deletions atomicapp/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions atomicapp/nulecule/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
from atomicapp.utils import Utils
from atomicapp.plugin import Plugin

plugin = Plugin()
plugin.load_plugins()


class NuleculeBase(object):
"""
This is the base class for Nulecule and NuleculeComponent in
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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit f0f21e2

Please sign in to comment.