diff --git a/packstack/installer/core/sequences.py b/packstack/installer/core/sequences.py index 932f846d1..d38a39310 100644 --- a/packstack/installer/core/sequences.py +++ b/packstack/installer/core/sequences.py @@ -31,28 +31,19 @@ class Step(object): # TO-DO: complete logger name when logging will be setup correctly logger = logging.getLogger() logger.debug('Running step %s.' % self.name) - sys.stdout.write('%s...' % self.title) - sys.stdout.flush() - - # count space needed for title print - title = self.title - for color in utils.COLORS.itervalues(): - title = re.sub(re.escape(color), '', title) - space = 70 - len(title) # execute and report state - state_fmt = '[ %s ]\n' try: self.function(config) except Exception, ex: logger.debug(traceback.format_exc()) - state = state_fmt % utils.color_text('ERROR', 'red') - sys.stdout.write(state.rjust(space)) + state = utils.state_message(self.title, 'ERROR', 'red') + sys.stdout.write('%s\n' % state) sys.stdout.flush() raise SequenceError(str(ex)) else: - state = state_fmt % utils.color_text('DONE', 'green') - sys.stdout.write(state.rjust(space)) + state = utils.state_message(self.title, 'DONE', 'green') + sys.stdout.write('%s\n' % state) sys.stdout.flush() diff --git a/packstack/installer/utils/__init__.py b/packstack/installer/utils/__init__.py index e73258c30..92031e65f 100644 --- a/packstack/installer/utils/__init__.py +++ b/packstack/installer/utils/__init__.py @@ -4,9 +4,10 @@ from .datastructures import SortedDict from .decorators import retry from .network import get_localhost_ip, host2ip, force_ip, device_from_ip from .shell import ScriptRunner, execute -from .shortcuts import host_iter, hosts, get_current_user,\ - get_current_username, split_hosts -from .strings import COLORS, color_text, mask_string +from .shortcuts import (host_iter, hosts, get_current_user, + get_current_username, split_hosts) +from .strings import (COLORS, color_text, mask_string, state_format, + state_message) __all__ = ('SortedDict', @@ -14,4 +15,5 @@ __all__ = ('SortedDict', 'get_localhost_ip', 'host2ip', 'force_ip', 'device_from_ip', 'ScriptRunner', 'execute', 'host_iter', 'hosts', 'get_current_user', 'get_current_username', - 'split_hosts', 'COLORS', 'color_text', 'mask_string') + 'split_hosts', 'COLORS', 'color_text', 'mask_string', + 'state_format', 'state_message') diff --git a/packstack/installer/utils/strings.py b/packstack/installer/utils/strings.py index f4be5bcd4..5c46eb16a 100644 --- a/packstack/installer/utils/strings.py +++ b/packstack/installer/utils/strings.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +import re + STR_MASK = '*' * 8 COLORS = {'nocolor': "\033[0m", 'red': "\033[0;31m", @@ -33,3 +35,23 @@ def mask_string(unmasked, mask_list=None, replace_list=None): word = word.replace(before, after) masked = masked.replace(word, STR_MASK) return masked + + +def state_format(msg, state, color): + """ + Formats state with offset according to given message. + """ + _msg = '%s' % msg.strip() + for clr in COLORS.values(): + _msg = re.sub(re.escape(clr), '', msg) + + space = 70 - len(_msg) + state = '[ %s ]' % color_text(state, color) + return state.rjust(space) + + +def state_message(msg, state, color): + """ + Formats given message with colored state information. + """ + return '%s%s' % (msg, state_format(msg, state, color)) diff --git a/packstack/plugins/puppet_950.py b/packstack/plugins/puppet_950.py index 4289904a9..3986c650a 100644 --- a/packstack/plugins/puppet_950.py +++ b/packstack/plugins/puppet_950.py @@ -11,7 +11,7 @@ import time from packstack.installer import utils from packstack.installer import basedefs, output_messages -from packstack.installer.exceptions import ScriptRuntimeError +from packstack.installer.exceptions import ScriptRuntimeError, PuppetError from packstack.modules.common import filtered_hosts from packstack.modules.ospluginutils import manifestfiles @@ -126,12 +126,11 @@ def waitforpuppet(currently_running): while currently_running: for hostname, finished_logfile in currently_running: log_file = os.path.splitext(os.path.basename(finished_logfile))[0] - space_len = basedefs.SPACE_LEN - len(log_file) if len(log_file) > log_len: log_len = len(log_file) if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): twirl = twirl[-1:] + twirl[:-1] - sys.stdout.write(("\rTesting if puppet apply is finished : %s" % log_file).ljust(40 + log_len)) + sys.stdout.write(("\rTesting if puppet apply is finished: %s" % log_file).ljust(40 + log_len)) sys.stdout.write("[ %s ]" % twirl[0]) sys.stdout.flush() try: @@ -149,7 +148,7 @@ def waitforpuppet(currently_running): # clean off the last "testing apply" msg if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): - sys.stdout.write(("\r").ljust(45 + log_len)) + sys.stdout.write(('\r').ljust(45 + log_len)) except ScriptRuntimeError: # the test raises an exception if the file doesn't exist yet @@ -161,13 +160,20 @@ def waitforpuppet(currently_running): controller.MESSAGES.extend(scan_logfile(log)) # check the log file for errors - validate_logfile(log) - sys.stdout.write(("\r%s : " % log_file).ljust(space_len)) - print ("[ " + utils.color_text(output_messages.INFO_DONE, 'green') + " ]") + sys.stdout.write('\r') + try: + validate_logfile(log) + state = utils.state_message('%s:' % log_file, 'DONE', 'green') + sys.stdout.write('%s\n' % state) + sys.stdout.flush() + except PuppetError: + state = utils.state_message('%s:' % log_file, 'ERROR', 'red') + sys.stdout.write('%s\n' % state) + sys.stdout.flush() + raise def applyPuppetManifest(config): - print if config.get("DRY_RUN"): return currently_running = [] @@ -189,7 +195,7 @@ def applyPuppetManifest(config): continue host_dir = config['HOST_DETAILS'][hostname]['tmpdir'] - print "Applying " + manifest + print "Applying %s" % manifest server = utils.ScriptRunner(hostname) man_path = os.path.join(config['HOST_DETAILS'][hostname]['tmpdir'], diff --git a/tests/installer/test_sequences.py b/tests/installer/test_sequences.py index fb8d64654..55c8b126c 100644 --- a/tests/installer/test_sequences.py +++ b/tests/installer/test_sequences.py @@ -86,12 +86,10 @@ class SequenceTestCase(PackstackTestCaseMixin, TestCase): assert contents.startswith('Step 2') output = [] - state_fmt = '[ %s ]\n' self.steps.insert(0, {'title': 'Step 2'}) for i in self.steps: - space = 70 - len(i['title']) - title = '[ %s ]\n' % utils.color_text('DONE', 'green') - output.append('%s...%s' % (i['title'], title.rjust(space))) + output.append('%s\n' % utils.state_message(i['title'], + 'DONE', 'green')) self.seq.run(config={'test': 'test'}) contents = sys.stdout.getvalue()