Skip to content

Commit 0773bef

Browse files
authored
Merge pull request #165 from shanejbrown/convert-to-python-on-whales-part1
Replace stop/start with python-on-whales
2 parents 122d7fd + a9601b7 commit 0773bef

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

buildrunner/docker/runner.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
from docker.utils import compare_version
2424
from retry import retry
2525
import docker.errors
26+
import python_on_whales
2627
import six
2728
import timeout_decorator
2829

30+
from buildrunner.config import BuildRunnerConfig
2931
from buildrunner.docker import (
3032
new_client,
3133
force_remove_container,
@@ -131,6 +133,10 @@ def __init__(self, image_config, dockerd_url=None, log=None):
131133
if log:
132134
log.write("\nImage pulled successfully\n")
133135

136+
self.use_docker_py = (
137+
BuildRunnerConfig.get_instance().run_config.use_legacy_builder
138+
)
139+
134140
def start(
135141
self,
136142
shell="/bin/sh",
@@ -236,7 +242,10 @@ def start(
236242

237243
# start the container
238244
self.container = self.docker_client.create_container(self.image_name, **kwargs)
239-
self.docker_client.start(self.container["Id"])
245+
if self.use_docker_py:
246+
self.docker_client.start(self.container["Id"])
247+
else:
248+
python_on_whales.docker.start(self.container["Id"])
240249

241250
# run any supplied provisioners
242251
if provisioners:
@@ -254,10 +263,13 @@ def stop(self):
254263
Stop the backing Docker container.
255264
"""
256265
if self.container:
257-
self.docker_client.stop(
258-
self.container["Id"],
259-
timeout=0,
260-
)
266+
if self.use_docker_py:
267+
self.docker_client.stop(
268+
self.container["Id"],
269+
timeout=0,
270+
)
271+
else:
272+
python_on_whales.docker.stop(self.container["Id"], time=0)
261273

262274
def cleanup(self):
263275
"""

tests/test_caching.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ def fixture_setup_runner():
3434
"docker.io/ubuntu:19.04",
3535
pull_image=False,
3636
)
37-
runner = DockerRunner(
38-
image_config=image_config,
39-
)
40-
runner.start(working_dir="/root")
37+
with mock.patch(
38+
"buildrunner.docker.runner.BuildRunnerConfig.get_instance"
39+
) as mock_build_runner_config:
40+
# Set to use docker-py to be used over python on whales
41+
mock_build_runner_config.run_config.use_legacy_builder.return_value = True
42+
runner = DockerRunner(
43+
image_config=image_config,
44+
)
45+
runner.start(working_dir="/root")
4146

42-
yield runner
47+
yield runner
4348

44-
runner.cleanup()
49+
runner.cleanup()
4550

4651

4752
@pytest.fixture(name="log_output")

0 commit comments

Comments
 (0)