Kaynağa Gözat

Better ps output

Ben Firshman 12 yıl önce
ebeveyn
işleme
9cf1d232b2
3 değiştirilmiş dosya ile 62 ekleme ve 7 silme
  1. 25 7
      plum/cli/main.py
  2. 31 0
      plum/container.py
  3. 6 0
      plum/service_collection.py

+ 25 - 7
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)

+ 31 - 0
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()

+ 6 - 0
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