mirror of
https://github.com/docker/compose.git
synced 2025-07-27 07:34:10 +02:00
Better ps output
This commit is contained in:
parent
bac37a19e3
commit
9cf1d232b2
@ -10,6 +10,7 @@ from inspect import getdoc
|
|||||||
from .. import __version__
|
from .. import __version__
|
||||||
from ..service_collection import ServiceCollection
|
from ..service_collection import ServiceCollection
|
||||||
from .command import Command
|
from .command import Command
|
||||||
|
from .formatter import Formatter
|
||||||
from .log_printer import LogPrinter
|
from .log_printer import LogPrinter
|
||||||
|
|
||||||
from docker.client import APIError
|
from docker.client import APIError
|
||||||
@ -78,10 +79,30 @@ class TopLevelCommand(Command):
|
|||||||
"""
|
"""
|
||||||
List services and containers.
|
List services and containers.
|
||||||
|
|
||||||
Usage: ps
|
Usage: ps [options]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-q Only display IDs
|
||||||
"""
|
"""
|
||||||
for container in self._get_containers(all=False):
|
if options['-q']:
|
||||||
print container.name
|
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):
|
def run(self, options):
|
||||||
"""
|
"""
|
||||||
@ -146,13 +167,10 @@ class TopLevelCommand(Command):
|
|||||||
|
|
||||||
Usage: logs
|
Usage: logs
|
||||||
"""
|
"""
|
||||||
containers = self._get_containers(all=False)
|
containers = self.service_collection.containers(all=False)
|
||||||
print "Attaching to", list_containers(containers)
|
print "Attaching to", list_containers(containers)
|
||||||
LogPrinter(containers, attach_params={'logs': True}).run()
|
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):
|
def list_containers(containers):
|
||||||
return ", ".join(c.name for c in containers)
|
return ", ".join(c.name for c in containers)
|
||||||
|
@ -37,10 +37,41 @@ class Container(object):
|
|||||||
def id(self):
|
def id(self):
|
||||||
return self.dictionary['ID']
|
return self.dictionary['ID']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def short_id(self):
|
||||||
|
return self.id[:10]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return self.dictionary['Name']
|
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
|
@property
|
||||||
def environment(self):
|
def environment(self):
|
||||||
self.inspect_if_not_inspected()
|
self.inspect_if_not_inspected()
|
||||||
|
@ -50,5 +50,11 @@ class ServiceCollection(list):
|
|||||||
for service in self:
|
for service in self:
|
||||||
service.stop()
|
service.stop()
|
||||||
|
|
||||||
|
def containers(self, *args, **kwargs):
|
||||||
|
l = []
|
||||||
|
for service in self:
|
||||||
|
for container in service.containers(*args, **kwargs):
|
||||||
|
l.append(container)
|
||||||
|
return l
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user