|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +S3 GardenLinux artifacts |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +from configparser import ConfigParser, UNNAMED_SECTION |
| 9 | +from datetime import datetime |
| 10 | +from hashlib import file_digest |
| 11 | +from os import PathLike, stat |
| 12 | +from pathlib import Path |
| 13 | +from tempfile import TemporaryFile |
| 14 | +from typing import Optional |
| 15 | +from urllib.parse import urlencode |
| 16 | + |
| 17 | +from .bucket import Bucket |
| 18 | +from ..features.cname import CName |
| 19 | +from ..logger import LoggerSetup |
| 20 | + |
| 21 | + |
| 22 | +class S3Artifacts(object): |
| 23 | + """ |
| 24 | + OCI container instance to provide methods for interaction. |
| 25 | +
|
| 26 | + :author: Garden Linux Maintainers |
| 27 | + :copyright: Copyright 2024 SAP SE |
| 28 | + :package: gardenlinux |
| 29 | + :subpackage: s3 |
| 30 | + :since: 0.8.0 |
| 31 | + :license: https://www.apache.org/licenses/LICENSE-2.0 |
| 32 | + Apache License, Version 2.0 |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + bucket_name: str, |
| 38 | + endpoint_url: Optional[str] = None, |
| 39 | + logger: Optional[logging.Logger] = None, |
| 40 | + ): |
| 41 | + """ |
| 42 | + Constructor __init__(Container) |
| 43 | +
|
| 44 | + :param bucket_name: S3 bucket name |
| 45 | + :param endpoint_url: S3 endpoint URL |
| 46 | + :param logger: Logger instance |
| 47 | +
|
| 48 | + :since: 0.7.0 |
| 49 | + """ |
| 50 | + |
| 51 | + """Get or create an S3 client.""" |
| 52 | + |
| 53 | + self._bucket = Bucket(bucket_name, endpoint_url) |
| 54 | + |
| 55 | + def push_from_directory( |
| 56 | + self, |
| 57 | + cname, |
| 58 | + artifacts_dir, |
| 59 | + ): |
| 60 | + """ |
| 61 | + Pushes S3 artifacts to the underlying bucket. |
| 62 | +
|
| 63 | + Args: |
| 64 | + S3Artifacts_name (str): Name of the S3 S3Artifacts |
| 65 | + prefix (str): Prefix for S3 objects |
| 66 | + cache_file (str): Path to cache file |
| 67 | + cache_ttl (int): Cache time-to-live in seconds |
| 68 | + logger (logging.Logger): Logger instance to use |
| 69 | +
|
| 70 | + Returns: |
| 71 | + dict: Dictionary containing 'index' and 'artifacts' keys |
| 72 | + """ |
| 73 | + |
| 74 | + if not isinstance(artifacts_dir, PathLike): |
| 75 | + artifacts_dir = Path(artifacts_dir) |
| 76 | + |
| 77 | + cname_object = CName(cname) |
| 78 | + |
| 79 | + if cname_object.arch is None: |
| 80 | + raise RuntimeError( |
| 81 | + "Architecture could not be determined from cname" |
| 82 | + ) |
| 83 | + |
| 84 | + if not artifacts_dir.is_dir(): |
| 85 | + raise RuntimeError(f"Artifacts directory given is invalid: {artifacts_dir}") |
| 86 | + |
| 87 | + release_file = artifacts_dir.joinpath(f"{cname}.release") |
| 88 | + release_timestamp = datetime.fromtimestamp(stat(release_file).st_ctime).isoformat() |
| 89 | + |
| 90 | + release_config = ConfigParser(allow_unnamed_section=True) |
| 91 | + release_config.read(release_file) |
| 92 | + |
| 93 | + if cname_object.version != release_config.get(UNNAMED_SECTION, "GARDENLINUX_VERSION"): |
| 94 | + raise RuntimeError( |
| 95 | + f"Release file data and given cname conflict detected: Version {cname_object.version}" |
| 96 | + ) |
| 97 | + |
| 98 | + if cname_object.commit_id != release_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID"): |
| 99 | + raise RuntimeError( |
| 100 | + f"Release file data and given cname conflict detected: Commit ID {cname_object.commit_id}" |
| 101 | + ) |
| 102 | + |
| 103 | + commit_hash = release_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID_LONG") |
| 104 | + |
| 105 | + feature_set = release_config.get(UNNAMED_SECTION, "GARDENLINUX_FEATURES") |
| 106 | + feature_list = feature_set.split(",") |
| 107 | + |
| 108 | + metadata = { |
| 109 | + "platform": cname_object.platform, |
| 110 | + "architecture": cname_object.arch, |
| 111 | + "base_image": None, |
| 112 | + "build_committish": commit_hash, |
| 113 | + "build_timestamp": release_timestamp, |
| 114 | + "gardenlinux_epoch": cname_object.version.split(".", 1)[0], |
| 115 | + "logs": None, |
| 116 | + "modifiers": feature_set, |
| 117 | + "require_uefi": "_usi" in feature_list, |
| 118 | + "secureboot": "_trustedboot" in feature_list, |
| 119 | + "published_image_metadata": None, |
| 120 | + "s3_bucket": self._bucket.name, |
| 121 | + "s3_key": f"meta/singles/{cname}", |
| 122 | + "test_result": None, |
| 123 | + "version": cname_object.version, |
| 124 | + "paths": [] |
| 125 | + } |
| 126 | + |
| 127 | + for artifact in artifacts_dir.iterdir(): |
| 128 | + if not artifact.match(f"{cname}*"): |
| 129 | + continue |
| 130 | + |
| 131 | + s3_key = f"objects/{cname}/{artifact.name}" |
| 132 | + |
| 133 | + with artifact.open("rb") as fp: |
| 134 | + md5sum = file_digest(fp, "md5").hexdigest() |
| 135 | + sha256sum = file_digest(fp, "sha256").hexdigest() |
| 136 | + |
| 137 | + artifact_metadata = { |
| 138 | + "name": artifact.name, |
| 139 | + "s3_bucket_name": self._bucket.name, |
| 140 | + "s3_key": s3_key, |
| 141 | + "suffix": "".join(artifact.suffixes), |
| 142 | + "md5sum": md5sum, |
| 143 | + "sha256sum": sha256sum, |
| 144 | + } |
| 145 | + |
| 146 | + s3_tags = { |
| 147 | + "architecture": cname_object.arch, |
| 148 | + "platform": cname_object.platform, |
| 149 | + "version": cname_object.version, |
| 150 | + "committish": commit_hash, |
| 151 | + "md5sum": md5sum, |
| 152 | + "sha256sum": sha256sum, |
| 153 | + } |
| 154 | + |
| 155 | + self._bucket.upload_file(artifact, s3_key, ExtraArgs={'Tagging': urlencode(s3_tags)}) |
| 156 | + |
| 157 | + metadata["paths"].append(artifact_metadata) |
| 158 | + |
| 159 | + with TemporaryFile(mode='w+') as fp: |
| 160 | + fp.write(yaml(metadata)) |
| 161 | + self._bucket.upload_fileobj(fp, f"meta/singles/{cname}", ExtraArgs={'ContentType': "text/yaml"}) |
0 commit comments