X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7334ee9ee6350f2b5f0384d8166e7c4f58d86864..540b72d62a94015f116ba077e279a5f10d666778:/sdk/cwl/arvados_cwl/arvdocker.py diff --git a/sdk/cwl/arvados_cwl/arvdocker.py b/sdk/cwl/arvados_cwl/arvdocker.py index 04e2a4cffc..f5e67a6649 100644 --- a/sdk/cwl/arvados_cwl/arvdocker.py +++ b/sdk/cwl/arvados_cwl/arvdocker.py @@ -17,9 +17,6 @@ import arvados.commands.keepdocker logger = logging.getLogger('arvados.cwl-runner') -cached_lookups = {} -cached_lookups_lock = threading.Lock() - def determine_image_id(dockerImageId): for line in ( subprocess.check_output( # nosec @@ -56,10 +53,16 @@ def determine_image_id(dockerImageId): return None -def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid, - force_pull, tmp_outdir_prefix, match_local_docker): +def arv_docker_get_image(api_client, dockerRequirement, pull_image, runtimeContext): """Check if a Docker image is available in Keep, if not, upload it using arv-keepdocker.""" + project_uuid = runtimeContext.project_uuid + force_pull = runtimeContext.force_docker_pull + tmp_outdir_prefix = runtimeContext.tmp_outdir_prefix + match_local_docker = runtimeContext.match_local_docker + copy_deps = runtimeContext.copy_deps + cached_lookups = runtimeContext.cached_docker_lookups + if "http://arvados.org/cwl#dockerCollectionPDH" in dockerRequirement: return dockerRequirement["http://arvados.org/cwl#dockerCollectionPDH"] @@ -69,22 +72,29 @@ def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid if hasattr(dockerRequirement, 'lc'): dockerRequirement.lc.data["dockerImageId"] = dockerRequirement.lc.data["dockerPull"] - global cached_lookups - global cached_lookups_lock - with cached_lookups_lock: - if dockerRequirement["dockerImageId"] in cached_lookups: - return cached_lookups[dockerRequirement["dockerImageId"]] + if dockerRequirement["dockerImageId"] in cached_lookups: + return cached_lookups[dockerRequirement["dockerImageId"]] with SourceLine(dockerRequirement, "dockerImageId", WorkflowException, logger.isEnabledFor(logging.DEBUG)): sp = dockerRequirement["dockerImageId"].split(":") image_name = sp[0] image_tag = sp[1] if len(sp) > 1 else "latest" - images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3, + out_of_project_images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3, image_name=image_name, - image_tag=image_tag) + image_tag=image_tag, + project_uuid=None) - if images and match_local_docker: + if copy_deps: + # Only images that are available in the destination project + images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3, + image_name=image_name, + image_tag=image_tag, + project_uuid=project_uuid) + else: + images = out_of_project_images + + if match_local_docker: local_image_id = determine_image_id(dockerRequirement["dockerImageId"]) if local_image_id: # find it in the list @@ -98,15 +108,26 @@ def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid # force re-upload. images = [] + for i in out_of_project_images: + if i[1]["dockerhash"] == local_image_id: + found = True + out_of_project_images = [i] + break + if not found: + # force re-upload. + out_of_project_images = [] + if not images: - # Fetch Docker image if necessary. - try: - result = cwltool.docker.DockerCommandLineJob.get_image(dockerRequirement, pull_image, - force_pull, tmp_outdir_prefix) - if not result: - raise WorkflowException("Docker image '%s' not available" % dockerRequirement["dockerImageId"]) - except OSError as e: - raise WorkflowException("While trying to get Docker image '%s', failed to execute 'docker': %s" % (dockerRequirement["dockerImageId"], e)) + if not out_of_project_images: + # Fetch Docker image if necessary. + try: + dockerjob = cwltool.docker.DockerCommandLineJob(None, None, None, None, None, None) + result = dockerjob.get_image(dockerRequirement, pull_image, + force_pull, tmp_outdir_prefix) + if not result: + raise WorkflowException("Docker image '%s' not available" % dockerRequirement["dockerImageId"]) + except OSError as e: + raise WorkflowException("While trying to get Docker image '%s', failed to execute 'docker': %s" % (dockerRequirement["dockerImageId"], e)) # Upload image to Arvados args = [] @@ -125,20 +146,14 @@ def arv_docker_get_image(api_client, dockerRequirement, pull_image, project_uuid images = arvados.commands.keepdocker.list_images_in_arv(api_client, 3, image_name=image_name, - image_tag=image_tag) + image_tag=image_tag, + project_uuid=project_uuid) if not images: raise WorkflowException("Could not find Docker image %s:%s" % (image_name, image_tag)) pdh = api_client.collections().get(uuid=images[0][0]).execute()["portable_data_hash"] - with cached_lookups_lock: - cached_lookups[dockerRequirement["dockerImageId"]] = pdh + cached_lookups[dockerRequirement["dockerImageId"]] = pdh return pdh - -def arv_docker_clear_cache(): - global cached_lookups - global cached_lookups_lock - with cached_lookups_lock: - cached_lookups = {}