|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | + |
| 4 | +from importlib.resources import files |
| 5 | + |
| 6 | +from kubernetes import client, config |
| 7 | +from kubernetes.dynamic import DynamicClient |
| 8 | + |
| 9 | + |
| 10 | +WAR_MANIFESTS = files("manifests") |
| 11 | + |
| 12 | +def get_static_client(): |
| 13 | + config.load_kube_config() |
| 14 | + return client.CoreV1Api() |
| 15 | + |
| 16 | +def get_dynamic_client(): |
| 17 | + config.load_kube_config() |
| 18 | + return DynamicClient(client.ApiClient()) |
| 19 | + |
| 20 | +def get_pods(): |
| 21 | + sclient = get_static_client() |
| 22 | + return sclient.list_namespaced_pod("warnet") |
| 23 | + |
| 24 | +def run_command(command, stream_output=False, env=None): |
| 25 | + # Merge the current environment with the provided env |
| 26 | + full_env = os.environ.copy() |
| 27 | + if env: |
| 28 | + # Convert all env values to strings (only a safeguard) |
| 29 | + env = {k: str(v) for k, v in env.items()} |
| 30 | + full_env.update(env) |
| 31 | + |
| 32 | + if stream_output: |
| 33 | + process = subprocess.Popen( |
| 34 | + ["/bin/bash", "-c", command], |
| 35 | + stdout=subprocess.PIPE, |
| 36 | + stderr=subprocess.STDOUT, |
| 37 | + text=True, |
| 38 | + bufsize=1, |
| 39 | + universal_newlines=True, |
| 40 | + env=full_env, |
| 41 | + ) |
| 42 | + |
| 43 | + for line in iter(process.stdout.readline, ""): |
| 44 | + print(line, end="") |
| 45 | + |
| 46 | + process.stdout.close() |
| 47 | + return_code = process.wait() |
| 48 | + |
| 49 | + if return_code != 0: |
| 50 | + print(f"Command failed with return code {return_code}") |
| 51 | + return False |
| 52 | + return True |
| 53 | + else: |
| 54 | + result = subprocess.run( |
| 55 | + command, shell=True, capture_output=True, text=True, executable="/bin/bash" |
| 56 | + ) |
| 57 | + if result.returncode != 0: |
| 58 | + print(f"Error: {result.stderr}") |
| 59 | + return False |
| 60 | + print(result.stdout) |
| 61 | + return True |
| 62 | + |
| 63 | + |
| 64 | +def set_kubectl_context(namespace: str): |
| 65 | + """ |
| 66 | + Set the default kubectl context to the specified namespace. |
| 67 | + """ |
| 68 | + command = f"kubectl config set-context --current --namespace={namespace}" |
| 69 | + result = run_command(command, stream_output=True) |
| 70 | + if result: |
| 71 | + print(f"Kubectl context set to namespace: {namespace}") |
| 72 | + else: |
| 73 | + print(f"Failed to set kubectl context to namespace: {namespace}") |
| 74 | + return result |
| 75 | + |
| 76 | + |
| 77 | +def deploy_base_configurations(): |
| 78 | + base_configs = [ |
| 79 | + "namespace.yaml", |
| 80 | + "rbac-config.yaml", |
| 81 | + ] |
| 82 | + |
| 83 | + for config in base_configs: |
| 84 | + command = f"kubectl apply -f {WAR_MANIFESTS}/{config}" |
| 85 | + if not run_command(command, stream_output=True): |
| 86 | + print(f"Failed to apply {config}") |
| 87 | + return False |
| 88 | + return True |
| 89 | + |
| 90 | + |
| 91 | +def apply_kubernetes_yaml(yaml_file: str): |
| 92 | + command = f"kubectl apply -f {yaml_file}" |
| 93 | + return run_command(command, stream_output=True) |
| 94 | + |
| 95 | + |
| 96 | +def delete_namespace(namespace: str): |
| 97 | + command = f"kubectl delete namespace {namespace}" |
| 98 | + return run_command(command, stream_output=True) |
| 99 | + |
0 commit comments