From 29555072d685b2084c443aead8d48ac0089a5a87 Mon Sep 17 00:00:00 2001 From: Nikhil Jha Date: Sun, 17 Mar 2024 00:00:42 -0700 Subject: [PATCH] feat: scripted version bumping --- poetry.lock | 15 +++++++++-- pyproject.toml | 1 + transpire/internal/cli/__init__.py | 3 ++- transpire/internal/cli/versions.py | 41 ++++++++++++++++++++++++++++++ transpire/internal/helm.py | 7 +++++ 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 transpire/internal/cli/versions.py diff --git a/poetry.lock b/poetry.lock index 188dbc0..d2375f2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "attrs" @@ -966,6 +966,17 @@ files = [ {file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, ] +[[package]] +name = "tomlkit" +version = "0.12.4" +description = "Style preserving TOML library" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomlkit-0.12.4-py3-none-any.whl", hash = "sha256:5cd82d48a3dd89dee1f9d64420aa20ae65cfbd00668d6f094d7578a78efbb77b"}, + {file = "tomlkit-0.12.4.tar.gz", hash = "sha256:7ca1cfc12232806517a8515047ba66a19369e71edf2439d0f5824f91032b6cc3"}, +] + [[package]] name = "types-pyyaml" version = "6.0.12.9" @@ -1082,4 +1093,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "c603a25a31e3c254fefeafa65ca59b4997efcc8d8971e7640ec6e8ccd8f614b9" +content-hash = "52434497ea7c55fc5795add04e719a2e291521919541793e9962ae203879d55b" diff --git a/pyproject.toml b/pyproject.toml index efd3d3d..75b54df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ loguru = "^0.7.2" pydantic = "^1.9.0" hvac = "^1.0.2" requests = "^2.28.2" +tomlkit = "^0.12.4" [tool.poetry.group.dev.dependencies] mypy = "^1.2.0" diff --git a/transpire/internal/cli/__init__.py b/transpire/internal/cli/__init__.py index 92eed53..1dae5cc 100644 --- a/transpire/internal/cli/__init__.py +++ b/transpire/internal/cli/__init__.py @@ -1,6 +1,6 @@ import click -from . import bootstrap, dev, image, obj, secrets, utils +from . import bootstrap, dev, image, obj, secrets, utils, versions @click.command(cls=utils.AliasedGroup) def cli() -> None: @@ -12,3 +12,4 @@ def cli() -> None: cli.add_command(obj.commands, "object") cli.add_command(image.commands, "image") cli.add_command(secrets.commands, "secret") +cli.add_command(versions.commands, "versions") diff --git a/transpire/internal/cli/versions.py b/transpire/internal/cli/versions.py new file mode 100644 index 0000000..b0fcdb8 --- /dev/null +++ b/transpire/internal/cli/versions.py @@ -0,0 +1,41 @@ +import click +import tomlkit + +from transpire.internal.cli.utils import AliasedGroup +from transpire.internal import helm +from loguru import logger + + +@click.command(cls=AliasedGroup) +def commands(**_): + """version management commands""" + pass + + +@commands.command() +@click.option("-f", "--file") +@click.argument("app_name", required=True) +def update(app_name: str, file: str, **_) -> None: + """update to the newest version of a given app""" + doc = tomlkit.parse(open(file).read()) + + if "helm" in doc[app_name]: + helm.add_repo(app_name, doc[app_name]["helm"]) + helm.update_repo(app_name) + chart_name = doc[app_name]["chart"] or app_name + search_results = helm.search_repo(chart_name) + latest_version_list = list( + x for x in search_results if x["name"] == f"{app_name}/{chart_name}" + ) + + if len(latest_version_list) != 1: + raise ValueError(f"expected 1 result, got {len(latest_version_list)}") + latest_version = latest_version_list[0]["version"] + + if latest_version != doc[app_name]["version"]: + logger.info(f"updating {app_name} from {doc[app_name]['version']} to {latest_version}") + doc[app_name]["version"] = latest_version + with open(file, "w") as f: + f.write(tomlkit.dumps(doc)) + + doc[app_name]["version"] diff --git a/transpire/internal/helm.py b/transpire/internal/helm.py index 9735995..b83e724 100644 --- a/transpire/internal/helm.py +++ b/transpire/internal/helm.py @@ -60,6 +60,13 @@ def update_repo(name: str) -> None: exec_helm(["repo", "update", name], check=False) +def search_repo(query: str) -> list[dict]: + """search a repository for a chart""" + + assert_helm() + stdout, _ = exec_helm(["search", "repo", query, "--output", "yaml"], check=True) + return yaml.safe_load(stdout) + def build_chart( repo_url: str, chart_name: str,