Skip to content

Commit 8b3f88c

Browse files
committed
update documentation/examples; use PluginContent
1 parent 746d397 commit 8b3f88c

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

docs/plugins.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,72 @@
11
# Plugins
22

3-
Plugins allow users to extend Warnet. Plugin authors can import commands from Warnet and plugin users can run plugin commands from the command line or on each invocation of `warnet deploy`.
3+
Plugins extend Warnet. Plugin authors can import commands from Warnet and interact with the kubernetes cluster, and plugin users can run plugins from the command line or from the `network.yaml` file.
44

5-
## Activating plugins from 'network.yaml'
5+
## Activating plugins from `network.yaml`
66

7-
You can activate a plugin command by placing it in the `plugin` section at the bottom of each `network.yaml` file like so:
7+
You can activate a plugin command by placing it in the `plugins` section at the bottom of each `network.yaml` file like so:
8+
9+
````yaml
10+
nodes:
11+
<<snip>>
12+
13+
plugins: # This marks the beginning of the plugin section
14+
preDeploy: # This is a hook. This particular hook will call plugins before deploying anything else.
15+
hello: # This is the name of the plugin.
16+
entrypoint: "../plugins/hello" # Every plugin must specify a path to its entrypoint.
17+
podName: "hello-pre-deploy" # Plugins can have their own particular configurations, such as how to name a pod.
18+
helloTo: "preDeploy!" # This configuration tells the hello plugin who to say "hello" to.
19+
````
20+
21+
## Many kinds of hooks
22+
There are many hooks to the Warnet `deploy` command. The example below specifies them:
823

924
````yaml
1025
nodes:
1126
<<snip>>
1227

1328
plugins:
14-
preDeploy:
29+
preDeploy: # Plugins will run before any other `deploy` code.
1530
hello:
1631
entrypoint: "../plugins/hello"
1732
podName: "hello-pre-deploy"
1833
helloTo: "preDeploy!"
19-
postDeploy:
34+
postDeploy: # Plugins will run after all the `deploy` code has run.
2035
simln:
21-
entrypoint: "../../../resources/plugins/simln"
36+
entrypoint: "../plugins/simln"
2237
activity: '[{"source": "tank-0003-ln", "destination": "tank-0005-ln", "interval_secs": 1, "amount_msat": 2000}]'
2338
hello:
2439
entrypoint: "../plugins/hello"
2540
podName: "hello-post-deploy"
2641
helloTo: "postDeploy!"
27-
preNode:
42+
preNode: # Plugins will run before `deploy` launches a node (once per node).
2843
hello:
2944
entrypoint: "../plugins/hello"
3045
helloTo: "preNode!"
31-
postNode:
46+
postNode: # Plugins will run after `deploy` launches a node (once per node).
3247
hello:
3348
entrypoint: "../plugins/hello"
3449
helloTo: "postNode!"
35-
preNetwork:
50+
preNetwork: # Plugins will run before `deploy` launches the network (essentially between logging and when nodes are deployed)
3651
hello:
3752
entrypoint: "../plugins/hello"
3853
helloTo: "preNetwork!"
3954
podName: "hello-pre-network"
40-
postNetwork:
55+
postNetwork: # Plugins will run after the network deploy threads have been joined.
4156
hello:
4257
entrypoint: "../plugins/hello"
4358
helloTo: "postNetwork!"
4459
podName: "hello-post-network"
4560
````
4661

47-
Warnet will execute these plugin commands after each invocation of `warnet deploy`.
62+
Warnet will execute these plugin commands during each invocation of `warnet deploy`.
63+
64+
4865

49-
## Example: SimLN
66+
## A "hello" example
5067

51-
To get started with an example plugin, review the `README` of the `simln` plugin found in any initialized Warnet directory:
68+
To get started with an example plugin, review the `README` of the `hello` plugin found in any initialized Warnet directory:
5269

5370
1. `warnet init`
54-
2. `cd plugins/simln/`
71+
2. `cd plugins/hello/`
5572

resources/plugins/hello/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Hello Plugin
22

33
## Hello World!
4-
*Hello* is an example plugin to demonstrate the features of Warnet's plugin architecture.
4+
*Hello* is an example plugin to demonstrate the features of Warnet's plugin architecture. It uses each of the hooks available in the `warnet deploy` command (see the example below for details).
55

66
## Usage
7-
In your python virtual environment with Warnet installed and started, create a new Warnet user folder (follow the prompts):
7+
In your python virtual environment with Warnet installed and setup, create a new Warnet user folder (follow the prompts):
88

