Skip to content

Commit e23a728

Browse files
committed
add pre/post deploy options
1 parent f042913 commit e23a728

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

resources/plugins/simln/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,26 @@ nodes:
9090
lnd: true
9191

9292
plugins:
93-
# Take note: the path to the plugin file is relative to the `network.yaml` file. The location of your `plugin.py` file and `network.yaml` file may differ than what is shown below.
94-
- "../../../resources/plugins/simln/plugin.py launch-activity '[{\"source\": \"tank-0003-ln\", \"destination\": \"tank-0005-ln\", \"interval_secs\": 1, \"amount_msat\": 2000}]'"
93+
postDeploy:
94+
# Take note: the path to the plugin file is relative to the `network.yaml` file. The location of your `plugin.py` file and `network.yaml` file may differ than what is shown below.
95+
- "../../../resources/plugins/simln/plugin.py launch-activity '[{\"source\": \"tank-0003-ln\", \"destination\": \"tank-0005-ln\", \"interval_secs\": 1, \"amount_msat\": 2000}]'"
9596
````
9697

9798
</details>
9899

100+
### preDeploy and postDeploy
101+
When using `warnet deploy <network folder>`, Warnet will look for a `network.yaml` in the network folder, and it will deploy the nodes listed in the `nodes` section of the file. We can choose to execute our plugin functions before Warnet deploys the nodes by including those functions in the `preDeploy` section of the `network.yaml` file. We can also choose to run plugin commands after Warnet deploys the nodes by including the function in the `postDeploy` section.
102+
103+
````yaml
104+
plugins:
105+
preDeploy:
106+
- path/to/plugin.py setup_function
107+
postDeploy:
108+
- path/to/plugin.py run
109+
- path/to/other/plugin.py run
110+
````
111+
112+
99113
## Generating your own SimLn image
100114
The SimLN plugin fetches a SimLN docker image from dockerhub. You can generate your own docker image if you choose:
101115

resources/plugins/simln/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SimLNError(Exception):
4545

4646

4747
# Warnet uses a python package called "click" to manage terminal interactions with the user.
48-
# Each plugin must declare a click "group" by decorating a function named after the plugin.
48+
# To use click, we must declare a click "group" by decorating a function named after the plugin.
4949
# Using click makes it easy for users to interact with your plugin.
5050
@click.group()
5151
@click.pass_context

src/warnet/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from enum import Enum
23
from importlib.resources import files
34
from pathlib import Path
45

@@ -25,6 +26,12 @@
2526
BITCOINCORE_CONTAINER = "bitcoincore"
2627
COMMANDER_CONTAINER = "commander"
2728

29+
30+
class HookValue(Enum):
31+
PRE_DEPLOY = "preDeploy"
32+
POST_DEPLOY = "postDeploy"
33+
34+
2835
# Directories and files for non-python assets, e.g., helm charts, example scenarios, default configs
2936
SRC_DIR = files("warnet")
3037
RESOURCES_DIR = files("resources")

src/warnet/deploy.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
NETWORK_FILE,
2525
SCENARIOS_DIR,
2626
WARGAMES_NAMESPACE_PREFIX,
27+
HookValue,
2728
)
2829
from .control import _run
2930
from .k8s import (
@@ -72,6 +73,8 @@ def _deploy(directory, debug, namespace, to_all_users):
7273
"""Deploy a warnet with topology loaded from <directory>"""
7374
directory = Path(directory)
7475

76+
run_plugins(directory, HookValue.PRE_DEPLOY)
77+
7578
if to_all_users:
7679
namespaces = get_namespaces_by_type(WARGAMES_NAMESPACE_PREFIX)
7780
processes = []
@@ -115,7 +118,7 @@ def _deploy(directory, debug, namespace, to_all_users):
115118
for p in processes:
116119
p.join()
117120

118-
run_plugins(directory)
121+
run_plugins(directory, HookValue.POST_DEPLOY)
119122

120123
elif (directory / NAMESPACES_FILE).exists():
121124
deploy_namespaces(directory)
@@ -125,17 +128,20 @@ def _deploy(directory, debug, namespace, to_all_users):
125128
)
126129

127130

128-
def run_plugins(directory):
131+
def run_plugins(directory, hook_value: HookValue):
129132
network_file_path = directory / NETWORK_FILE
130133

131134
with network_file_path.open() as f:
132-
network_file = yaml.safe_load(f)
135+
network_file = yaml.safe_load(f) or {}
136+
if not isinstance(network_file, dict):
137+
raise ValueError(f"Invalid network file structure: {network_file_path}")
133138

134-
plugins = network_file.get("plugins") or []
139+
plugins_section = network_file.get("plugins", {})
140+
plugins = plugins_section.get(hook_value.value) or []
135141
for plugin_cmd in plugins:
136-
fully_qualified_cmd = f"{network_file_path.parent}/{plugin_cmd}" # relative to network.yaml
137-
print(fully_qualified_cmd)
138-
print(run_command(fully_qualified_cmd))
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)))
139145

140146

141147
def check_logging_required(directory: Path):

test/data/ln/network.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ nodes:
5454
lnd: true
5555

5656
plugins:
57-
- "../../../resources/plugins/simln/plugin.py launch-activity '[{\"source\": \"tank-0003-ln\", \"destination\": \"tank-0005-ln\", \"interval_secs\": 1, \"amount_msat\": 2000}]'"
57+
preDeploy:
58+
- "../../../resources/plugins/simln/plugin.py"
59+
postDeploy:
60+
- "../../../resources/plugins/simln/plugin.py launch-activity '[{\"source\": \"tank-0003-ln\", \"destination\": \"tank-0005-ln\", \"interval_secs\": 1, \"amount_msat\": 2000}]'"

0 commit comments

Comments
 (0)