瀏覽代碼

plum start runs in foreground by default

Also fixed LogPrinter regressions. Sorry for not doing that in a
separate commit.

Also made 'plum logs' show backlog. Yep, rolled that right in too. Gonna
go whip myself now.
Aanand Prasad 12 年之前
父節點
當前提交
730f9772f9
共有 3 個文件被更改,包括 56 次插入29 次删除
  1. 24 24
      plum/cli/log_printer.py
  2. 29 5
      plum/cli/main.py
  3. 3 0
      plum/container.py

+ 24 - 24
plum/cli/log_printer.py

@@ -2,48 +2,48 @@ import sys
 
 from itertools import cycle
 
-from ..service import get_container_name
 from .multiplexer import Multiplexer
 from . import colors
 
 
 class LogPrinter(object):
-    def __init__(self, client):
-        self.client = client
+    def __init__(self, containers, attach_params=None):
+        self.containers = containers
+        self.attach_params = attach_params or {}
+        self.generators = self._make_log_generators()
 
-    def attach(self, containers):
-        generators = self._make_log_generators(containers)
-        mux = Multiplexer(generators)
+    def run(self):
+        mux = Multiplexer(self.generators)
         for line in mux.loop():
             sys.stdout.write(line)
 
-    def _make_log_generators(self, containers):
+    def _make_log_generators(self):
         color_fns = cycle(colors.rainbow())
         generators = []
 
-        for container in containers:
+        for container in self.containers:
             color_fn = color_fns.next()
             generators.append(self._make_log_generator(container, color_fn))
 
         return generators
 
     def _make_log_generator(self, container, color_fn):
-        container_name = get_container_name(container)
-        format = lambda line: color_fn(container_name + " | ") + line
-        return (format(line) for line in self._readlines(container))
-
-    def _readlines(self, container, logs=False, stream=True):
-        socket = self.client.attach_socket(
-            container['Id'],
-            params={
-                'stdin': 0,
-                'stdout': 1,
-                'stderr': 1,
-                'logs': 1 if logs else 0,
-                'stream': 1 if stream else 0
-            },
-        )
-
+        format = lambda line: color_fn(container.name + " | ") + line
+        return (format(line) for line in self._readlines(self._attach(container)))
+
+    def _attach(self, container):
+        params = {
+            'stdin': False,
+            'stdout': True,
+            'stderr': True,
+            'logs': False,
+            'stream': True,
+        }
+        params.update(self.attach_params)
+        params = dict((name, 1 if value else 0) for (name, value) in params.items())
+        return container.attach_socket(params=params)
+
+    def _readlines(self, socket):
         for line in iter(socket.makefile().readline, b''):
             if not line.endswith('\n'):
                 line += '\n'

+ 29 - 5
plum/cli/main.py

@@ -8,7 +8,6 @@ from docopt import docopt
 from inspect import getdoc
 
 from .. import __version__
-from ..service import get_container_name
 from ..service_collection import ServiceCollection
 from .command import Command
 from .log_printer import LogPrinter
@@ -103,9 +102,30 @@ class TopLevelCommand(Command):
         """
         Start all services
 
-        Usage: start
+        Usage: start [-d]
         """
-        self.service_collection.start()
+        if options['-d']:
+            self.service_collection.start()
+            return
+
+        running = []
+        unstarted = []
+
+        for s in self.service_collection:
+            if len(s.containers()) == 0:
+                unstarted.append((s, s.create_container()))
+            else:
+                running += s.get_containers(all=False)
+
+        log_printer = LogPrinter(running + [c for (s, c) in unstarted])
+
+        for (s, c) in unstarted:
+            s.start_container(c)
+
+        try:
+            log_printer.run()
+        finally:
+            self.service_collection.stop()
 
     def stop(self, options):
         """
@@ -122,8 +142,12 @@ class TopLevelCommand(Command):
         Usage: logs
         """
         containers = self._get_containers(all=False)
-        print "Attaching to", ", ".join(get_container_name(c) for c in containers)
-        LogPrinter(client=self.client).attach(containers)
+        print "Attaching to", list_containers(containers)
+        LogPrinter(containers, attach_params={'logs': True}).run()
 
     def _get_containers(self, all):
         return [c for s in self.service_collection for c in s.containers(all=all)]
+
+
+def list_containers(containers):
+    return ", ".join(c.name for c in containers)

+ 3 - 0
plum/container.py

@@ -84,3 +84,6 @@ class Container(object):
                 if len(bits) > 2 and bits[1] == self.name[1:]:
                     links.append(bits[2])
         return links
+
+    def attach_socket(self, **kwargs):
+        return self.client.attach_socket(self.id, **kwargs)