Browse Source

Merge "Revert "Handle defined containers that are stopped."" into stable/stein

tags/4.5.1
Zuul 3 weeks ago
parent
commit
f9349e0328
2 changed files with 13 additions and 87 deletions
  1. 8
    29
      paunch/builder/base.py
  2. 5
    58
      paunch/tests/test_builder_base.py

+ 8
- 29
paunch/builder/base.py View File

@@ -41,18 +41,19 @@ class BaseBuilder(object):
41 41
         stdout = []
42 42
         stderr = []
43 43
         pull_returncode = self.pull_missing_images(stdout, stderr)
44
-
45 44
         if pull_returncode != 0:
46 45
             return stdout, stderr, pull_returncode
47 46
 
48 47
         self.delete_missing_and_updated()
48
+
49 49
         deploy_status_code = 0
50 50
         key_fltr = lambda k: self.config[k].get('start_order', 0)
51
+
51 52
         container_names = self.runner.container_names(self.config_id)
52 53
         desired_names = set([cn[-1] for cn in container_names])
53 54
 
54 55
         for container in sorted(self.config, key=key_fltr):
55
-            self.log.debug("Apply for container {}.".format(container))
56
+            self.log.debug("Running container: %s" % container)
56 57
             cconfig = self.config[container]
57 58
             action = cconfig.get('action', 'run')
58 59
             restart = cconfig.get('restart', 'none')
@@ -62,7 +63,6 @@ class BaseBuilder(object):
62 63
                                and self.runner.cont_cmd == 'podman'
63 64
                                and action == 'run')
64 65
             start_cmd = 'create' if systemd_managed else 'run'
65
-            cmd = []
66 66
 
67 67
             # When upgrading from Docker to Podman, we want to stop the
68 68
             # container that runs under Docker first before starting it with
@@ -72,32 +72,11 @@ class BaseBuilder(object):
72 72
                     os.path.exists('/var/run/docker.sock'):
73 73
                 self.runner.stop_container(container, 'docker', quiet=True)
74 74
 
75
-            self.log.debug("Apply action {} for container {}.".format(
76
-                action, container))
77
-
78 75
             if action == 'run':
79
-
80 76
                 if container in desired_names:
81
-                    discover_container = self.runner.discover_container_name(
82
-                        container, self.config_id)
83
-                    inspect_container = self.runner.inspect(discover_container)
84
-
85
-                    try:
86
-                        self.log.debug("Container running state for %s: %s." %
87
-                                       (container,
88
-                                        inspect_container["State"]["Running"]))
89
-                    except Exception:
90
-                        self.log.warn("Unable to find the container state" +
91
-                                      " for %s." % container)
92
-
93
-                    if inspect_container["State"]["Running"]:
94
-                        self.log.debug("Container %s is already running." %
95
-                                       container)
96
-                        continue
97
-                    else:
98
-                        self.log.debug("Deleting stopped container %s." %
99
-                                       container)
100
-                        self.runner.remove_container(container)
77
+                    self.log.debug('Skipping existing container: %s' %
78
+                                   container)
79
+                    continue
101 80
 
102 81
                 cmd = [
103 82
                     self.runner.cont_cmd,
@@ -105,8 +84,8 @@ class BaseBuilder(object):
105 84
                     '--name',
106 85
                     container_name
107 86
                 ]
87
+
108 88
                 self.label_arguments(cmd, container)
109
-                self.log.debug("Start container {}.".format(container))
110 89
                 validations_passed = self.container_run_args(cmd,
111 90
                                                              container,
112 91
                                                              container_name)
@@ -137,11 +116,11 @@ class BaseBuilder(object):
137 116
 
138 117
             (cmd_stdout, cmd_stderr, returncode) = self.runner.execute(
139 118
                 cmd, self.log)
140
-
141 119
             if cmd_stdout:
142 120
                 stdout.append(cmd_stdout)
143 121
             if cmd_stderr:
144 122
                 stderr.append(cmd_stderr)
123
+
145 124
             if returncode not in exit_codes:
146 125
                 self.log.error("Error running %s. [%s]\n" % (cmd, returncode))
147 126
                 self.log.error("stdout: %s" % cmd_stdout)

+ 5
- 58
paunch/tests/test_builder_base.py View File

