|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +OCI podman |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +from os import PathLike |
| 9 | +from pathlib import Path |
| 10 | +from tarfile import open as tarfile_open |
| 11 | +from urllib.parse import urlencode |
| 12 | +from tempfile import TemporaryDirectory |
| 13 | +from typing import Any, Dict, List, Optional |
| 14 | + |
| 15 | +from podman.domain.images import Image as _Image |
| 16 | + |
| 17 | +from ..constants import ( |
| 18 | + PODMAN_FS_CHANGE_ADDED, |
| 19 | + PODMAN_FS_CHANGE_DELETED, |
| 20 | + PODMAN_FS_CHANGE_MODIFIED, |
| 21 | + PODMAN_FS_CHANGE_UNSUPPORTED, |
| 22 | +) |
| 23 | +from .podman_context import PodmanContext |
| 24 | +from .podman_object_context import PodmanObjectContext |
| 25 | + |
| 26 | +PODMAN_CHANGES_KINDS = { |
| 27 | + 0: PODMAN_FS_CHANGE_MODIFIED, |
| 28 | + 1: PODMAN_FS_CHANGE_ADDED, |
| 29 | + 2: PODMAN_FS_CHANGE_DELETED, |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +class Image(PodmanObjectContext): |
| 34 | + """ |
| 35 | + Podman image class with extended API features support. |
| 36 | +
|
| 37 | + :author: Garden Linux Maintainers |
| 38 | + :copyright: Copyright 2024 SAP SE |
| 39 | + :package: gardenlinux |
| 40 | + :subpackage: oci |
| 41 | + :since: 1.0.0 |
| 42 | + :license: https://www.apache.org/licenses/LICENSE-2.0 |
| 43 | + Apache License, Version 2.0 |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, image: _Image, logger: Optional[logging.Logger] = None): |
| 47 | + """ |
| 48 | + Constructor __init__(Image) |
| 49 | +
|
| 50 | + :since: 1.0.0 |
| 51 | + """ |
| 52 | + |
| 53 | + PodmanObjectContext.__init__(self, logger) |
| 54 | + self._image_id = image.id |
| 55 | + |
| 56 | + @property |
| 57 | + def id(self) -> str: |
| 58 | + """ |
| 59 | + podman-py.readthedocs.io: Returns the identifier for the object. |
| 60 | +
|
| 61 | + :return: (str) Identifier for the object |
| 62 | + :since: 1.0.0 |
| 63 | + """ |
| 64 | + |
| 65 | + return self._image_id # type: ignore[no-any-return] |
| 66 | + |
| 67 | + @property |
| 68 | + @PodmanContext.wrap |
| 69 | + def labels(self, podman: PodmanContext) -> Dict[str, str]: |
| 70 | + """ |
| 71 | + podman-py.readthedocs.io: Returns the identifier for the object. |
| 72 | +
|
| 73 | + :return: (str) Identifier for the object |
| 74 | + :since: 1.0.0 |
| 75 | + """ |
| 76 | + |
| 77 | + return self._get(podman=podman).labels # type: ignore[no-any-return] |
| 78 | + |
| 79 | + @property |
| 80 | + @PodmanContext.wrap |
| 81 | + def layer_image_ids(self, podman: PodmanContext) -> List[str]: |
| 82 | + """ |
| 83 | + Returns the podman image IDs of all parent layers. |
| 84 | +
|
| 85 | + :param podman: Podman context |
| 86 | +
|
| 87 | + :return: (list) Podman layer image IDs |
| 88 | + :since: 1.0.0 |
| 89 | + """ |
| 90 | + |
| 91 | + return [ |
| 92 | + image_data["Id"] |
| 93 | + for image_data in self.history(podman=podman) |
| 94 | + if len(image_data["Id"]) == 64 |
| 95 | + ] |
| 96 | + |
| 97 | + def __getattr__( |
| 98 | + self, |
| 99 | + name: str, |
| 100 | + ) -> Any: |
| 101 | + """ |
| 102 | + python.org: Called when an attribute lookup has not found the attribute in |
| 103 | + the usual places (i.e. it is not an instance attribute nor is it found in the |
| 104 | + class tree for self). |
| 105 | +
|
| 106 | + :param name: Attribute name |
| 107 | +
|
| 108 | + :return: (mixed) Attribute |
| 109 | + :since: 1.0.0 |
| 110 | + """ |
| 111 | + |
| 112 | + @PodmanObjectContext.wrap |
| 113 | + def wrapped_context(podman: PodmanContext, *args: Any, **kwargs: Any) -> Any: |
| 114 | + """ |
| 115 | + Wrapping function to use the podman context. |
| 116 | + """ |
| 117 | + |
| 118 | + py_attr = getattr(self._get(podman=podman), name) |
| 119 | + return py_attr(*args, **kwargs) |
| 120 | + |
| 121 | + return wrapped_context |
| 122 | + |
| 123 | + def _get(self, podman: PodmanContext) -> _Image: |
| 124 | + """ |
| 125 | + Returns the underlying podman image object. |
| 126 | +
|
| 127 | + :param podman: Podman context |
| 128 | +
|
| 129 | + :return: (podman.domains.images.Image) Podman image object |
| 130 | + :since: 1.0.0 |
| 131 | + """ |
| 132 | + |
| 133 | + return podman.images.get(self._image_id) |
| 134 | + |
| 135 | + @PodmanContext.wrap |
| 136 | + def get_filesystem_changes( |
| 137 | + self, podman: PodmanContext, parent_layer_image_id: Optional[str] = None |
| 138 | + ) -> Dict[str, List[str]]: |
| 139 | + """ |
| 140 | + Returns the underlying podman image object. |
| 141 | +
|
| 142 | + :param podman: Podman context |
| 143 | +
|
| 144 | + :return: (_Image) Podman image object |
| 145 | + :since: 1.0.0 |
| 146 | + """ |
| 147 | + |
| 148 | + changes: Dict[str, List[str]] = { |
| 149 | + PODMAN_FS_CHANGE_ADDED: [], |
| 150 | + PODMAN_FS_CHANGE_DELETED: [], |
| 151 | + PODMAN_FS_CHANGE_MODIFIED: [], |
| 152 | + PODMAN_FS_CHANGE_UNSUPPORTED: [], |
| 153 | + } |
| 154 | + |
| 155 | + query = "" |
| 156 | + |
| 157 | + if parent_layer_image_id is not None: |
| 158 | + query = urlencode({"parent": parent_layer_image_id}) |
| 159 | + |
| 160 | + resp = self._raw_request( |
| 161 | + "get", f"/images/{self._image_id}/changes?{query}", podman=podman |
| 162 | + ) |
| 163 | + |
| 164 | + resp.raise_for_status() |
| 165 | + |
| 166 | + for entry in resp.json(): |
| 167 | + changes[ |
| 168 | + PODMAN_CHANGES_KINDS.get(entry["Kind"], PODMAN_FS_CHANGE_UNSUPPORTED) |
| 169 | + ].append(entry["Path"]) |
| 170 | + |
| 171 | + return changes |
| 172 | + |
| 173 | + @staticmethod |
| 174 | + @PodmanContext.wrap |
| 175 | + def import_plain_tar( |
| 176 | + tar_file_name: PathLike[str], podman: PodmanContext |
| 177 | + ) -> str: |
| 178 | + """ |
| 179 | + Import a plain filesystem tar archive into an OCI image. |
| 180 | +
|
| 181 | + :param tar_file_name: Plain filesystem tar archive |
| 182 | + :param podman: Podman context |
| 183 | +
|
| 184 | + :return: (str) Podman image ID |
| 185 | + :since: 1.0.0 |
| 186 | + """ |
| 187 | + |
| 188 | + image_id = None |
| 189 | + |
| 190 | + with TemporaryDirectory() as tmpdir: |
| 191 | + container_file_name = Path(tmpdir, "ContainerFile") |
| 192 | + tarfile_open(tar_file_name, dereference=True).extractall( |
| 193 | + path=Path(tmpdir, "archive_content"), |
| 194 | + filter="fully_trusted", |
| 195 | + numeric_owner=True, |
| 196 | + ) |
| 197 | + |
| 198 | + with container_file_name.open("w") as container_file: |
| 199 | + container_file.write("FROM scratch\nCOPY archive_content/ /") |
| 200 | + |
| 201 | + image, _ = podman.images.build(path=tmpdir, dockerfile=container_file_name) |
| 202 | + image_id = image.id |
| 203 | + |
| 204 | + return image_id # type: ignore[no-any-return] |
0 commit comments