Skip to content

Commit

Permalink
container: Manipulate Dangerzone image tags
Browse files Browse the repository at this point in the history
Add the following methods that allow the `Container` isolation provider
to work with tags for the Dangerzone image:
* `list_image_tag()`
* `delete_image_tag()`
* `add_image_tag()`
  • Loading branch information
apyrgio committed Dec 2, 2024
1 parent b542668 commit c133a5a
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions dangerzone/isolation_provider/container.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gzip
import json
import logging
import os
import platform
Expand Down Expand Up @@ -154,6 +155,73 @@ def get_runtime_security_args() -> List[str]:

return security_args

@staticmethod
def list_image_tags() -> dict[str, str]:
"""Get the tags of all loaded Dangerzone images.
This method returns a mapping of image tags to image IDs, for all Dangerzone
images. This can be useful when we want to find which are the local image tags,
and which image ID does the "latest" tag point to.
"""
images = json.loads(
subprocess.check_output(
[
Container.get_runtime(),
"image",
"list",
"--format",
"json",
Container.CONTAINER_NAME,
],
text=True,
startupinfo=get_subprocess_startupinfo(),
)
)

# Grab every image name and associate it with an image ID.
tags = {}
for image in images:
for name in image["Names"]:
tag = name.split(":")[1]
tags[tag] = image["Id"]

return tags

@staticmethod
def delete_image_tag(tag: str) -> None:
"""Delete a Dangerzone image tag."""
name = Container.CONTAINER_NAME + ":" + tag
log.warning(f"Deleting old container image: {name}")
try:
subprocess.check_output(
[Container.get_runtime(), "rmi", "--force", name],
startupinfo=get_subprocess_startupinfo(),
)
except Exception as e:
log.warning(
f"Couldn't delete old container image '{name}', so leaving it there."
f" Original error: {e}"
)

@staticmethod
def add_image_tag(cur_tag: str, new_tag: str) -> None:
"""Add a tag to an existing Dangerzone image."""
cur_image_name = Container.CONTAINER_NAME + ":" + cur_tag
new_image_name = Container.CONTAINER_NAME + ":" + new_tag
subprocess.check_output(
[
Container.get_runtime(),
"tag",
cur_image_name,
new_image_name,
],
startupinfo=get_subprocess_startupinfo(),
)

log.info(
f"Successfully tagged container image '{cur_image_name}' as '{new_image_name}'"
)

@staticmethod
def install() -> bool:
"""
Expand Down

0 comments on commit c133a5a

Please sign in to comment.