@@ -217,11 +217,11 @@ three-12345678 three''', '', 0),
217 217
             ('', '', 0),
218 218
             # rm six
219 219
             ('', '', 0),
220
-            # inspect config data for two, we made it changed
220
+            # inspect two
221 221
             ('{"start_order": 1, "image": "centos:6"}', '', 0),
222
-            # rm two
222
+            # rm two, changed config data
223 223
             ('', '', 0),
224
-            # inspect config data for three
224
+            # inspect three
225 225
             ('{"start_order": 2, "image": "centos:7"}', '', 0),
226 226
             # ps for after delete_missing_and_updated renames
227 227
             ('', '', 0),
@@ -229,10 +229,8 @@ three-12345678 three''', '', 0),
229 229
             ('three-12345678 three', '', 0),
230 230
             ('Created one-12345678', '', 0),
231 231
             ('Created two-12345678', '', 0),
232
-            # inspect state for three
233
-            ('[{"State": {"Running": true}}]', '', 0),
234 232
             ('Created four-12345678', '', 0),
235
-            ('a\nb\nc', '', 0),
233
+            ('a\nb\nc', '', 0)
236 234
         ]
237 235
         r.discover_container_name = lambda n, c: '%s-12345678' % n
238 236
         r.unique_container_name = lambda n: '%s-12345678' % n
@@ -248,6 +246,7 @@ three-12345678 three''', '', 0),
248 246
             'a\nb\nc'
249 247
         ], stdout)
250 248
         self.assertEqual([], stderr)
249
+
251 250
         exe.assert_has_calls([
252 251
             # inspect image centos:7
253 252
             mock.call(
@@ -307,9 +306,6 @@ three-12345678 three''', '', 0),
307 306
                  '--label', 'config_data=%s' % json.dumps(config['two']),
308 307
                  '--detach=true', 'centos:7'], mock.ANY
309 308
             ),
310
-            # check container three status via inspect
311
-            mock.call(['docker', 'inspect', '--type', 'container',
312
-                       'three-12345678'], mock.ANY, False),
313 309
             # don't run three, its already running
314 310
             # run four
315 311
             mock.call(
@@ -537,52 +533,3 @@ three-12345678 three''', '', 0),
537 533
             ['ls', '-l', '"/foo', 'bar"'],
538 534
             b.command_argument('ls -l "/foo bar"')
539 535
         )
540
-
541
-    @mock.patch('paunch.runner.DockerRunner.execute')
542
-    @mock.patch('paunch.runner.DockerRunner.remove_container')
543
-    @mock.patch('paunch.builder.base.BaseBuilder.pull_missing_images')
544
-    @mock.patch('paunch.builder.base.BaseBuilder.delete_missing_and_updated')
545
-    @mock.patch('paunch.runner.DockerRunner.container_names')
546
-    @mock.patch('paunch.runner.DockerRunner.unique_container_name')
547
-    @mock.patch('paunch.runner.DockerRunner.discover_container_name')
548
-    @mock.patch('paunch.runner.DockerRunner.inspect')
549
-    def test_recreate_stopped_container(self, inspect, discover_container_name,
550
-                                        unique_container_name, container_names,
551
-                                        delete_missing_and_updated,
552
-                                        pull_missing_images, remove_container,
553
-                                        execute):
554
-        orig_call = tenacity.wait.wait_random_exponential.__call__
555
-        orig_argspec = inspect.getargspec(orig_call)
556
-        config = {
557
-            # not running yet
558
-            'one': {
559
-                'start_order': 0,
560
-                'image': 'centos:7'
561
-            }
562
-        }
563
-        r = runner.DockerRunner(managed_by='tester', cont_cmd='docker')
564
-
565
-        with mock.patch('tenacity.wait.wait_random_exponential.__call__') as f:
566
-            f.return_value = 0
567
-
568
-            with mock.patch('inspect.getargspec') as mock_args:
569
-                mock_args.return_value = orig_argspec
570
-                builder = compose1.ComposeV1Builder('foo', config, r)
571
-
572
-        pull_missing_images.return_value = 0
573
-        # No value is returned
574
-        delete_missing_and_updated.return_value = None
575
-        container_names.return_value = [['one', 'one']]
576
-        unique_container_name.return_value = 'one'
577
-        # This can be a no-op since inspect_container() uses this return value
578
-        # and that will also be overridden.
579
-        discover_container_name.return_value = None
580
-        inspect.return_value = {"State": {"Running": False}}
581
-        remove_container.return_value = ('stdout', 'stderr', 0)
582
-        execute.return_value = ('stdout', 'stderr', 0)
583
-        # Run the main apply() method for this test.
584
-        stdout, stderr, deploy_status_code = builder.apply()
585
-        pull_missing_images.assert_called_once()
586
-        container_names.assert_called_once()
587
-        unique_container_name.assert_called_once()
588
-        delete_missing_and_updated.assert_called()

Loading…
Cancel
Save