|
33 | 33 | import tarfile |
34 | 34 | from datetime import datetime, timezone |
35 | 35 | from pathlib import Path |
| 36 | +from uuid import uuid4 |
36 | 37 |
|
37 | 38 | from captain import artifacts, buildah, skopeo |
38 | 39 | from captain.config import Config |
@@ -206,6 +207,37 @@ def _build_platform_image( |
206 | 207 | return current |
207 | 208 |
|
208 | 209 |
|
| 210 | +def _create_push_cleanup( |
| 211 | + image_ids: list[str], |
| 212 | + dest_ref: str, |
| 213 | + logger: StageLogger, |
| 214 | +) -> None: |
| 215 | + """Create a manifest list from *image_ids*, push it to *dest_ref*, and clean up. |
| 216 | +
|
| 217 | + Uses a temporary local manifest name to avoid collisions on repeated |
| 218 | + publishes. After a successful (or failed) push, the local manifest |
| 219 | + and all *image_ids* are removed on a best-effort basis. |
| 220 | + """ |
| 221 | + temp_name = f"captain-local-{uuid4().hex[:12]}" |
| 222 | + manifest_id: str | None = None |
| 223 | + try: |
| 224 | + manifest_id = buildah.manifest_create(temp_name, logger=logger) |
| 225 | + for image_id in image_ids: |
| 226 | + buildah.manifest_add(manifest_id, image_id, logger=logger) |
| 227 | + buildah.manifest_push(manifest_id, dest_ref, logger=logger) |
| 228 | + finally: |
| 229 | + if manifest_id is not None: |
| 230 | + try: |
| 231 | + buildah.rmi(manifest_id, logger=logger) |
| 232 | + except Exception: |
| 233 | + pass |
| 234 | + for image_id in image_ids: |
| 235 | + try: |
| 236 | + buildah.rmi(image_id, logger=logger) |
| 237 | + except Exception: |
| 238 | + pass |
| 239 | + |
| 240 | + |
209 | 241 | def _publish_single_arch( |
210 | 242 | *, |
211 | 243 | layer_tars: list[Path], |
@@ -236,10 +268,7 @@ def _publish_single_arch( |
236 | 268 | ) |
237 | 269 | image_ids.append(image_id) |
238 | 270 |
|
239 | | - manifest_id = buildah.manifest_create(ref, logger=logger) |
240 | | - for image_id in image_ids: |
241 | | - buildah.manifest_add(manifest_id, image_id, logger=logger) |
242 | | - buildah.manifest_push(manifest_id, ref, logger=logger) |
| 271 | + _create_push_cleanup(image_ids, ref, logger) |
243 | 272 |
|
244 | 273 |
|
245 | 274 | def _publish_combined( |
@@ -314,10 +343,7 @@ def _publish_combined( |
314 | 343 | ) |
315 | 344 | image_ids.append(image_id) |
316 | 345 |
|
317 | | - manifest_id = buildah.manifest_create(combined_ref, logger=logger) |
318 | | - for image_id in image_ids: |
319 | | - buildah.manifest_add(manifest_id, image_id, logger=logger) |
320 | | - buildah.manifest_push(manifest_id, combined_ref, logger=logger) |
| 346 | + _create_push_cleanup(image_ids, combined_ref, logger) |
321 | 347 | return True |
322 | 348 |
|
323 | 349 |
|
|
0 commit comments