-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
113 lines (96 loc) · 3.98 KB
/
cli.py
File metadata and controls
113 lines (96 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import requests
import argparse
import sys
API_URL = "http://localhost:5000"
def add_node(cpu_cores):
response = requests.post(f"{API_URL}/add_node", json={"cpu_cores": cpu_cores})
res = response.json()
if 'error' not in res:
print(f"Message: {res['message']}")
else:
print(res['error'], file=sys.stderr)
exit(1)
def launch_pod(cpu_cores):
response = requests.post(f"{API_URL}/launch_pod", json={"cpu_cores": cpu_cores})
res = response.json()
if 'error' not in res:
print(f"Message: {res['message']}, Node ID: {res['node_id']}, Pod ID: {res['pod_id']}")
else:
print(res['error'], file=sys.stderr)
exit(1)
def list_nodes():
response = requests.get(f"{API_URL}/list_nodes")
try:
for node in response.json():
print(f"Node {node['node_id']}: CPU={node['cpu_cores']}, Available={node['available_cores']}, Status={node['status']}, Pods={node['pods']}")
except Exception as e:
print("Error:", e)
def add_policy(source_pod, target_pod, action):
response = requests.post(f"{API_URL}/add_policy", json={"source_pod": source_pod, "target_pod": target_pod, "action": action})
res = response.json()
if 'error' not in res:
print(f"Message: {res['message']}")
else:
print(res['error'], file=sys.stderr)
exit(1)
def update_policy(source_pod, target_pod, new_action):
response = requests.post(f"{API_URL}/update_policy", json={
"source_pod": source_pod,
"target_pod": target_pod,
"new_action": new_action
})
res = response.json()
if 'error' not in res:
print(f"Message: {res['message']}")
else:
print(res['error'], file=sys.stderr)
exit(1)
def list_policies():
response = requests.get(f"{API_URL}/list_policies")
try:
policies = response.json()
for policy in policies:
print(f"Source: {policy['source_pod']}, Target: {policy['target_pod']}, Action: {policy['action']}")
except Exception as e:
print("Error:", e)
def pod_usage():
response = requests.get(f"{API_URL}/pod_usage")
try:
usages = response.json()
for usage in usages:
print(f"Node: {usage['node_id']}, Pod: {usage['pod_id']}, CPU Cores: {usage['cpu_cores_allocated']}")
except Exception as e:
print("Error:", e)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Kubernetes-like System CLI")
subparsers = parser.add_subparsers(dest="command")
add_parser = subparsers.add_parser("add_node")
add_parser.add_argument("--cpu_cores", type=int, required=True)
launch_parser = subparsers.add_parser("launch_pod")
launch_parser.add_argument("--cpu_cores", type=int, required=True)
subparsers.add_parser("list_nodes")
add_policy_parser = subparsers.add_parser("add_policy")
add_policy_parser.add_argument("--source_pod", required=True)
add_policy_parser.add_argument("--target_pod", required=True)
add_policy_parser.add_argument("--action", required=True, choices=["allow", "deny"])
add_policy_parser = subparsers.add_parser("update_policy")
add_policy_parser.add_argument("--source_pod", required=True)
add_policy_parser.add_argument("--target_pod", required=True)
add_policy_parser.add_argument("--new_action", required=True, choices=["allow", "deny"])
subparsers.add_parser("list_policies")
subparsers.add_parser("pod_usage")
args = parser.parse_args()
if args.command == "add_node":
add_node(args.cpu_cores)
elif args.command == "launch_pod":
launch_pod(args.cpu_cores)
elif args.command == "list_nodes":
list_nodes()
elif args.command == "add_policy":
add_policy(args.source_pod, args.target_pod, args.action)
elif args.command == "list_policies":
list_policies()
elif args.command == "pod_usage":
pod_usage()
elif args.command == "update_policy":
update_policy(args.source_pod, args.target_pod, args.new_action)