mirror of https://github.com/docker/compose.git
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:
parent
26ea08087a
commit
730f9772f9
|
@ -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(
|
|
||||||
container['Id'],
|
|
||||||
params = {
|
params = {
|
||||||
'stdin': 0,
|
'stdin': False,
|
||||||
'stdout': 1,
|
'stdout': True,
|
||||||
'stderr': 1,
|
'stderr': True,
|
||||||
'logs': 1 if logs else 0,
|
'logs': False,
|
||||||
'stream': 1 if stream else 0
|
'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''):
|
for line in iter(socket.makefile().readline, b''):
|
||||||
if not line.endswith('\n'):
|
if not line.endswith('\n'):
|
||||||
line += '\n'
|
line += '\n'
|
||||||
|
|
|
@ -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]
|
||||||
"""
|
"""
|
||||||
|
if options['-d']:
|
||||||
self.service_collection.start()
|
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)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue