浏览代码

Fix Reducing the amount being downloaded every time the hash changes. (#4078)

tofarr 1 年之前
父节点
当前提交
c5025fb66e

+ 2 - 1
openhands/runtime/builder/base.py

@@ -26,12 +26,13 @@ class RuntimeBuilder(abc.ABC):
         pass
         pass
 
 
     @abc.abstractmethod
     @abc.abstractmethod
-    def image_exists(self, image_name: str) -> bool:
+    def image_exists(self, image_name: str, pull_from_repo: bool = True) -> bool:
         """
         """
         Check if the runtime image exists.
         Check if the runtime image exists.
 
 
         Args:
         Args:
             image_name (str): The name of the runtime image (e.g., "repo:sha").
             image_name (str): The name of the runtime image (e.g., "repo:sha").
+            pull_from_repo (bool): Whether to pull from the remote repo if the image not present locally
 
 
         Returns:
         Returns:
             bool: Whether the runtime image exists.
             bool: Whether the runtime image exists.

+ 7 - 1
openhands/runtime/builder/docker.py

@@ -166,11 +166,12 @@ class DockerRuntimeBuilder(RuntimeBuilder):
         )
         )
         return target_image_hash_name
         return target_image_hash_name
 
 
-    def image_exists(self, image_name: str) -> bool:
+    def image_exists(self, image_name: str, pull_from_repo: bool = True) -> bool:
         """Check if the image exists in the registry (try to pull it first) or in the local store.
         """Check if the image exists in the registry (try to pull it first) or in the local store.
 
 
         Args:
         Args:
             image_name (str): The Docker image to check (<image repo>:<image tag>)
             image_name (str): The Docker image to check (<image repo>:<image tag>)
+            pull_from_repo (bool): Whether to pull from the remote repo if the image not present locally
         Returns:
         Returns:
             bool: Whether the Docker image exists in the registry or in the local store
             bool: Whether the Docker image exists in the registry or in the local store
         """
         """
@@ -184,6 +185,11 @@ class DockerRuntimeBuilder(RuntimeBuilder):
             logger.debug('Image found locally.')
             logger.debug('Image found locally.')
             return True
             return True
         except docker.errors.ImageNotFound:
         except docker.errors.ImageNotFound:
+            if not pull_from_repo:
+                logger.debug(
+                    f'Image {image_name} not found locally'
+                )
+                return False
             try:
             try:
                 logger.debug(
                 logger.debug(
                     'Image not found locally. Trying to pull it, please wait...'
                     'Image not found locally. Trying to pull it, please wait...'

+ 1 - 1
openhands/runtime/builder/remote.py

@@ -98,7 +98,7 @@ class RemoteRuntimeBuilder(RuntimeBuilder):
             # Wait before polling again
             # Wait before polling again
             sleep_if_should_continue(30)
             sleep_if_should_continue(30)
 
 
-    def image_exists(self, image_name: str) -> bool:
+    def image_exists(self, image_name: str, pull_from_repo: bool = True) -> bool:
         """Checks if an image exists in the remote registry using the /image_exists endpoint."""
         """Checks if an image exists in the remote registry using the /image_exists endpoint."""
         params = {'image': image_name}
         params = {'image': image_name}
         response = send_request(
         response = send_request(

+ 1 - 1
openhands/runtime/utils/runtime_build.py

@@ -251,7 +251,7 @@ def build_runtime_image(
 
 
     # Scenario 1: If we already have an image with the exact same hash, then it means the image is already built
     # Scenario 1: If we already have an image with the exact same hash, then it means the image is already built
     # with the exact same source code and Dockerfile, so we will reuse it. Building it is not required.
     # with the exact same source code and Dockerfile, so we will reuse it. Building it is not required.
-    if not force_rebuild and runtime_builder.image_exists(hash_runtime_image_name):
+    if not force_rebuild and runtime_builder.image_exists(hash_runtime_image_name, False):
         logger.info(
         logger.info(
             f'Image [{hash_runtime_image_name}] already exists so we will reuse it.'
             f'Image [{hash_runtime_image_name}] already exists so we will reuse it.'
         )
         )