add(app): create python application
This commit is contained in:
0
cli/commands/__init__.py
Normal file
0
cli/commands/__init__.py
Normal file
36
cli/commands/export.py
Normal file
36
cli/commands/export.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/export" # Backend URL for exporting data
|
||||
|
||||
@click.command()
|
||||
@click.option(
|
||||
"--format",
|
||||
type=click.Choice(["yaml", "json"], case_sensitive=False),
|
||||
default="json",
|
||||
help="Specify the export format: yaml or json. Default is json.",
|
||||
)
|
||||
@click.argument("output", required=False, type=click.Path(writable=True))
|
||||
def export_data(format, output):
|
||||
"""
|
||||
Export Metal Check data in the specified format (yaml or json).
|
||||
|
||||
If an OUTPUT file is provided, the data will be saved to the file.
|
||||
Otherwise, it will be printed to the console.
|
||||
"""
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}?format={format}")
|
||||
if response.status_code == 200:
|
||||
data = response.text
|
||||
if output:
|
||||
with open(output, "w") as file:
|
||||
file.write(data)
|
||||
click.echo(f"Data successfully exported to {output}.")
|
||||
else:
|
||||
click.echo("\n📤 Exported Data:")
|
||||
click.echo(data)
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
57
cli/commands/k8s.py
Normal file
57
cli/commands/k8s.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/k8s" # Backend URL for Kubernetes API
|
||||
|
||||
@click.group()
|
||||
def kubernetes_nodes():
|
||||
"""Commands for managing Kubernetes Nodes."""
|
||||
pass
|
||||
|
||||
@kubernetes_nodes.command("list")
|
||||
def list_command():
|
||||
handle_command("list")
|
||||
|
||||
@kubernetes_nodes.command("think")
|
||||
def think_command():
|
||||
handle_command("think")
|
||||
|
||||
|
||||
def handle_command(action):
|
||||
"""Handle commands related to Kubernetes nodes."""
|
||||
if action == "list":
|
||||
list_kubernetes_nodes()
|
||||
elif action == "analyze":
|
||||
analyze_kubernetes_cluster()
|
||||
|
||||
def list_kubernetes_nodes():
|
||||
"""List all Kubernetes nodes."""
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/data")
|
||||
if response.status_code == 200:
|
||||
nodes = response.json().get("nodes", [])
|
||||
click.echo("\n📦 Kubernetes Nodes:")
|
||||
for node in nodes:
|
||||
click.echo(
|
||||
f"Node Name: {node['node_name']}, CPU: {node['cpu']}, "
|
||||
f"Memory: {node['memory']} MiB, Storage: {node['storage']}, "
|
||||
f"Type: {node['instance_type']}, Max Pods: {node['pods_allocatable']}, "
|
||||
f"Time on Duty: {node['time_on_duty']}"
|
||||
)
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def analyze_kubernetes_cluster():
|
||||
"""Request an AI analysis of the Kubernetes cluster."""
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/think/k8s")
|
||||
if response.status_code == 200:
|
||||
summary = response.json().get("summary", "No analysis provided.")
|
||||
click.echo("\n🤖 AI Analysis of Kubernetes Cluster:")
|
||||
click.echo(summary)
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
96
cli/commands/metal.py
Normal file
96
cli/commands/metal.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/metal" # Backend URL for Metal Nodes API
|
||||
|
||||
@click.group()
|
||||
def metal_nodes():
|
||||
"""Commands for managing Metal Nodes."""
|
||||
pass
|
||||
|
||||
@metal_nodes.command("list")
|
||||
def list_command():
|
||||
handle_command("list")
|
||||
|
||||
@metal_nodes.command("add")
|
||||
def add_command():
|
||||
handle_command("add")
|
||||
|
||||
@metal_nodes.command("delete")
|
||||
def delete_command():
|
||||
handle_command("delete")
|
||||
|
||||
def handle_command(action):
|
||||
"""Handle commands related to Metal Nodes."""
|
||||
if action == "list":
|
||||
list_metal_nodes()
|
||||
elif action == "add":
|
||||
add_metal_node()
|
||||
elif action == "delete":
|
||||
delete_metal_node()
|
||||
|
||||
def list_metal_nodes():
|
||||
"""List all metal nodes."""
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/data")
|
||||
if response.status_code == 200:
|
||||
metal_nodes = response.json().get("metal_nodes", [])
|
||||
click.echo("\n🖥️ Metal Nodes:")
|
||||
for node in metal_nodes:
|
||||
click.echo(
|
||||
f"ID: {node[0]}, Name: {node[1]}, Location: {node[2]}, "
|
||||
f"Vendor: {node[3]}, CPU: {node[4]}, Memory: {node[5]}, "
|
||||
f"Storage: {node[6]}, Time on Duty: {node[7]}"
|
||||
)
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def add_metal_node():
|
||||
"""Add a new metal node."""
|
||||
try:
|
||||
# Gather inputs from the prompt
|
||||
name = click.prompt("Name")
|
||||
location = click.prompt("Location")
|
||||
vendor = click.prompt("Vendor")
|
||||
cpu = click.prompt("CPU (cores)", type=int)
|
||||
memory = click.prompt("Memory (GB)")
|
||||
storage = click.prompt("Storage (e.g., 1TB SSD)")
|
||||
time_on_duty = click.prompt("Time on Duty (hours)", type=int)
|
||||
initial_cost = click.prompt("Initial Cost ($)", type=float)
|
||||
|
||||
data = {
|
||||
"name": name,
|
||||
"location": location,
|
||||
"vendor": vendor,
|
||||
"cpu": cpu,
|
||||
"memory": memory,
|
||||
"storage": storage,
|
||||
"time_on_duty": time_on_duty,
|
||||
"initial_cost": initial_cost,
|
||||
}
|
||||
|
||||
# Send the POST request to the backend
|
||||
response = requests.post(f"{BASE_URL}/data", json=data)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
response_data = response.json()
|
||||
message = response_data.get("message", "Metal node added successfully!")
|
||||
click.echo(f"✅ {message}")
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def delete_metal_node():
|
||||
"""Delete a metal node."""
|
||||
try:
|
||||
node_id = click.prompt("Enter the ID of the metal node to delete", type=int)
|
||||
response = requests.delete(f"{BASE_URL}/data/{node_id}")
|
||||
if response.status_code == 200:
|
||||
click.echo("✅ Metal node deleted successfully!")
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
89
cli/commands/vm.py
Normal file
89
cli/commands/vm.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/vm" # Backend URL for Virtual Machines API
|
||||
|
||||
@click.group()
|
||||
def virtual_machines():
|
||||
"""Commands for managing Virtual Machines."""
|
||||
pass
|
||||
|
||||
@virtual_machines.command("list")
|
||||
def list_command():
|
||||
handle_command("list")
|
||||
|
||||
@virtual_machines.command("add")
|
||||
def add_command():
|
||||
handle_command("add")
|
||||
|
||||
@virtual_machines.command("delete")
|
||||
def delete_command():
|
||||
handle_command("delete")
|
||||
|
||||
def handle_command(action):
|
||||
"""Handle commands related to Virtual Machines."""
|
||||
if action == "list":
|
||||
list_virtual_machines()
|
||||
elif action == "add":
|
||||
add_virtual_machine()
|
||||
elif action == "delete":
|
||||
delete_virtual_machine()
|
||||
|
||||
def list_virtual_machines():
|
||||
"""List all virtual machines."""
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/data")
|
||||
if response.status_code == 200:
|
||||
virtual_machines = response.json().get("virtual_machines", [])
|
||||
click.echo("\n💻 Virtual Machines:")
|
||||
for vm in virtual_machines:
|
||||
click.echo(
|
||||
f"ID: {vm[0]}, Name: {vm[1]}, Location: {vm[2]}, "
|
||||
f"CPU: {vm[3]}, Memory: {vm[4]}, Storage: {vm[5]}, "
|
||||
f"Type: {vm[6]}"
|
||||
)
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def add_virtual_machine():
|
||||
"""Add a new virtual machine."""
|
||||
try:
|
||||
name = click.prompt("Name")
|
||||
location = click.prompt("Location")
|
||||
cpu = click.prompt("CPU (cores)", type=int)
|
||||
memory = click.prompt("Memory (GB)")
|
||||
storage = click.prompt("Storage (e.g., 500GB SSD)")
|
||||
vm_type = click.prompt("Type (e.g., cx-21)")
|
||||
|
||||
data = {
|
||||
"name": name,
|
||||
"location": location,
|
||||
"cpu": cpu,
|
||||
"memory": memory,
|
||||
"storage": storage,
|
||||
"type": vm_type,
|
||||
}
|
||||
|
||||
response = requests.post(f"{BASE_URL}/data", json=data)
|
||||
if response.status_code in [200, 201]:
|
||||
response_data = response.json()
|
||||
message = response_data.get("message", "Virtual machine added successfully!")
|
||||
click.echo(f"✅ {message}")
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def delete_virtual_machine():
|
||||
"""Delete a virtual machine."""
|
||||
try:
|
||||
vm_id = click.prompt("Enter the ID of the virtual machine to delete", type=int)
|
||||
response = requests.delete(f"{BASE_URL}/data/{vm_id}")
|
||||
if response.status_code == 200:
|
||||
click.echo("✅ Virtual machine deleted successfully!")
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
Reference in New Issue
Block a user