Skip to content

Commit

Permalink
validation: improve detection of local images
Browse files Browse the repository at this point in the history
Delegate the detection of local docker images to `docker`, instead of
manually checking the list of local images. This also handles correctly
fully qualified image names.
  • Loading branch information
mdonadoni committed Aug 9, 2023
1 parent 0318b3f commit e074ef3
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions reana_client/validation/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
WORKFLOW_RUNTIME_USER_GID,
WORKFLOW_RUNTIME_USER_UID,
)
from reana_commons.utils import run_command


from reana_client.errors import EnvironmentValidationError
Expand Down Expand Up @@ -222,24 +223,22 @@ def _validate_image_tag(self, image):
def _image_exists_locally(self, image, tag):
"""Verify if image exists locally."""
full_image = self._get_full_image_name(image, tag or "latest")
local_images = self._get_local_docker_images()
if full_image in local_images:
image_id = run_command(
f'docker images -q "{full_image}"', display=False, return_output=True
)
if image_id:
self.messages.append(
{
"type": "success",
"message": "Environment image {} exists locally.".format(
full_image
),
"message": f"Environment image {full_image} exists locally.",
}
)
return True
else:
self.messages.append(
{
"type": "warning",
"message": "Environment image {} does not exist locally.".format(
full_image
),
"message": f"Environment image {full_image} does not exist locally.",
}
)
return False
Expand Down Expand Up @@ -399,19 +398,6 @@ def _get_full_image_name(self, image, tag=None):
"""Return full image name with tag if is passed."""
return "{}{}".format(image, ":{}".format(tag) if tag else "")

def _get_local_docker_images(self):
"""Return a list with local docker images."""
from reana_commons.utils import run_command

# Check if docker is installed.
run_command("docker version", display=False, return_output=True)
docker_images = run_command(
'docker images --format "{{ .Repository }}:{{ .Tag }}"',
display=False,
return_output=True,
)
return docker_images.splitlines()


class EnvironmentValidatorSerial(EnvironmentValidatorBase):
"""REANA serial workflow environments validation."""
Expand Down

0 comments on commit e074ef3

Please sign in to comment.