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.
This commit is contained in:
Aanand Prasad 2013-12-18 19:01:53 +00:00
parent 26ea08087a
commit 730f9772f9
3 changed files with 54 additions and 27 deletions

View File

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

View File

@ -8,7 +8,6 @@ from docopt import docopt
from inspect import getdoc from inspect import getdoc
from .. import __version__ from .. import __version__
from ..service import get_container_name
from ..service_collection import ServiceCollection from ..service_collection import ServiceCollection
from .command import Command from .command import Command
from .log_printer import LogPrinter from .log_printer import LogPrinter
@ -103,9 +102,30 @@ class TopLevelCommand(Command):
""" """
Start all services 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): def stop(self, options):
""" """
@ -122,8 +142,12 @@ class TopLevelCommand(Command):
Usage: logs Usage: logs
""" """
containers = self._get_containers(all=False) containers = self._get_containers(all=False)
print "Attaching to", ", ".join(get_container_name(c) for c in containers) print "Attaching to", list_containers(containers)
LogPrinter(client=self.client).attach(containers) LogPrinter(containers, attach_params={'logs': True}).run()
def _get_containers(self, all): def _get_containers(self, all):
return [c for s in self.service_collection for c in s.containers(all=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)

View File

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