1
1
import os
2
+ import random
2
3
import subprocess
3
4
import sys
4
5
from importlib .resources import files
16
17
from .image import image
17
18
from .network import copy_network_defaults , copy_scenario_defaults
18
19
from .status import status as status_command
20
+ from .util import SUPPORTED_TAGS
19
21
20
22
QUICK_START_PATH = files ("resources.scripts" ).joinpath ("quick_start.sh" )
21
23
@@ -57,19 +59,36 @@ def quickstart():
57
59
return False
58
60
59
61
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
+ )
60
72
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 :
69
75
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
+ )
70
89
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 )
73
92
74
93
except Exception as e :
75
94
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):
91
110
richprint (f"[green]Created warnet project structure in { directory } [/green]" )
92
111
except Exception as e :
93
112
richprint (f"[red]Error creating project: { e } [/red]" )
113
+ raise e
114
+
94
115
95
116
@cli .command ()
96
117
@click .argument (
@@ -103,6 +124,7 @@ def create(directory: Path):
103
124
return
104
125
create_warnet_project (directory )
105
126
127
+
106
128
@cli .command ()
107
129
def init ():
108
130
"""Initialize a warnet project in the current directory"""
@@ -149,3 +171,71 @@ def auth(kube_config: str) -> None:
149
171
150
172
if __name__ == "__main__" :
151
173
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