|
1 | 1 | #!/usr/bin/env python3 |
| 2 | +import json |
2 | 3 | import logging |
3 | 4 | from pathlib import Path |
4 | 5 | from typing import Optional |
5 | 6 |
|
6 | 7 | import click |
7 | | -import yaml |
8 | 8 | from kubernetes.stream import stream |
9 | 9 |
|
10 | | -from warnet.constants import HookValue |
| 10 | +from warnet.constants import PLUGIN_ANNEX, AnnexMember, HookValue, WarnetContent |
11 | 11 | from warnet.k8s import ( |
12 | 12 | get_default_namespace, |
13 | 13 | get_static_client, |
@@ -45,59 +45,59 @@ def hello(ctx): |
45 | 45 |
|
46 | 46 |
|
47 | 47 | @hello.command() |
48 | | -@click.argument("network_file_path", type=str) |
49 | | -@click.argument("hook_value", type=str) |
50 | | -@click.argument("namespace", type=str) |
51 | | -@click.argument("nargs", nargs=-1) |
| 48 | +@click.argument("plugin_content", type=str) |
| 49 | +@click.argument("warnet_content", type=str) |
52 | 50 | @click.pass_context |
53 | | -def entrypoint(ctx, network_file_path: str, hook_value: str, namespace: str, nargs): |
| 51 | +def entrypoint(ctx, plugin_content: str, warnet_content: str): |
54 | 52 | """Plugin entrypoint""" |
| 53 | + plugin_content: dict = json.loads(plugin_content) |
| 54 | + warnet_content: dict = json.loads(warnet_content) |
| 55 | + |
| 56 | + hook_value = warnet_content.get(WarnetContent.HOOK_VALUE.value) |
| 57 | + |
55 | 58 | assert hook_value in { |
56 | 59 | item.value for item in HookValue |
57 | 60 | }, f"{hook_value} is not a valid HookValue" |
58 | 61 |
|
59 | | - network_file_path = Path(network_file_path) |
60 | | - |
61 | | - with network_file_path.open() as f: |
62 | | - network_file = yaml.safe_load(f) or {} |
63 | | - if not isinstance(network_file, dict): |
64 | | - raise ValueError(f"Invalid network file structure: {network_file_path}") |
| 62 | + if warnet_content.get(PLUGIN_ANNEX): |
| 63 | + for annex_member in [annex_item for annex_item in warnet_content.get(PLUGIN_ANNEX)]: |
| 64 | + assert annex_member in { |
| 65 | + item.value for item in AnnexMember |
| 66 | + }, f"{annex_member} is not a valid AnnexMember" |
65 | 67 |
|
66 | | - plugins_section = network_file.get("plugins", {}) |
67 | | - hook_section = plugins_section.get(hook_value, {}) |
| 68 | + warnet_content[WarnetContent.HOOK_VALUE.value] = HookValue(hook_value) |
68 | 69 |
|
69 | | - plugin_name = Path(__file__).resolve().parent.stem |
70 | | - plugin_data = hook_section.get(plugin_name) |
71 | | - if not plugin_data: |
72 | | - raise PluginError(f"Could not find {plugin_name} in {network_file_path}") |
| 70 | + _entrypoint(ctx, plugin_content, warnet_content) |
73 | 71 |
|
74 | | - _entrypoint(ctx, plugin_data, HookValue(hook_value), namespace, nargs) |
75 | 72 |
|
76 | | - |
77 | | -def _entrypoint(ctx, plugin_data: dict, hook_value: HookValue, namespace: str, nargs): |
| 73 | +def _entrypoint(ctx, plugin_content: dict, warnet_content: dict): |
78 | 74 | """Called by entrypoint""" |
| 75 | + hook_value = warnet_content[WarnetContent.HOOK_VALUE.value] |
| 76 | + |
79 | 77 | match hook_value: |
80 | 78 | case ( |
81 | 79 | HookValue.PRE_NETWORK |
82 | 80 | | HookValue.POST_NETWORK |
83 | 81 | | HookValue.PRE_DEPLOY |
84 | 82 | | HookValue.POST_DEPLOY |
85 | 83 | ): |
86 | | - data = get_data(plugin_data) |
| 84 | + data = get_data(plugin_content) |
87 | 85 | if data: |
88 | 86 | _launch_pod(ctx, install_name=hook_value.value.lower() + "-hello", **data) |
89 | 87 | else: |
90 | 88 | _launch_pod(ctx, install_name=hook_value.value.lower() + "-hello") |
91 | 89 | case HookValue.PRE_NODE: |
92 | | - name = nargs[0] + "-pre-hello-pod" |
| 90 | + name = warnet_content[PLUGIN_ANNEX][AnnexMember.NODE_NAME.value] + "-pre-hello-pod" |
93 | 91 | _launch_pod(ctx, install_name=hook_value.value.lower() + "-" + name, podName=name) |
94 | 92 | case HookValue.POST_NODE: |
95 | | - name = nargs[0] + "-post-hello-pod" |
| 93 | + name = warnet_content[PLUGIN_ANNEX][AnnexMember.NODE_NAME.value] + "-post-hello-pod" |
96 | 94 | _launch_pod(ctx, install_name=hook_value.value.lower() + "-" + name, podName=name) |
97 | 95 |
|
98 | 96 |
|
99 | | -def get_data(plugin_data: dict) -> Optional[dict]: |
100 | | - data = {key: plugin_data.get(key) for key in ("podName", "helloTo") if plugin_data.get(key)} |
| 97 | +def get_data(plugin_content: dict) -> Optional[dict]: |
| 98 | + data = { |
| 99 | + key: plugin_content.get(key) for key in ("podName", "helloTo") if plugin_content.get(key) |
| 100 | + } |
101 | 101 | return data or None |
102 | 102 |
|
103 | 103 |
|
|
0 commit comments