diff --git a/examples/values.yaml b/examples/values.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8ac2fac805b9cf412bf902a5d5db6ec443d82bfd --- /dev/null +++ b/examples/values.yaml @@ -0,0 +1,20 @@ +swh_graphql_image: softwareheritage/graphql +swh_graphql_image_version: '20220806.2' +swh_indexer_image: container-registry.softwareheritage.org/swh/infra/swh-apps/indexer +swh_indexer_image_version: '20221028.1' +swh_loader_bzr_image: softwareheritage/loader_bzr +swh_loader_bzr_image_version: '20220825.1' +swh_loader_cvs_image: softwareheritage/loader_cvs +swh_loader_cvs_image_version: '20220712.1' +swh_loader_git_image: container-registry.softwareheritage.org/swh/infra/swh-apps/loader_git +swh_loader_git_image_version: '20211002.2' +swh_loader_highpriority_image: container-registry.softwareheritage.org/swh/infra/swh-apps/loader_highpriority +swh_loader_highpriority_image_version: '20221213.1' +swh_loader_mercurial_image: softwareheritage/loader_mercurial +swh_loader_mercurial_image_version: '20220925.1' +swh_loader_package_image: softwareheritage/loader_package +swh_loader_package_image_version: '20221025.1' +swh_loader_svn_image: container-registry.softwareheritage.org/swh/infra/swh-apps/loader_svn +swh_loader_svn_image_version: '20221112.1' +swh_storage_replayer_image: container-registry.softwareheritage.org/swh/infra/swh-apps/storage-replayer +swh_storage_replayer_image_version: '20221226.1' diff --git a/scripts/app_manager.py b/scripts/app_manager.py index 7da71da16fedd0ec7fdc3b0aa52fa660aa8070f6..600678b5afd0a10f3233f8fccd636d780aa33c7d 100755 --- a/scripts/app_manager.py +++ b/scripts/app_manager.py @@ -15,9 +15,11 @@ import os import pathlib import subprocess import sys +import yaml +from collections import defaultdict import tempfile from venv import EnvBuilder -from typing import Iterator +from typing import Dict, Iterator, Set APPS_DIR = pathlib.Path(__file__).absolute().parent.parent / "apps" @@ -179,5 +181,98 @@ def list(ctx, application: str, version: str) -> None: print(app) +def compute_information(current_values: Dict): + """Compute pivot structure as a Dict from the values-swh-application-versions.yaml + file. The dict has keys a _ separated image name (e.g. swh_vault_cookers, ...). + The associated value is also dict with key {"image" registry uri, image "version"} + and their respective value. + + """ + current_information: Dict[str, Dict[str, str]] = defaultdict(dict) + for key, value in current_values.items(): + if key.endswith("_image"): + str_key = key.replace("_image", "") + current_information[str_key].update({"image": value}) + elif key.endswith("_image_version"): + str_key = key.replace("_image_version", "") + current_information[str_key].update({"version": value}) + return current_information + + +def compute_yaml(updated_information: Dict[str, Dict[str, str]]) -> Dict[str, str]: + """Computes the yaml dict to serialize in the values...yaml file. + + """ + yaml_dict = {} + for image_name, info in updated_information.items(): + yaml_dict[f"{image_name}_image"] = info["image"] + yaml_dict[f"{image_name}_image_version"] = info["version"] + + return yaml_dict + + +@app.command("update-values") +@click.option("-v", "--values-filepath", + help="Path to file swh-charts:/values-swh-application-versions.yaml") +@click.pass_context +def update_values(ctx, values_filepath: str) -> None: + """Update docker image version in swh-charts:values-swh-application-versions.yaml + based on swh-apps' git tags. + + """ + + with open(values_filepath, 'r') as f: + yml_dict = yaml.safe_load(f) + current_information = compute_information(yml_dict) + + # The information to update in the yaml configuration + updated_information: Dict[str, Dict[str, str]] = {} + + already_treated: Set[str] = set() + # This reads tags (tags) from the standard input entry. Those swh-apps tags are + # sorted in most recent order first (`git tag -l | sort -r`). So, we treat the first + # application tag, then discards the next: + # <image-name-with-dash>-<release-date> + # swh-vault-cookers-20221107.1 + # swh-vault-cookers-20220926.2 <- discarded + # swh-vault-cookers-20220926.1 <- discarded + # swh-storage-replayer-20221227.1 + # swh-storage-replayer-20220927.1 <- discarded + # swh-storage-replayer-20220819.1 <- discarded + # swh-storage-replayer-20220817.1 <- discarded + for line in sys.stdin: + line = line.strip() + line_data = line.split("-") + + # Image name is _ separated in the values.yaml file + image_name = "_".join(line_data[:-1]) + image_version = line_data[-1] + + if image_name in already_treated: + # Most recent version already treated, we discard the rest + continue + + already_treated.add(image_name) + + current_info = current_information[image_name] + if not current_info: + print(f"Missing or inconsistent current_information for {image_name}") + continue + + if image_version != current_info["version"]: + info = { + "image": current_info["image"], + "version": image_version, + } + else: + info = current_info + + updated_information[image_name] = info + + with open(values_filepath, 'w') as f: + yaml_dict = compute_yaml(updated_information) + f.write(yaml.dump(yaml_dict)) + + if __name__ == "__main__": app()