Browse Source

Add Container class

tags/v0.1.1
Chris Smith 7 years ago
parent
commit
eec6a841a1
1 changed files with 47 additions and 3 deletions
  1. 47
    3
      docker-rerun

+ 47
- 3
docker-rerun View File

@@ -16,12 +16,56 @@ import subprocess
16 16
 import sys
17 17
 
18 18
 
19
-def docker_inspect(target, type):
19
+class Container(object):
20
+    """Encapsulates information about a container."""
21
+
22
+    def __init__(self, name, container_info, image_info):
23
+        """Creates a new Container.
24
+
25
+        Args:
26
+            name (str): The name of the container.
27
+            container_info (dict): Dictionary describing the container state.
28
+            image_info (dict): Dictionary describing the image state.
29
+        """
30
+        self.args = ['--name=%s' % name]
31
+        """The arguments passed to docker to create the container."""
32
+
33
+        self.image = ''
34
+        """The image that the container uses."""
35
+
36
+        self.cmd = []
37
+        """The command executed within the container."""
38
+
39
+        self.info = container_info
40
+        """The container information as returned by `docker inspect`"""
41
+
42
+        self.image_info = image_info
43
+        """The image information as returned by `docker inspect`"""
44
+
45
+
46
+    def command_line(self):
47
+        """Gets the full command-line used to run this container."""
48
+        return ['docker', 'run'] + sorted(self.args) + [self.image] + self.cmd
49
+
50
+
51
+    def add_args_from_list(self, template, selector):
52
+        """Adds an argument for each item in a list.
53
+
54
+        Args:
55
+            template (str): Template to use for the argument. Use %s for value.
56
+            selector (func): Function to extract the list from our info.
57
+        """
58
+        target = selector(self.info)
59
+        if target:
60
+            self.args.extend([template % entry for entry in target])
61
+
62
+
63
+def docker_inspect(target, what):
20 64
     """Uses `docker inspect` to get details about the given container or image.
21 65
 
22 66
     Args:
23 67
         target (str): The name of the container or image to inspect.
24
-        type (str): The type of object to inspect ('container' or 'image').
68
+        what (str): The type of object to inspect ('container' or 'image').
25 69
 
26 70
     Returns:
27 71
         dict: Detailed information about the  target.
@@ -30,7 +74,7 @@ def docker_inspect(target, type):
30 74
         CalledProcessError: An error occurred talking to Docker.
31 75
     """
32 76
     output = subprocess.check_output(['docker', 'inspect',
33
-                                      '--type=%s' % type, target])
77
+                                      '--type=%s' % what, target])
34 78
     return json.loads(output.decode('utf-8'))[0]
35 79
 
36 80
 

Loading…
Cancel
Save