11import json
22import logging
3- import random
3+ import time
44from pathlib import Path
55from subprocess import run
66from time import sleep
1717 get_default_namespace ,
1818 get_mission ,
1919 get_static_client ,
20+ wait_for_init ,
2021 wait_for_pod ,
22+ write_file_to_container ,
2123)
2224from warnet .plugins import get_plugins_directory_or
2325from warnet .process import run_command
@@ -63,7 +65,7 @@ def warnet_register_plugin(register_command):
6365
6466
6567# The group function name is then used in decorators to create commands. These commands are
66- # available to users when the access your plugin from the command line in Warnet.
68+ # available to users when they access your plugin from the command line in Warnet.
6769@simln .command ()
6870def run_demo ():
6971 """Run the SimLN Plugin demo"""
@@ -128,22 +130,40 @@ def get_example_activity():
128130
129131def _launch_activity (activity : list [dict ], user_dir : Optional [str ] = None ) -> str :
130132 """Launch a SimLN chart which includes the `activity`"""
131- random_digits = "" .join (random .choices ("0123456789" , k = 10 ))
132133 plugin_dir = get_plugins_directory_or (user_dir )
133- _generate_nodes_file (activity , plugin_dir / Path ("simln/charts/simln/files/sim.json" ))
134- command = f"helm upgrade --install simln-{ random_digits } { plugin_dir } /simln/charts/simln"
135- log .info (f"generate activity: { command } " )
134+
135+ timestamp = int (time .time ())
136+ name = f"simln-{ timestamp } "
137+
138+ command = f"helm upgrade --install { timestamp } { plugin_dir } /simln/charts/simln"
136139 run_command (command )
137- return f"simln-simln-{ random_digits } "
140+
141+ activity_json = _generate_activity_json (activity )
142+ wait_for_init (name , namespace = get_default_namespace (), quiet = True )
143+ if write_file_to_container (
144+ name ,
145+ "init" ,
146+ "/working/sim.json" ,
147+ activity_json ,
148+ namespace = get_default_namespace (),
149+ quiet = True ,
150+ ):
151+ return name
152+ else :
153+ raise SimLNError (f"Could not write sim.json to the init container: { name } " )
138154
139155
140156# Take note of how click expects us to explicitly declare command line arguments.
141157@simln .command ()
142158@click .argument ("activity" , type = str )
143159@click .pass_context
144160def launch_activity (ctx , activity : str ):
145- """Takes a SimLN Activity which is a JSON list of objects."""
146- parsed_activity = json .loads (activity )
161+ """Deploys a SimLN Activity which is a JSON list of objects"""
162+ try :
163+ parsed_activity = json .loads (activity )
164+ except json .JSONDecodeError :
165+ log .error ("Invalid JSON input for activity." )
166+ raise click .BadArgumentUsage ("Activity must be a valid JSON string." ) from None
147167 user_dir = ctx .obj .get (USER_DIR_TAG )
148168 print (_launch_activity (parsed_activity , user_dir ))
149169
@@ -283,7 +303,7 @@ def warnet(cmd: str = "--help"):
283303 return proc .stdout .decode ()
284304
285305
286- def _generate_nodes_file (activity : list [dict ], output_file : Path = Path ( "nodes.json" )) :
306+ def _generate_activity_json (activity : list [dict ]) -> str :
287307 nodes = []
288308
289309 for i in get_mission (LIGHTNING_MISSION ):
@@ -298,8 +318,7 @@ def _generate_nodes_file(activity: list[dict], output_file: Path = Path("nodes.j
298318
299319 data = {"nodes" : nodes , "activity" : activity }
300320
301- with open (output_file , "w" ) as f :
302- json .dump (data , f , indent = 2 )
321+ return json .dumps (data , indent = 2 )
303322
304323
305324def manual_open_channels ():
@@ -342,7 +361,7 @@ def wait_for_two_txs():
342361 warnet ("bitcoin rpc tank-0000 -generate 10" )
343362
344363
345- def _rpc (pod , method : str , params : tuple [str , ...]) -> str :
364+ def _sh (pod , method : str , params : tuple [str , ...]) -> str :
346365 namespace = get_default_namespace ()
347366
348367 sclient = get_static_client ()
@@ -356,7 +375,7 @@ def _rpc(pod, method: str, params: tuple[str, ...]) -> str:
356375 sclient .connect_get_namespaced_pod_exec ,
357376 pod ,
358377 namespace ,
359- container = "simln" ,
378+ container = CONTAINER ,
360379 command = cmd ,
361380 stderr = True ,
362381 stdin = False ,
@@ -383,6 +402,6 @@ def _rpc(pod, method: str, params: tuple[str, ...]) -> str:
383402@click .argument ("pod" , type = str )
384403@click .argument ("method" , type = str )
385404@click .argument ("params" , type = str , nargs = - 1 ) # this will capture all remaining arguments
386- def rpc (pod : str , method : str , params : tuple [str , ...]):
405+ def sh (pod : str , method : str , params : tuple [str , ...]):
387406 """Run commands on a pod"""
388- print (_rpc (pod , method , params ))
407+ print (_sh (pod , method , params ))
0 commit comments