-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscale_tools.py
More file actions
385 lines (324 loc) · 12.5 KB
/
scale_tools.py
File metadata and controls
385 lines (324 loc) · 12.5 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import httpx
import subprocess
import pickle
import os
import time
import sqlite3
import json
from typing import Any
from mcp.server.fastmcp import FastMCP
from config_alt import *
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
mcp = FastMCP("scale-api-proxy")
async def make_request(url: str, method: str, headers: dict = None, data: dict = None, params: dict = None) -> dict[str, Any] | None:
headers = headers or {}
headers["User-Agent"] = USER_AGENT
async with httpx.AsyncClient(verify=False) as client:
try:
method_lower = method.lower()
request_args = {"headers": headers, "timeout": 30.0}
if method_lower in ["get", "delete", "head", "options"]:
request_args["params"] = data or params
else:
request_args["json"] = data
if params:
request_args["params"] = params
response = await getattr(client, method_lower)(url, **request_args)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
return {"error": str(e), "details": e.response.text}
except Exception as e:
return {"error": str(e)}
def _load_session_headers() -> dict:
with open("/Projects/api_scale/session/aisys_sessionLogin.p", "rb") as f:
return pickle.load(f)
def search_endpoint(query: str):
conn = sqlite3.connect("api_schema.db")
try:
cursor = conn.cursor()
cursor.execute(
"SELECT path, method, description, request_body, responses FROM api_endpoints WHERE path LIKE ?",
(f"%{query}%",)
)
results = cursor.fetchall()
finally:
conn.close()
return [
{
"path": path,
"method": method,
"description": description,
"request_body": json.loads(request_body) if request_body != "None" else None,
"responses": json.loads(responses),
}
for path, method, description, request_body, responses in results
]
@mcp.tool()
async def run_api(query: str, method: str, payload: dict = None) -> str:
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1{query}"
return await make_request(scale_api_url, method, headers=_load_session_headers(), data=payload)
@mcp.tool()
async def query_api(query: str) -> str:
"""
Queries the local SCALE REST API schema for matching endpoints.
"""
results = search_endpoint(query)
if not results:
return json.dumps({"error": "No matching endpoints found"})
available_paths = [
{
"path": e["path"],
"description": e["description"],
"method": e["method"],
"request_body": e["request_body"],
"response": e["responses"],
}
for e in results
]
return json.dumps({"available_paths": available_paths})
@mcp.tool()
async def fetch_vm(user: str = None, name: str = None) -> str:
"""
Look up VMs by Name OR User via NMAPX Dash [WebAD].
"""
if not user and not name:
return json.dumps({"error": "Must provide either 'user' or 'name' parameter"})
params = {"user": user} if user else {"name": name}
async with httpx.AsyncClient(timeout=5.0, verify=False) as client:
try:
response = await client.get("http://172.18.33.140:444/api/symconsole?", params=params)
response.raise_for_status()
return response.text
except httpx.HTTPError as e:
return json.dumps({"error": f"Remote request failed: {str(e)}"})
except Exception as e:
return json.dumps({"error": f"Unexpected error: {str(e)}"})
@mcp.tool()
async def iso_read() -> str:
"""
Retrieves a list of all ISOs from the SCALE API server.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/ISO"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def drive_read() -> str:
"""
Retrieves a list of all Physical Drives from the SCALE API server.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/Drive"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def ping_read() -> str:
"""
Retrieve a ping response from the SCALE API server to check connectivity and session validity.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/ping"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def condition_read() -> str:
"""
Retrieves a list of all active conditions from the SCALE API server.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/Condition/filter?includeSet=true"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def clusterSpec_read() -> str:
"""
Retrieves the current ClusterSpec record from the SCALE API server.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/ClusterSpec"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def cluster_read() -> str:
"""
Retrieves information about a given system (SCALE API Server)
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/Cluster"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def user_read() -> str:
"""
Retrieves information about all users present in system (SCALE API Server)
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/User"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def virtualDisk_read() -> str:
"""
Retrieves information about all users present in system (SCALE API Server)
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirtualDisk"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def taskTagStatus_read() -> str:
"""
Retrieves information about all statuses (SCALE API Server)
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/TaskTag"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def vmNetDevices_read() -> str:
"""
Retrieves list of all virtual network devices (SCALE API Server)
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomainNetDevice"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def vmSnapshots_read() -> str:
"""
Retrieves list of all virtual network devices (SCALE API Server)
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomainSnapshot"
return await make_request(scale_api_url, "GET", headers=_load_session_headers())
@mcp.tool()
async def taskTagStatusFilter(taskTag: str) -> str:
"""
Retrieves information about all statuses, filteed by specific task (status) ID (SCALE API Server)
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/TaskTag/{taskTag}"
method = "GET"
return await make_request(scale_api_url, method, headers=_load_session_headers())
@mcp.tool()
async def getVM_details(vmUUID: str) -> str:
"""
Retrieves information about a specific VM (SCALE API Server). Use fetch_vm to get VM UUIDs based on username or VM name.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomain/{vmUUID}"
method = "GET"
return await make_request(scale_api_url, method, headers=_load_session_headers())
@mcp.tool()
async def getVM_sysStats(vmUUID: str) -> str:
"""
Retrieves CPU, Memory, Disks and Network info about a specific VM (SCALE API Server). Use fetch_vm to get VM UUIDs based on username or VM name.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomainStats/{vmUUID}"
method = "GET"
return await make_request(scale_api_url, method, headers=_load_session_headers())
@mcp.tool()
async def cloneVM(vmUUID: str) -> str:
"""
Clones a specific VM (SCALE API Server). Use fetch_vm to get VM UUIDs based on username or VM name.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomain/{vmUUID}/clone"
method = "POST"
return await make_request(scale_api_url, method, headers=_load_session_headers())
@mcp.tool()
async def exportVM(vmUUID: str) -> str:
"""
Exports a specific VM (SCALE API Server) to the Archive Server. Use fetch_vm to get VM UUIDs based on username or VM name.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomain/{vmUUID}/export"
method = "POST"
payload = {
"target": {
"pathURI": f"{smbConnectionStuff}",
"compress": True,
"allowNonSequentialWrites": True,
"parallelCountPerTransfer": 4
}
}
return await make_request(scale_api_url, method, headers=_load_session_headers(), data=payload)
@mcp.tool()
async def changeVM_state(vmUUID: str, actionType: str) -> str:
"""
Take action on a specific VM (SCALE API Server) - actionType can be one of: "START", "SHUTDOWN", "STOP", "PAUSE", "REBOOT", "RESET", "LIVEMIGRATE", "NMI"
Use fetch_vm to get VM UUIDs based on username or VM name.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomain/action"
method = "POST"
payload = [
{
"virDomainUUID": f"{vmUUID}",
"actionType": f"{actionType}",
}
]
return await make_request(scale_api_url, method, headers=_load_session_headers(), data=payload)
@mcp.tool()
async def snapshotVM(vmUUID: str, label: str) -> str:
"""
Snapshots a specific VM (SCALE API Server) in real-time. Use fetch_vm to get VM UUIDs based on username or VM name.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomainSnapshot"
method = "POST"
payload = {
"domainUUID": f"{vmUUID}",
"label": f"{label}",
"type": "USER",
"blockCountDiffFromSerialNumber": -1,
"replication": False,
}
return await make_request(scale_api_url, method, headers=_load_session_headers(), data=payload)
@mcp.tool()
async def delete_snapshotVM(SnapUUID: str) -> str:
"""
Deletes a specific snapshot from a singular VM (SCALE API Server) in real-time. Use vmSnapshots_read to get snapshot UUIDs based on VM UUID.
"""
host = "172.18.33.216"
scale_api_url = f"https://{host}/rest/v1/VirDomainSnapshot/{SnapUUID}"
method = "DELETE"
return await make_request(scale_api_url, method, headers=_load_session_headers())
@mcp.tool()
async def generate_session() -> str:
"""
Runs gen_sessionID.py to generate a new session ID.
"""
try:
result = subprocess.run(
['/Projects/api_scale/vfx/bin/python', '/Projects/api_scale/gen_sessionID.py'],
capture_output=True, text=True
)
result.check_returncode()
return result.stdout.strip()
except subprocess.CalledProcessError as e:
return f"Error generating session ID:\n{e.stderr or 'No stderr'}\n\nstdout:\n{e.stdout}"
@mcp.tool()
async def get_session() -> str:
"""
Validates the current session based on a 12-hour window.
"""
try:
SESSION_FILE = "/Projects/api_scale/session/aisys_sessionLogin.p"
age = int(time.time()) - os.path.getmtime(SESSION_FILE)
with open(SESSION_FILE, "rb") as f:
cookie_header = pickle.load(f).get('Cookie', '')
session_id = cookie_header[len('sessionID='):] if cookie_header.startswith('sessionID=') else ''
if age < 43200:
return f"Found Valid Session: {session_id} | Age: {age} seconds"
return f"Session expired: {session_id}"
except Exception as e:
return f"Session check failed: {e}"
@mcp.tool()
async def kill_session() -> str:
"""
Terminates the current session.
"""
try:
result = subprocess.run(
['/Projects/api_scale/vfx/bin/python', '/Projects/api_scale/kill_sessionID.py'],
capture_output=True, text=True
)
result.check_returncode()
return "Session killed."
except subprocess.CalledProcessError as e:
return f"Error killing session: {e}"
mcp_http_app = mcp.streamable_http_app()