@@ -19,19 +19,7 @@ def network():
1919 """Network commands"""
2020
2121
22- def set_kubectl_context (namespace : str ):
23- """
24- Set the default kubectl context to the specified namespace.
25- """
26- command = f"kubectl config set-context --current --namespace={ namespace } "
27- result = run_command (command , stream_output = True )
28- if result :
29- print (f"Kubectl context set to namespace: { namespace } " )
30- else :
31- print (f"Failed to set kubectl context to namespace: { namespace } " )
32- return result
33-
34-
22+ # High-level network operations
3523@network .command ()
3624@click .argument ("graph_file" , default = DEFAULT_GRAPH_FILE , type = click .Path ())
3725@click .option ("--network" , default = "warnet" , show_default = True )
@@ -40,53 +28,27 @@ def start(graph_file: Path, logging: bool, network: str):
4028 """
4129 Start a warnet with topology loaded from a <graph_file> into [network]
4230 """
43- # Generate the Kubernetes YAML
4431 graph = read_graph_file (graph_file )
4532 kubernetes_yaml = generate_kubernetes_yaml (graph )
4633
47- # Write the YAML to a temporary file
4834 with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".yaml" , delete = False ) as temp_file :
4935 yaml .dump_all (kubernetes_yaml , temp_file )
5036 temp_file_path = temp_file .name
5137
5238 try :
53- # Deploy base configurations
54- base_configs = [
55- "namespace.yaml" ,
56- "rbac-config.yaml" ,
57- ]
58-
59- for config in base_configs :
60- command = f"kubectl apply -f { WAR_MANIFESTS } /{ config } "
61- result = run_command (command , stream_output = True )
62- if not result :
63- print (f"Failed to apply { config } " )
64- return
65-
66- # Apply the YAML using kubectl
67- command = f"kubectl apply -f { temp_file_path } "
68- result = run_command (command , stream_output = True )
69-
70- if result :
39+ if deploy_base_configurations () and apply_kubernetes_yaml (temp_file_path ):
7140 print (f"Warnet '{ network } ' started successfully." )
72-
73- # Set kubectl context to the warnet namespace
74- context_result = set_kubectl_context (network )
75- if not context_result :
41+ if not set_kubectl_context (network ):
7642 print (
7743 "Warning: Failed to set kubectl context. You may need to manually switch to the warnet namespace."
7844 )
79-
80- if logging :
81- helm_result = setup_logging_helm ()
82- if helm_result :
83- print ("Helm charts installed successfully." )
84- else :
85- print ("Failed to install Helm charts." )
45+ if logging and setup_logging_helm ():
46+ print ("Helm charts installed successfully." )
47+ else :
48+ print ("Failed to install Helm charts." )
8649 else :
8750 print (f"Failed to start warnet '{ network } '." )
8851 finally :
89- # Clean up the temporary file
9052 Path (temp_file_path ).unlink ()
9153
9254
@@ -96,30 +58,20 @@ def down(network: str):
9658 """
9759 Bring down a running warnet named [network]
9860 """
99- # Delete the namespace
100- command = f"kubectl delete namespace { network } "
101- result = run_command (command , stream_output = True )
102- # TODO: Fix this
103- command = "kubectl delete namespace warnet-logging"
104- result = run_command (command , stream_output = True )
105-
106- if result :
107- print (f"Warnet '{ network } ' has been successfully brought down and the namespace deleted." )
61+ if delete_namespace (network ) and delete_namespace ("warnet-logging" ):
62+ print (f"Warnet '{ network } ' has been successfully brought down and the namespaces deleted." )
10863 else :
109- print (f"Failed to bring down warnet '{ network } ' or delete the namespace ." )
64+ print (f"Failed to bring down warnet '{ network } ' or delete the namespaces ." )
11065
11166
11267@network .command ()
11368@click .option ("--follow" , "-f" , is_flag = True , help = "Follow logs" )
11469def logs (follow : bool ):
11570 """Get Kubernetes logs from the RPC server"""
11671 command = "kubectl logs rpc-0"
117- stream_output = False
11872 if follow :
11973 command += " --follow"
120- stream_output = True
121-
122- run_command (command , stream_output = stream_output )
74+ run_command (command , stream_output = follow )
12375
12476
12577@network .command ()
@@ -129,44 +81,52 @@ def generate_yaml(graph_file: Path, output: str):
12981 """
13082 Generate a Kubernetes YAML file from a graph file for deploying warnet nodes.
13183 """
132- # Read and parse the graph file
13384 graph = read_graph_file (graph_file )
134-
135- # Generate the Kubernetes YAML
13685 kubernetes_yaml = generate_kubernetes_yaml (graph )
13786
138- # Write the YAML to a file
13987 with open (output , "w" ) as f :
14088 yaml .dump_all (kubernetes_yaml , f )
14189
14290 print (f"Kubernetes YAML file generated: { output } " )
14391
14492
145- def read_graph_file (graph_file : Path ) -> nx .Graph :
146- with open (graph_file ) as f :
147- return nx .parse_graphml (f .read ())
93+ @network .command ()
94+ @click .argument ("graph_file" , default = DEFAULT_GRAPH_FILE , type = click .Path (exists = True ))
95+ def connect (graph_file : Path ):
96+ """
97+ Connect nodes based on the edges defined in the graph file.
98+ """
99+ tree = ET .parse (graph_file )
100+ root = tree .getroot ()
101+ edges = root .findall (".//{http://graphml.graphdrawing.org/xmlns}edge" )
148102
103+ for edge in edges :
104+ source = edge .get ("source" )
105+ target = edge .get ("target" )
106+ command = f"kubectl exec -it warnet-node-{ source } -- bitcoin-cli -rpcuser=user -rpcpassword=password addnode warnet-node-{ target } -service:8333 add"
149107
150- def generate_kubernetes_yaml (graph : nx .Graph ) -> list :
151- kubernetes_objects = []
108+ print (f"Connecting node { source } to node { target } " )
109+ if run_command (command , stream_output = True ):
110+ print (f"Successfully connected node { source } to node { target } " )
111+ else :
112+ print (f"Failed to connect node { source } to node { target } " )
152113
153- # Add Namespace object
154- namespace = create_namespace ()
155- kubernetes_objects .append (namespace )
114+ print ("All connections attempted." )
156115
157- for node , data in graph .nodes (data = True ):
158- # Create a ConfigMap for each node
159- config = generate_node_config (node , data )
160- config_map = create_config_map (node , config )
161- kubernetes_objects .append (config_map )
162116
163- # Create a deployment for each node
164- deployment = create_node_deployment ( node , data )
165- kubernetes_objects . append ( deployment )
117+ # Kubernetes object generation
118+ def generate_kubernetes_yaml ( graph : nx . Graph ) -> list :
119+ kubernetes_objects = [ create_namespace ()]
166120
167- # Create a service for each node
168- service = create_node_service (node )
169- kubernetes_objects .append (service )
121+ for node , data in graph .nodes (data = True ):
122+ config = generate_node_config (node , data )
123+ kubernetes_objects .extend (
124+ [
125+ create_config_map (node , config ),
126+ create_node_deployment (node , data ),
127+ create_node_service (node ),
128+ ]
129+ )
170130
171131 return kubernetes_objects
172132
@@ -221,57 +181,22 @@ def create_node_service(node: int) -> dict:
221181 }
222182
223183
224- def setup_logging_helm ():
225- """
226- Run the required Helm commands for setting up Grafana, Prometheus, and Loki.
227- """
228- helm_commands = [
229- "helm repo add grafana https://grafana.github.io/helm-charts" ,
230- "helm repo add prometheus-community https://prometheus-community.github.io/helm-charts" ,
231- "helm repo update" ,
232- f"helm upgrade --install --namespace warnet-logging --create-namespace --values { WAR_MANIFESTS } /loki_values.yaml loki grafana/loki --version 5.47.2" ,
233- "helm upgrade --install --namespace warnet-logging promtail grafana/promtail" ,
234- "helm upgrade --install --namespace warnet-logging prometheus prometheus-community/kube-prometheus-stack --namespace warnet-logging --set grafana.enabled=false" ,
235- f"helm upgrade --install --namespace warnet-logging loki-grafana grafana/grafana --values { WAR_MANIFESTS } /grafana_values.yaml" ,
236- ]
237-
238- for command in helm_commands :
239- result = run_command (command , stream_output = True )
240- if not result :
241- print (f"Failed to run Helm command: { command } " )
242- return False
243- return True
244-
245-
246- @network .command ()
247- @click .argument ("graph_file" , default = DEFAULT_GRAPH_FILE , type = click .Path (exists = True ))
248- def connect (graph_file : Path ):
249- """
250- Connect nodes based on the edges defined in the graph file.
251- """
252- # Parse the GraphML file
253- tree = ET .parse (graph_file )
254- root = tree .getroot ()
255-
256- # Find all edge elements
257- edges = root .findall (".//{http://graphml.graphdrawing.org/xmlns}edge" )
258-
259- for edge in edges :
260- source = edge .get ("source" )
261- target = edge .get ("target" )
262-
263- # Construct the kubectl command
264- command = f"kubectl exec -it warnet-node-{ source } -- bitcoin-cli -rpcuser=user -rpcpassword=password addnode warnet-node-{ target } -service:8333 add"
184+ def create_config_map (node : int , config : str ) -> dict :
185+ return {
186+ "apiVersion" : "v1" ,
187+ "kind" : "ConfigMap" ,
188+ "metadata" : {
189+ "name" : f"bitcoin-config-node-{ node } " ,
190+ "namespace" : "warnet" ,
191+ },
192+ "data" : {"bitcoin.conf" : config },
193+ }
265194
266- print (f"Connecting node { source } to node { target } " )
267- result = run_command (command , stream_output = True )
268195
269- if result :
270- print (f"Successfully connected node { source } to node { target } " )
271- else :
272- print (f"Failed to connect node { source } to node { target } " )
273-
274- print ("All connections attempted." )
196+ # Utility functions
197+ def read_graph_file (graph_file : Path ) -> nx .Graph :
198+ with open (graph_file ) as f :
199+ return nx .parse_graphml (f .read ())
275200
276201
277202def generate_node_config (node : int , data : dict ) -> str :
@@ -300,13 +225,59 @@ def generate_node_config(node: int, data: dict) -> str:
300225 return f"{ base_config } \n { node_specific_config } "
301226
302227
303- def create_config_map (node : int , config : str ) -> dict :
304- return {
305- "apiVersion" : "v1" ,
306- "kind" : "ConfigMap" ,
307- "metadata" : {
308- "name" : f"bitcoin-config-node-{ node } " ,
309- "namespace" : "warnet" ,
310- },
311- "data" : {"bitcoin.conf" : config },
312- }
228+ def set_kubectl_context (namespace : str ):
229+ """
230+ Set the default kubectl context to the specified namespace.
231+ """
232+ command = f"kubectl config set-context --current --namespace={ namespace } "
233+ result = run_command (command , stream_output = True )
234+ if result :
235+ print (f"Kubectl context set to namespace: { namespace } " )
236+ else :
237+ print (f"Failed to set kubectl context to namespace: { namespace } " )
238+ return result
239+
240+
241+ def deploy_base_configurations ():
242+ base_configs = [
243+ "namespace.yaml" ,
244+ "rbac-config.yaml" ,
245+ ]
246+
247+ for config in base_configs :
248+ command = f"kubectl apply -f { WAR_MANIFESTS } /{ config } "
249+ if not run_command (command , stream_output = True ):
250+ print (f"Failed to apply { config } " )
251+ return False
252+ return True
253+
254+
255+ def apply_kubernetes_yaml (yaml_file : str ):
256+ command = f"kubectl apply -f { yaml_file } "
257+ return run_command (command , stream_output = True )
258+
259+
260+ def delete_namespace (namespace : str ):
261+ command = f"kubectl delete namespace { namespace } "
262+ return run_command (command , stream_output = True )
263+
264+
265+ def setup_logging_helm ():
266+ """
267+ Run the required Helm commands for setting up Grafana, Prometheus, and Loki.
268+ """
269+ helm_commands = [
270+ "helm repo add grafana https://grafana.github.io/helm-charts" ,
271+ "helm repo add prometheus-community https://prometheus-community.github.io/helm-charts" ,
272+ "helm repo update" ,
273+ f"helm upgrade --install --namespace warnet-logging --create-namespace --values { WAR_MANIFESTS } /loki_values.yaml loki grafana/loki --version 5.47.2" ,
274+ "helm upgrade --install --namespace warnet-logging promtail grafana/promtail" ,
275+ "helm upgrade --install --namespace warnet-logging prometheus prometheus-community/kube-prometheus-stack --namespace warnet-logging --set grafana.enabled=false" ,
276+ f"helm upgrade --install --namespace warnet-logging loki-grafana grafana/grafana --values { WAR_MANIFESTS } /grafana_values.yaml" ,
277+ ]
278+
279+ for command in helm_commands :
280+ if not run_command (command , stream_output = True ):
281+ print (f"Failed to run Helm command: { command } " )
282+ return False
283+ return True
0 commit comments