Skip to content

Commit

Permalink
Mark docker-py calls
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejbrown committed Aug 22, 2024
1 parent 9f5462e commit 6144179
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions buildrunner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,14 @@ def run(self): # pylint: disable=too-many-statements,too-many-branches,too-many
push_kwargs = {
"stream": True,
}
# TODO replace with python on whales
if (
"insecure_registry"
in inspect.getfullargspec(_docker_client.push).args
):
push_kwargs["insecure_registry"] = _insecure_registry

# TODO replace with python on whales
stream = _docker_client.push(_repo_tag, **push_kwargs)
previous_status = None
for msg_str in stream:
Expand Down Expand Up @@ -500,6 +502,7 @@ def run(self): # pylint: disable=too-many-statements,too-many-branches,too-many
# cleanup the source image
if self._source_image:
self.log.write(f"Destroying source image {self._source_image}\n")
# TODO replace with python on whales
_docker_client.remove_image(
self._source_image,
noprune=False,
Expand All @@ -512,6 +515,7 @@ def run(self): # pylint: disable=too-many-statements,too-many-branches,too-many
# reverse the order of the images since child images would likely come after parent images
for _image in self.generated_images[::-1]:
try:
# TODO replace with python on whales
_docker_client.remove_image(
_image,
noprune=False,
Expand Down
1 change: 1 addition & 0 deletions buildrunner/docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def force_remove_container(docker_client, container):
:param docker_client: the docker client
:param container: the container
"""
# TODO replace with python on whales
docker_client.remove_container(
container,
force=True,
Expand Down
3 changes: 3 additions & 0 deletions buildrunner/docker/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def build(
tfile.add(self.dockerfile, arcname="./Dockerfile")
_fileobj.seek(0)

# TODO don't need to replace with python on whales
stream = self.docker_client.build(
path=None,
nocache=nocache,
Expand All @@ -206,6 +207,7 @@ def build(
target=target,
)
else:
# TODO don't need to replace with python on whales
stream = self.docker_client.build(
path=self.path,
nocache=nocache,
Expand Down Expand Up @@ -277,6 +279,7 @@ def cleanup(self):
# iterate through and destroy intermediate containers
for container in self.intermediate_containers:
try:
# TODO don't need to replace with python on whales
force_remove_container(self.docker_client, container)
except docker.errors.APIError as err:
logger.debug(
Expand Down
8 changes: 8 additions & 0 deletions buildrunner/docker/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DockerDaemonProxy:
Class used to encapsulate Docker daemon information within a container.
"""

# TODO replace with python on whales
def __init__(self, docker_client, log, docker_registry):
""" """
self.docker_client = docker_client
Expand Down Expand Up @@ -76,12 +77,18 @@ def start(self):
self._env["DOCKER_HOST"] = "unix:///dockerdaemon/docker.sock"

# create and start the Docker container
image_name = f"{self.docker_registry}/busybox:latest"
# TODO replace with python on whales
self.docker_client.pull(image_name)
# TODO replace with python on whales
self._daemon_container = self.docker_client.create_container(
f"{self.docker_registry}/{DAEMON_IMAGE_NAME}",
command="/bin/sh",
volumes=_volumes,
# TODO replace with python on whales
host_config=self.docker_client.create_host_config(binds=_binds),
)["Id"]
# TODO replace with python on whales
self.docker_client.start(self._daemon_container)
self.log.write(
f"Created Docker daemon container {self._daemon_container:.10}\n"
Expand All @@ -96,6 +103,7 @@ def stop(self):
f"Destroying Docker daemon container {self._daemon_container:.10}\n"
)
if self._daemon_container:
# TODO replace with python on whales
self.docker_client.remove_container(
self._daemon_container,
force=True,
Expand Down
1 change: 1 addition & 0 deletions buildrunner/docker/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def import_image(self):
"""

try:
# TODO replace with python on whales
import_return = self.docker_client.import_image(self.src)
except docker.errors.APIError as apie:
raise BuildRunnerProcessingError(
Expand Down
18 changes: 17 additions & 1 deletion buildrunner/docker/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def __init__(self, image_config, dockerd_url=None, log=None):
#
# Pull all images to ensure we get the hashes for intermediate images
found_image = False
# TODO replace with python on whales
for image in self.docker_client.images(all=True):
if (
image["Id"].startswith("sha256:" + self.image_name)
Expand All @@ -124,6 +125,7 @@ def __init__(self, image_config, dockerd_url=None, log=None):
if log:
log.write(f"Pulling image {self.image_name}\n")
with DockerPullProgress() as docker_progress:
# TODO replace with python on whales
for data in self.docker_client.pull(
self.image_name, stream=True, decode=True, platform=self.platform
):
Expand Down Expand Up @@ -214,6 +216,7 @@ def start(
"user": user,
"working_dir": working_dir,
"hostname": hostname,
# TODO replace with python on whales
"host_config": self.docker_client.create_host_config(
binds=_binds,
links=links,
Expand All @@ -231,9 +234,11 @@ def start(
kwargs["entrypoint"] = entrypoint
del kwargs["command"]

# TODO replace with python on whales
if compare_version("1.10", self.docker_client.api_version) < 0:
kwargs["dns"] = dns

# TODO replace with python on whales
# start the container
self.container = self.docker_client.create_container(self.image_name, **kwargs)
self.docker_client.start(self.container["Id"])
Expand All @@ -254,6 +259,7 @@ def stop(self):
Stop the backing Docker container.
"""
if self.container:
# TODO replace with python on whales
self.docker_client.stop(
self.container["Id"],
timeout=0,
Expand All @@ -269,11 +275,13 @@ def cleanup(self):
force_remove_container(self.docker_client, container)
except docker.errors.NotFound:
try:
# TODO replace with python on whales
container_ids = self.docker_client.containers(
filters={"label": container}, quiet=True
)
if container_ids:
for container_id in container_ids:
# TODO replace with python on whales
self.docker_client.remove_container(
container_id["Id"],
force=True,
Expand All @@ -287,7 +295,7 @@ def cleanup(self):
print(
f'Unable to find docker container with name or label "{container}"'
)

# TODO replace with python on whales
self.docker_client.remove_container(
self.container["Id"],
force=True,
Expand Down Expand Up @@ -354,6 +362,7 @@ def _put_cache_in_container(self, docker_path: str, file_obj: io.IOBase) -> bool
:param file_obj: Opened file object of cache
:return: True if the call succeeds.
"""
# TODO replace with python on whales
return self.docker_client.put_archive(
self.container["Id"], docker_path, file_obj
)
Expand Down Expand Up @@ -429,6 +438,7 @@ def _write_cache(self, docker_path: str, file_obj: io.IOBase):
:param docker_path: Path of file or folder in the container
:param file_obj: Opened file object to write cache
"""
# TODO replace with python on whales
bits, _ = self.docker_client.get_archive(
self.container["Id"], f"{docker_path}/."
)
Expand Down Expand Up @@ -556,6 +566,7 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None):
tty=False,
# workdir=workdir,
)
# TODO replace with python on whales
output_buffer = self.docker_client.exec_start(
create_res,
stream=stream,
Expand All @@ -581,6 +592,7 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None):
console.write(warning)
if log:
log.write(warning)
# TODO replace with python on whales
inspect_res = self.docker_client.exec_inspect(create_res)
if "ExitCode" in inspect_res:
if inspect_res["ExitCode"] is None:
Expand Down Expand Up @@ -628,6 +640,7 @@ def _get_status(self):
"""
status = None
try:
# TODO replace with python on whales
status = self.docker_client.inspect_container(
self.container["Id"],
)
Expand All @@ -642,6 +655,7 @@ def get_ip(self):
ipaddr = None
try:
if self.is_running():
# TODO replace with python on whales
inspection = self.docker_client.inspect_container(
self.container["Id"],
)
Expand Down Expand Up @@ -680,6 +694,7 @@ def attach_until_finished(self, stream=None):
Attach to the container, writing output to the given log stream until
the container exits.
"""
# TODO replace with python on whales
docker_socket: socket.SocketIO = self.docker_client.attach_socket(
self.container["Id"],
)
Expand Down Expand Up @@ -710,6 +725,7 @@ def commit(self, stream):
stream.write(
f"Committing build container {self.container['Id']:.10} as an image...\n"
)
# TODO replace with python on whales
self.committed_image = self.docker_client.commit(
self.container["Id"],
)["Id"]
Expand Down
1 change: 1 addition & 0 deletions buildrunner/sshagent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class DockerSSHAgentProxy:
implementation that is managed by this class.
"""

# TODO replace with python on whales
def __init__(self, docker_client, log, docker_registry):
""" """
self.docker_client = docker_client
Expand Down
1 change: 1 addition & 0 deletions buildrunner/steprunner/tasks/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
for cache_from_image in self.cache_from:
if isinstance(cache_from_image, str):
try:
# TODO replace with python on whales
self._docker_client.pull(
cache_from_image, platform=self.platform
)
Expand Down
2 changes: 2 additions & 0 deletions buildrunner/steprunner/tasks/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def _security_scan_trivy(
) -> dict:
# Pull image for scanning (if not already pulled) so that it can be scanned locally
if pull:
# TODO replace with python on whales
self._docker_client.pull(repository, tag)

buildrunner_config = BuildRunnerConfig.get_instance()
Expand Down Expand Up @@ -400,6 +401,7 @@ def run(self, context): # pylint: disable=too-many-branches

# Tag the image
for tag in repo.tags:
# TODO replace with python on whales
self._docker_client.tag(
image_to_use,
repo.repository,
Expand Down
8 changes: 8 additions & 0 deletions buildrunner/steprunner/tasks/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ def _get_source_container(self):
source container.
"""
if not self._source_container:
# TODO replace with python on whales
self._source_container = self._docker_client.create_container(
self.step_runner.build_runner.get_source_image(),
command="/bin/sh",
)["Id"]
# TODO replace with python on whales
self._docker_client.start(
self._source_container,
)
Expand Down Expand Up @@ -655,6 +657,7 @@ def wait(self, name, wait_for_data):
"""
Wait for listening port on named container
"""
# TODO replace with python on whales
ipaddr = self._docker_client.inspect_container(name)["NetworkSettings"][
"IPAddress"
]
Expand Down Expand Up @@ -682,6 +685,7 @@ def wait(self, name, wait_for_data):
)

# check that the container is still available
# TODO replace with python on whales
container = self._docker_client.inspect_container(name)
container_status = container.get("State", {}).get("Status")
if container_status not in ["created", "running"]:
Expand Down Expand Up @@ -821,6 +825,7 @@ def run(self, context: dict): # pylint: disable=too-many-statements,too-many-br

# see if we need to inject ssh keys
if self.step.ssh_keys:
# TODO replace with python on whales
self._sshagent = DockerSSHAgentProxy(
self._docker_client,
self.step_runner.log,
Expand All @@ -833,6 +838,7 @@ def run(self, context: dict): # pylint: disable=too-many-statements,too-many-br
)

# start the docker daemon proxy
# TODO replace with python on whales
self._dockerdaemonproxy = DockerDaemonProxy(
self._docker_client,
self.step_runner.log,
Expand Down Expand Up @@ -1133,6 +1139,7 @@ def cleanup(self, context): # pylint: disable=unused-argument
self.step_runner.log.write(
f"Destroying source container {self._source_container:.10}\n"
)
# TODO replace with python on whales
self._docker_client.remove_container(
self._source_container,
force=True,
Expand All @@ -1152,6 +1159,7 @@ def is_systemd(self, run_service: RunAndServicesBase, image, logger):
rval = run_service.systemd
else:
labels = (
# TODO replace with python on whales
self._docker_client.inspect_image(image)
.get("Config", {})
.get("Labels", {})
Expand Down

0 comments on commit 6144179

Please sign in to comment.