From 10725136d834eb678df7b2673cc8c1a39f3b54d8 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Tue, 29 Apr 2014 09:20:29 +0100 Subject: [PATCH 1/3] Add tests for names on containers --- tests/unit/container_test.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/unit/container_test.py b/tests/unit/container_test.py index b1d87f7cf..445892418 100644 --- a/tests/unit/container_test.py +++ b/tests/unit/container_test.py @@ -13,12 +13,12 @@ class ContainerTest(unittest.TestCase): "Ports":None, "SizeRw":0, "SizeRootFs":0, - "Names":["/db_1"] + "Names":["/figtest_db_1"] }, has_been_inspected=True) self.assertEqual(container.dictionary, { "ID": "abc", "Image":"ubuntu:12.04", - "Name": "/db_1", + "Name": "/figtest_db_1", }) def test_environment(self): @@ -46,6 +46,24 @@ class ContainerTest(unittest.TestCase): "Ports":None, "SizeRw":0, "SizeRootFs":0, - "Names":["/db_1"] + "Names":["/figtest_db_1"] }, has_been_inspected=True) self.assertEqual(container.number, 1) + + def test_name(self): + container = Container.from_ps(None, { + "Id":"abc", + "Image":"ubuntu:12.04", + "Command":"sleep 300", + "Names":["/figtest_db_1"] + }, has_been_inspected=True) + self.assertEqual(container.name, "figtest_db_1") + + def test_name_without_project(self): + container = Container.from_ps(None, { + "Id":"abc", + "Image":"ubuntu:12.04", + "Command":"sleep 300", + "Names":["/figtest_db_1"] + }, has_been_inspected=True) + self.assertEqual(container.name_without_project, "db_1") From a724aa5717183e08982a3e2f160fa2425664a574 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Tue, 29 Apr 2014 09:22:20 +0100 Subject: [PATCH 2/3] Use name without project for log printing --- fig/cli/log_printer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fig/cli/log_printer.py b/fig/cli/log_printer.py index 35d362821..c528a126b 100644 --- a/fig/cli/log_printer.py +++ b/fig/cli/log_printer.py @@ -31,7 +31,7 @@ class LogPrinter(object): return generators def _make_log_generator(self, container, color_fn): - prefix = color_fn(container.name + " | ") + prefix = color_fn(container.name_without_project + " | ") # Attach to container before log printer starts running line_generator = split_buffer(self._attach(container), '\n') From fff5e51426f586562b8438d84ced8330ef3c6975 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Tue, 29 Apr 2014 09:31:57 +0100 Subject: [PATCH 3/3] Make log messages line up with each other --- fig/cli/log_printer.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/fig/cli/log_printer.py b/fig/cli/log_printer.py index c528a126b..e302aecb7 100644 --- a/fig/cli/log_printer.py +++ b/fig/cli/log_printer.py @@ -13,6 +13,7 @@ class LogPrinter(object): def __init__(self, containers, attach_params=None): self.containers = containers self.attach_params = attach_params or {} + self.prefix_width = self._calculate_prefix_width(containers) self.generators = self._make_log_generators() def run(self): @@ -20,6 +21,19 @@ class LogPrinter(object): for line in mux.loop(): sys.stdout.write(line.encode(sys.__stdout__.encoding or 'utf-8')) + def _calculate_prefix_width(self, containers): + """ + Calculate the maximum width of container names so we can make the log + prefixes line up like so: + + db_1 | Listening + web_1 | Listening + """ + prefix_width = 0 + for container in containers: + prefix_width = max(prefix_width, len(container.name_without_project)) + return prefix_width + def _make_log_generators(self): color_fns = cycle(colors.rainbow()) generators = [] @@ -31,7 +45,7 @@ class LogPrinter(object): return generators def _make_log_generator(self, container, color_fn): - prefix = color_fn(container.name_without_project + " | ") + prefix = color_fn(self._generate_prefix(container)) # Attach to container before log printer starts running line_generator = split_buffer(self._attach(container), '\n') @@ -42,6 +56,14 @@ class LogPrinter(object): yield color_fn("%s exited with code %s\n" % (container.name, exit_code)) yield STOP + def _generate_prefix(self, container): + """ + Generate the prefix for a log line without colour + """ + name = container.name_without_project + padding = ' ' * (self.prefix_width - len(name)) + return ''.join([name, padding, ' | ']) + def _attach(self, container): params = { 'stdout': True,