|
|
@@ -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'
|