1+ import os
12import subprocess
23import sys
34import tempfile
2425 NETWORK_FILE ,
2526 SCENARIOS_DIR ,
2627 WARGAMES_NAMESPACE_PREFIX ,
28+ HookOptions ,
2729 HookValue ,
2830)
2931from .control import _run
3537 wait_for_ingress_controller ,
3638 wait_for_pod_ready ,
3739)
38- from .process import run_command , stream_command
40+ from .process import run_command , stream_command , wait_for_run
3941
4042HINT = "\n Are you trying to run a scenario? See `warnet run --help`"
4143
@@ -129,6 +131,12 @@ def _deploy(directory, debug, namespace, to_all_users):
129131
130132
131133def run_plugins (directory , hook_value : HookValue ):
134+ """ " Run the plugin commands within a given hook value"""
135+
136+ def is_relative (path : str ) -> bool :
137+ """Determine if the path is a command or a path to a command"""
138+ return os .path .dirname (path ) != ""
139+
132140 network_file_path = directory / NETWORK_FILE
133141
134142 with network_file_path .open () as f :
@@ -139,9 +147,39 @@ def run_plugins(directory, hook_value: HookValue):
139147 plugins_section = network_file .get ("plugins" , {})
140148 plugins = plugins_section .get (hook_value .value ) or []
141149 for plugin_cmd in plugins :
142- fully_qualified_cmd = network_file_path .parent / plugin_cmd # relative to network.yaml
143- print (f"Plugin command: { fully_qualified_cmd } " )
144- print (run_command (str (fully_qualified_cmd )))
150+ match plugin_cmd :
151+ case {HookOptions .EXEC .value : cmd , HookOptions .WAIT_FOR .value : predicate }:
152+ if is_relative (cmd ):
153+ cmd = network_file_path .parent / cmd
154+ print (f"{ HookOptions .EXEC .value } : { cmd } " )
155+
156+ if is_relative (predicate ):
157+ predicate = network_file_path .parent / predicate
158+ print (f"{ HookOptions .WAIT_FOR .value } : { predicate } " )
159+
160+ wait_for_run (str (predicate ))
161+ print (run_command (str (cmd )))
162+
163+ case {HookOptions .EXEC .value : cmd }:
164+ if is_relative (cmd ):
165+ cmd = network_file_path .parent / cmd
166+ print (f"{ HookOptions .EXEC .value } : { cmd } " )
167+ print (run_command (str (cmd )))
168+
169+ case str ():
170+ cmd = plugin_cmd
171+ if is_relative (cmd ):
172+ cmd = network_file_path .parent / plugin_cmd
173+ print (f"{ cmd } " )
174+ print (run_command (str (cmd )))
175+
176+ case _:
177+ print (
178+ f"The following plugin command does not match known plugin command structures: { plugin_cmd } "
179+ )
180+ print (f"Known hook values: { [v .value for v in HookValue ]} " )
181+ print (f"Known hook options: { [v .value for v in HookOptions ]} " )
182+ sys .exit (1 )
145183
146184
147185def check_logging_required (directory : Path ):
@@ -358,9 +396,14 @@ def deploy_single_node(node, directory: Path, debug: bool, namespace: str):
358396 temp_override_file_path = Path (temp_file .name )
359397 cmd = f"{ cmd } -f { temp_override_file_path } "
360398
399+ run_plugins (directory , HookValue .PRE_NODE )
400+
361401 if not stream_command (cmd ):
362402 click .echo (f"Failed to run Helm command: { cmd } " )
363403 return
404+
405+ run_plugins (directory , HookValue .POST_NODE )
406+
364407 except Exception as e :
365408 click .echo (f"Error: { e } " )
366409 return
0 commit comments