99
`$ warnet new user_folder`
1010

@@ -18,14 +18,14 @@ While that is launching, take a look inside the `networks/hello/network.yaml` fi
1818

1919
Also, take a look at the `plugins/hello/plugin.py` file to see how plugins work and to find out how to author your own plugin.
2020

21-
To view the pods that the *hello* network launched, run `kubectl get all -A`
21+
Once `deploy` completes, view the pods of the *hello* network by invoking `kubectl get all -A`.
2222

2323
To view the various "Hello World!" messages, run `kubectl logs pod/POD_NAME`
2424

2525
### A `network.yaml` example
2626
When you initialize a new Warnet network, Warnet will create a new `network.yaml` file. You can modify these files to fit your needs.
2727

28-
For example, example `network.yaml` file includes the *hello* plugin, lightning nodes and the *simln* plugin.
28+
For example, the `network.yaml` file below includes the *hello* plugin, lightning nodes, and the *simln* plugin.
2929

3030
<details>
3131
<summary>network.yaml</summary>

resources/plugins/hello/plugin.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from warnet.constants import PLUGIN_ANNEX, AnnexMember, HookValue, WarnetContent
1111
from warnet.process import run_command
1212

13-
# Tt is common for Warnet objects to have a "mission" tag to query them in the cluster.
13+
# Tt is common for Warnet objects to have a "mission" label to help query for them in the cluster.
1414
MISSION = "hello"
1515
PRIMARY_CONTAINER = MISSION
1616

@@ -32,7 +32,7 @@ class PluginError(Exception):
3232
log.propagate = True
3333

3434

35-
# Plugins look like this in the network.yaml file:
35+
# Plugins look like this in the `network.yaml` file:
3636
#
3737
# plugins:
3838
# hello:
@@ -42,7 +42,7 @@ class PluginError(Exception):
4242
# "podName" and "helloTo" are essentially dictionary keys, and it helps to keep those keys in an
4343
# enum in order to prevent typos.
4444
class PluginContent(Enum):
45-
POD_NAME = ("podName",)
45+
POD_NAME = "podName"
4646
HELLO_TO = "helloTo"
4747

4848

@@ -113,7 +113,9 @@ def _entrypoint(ctx, plugin_content: dict, warnet_content: dict):
113113

114114
def get_data(plugin_content: dict) -> Optional[dict]:
115115
data = {
116-
key: plugin_content.get(key) for key in ("podName", "helloTo") if plugin_content.get(key)
116+
key: plugin_content.get(key)
117+
for key in (PluginContent.POD_NAME.value, PluginContent.HELLO_TO.value)
118+
if plugin_content.get(key)
117119
}
118120
return data or None
119121

resources/plugins/simln/plugin.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
import time
5+
from enum import Enum
56
from pathlib import Path
67

78
import click
@@ -37,6 +38,10 @@ class PluginError(Exception):
3738
log.addHandler(console_handler)
3839

3940

41+
class PluginContent(Enum):
42+
ACTIVITY = "activity"
43+
44+
4045
@click.group()
4146
@click.pass_context
4247
def simln(ctx):
@@ -75,7 +80,7 @@ def entrypoint(ctx, plugin_content: str, warnet_content: str):
7580
def _entrypoint(ctx, plugin_content: dict, warnet_content: dict):
7681
"""Called by entrypoint"""
7782
# write your plugin startup commands here
78-
activity = plugin_content.get("activity")
83+
activity = plugin_content.get(PluginContent.ACTIVITY.value)
7984
activity = json.loads(activity)
8085
print(activity)
8186
_launch_activity(activity, ctx.obj.get(PLUGIN_DIR_TAG))
@@ -114,7 +119,7 @@ def get_example_activity():
114119

115120

116121
@simln.command()
117-
@click.argument("activity", type=str)
122+
@click.argument(PluginContent.ACTIVITY.value, type=str)
118123
@click.pass_context
119124
def launch_activity(ctx, activity: str):
120125
"""Deploys a SimLN Activity which is a JSON list of objects"""
@@ -164,7 +169,7 @@ def _generate_activity_json(activity: list[dict]) -> str:
164169
}
165170
nodes.append(node)
166171

167-
data = {"nodes": nodes, "activity": activity}
172+
data = {"nodes": nodes, PluginContent.ACTIVITY.value: activity}
168173

169174
return json.dumps(data, indent=2)
170175

0 commit comments

Comments
 (0)