|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +import yaml |
| 7 | + |
| 8 | +from lib.base_logger import logger |
| 9 | +from scripts.release.build.build_info import * |
| 10 | + |
| 11 | +CHART_DIR = "helm_chart" |
| 12 | + |
| 13 | + |
| 14 | +def run_command(command: list[str]): |
| 15 | + try: |
| 16 | + # Using capture_output=True to grab stdout/stderr for better error logging. |
| 17 | + process = subprocess.run(command, check=True, text=True, capture_output=True) |
| 18 | + logger.info(f"Successfully executed: {' '.join(command)}") |
| 19 | + if process.stdout: |
| 20 | + logger.info(process.stdout) |
| 21 | + except subprocess.CalledProcessError as e: |
| 22 | + raise RuntimeError(f"Command {' '.join(command)} failed. Stderr: {e.stderr.strip()}") from e |
| 23 | + except FileNotFoundError: |
| 24 | + raise FileNotFoundError( |
| 25 | + f"Error: {command[0]} command not found. Ensure {command[0]} is installed and in your PATH." |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +# update_chart_and_get_metadata updates the helm chart's Chart.yaml and sets the version |
| 30 | +# to either evg patch id or commit which is set in OPERATOR_VERSION. |
| 31 | +def update_chart_and_get_metadata(chart_dir: str) -> tuple[str, str]: |
| 32 | + chart_path = os.path.join(chart_dir, "Chart.yaml") |
| 33 | + version_id = os.environ.get("OPERATOR_VERSION") |
| 34 | + if not version_id: |
| 35 | + raise ValueError( |
| 36 | + "Error: Environment variable 'OPERATOR_VERSION' must be set to determine the chart version to publish." |
| 37 | + ) |
| 38 | + |
| 39 | + new_version = f"0.0.0+{version_id}" |
| 40 | + logger.info(f"New helm chart version will be: {new_version}") |
| 41 | + |
| 42 | + if not os.path.exists(chart_path): |
| 43 | + raise FileNotFoundError( |
| 44 | + f"Error: Chart.yaml not found in directory '{chart_dir}'. " |
| 45 | + "Please ensure the directory exists and contains a valid Chart.yaml." |
| 46 | + ) |
| 47 | + |
| 48 | + try: |
| 49 | + with open(chart_path, "r") as f: |
| 50 | + data = yaml.safe_load(f) |
| 51 | + |
| 52 | + chart_name = data.get("name") |
| 53 | + if not chart_name: |
| 54 | + raise ValueError("Chart.yaml is missing required 'name' field.") |
| 55 | + |
| 56 | + data["version"] = new_version |
| 57 | + |
| 58 | + with open(chart_path, "w") as f: |
| 59 | + yaml.safe_dump(data, f, sort_keys=False) |
| 60 | + |
| 61 | + logger.info(f"Successfully updated version for chart '{chart_name}' to '{new_version}'.") |
| 62 | + return chart_name, new_version |
| 63 | + except Exception as e: |
| 64 | + raise RuntimeError(f"Failed to read or update Chart.yaml: {e}") |
| 65 | + |
| 66 | + |
| 67 | +def get_oci_registry(chart_info: HelmChartInfo) -> str: |
| 68 | + registry = chart_info.registry |
| 69 | + repo = chart_info.repository |
| 70 | + |
| 71 | + if not registry: |
| 72 | + raise ValueError("Error: registry doesn't seem to be set in HelmChartInfo.") |
| 73 | + |
| 74 | + if not repo: |
| 75 | + raise ValueError("Error: reposiotry doesn't seem to be set in HelmChartInfo.") |
| 76 | + |
| 77 | + oci_registry = f"oci://{registry}/{repo}" |
| 78 | + logger.info(f"Determined OCI Registry: {oci_registry}") |
| 79 | + return oci_registry |
| 80 | + |
| 81 | + |
| 82 | +def publish_helm_chart(chart_info: HelmChartInfo): |
| 83 | + try: |
| 84 | + oci_registry = get_oci_registry(chart_info) |
| 85 | + chart_name, chart_version = update_chart_and_get_metadata(CHART_DIR) |
| 86 | + tgz_filename = f"{chart_name}-{chart_version}.tgz" |
| 87 | + |
| 88 | + logger.info(f"Packaging chart: {chart_name} with Version: {chart_version}") |
| 89 | + package_command = ["helm", "package", CHART_DIR] |
| 90 | + run_command(package_command) |
| 91 | + |
| 92 | + logger.info(f"Pushing chart to registry: {oci_registry}") |
| 93 | + push_command = ["helm", "push", tgz_filename, oci_registry] |
| 94 | + run_command(push_command) |
| 95 | + |
| 96 | + logger.info(f"Helm Chart {chart_name}:{chart_version} was published successfully!") |
| 97 | + except Exception as e: |
| 98 | + raise Exception(f"Failed publishing the helm chart {e}") |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + parser = argparse.ArgumentParser( |
| 103 | + description="Script to publish helm chart to the OCI container registry, based on the build scenario." |
| 104 | + ) |
| 105 | + parser.add_argument("--build_scenario", type=str, help="Build scenario (e.g., patch, staging etc).") |
| 106 | + args = parser.parse_args() |
| 107 | + |
| 108 | + build_scenario = args.build_scenario |
| 109 | + build_info = load_build_info(build_scenario) |
| 110 | + |
| 111 | + return publish_helm_chart(build_info.helm_charts["mongodb-kubernetes"]) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + try: |
| 116 | + main() |
| 117 | + except Exception as e: |
| 118 | + logger.error(f"Failure in the helm publishing process {e}") |
| 119 | + sys.exit(1) |
0 commit comments