|
| 1 | +""" |
| 2 | +Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | +Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 4 | +""" |
| 5 | +# spell-checker:ignore kubeconfig |
| 6 | + |
| 7 | +import subprocess |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import time |
| 12 | + |
| 13 | +# --- Constants --- |
| 14 | +HELM_NAME = "ai-optimizer" |
| 15 | +HELM_REPO = "https://oracle-samples.github.io/ai-optimizer/helm" |
| 16 | +STAGE_PATH = os.path.join(os.path.dirname(__file__), "stage") |
| 17 | +os.environ["KUBECONFIG"] = os.path.join(STAGE_PATH, "kubeconfig") |
| 18 | + |
| 19 | + |
| 20 | +# --- Utility Functions --- |
| 21 | +def run_cmd(cmd, capture_output=True): |
| 22 | + """Generic subprocess execution""" |
| 23 | + try: |
| 24 | + result = subprocess.run( |
| 25 | + cmd, |
| 26 | + stdout=subprocess.PIPE if capture_output else None, |
| 27 | + stderr=subprocess.PIPE if capture_output else None, |
| 28 | + text=True, |
| 29 | + check=False, |
| 30 | + ) |
| 31 | + stdout = result.stdout.strip() if result.stdout else "" |
| 32 | + stderr = result.stderr.strip() if result.stderr else "" |
| 33 | + return stdout, stderr, result.returncode |
| 34 | + except subprocess.SubprocessError as e: |
| 35 | + return "", str(e), 1 |
| 36 | + |
| 37 | + |
| 38 | +def retry(func, retries=3, delay=10): |
| 39 | + """Retry a function with given arguments on failure.""" |
| 40 | + for attempt in range(1, retries + 1): |
| 41 | + print(f"🔁 Attempt {attempt}/{retries}") |
| 42 | + if func(): |
| 43 | + return True |
| 44 | + if attempt < retries: |
| 45 | + print(f"⏳ Retrying in {delay} seconds...") |
| 46 | + time.sleep(delay) |
| 47 | + print("🚨 Maximum retries reached. Exiting.") |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + |
| 51 | +# --- Core Functionalities --- |
| 52 | +def helm_repo_add_if_missing(): |
| 53 | + """Add/Update Helm Repo""" |
| 54 | + print(f"➕ Adding Helm repo '{HELM_NAME}'...") |
| 55 | + _, stderr, rc = run_cmd(["helm", "repo", "add", HELM_NAME, HELM_REPO], capture_output=False) |
| 56 | + if rc != 0: |
| 57 | + print(f"❌ Failed to add repo:\n{stderr}") |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | + print("⬆️ Checking for Helm updates...") |
| 61 | + _, stderr, rc = run_cmd(["helm", "repo", "update"], capture_output=False) |
| 62 | + if rc != 0: |
| 63 | + print(f"❌ Failed to update repos:\n{stderr}") |
| 64 | + sys.exit(1) |
| 65 | + print(f"✅ Repo '{HELM_NAME}' added and updated.\n") |
| 66 | + |
| 67 | + |
| 68 | +def apply_helm_chart_inner(release_name, namespace): |
| 69 | + """Apply Helm Chart""" |
| 70 | + values_path = os.path.join(STAGE_PATH, "helm-values.yaml") |
| 71 | + if not os.path.isfile(values_path): |
| 72 | + print(f"⚠️ Values file not found: {values_path}") |
| 73 | + return False |
| 74 | + |
| 75 | + helm_repo_add_if_missing() |
| 76 | + |
| 77 | + cmd = [ |
| 78 | + "helm", |
| 79 | + "upgrade", |
| 80 | + "--install", |
| 81 | + release_name, |
| 82 | + f"{HELM_NAME}/{HELM_NAME}", |
| 83 | + "--namespace", |
| 84 | + namespace, |
| 85 | + "--values", |
| 86 | + values_path, |
| 87 | + ] |
| 88 | + |
| 89 | + print(f"🚀 Applying Helm chart '{HELM_NAME}' to namespace '{namespace}'...") |
| 90 | + stdout, stderr, rc = run_cmd(cmd) |
| 91 | + if rc == 0: |
| 92 | + print("✅ Helm chart applied:") |
| 93 | + print(f"Apply Helm Chart: {stdout}") |
| 94 | + return True |
| 95 | + else: |
| 96 | + print(f"❌ Failed to apply Helm chart:\n{stderr}") |
| 97 | + return False |
| 98 | + |
| 99 | + |
| 100 | +def apply_helm_chart(release_name, namespace): |
| 101 | + """Retry Enabled Add/Update Helm Chart""" |
| 102 | + retry(lambda: apply_helm_chart_inner(release_name, namespace)) |
| 103 | + |
| 104 | + |
| 105 | +def apply_manifest_inner(): |
| 106 | + """Apply Manifest""" |
| 107 | + manifest_path = os.path.join(STAGE_PATH, "k8s-manifest.yaml") |
| 108 | + if not os.path.isfile(manifest_path): |
| 109 | + print(f"⚠️ Manifest not found: {manifest_path}") |
| 110 | + return False |
| 111 | + |
| 112 | + print("🚀 Applying Kubernetes manifest: k8s-manifest.yaml") |
| 113 | + _, stderr, rc = run_cmd(["kubectl", "apply", "-f", manifest_path], capture_output=False) |
| 114 | + if rc == 0: |
| 115 | + print("✅ Manifest applied.\n") |
| 116 | + return True |
| 117 | + else: |
| 118 | + print(f"❌ Failed to apply manifest:\n{stderr}") |
| 119 | + return False |
| 120 | + |
| 121 | + |
| 122 | +def apply_manifest(): |
| 123 | + """Retry Enabled Add/Update Manifest""" |
| 124 | + retry(apply_manifest_inner) |
| 125 | + |
| 126 | + |
| 127 | +# --- Entry Point --- |
| 128 | +if __name__ == "__main__": |
| 129 | + parser = argparse.ArgumentParser(description="Apply a Helm chart and a Kubernetes manifest.") |
| 130 | + parser.add_argument("release_name", help="Helm release name") |
| 131 | + parser.add_argument("namespace", help="Kubernetes namespace") |
| 132 | + args = parser.parse_args() |
| 133 | + |
| 134 | + apply_manifest() |
| 135 | + apply_helm_chart(args.release_name, args.namespace) |
0 commit comments