Skip to content

Commit adc569a

Browse files
committed
add namespaces cmds to cli
1 parent b154e4d commit adc569a

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

src/warnet/cli/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .image import image
1111

1212
# from .ln import ln
13+
from .namespaces import namespaces
1314
from .network import network
1415
from .scenarios import scenarios
1516

@@ -25,6 +26,7 @@ def cli():
2526
cli.add_command(graph)
2627
cli.add_command(image)
2728
# cli.add_command(ln)
29+
cli.add_command(namespaces)
2830
cli.add_command(network)
2931
cli.add_command(scenarios)
3032

src/warnet/cli/namespaces.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import os
2+
import tempfile
3+
from pathlib import Path
4+
5+
import click
6+
import yaml
7+
8+
from .process import stream_command, run_command
9+
10+
NAMESPACES_DIR = Path("namespaces")
11+
DEFAULT_NAMESPACES = "two_namespaces_two_users"
12+
NAMESPACES_FILE = "namespaces.yaml"
13+
DEFAULTS_FILE = "defaults.yaml"
14+
HELM_COMMAND = "helm upgrade --install"
15+
BITCOIN_CHART_LOCATION = "./resources/charts/namespaces"
16+
17+
@click.group(name="namespaces")
18+
def namespaces():
19+
"""Namespaces commands"""
20+
21+
22+
@namespaces.command()
23+
@click.argument("namespaces", default=DEFAULT_NAMESPACES)
24+
def deploy(namespaces: str):
25+
"""Deploy namespaces with users from a <namespaces_file>"""
26+
full_path = os.path.join(NAMESPACES_DIR, namespaces)
27+
namespaces_file_path = os.path.join(full_path, NAMESPACES_FILE)
28+
defaults_file_path = os.path.join(full_path, DEFAULTS_FILE)
29+
30+
namespaces_file = {}
31+
with open(namespaces_file_path) as f:
32+
namespaces_file = yaml.safe_load(f)
33+
34+
# validate names before deploying
35+
names = [n.get("name") for n in namespaces_file["namespaces"]]
36+
for n in names:
37+
if not n.startswith("warnet-"):
38+
print(f"Failled to create namespace: {n}. Namespaces must start with a 'warnet-' prefix.")
39+
40+
# deploy namespaces
41+
for namespace in namespaces_file["namespaces"]:
42+
print(f"Deploying namespace: {namespace.get('name')}")
43+
try:
44+
temp_override_file_path = ""
45+
namespace_name = namespace.get("name")
46+
# all the keys apart from name
47+
namespace_config_override = {k: v for k, v in namespace.items() if k != "name"}
48+
49+
cmd = f"{HELM_COMMAND} {namespace_name} {BITCOIN_CHART_LOCATION} -f {defaults_file_path}"
50+
51+
if namespace_config_override:
52+
with tempfile.NamedTemporaryFile(
53+
mode="w", suffix=".yaml", delete=False
54+
) as temp_file:
55+
yaml.dump(namespace_config_override, temp_file)
56+
temp_override_file_path = temp_file.name
57+
cmd = f"{cmd} -f {temp_override_file_path}"
58+
59+
if not stream_command(cmd):
60+
print(f"Failed to run Helm command: {cmd}")
61+
return
62+
except Exception as e:
63+
print(f"Error: {e}")
64+
return
65+
66+
@namespaces.command()
67+
def list():
68+
"""List all namespaces with 'warnet-' prefix"""
69+
cmd = "kubectl get namespaces -o jsonpath='{.items[*].metadata.name}'"
70+
res = run_command(cmd)
71+
all_namespaces = res.split()
72+
warnet_namespaces = [ns for ns in all_namespaces if ns.startswith("warnet-")]
73+
74+
if warnet_namespaces:
75+
print("Warnet namespaces:")
76+
for ns in warnet_namespaces:
77+
print(f"- {ns}")
78+
else:
79+
print("No warnet namespaces found.")
80+
81+
@namespaces.command()
82+
@click.option("--all", "destroy_all", is_flag=True, help="Destroy all warnet- prefixed namespaces")
83+
@click.argument("namespace", required=False)
84+
def destroy(destroy_all: bool, namespace: str):
85+
"""Destroy a specific namespace or all warnet- prefixed namespaces"""
86+
if destroy_all:
87+
cmd = "kubectl get namespaces -o jsonpath='{.items[*].metadata.name}'"
88+
res = run_command(cmd)
89+
90+
# Get the list of namespaces
91+
all_namespaces = res.split()
92+
warnet_namespaces = [ns for ns in all_namespaces if ns.startswith("warnet-")]
93+
94+
if not warnet_namespaces:
95+
print("No warnet namespaces found to destroy.")
96+
return
97+
98+
for ns in warnet_namespaces:
99+
destroy_cmd = f"kubectl delete namespace {ns}"
100+
if not stream_command(destroy_cmd):
101+
print(f"Failed to destroy namespace: {ns}")
102+
else:
103+
print(f"Destroyed namespace: {ns}")
104+
elif namespace:
105+
if not namespace.startswith("warnet-"):
106+
print("Error: Can only destroy namespaces with 'warnet-' prefix")
107+
return
108+
109+
destroy_cmd = f"kubectl delete namespace {namespace}"
110+
if not stream_command(destroy_cmd):
111+
print(f"Failed to destroy namespace: {namespace}")
112+
else:
113+
print(f"Destroyed namespace: {namespace}")
114+
else:
115+
print("Error: Please specify a namespace or use --all flag.")

0 commit comments

Comments
 (0)