From 6144179e6cca1458b857ebd09df6d743ebd63870 Mon Sep 17 00:00:00 2001 From: Shane Brown Date: Thu, 22 Aug 2024 10:42:41 -0600 Subject: [PATCH] Mark docker-py calls --- buildrunner/__init__.py | 4 ++++ buildrunner/docker/__init__.py | 1 + buildrunner/docker/builder.py | 3 +++ buildrunner/docker/daemon.py | 8 ++++++++ buildrunner/docker/importer.py | 1 + buildrunner/docker/runner.py | 18 +++++++++++++++++- buildrunner/sshagent/__init__.py | 1 + buildrunner/steprunner/tasks/build.py | 1 + buildrunner/steprunner/tasks/push.py | 2 ++ buildrunner/steprunner/tasks/run.py | 8 ++++++++ 10 files changed, 46 insertions(+), 1 deletion(-) diff --git a/buildrunner/__init__.py b/buildrunner/__init__.py index 97945bb6..23c001cd 100644 --- a/buildrunner/__init__.py +++ b/buildrunner/__init__.py @@ -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: @@ -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, @@ -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, diff --git a/buildrunner/docker/__init__.py b/buildrunner/docker/__init__.py index d7267e6b..aaa51754 100644 --- a/buildrunner/docker/__init__.py +++ b/buildrunner/docker/__init__.py @@ -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, diff --git a/buildrunner/docker/builder.py b/buildrunner/docker/builder.py index 962346b9..5066cdb6 100644 --- a/buildrunner/docker/builder.py +++ b/buildrunner/docker/builder.py @@ -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, @@ -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, @@ -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( diff --git a/buildrunner/docker/daemon.py b/buildrunner/docker/daemon.py index 21a6b7c6..053c4755 100644 --- a/buildrunner/docker/daemon.py +++ b/buildrunner/docker/daemon.py @@ -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 @@ -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" @@ -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, diff --git a/buildrunner/docker/importer.py b/buildrunner/docker/importer.py index 074cfab9..2646a85a 100644 --- a/buildrunner/docker/importer.py +++ b/buildrunner/docker/importer.py @@ -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( diff --git a/buildrunner/docker/runner.py b/buildrunner/docker/runner.py index e2693ba8..e75240c5 100644 --- a/buildrunner/docker/runner.py +++ b/buildrunner/docker/runner.py @@ -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) @@ -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 ): @@ -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, @@ -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"]) @@ -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, @@ -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, @@ -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, @@ -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 ) @@ -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}/." ) @@ -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, @@ -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: @@ -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"], ) @@ -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"], ) @@ -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"], ) @@ -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"] diff --git a/buildrunner/sshagent/__init__.py b/buildrunner/sshagent/__init__.py index 7c04977e..d56dddf4 100644 --- a/buildrunner/sshagent/__init__.py +++ b/buildrunner/sshagent/__init__.py @@ -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 diff --git a/buildrunner/steprunner/tasks/build.py b/buildrunner/steprunner/tasks/build.py index d6ef0f35..5351d7a6 100644 --- a/buildrunner/steprunner/tasks/build.py +++ b/buildrunner/steprunner/tasks/build.py @@ -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 ) diff --git a/buildrunner/steprunner/tasks/push.py b/buildrunner/steprunner/tasks/push.py index 8afba65b..f90d4aed 100644 --- a/buildrunner/steprunner/tasks/push.py +++ b/buildrunner/steprunner/tasks/push.py @@ -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() @@ -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, diff --git a/buildrunner/steprunner/tasks/run.py b/buildrunner/steprunner/tasks/run.py index 618627ad..a39a862c 100644 --- a/buildrunner/steprunner/tasks/run.py +++ b/buildrunner/steprunner/tasks/run.py @@ -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, ) @@ -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" ] @@ -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"]: @@ -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, @@ -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, @@ -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, @@ -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", {})