diff --git a/plum/cli/main.py b/plum/cli/main.py index 1db3873a2..688bb1800 100644 --- a/plum/cli/main.py +++ b/plum/cli/main.py @@ -10,6 +10,7 @@ from inspect import getdoc from .. import __version__ from ..service_collection import ServiceCollection from .command import Command +from .formatter import Formatter from .log_printer import LogPrinter from docker.client import APIError @@ -78,10 +79,30 @@ class TopLevelCommand(Command): """ List services and containers. - Usage: ps + Usage: ps [options] + + Options: + -q Only display IDs """ - for container in self._get_containers(all=False): - print container.name + if options['-q']: + for container in self.service_collection.containers(all=True): + print container.id + else: + headers = [ + 'Name', + 'Command', + 'State', + 'Ports', + ] + rows = [] + for container in self.service_collection.containers(all=True): + rows.append([ + container.name, + container.human_readable_command, + container.human_readable_state, + container.human_readable_ports, + ]) + print Formatter().table(headers, rows) def run(self, options): """ @@ -146,13 +167,10 @@ class TopLevelCommand(Command): Usage: logs """ - containers = self._get_containers(all=False) + containers = self.service_collection.containers(all=False) 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) diff --git a/plum/container.py b/plum/container.py index abd1c3f55..eb65fc12a 100644 --- a/plum/container.py +++ b/plum/container.py @@ -37,10 +37,41 @@ class Container(object): def id(self): return self.dictionary['ID'] + @property + def short_id(self): + return self.id[:10] + @property def name(self): return self.dictionary['Name'] + @property + def human_readable_ports(self): + self.inspect_if_not_inspected() + if not self.dictionary['NetworkSettings']['Ports']: + return '' + ports = [] + for private, public in self.dictionary['NetworkSettings']['Ports'].items(): + if public: + ports.append('%s->%s' % (public[0]['HostPort'], private)) + return ', '.join(ports) + + @property + def human_readable_state(self): + self.inspect_if_not_inspected() + if self.dictionary['State']['Running']: + if self.dictionary['State']['Ghost']: + return 'Ghost' + else: + return 'Up' + else: + return 'Exit %s' % self.dictionary['State']['ExitCode'] + + @property + def human_readable_command(self): + self.inspect_if_not_inspected() + return ' '.join(self.dictionary['Config']['Cmd']) + @property def environment(self): self.inspect_if_not_inspected() diff --git a/plum/service_collection.py b/plum/service_collection.py index 3c59c6ed5..a72835b0f 100644 --- a/plum/service_collection.py +++ b/plum/service_collection.py @@ -50,5 +50,11 @@ class ServiceCollection(list): for service in self: service.stop() + def containers(self, *args, **kwargs): + l = [] + for service in self: + for container in service.containers(*args, **kwargs): + l.append(container) + return l