Skip to content

Commit

Permalink
Check the image is outdated and pop warning to pull the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Oct 3, 2023
1 parent 33c7a26 commit b884cd9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions aiidalab_launch/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ def callback(ctx, param, value): # noqa: U100
)(cmd)


def image_is_latest(docker_client, image: str):
"""Check if the local image has the same digest as the image
on remote registry.
"""
try:
local_image = docker_client.images.get(image)
except docker.errors.ImageNotFound:
return False

try:
remote_image = docker_client.images.get_registry_data(image)
except docker.errors.APIError:
return False

# There is no need to check creation date of the image, since the once
# there is a new image with the same tag, the id will be different.
# We can not use image id, see https://windsock.io/explaining-docker-image-ids/
local_digest = local_image.attrs.get("RepoDigests")[0].split("@")[-1]
remote_digest = remote_image.attrs.get("Descriptor", {}).get("digest")

return local_digest == remote_digest


@click.group(context_settings={"help_option_names": ["-h", "--help"]})
@click.option(
"-v",
Expand Down Expand Up @@ -350,6 +373,13 @@ async def _async_start(
# use local image
msg = f"Using local image '{profile.image}'."

# check if local image is outdated and pull latest version if so
if not image_is_latest(instance.client, profile.image):
click.secho(
"Warning! Local image is outdated, please run with --pull to update.",
fg="yellow",
)

if instance.image is None:
raise click.ClickException(
f"Unable to find image '{profile.image}'. "
Expand Down

0 comments on commit b884cd9

Please sign in to comment.