Skip to content

Commit 6850b47

Browse files
committed
update quickstart
1 parent 992d0a0 commit 6850b47

File tree

1 file changed

+100
-10
lines changed

1 file changed

+100
-10
lines changed

Diff for: src/warnet/main.py

+100-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import random
23
import subprocess
34
import sys
45
from importlib.resources import files
@@ -16,6 +17,7 @@
1617
from .image import image
1718
from .network import copy_network_defaults, copy_scenario_defaults
1819
from .status import status as status_command
20+
from .util import SUPPORTED_TAGS
1921

2022
QUICK_START_PATH = files("resources.scripts").joinpath("quick_start.sh")
2123

@@ -57,19 +59,36 @@ def quickstart():
5759
return False
5860

5961
create_project = click.confirm("Do you want to create a new project?", default=True)
62+
if not create_project:
63+
click.echo("Setup completed successfully!")
64+
return True
65+
66+
default_path = os.path.abspath(os.getcwd())
67+
project_path = click.prompt(
68+
"Enter the project directory path",
69+
default=default_path,
70+
type=click.Path(file_okay=False, dir_okay=True, resolve_path=True),
71+
)
6072

61-
if create_project:
62-
default_path = os.path.abspath(os.getcwd())
63-
project_path = click.prompt(
64-
"Enter the project directory path",
65-
default=default_path,
66-
type=click.Path(file_okay=False, dir_okay=True, resolve_path=True),
67-
)
68-
73+
custom_network = click.confirm("Do you want to create a custom network?", default=True)
74+
if not custom_network:
6975
create_warnet_project(Path(project_path))
76+
click.echo("Setup completed successfully!")
77+
return True
78+
79+
network_name = click.prompt(
80+
"Enter the network name",
81+
type=str,
82+
)
83+
nodes = click.prompt("How many nodes would you like?", type=int)
84+
connections = click.prompt("How many connects would you like each node to have?", type=int)
85+
version = click.prompt(
86+
"Which version would you like nodes to have by default?",
87+
type=click.Choice(SUPPORTED_TAGS, case_sensitive=False),
88+
)
7089

71-
click.echo("Setup completed successfully!")
72-
return True
90+
create_warnet_project(Path(project_path))
91+
custom_graph(nodes, connections, version, Path(project_path) / "networks" / network_name)
7392

7493
except Exception as e:
7594
print(f"An error occurred while running the quick start script:\n\n{e}\n\n")
@@ -91,6 +110,8 @@ def create_warnet_project(directory: Path, check_empty: bool = False):
91110
richprint(f"[green]Created warnet project structure in {directory}[/green]")
92111
except Exception as e:
93112
richprint(f"[red]Error creating project: {e}[/red]")
113+
raise e
114+
94115

95116
@cli.command()
96117
@click.argument(
@@ -103,6 +124,7 @@ def create(directory: Path):
103124
return
104125
create_warnet_project(directory)
105126

127+
106128
@cli.command()
107129
def init():
108130
"""Initialize a warnet project in the current directory"""
@@ -149,3 +171,71 @@ def auth(kube_config: str) -> None:
149171

150172
if __name__ == "__main__":
151173
cli()
174+
175+
176+
def custom_graph(num_nodes: int, num_connections: int, version: str, datadir: Path):
177+
datadir.mkdir(parents=False, exist_ok=False)
178+
# Generate network.yaml
179+
nodes = []
180+
181+
for i in range(num_nodes):
182+
node = {"name": f"tank-{i:04d}", "connect": []}
183+
184+
# Add round-robin connection
185+
next_node = (i + 1) % num_nodes
186+
node["connect"].append(f"tank-{next_node:04d}")
187+
188+
# Add random connections
189+
available_nodes = list(range(num_nodes))
190+
available_nodes.remove(i)
191+
if next_node in available_nodes:
192+
available_nodes.remove(next_node)
193+
194+
for _ in range(min(num_connections - 1, len(available_nodes))):
195+
random_node = random.choice(available_nodes)
196+
node["connect"].append(f"tank-{random_node:04d}")
197+
available_nodes.remove(random_node)
198+
199+
nodes.append(node)
200+
201+
# Add image tag to the first node
202+
nodes[0]["image"] = {"tag": "v0.20.0"}
203+
204+
network_yaml_data = {"nodes": nodes}
205+
206+
with open(os.path.join(datadir, "network.yaml"), "w") as f:
207+
yaml.dump(network_yaml_data, f, default_flow_style=False)
208+
209+
# Generate defaults.yaml
210+
defaults_yaml_content = """
211+
chain: regtest
212+
213+
collectLogs: true
214+
metricsExport: true
215+
216+
resources: {}
217+
# We usually recommend not to specify default resources and to leave this as a conscious
218+
# choice for the user. This also increases chances charts run on environments with little
219+
# resources, such as Minikube. If you do want to specify resources, uncomment the following
220+
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
221+
# limits:
222+
# cpu: 100m
223+
# memory: 128Mi
224+
# requests:
225+
# cpu: 100m
226+
# memory: 128Mi
227+
228+
image:
229+
repository: bitcoindevproject/bitcoin
230+
pullPolicy: IfNotPresent
231+
# Overrides the image tag whose default is the chart appVersion.
232+
tag: "27.0"
233+
234+
config: |
235+
dns=1
236+
"""
237+
238+
with open(os.path.join(datadir, "defaults.yaml"), "w") as f:
239+
f.write(defaults_yaml_content.strip())
240+
241+
click.echo(f"Project '{datadir}' has been created with 'network.yaml' and 'defaults.yaml'.")

0 commit comments

Comments
 (0)