mirror of
https://github.com/OVERLORD7F/SVMU.git
synced 2025-10-01 21:52:47 +03:00
Complete menus overhaul
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
# functions for working with domain-api
|
||||
import requests
|
||||
import secrets #for generating unique names
|
||||
|
||||
from rich.console import Console
|
||||
import os
|
||||
from rich.console import Console , Align
|
||||
from rich.columns import Columns
|
||||
from rich.panel import Panel
|
||||
from rich.prompt import Prompt
|
||||
|
||||
console = Console() #necessary for pretty menus & output
|
||||
power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown
|
||||
|
||||
|
||||
@@ -74,57 +76,70 @@ def delete_disk(base_url , api_key , vdisk_uuid):
|
||||
|
||||
|
||||
def get_disk_info(domain_all_content):
|
||||
console = Console()
|
||||
# check for "vdisks" field in recieved json response
|
||||
if 'vdisks' not in domain_all_content:
|
||||
print("No 'vdisks' field in recieved data")
|
||||
return
|
||||
|
||||
# get vdisk list
|
||||
disks = domain_all_content['vdisks']
|
||||
|
||||
# check for disks
|
||||
if not disks:
|
||||
print("No 'disks' field in recieved data. \nProbably VM does not have any attached disks?")
|
||||
console.print("[bold yellow]No 'disks' field in recieved data. \nProbably VM does not have any attached disks?")
|
||||
return
|
||||
|
||||
disk_info_renderables = []
|
||||
# Print info for each disk
|
||||
for disk in disks:
|
||||
# check for requiered fileds
|
||||
# check for required fields
|
||||
if 'id' in disk and 'verbose_name' in disk and 'size' in disk:
|
||||
print(f"Name: {disk['verbose_name']}")
|
||||
print(f"UUID: {disk['id']}")
|
||||
print(f"Size: {disk['size']} GB")
|
||||
print("-" * 51)
|
||||
output_string = (
|
||||
f"[bold]Name:[/] {disk['verbose_name']}\n"
|
||||
f"[bold]UUID:[/] [italic]{disk['id']}[/italic]\n"
|
||||
f"[bold]Size:[/] {disk['size']} GB")
|
||||
disk_info_renderables.append(Panel(output_string, expand=False, border_style="magenta"))
|
||||
else:
|
||||
print("ERROR: failed to retrieve vdisk data.")
|
||||
|
||||
console.print(Columns(disk_info_renderables))
|
||||
|
||||
def vm_info(base_url , api_key , vm_uuids):
|
||||
domain_info = get_domain_info(base_url , api_key , vm_uuids)
|
||||
domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids)
|
||||
def vm_info(base_url, api_key, vm_uuids):
|
||||
domain_info = get_domain_info(base_url, api_key, vm_uuids)
|
||||
domain_all_content = get_domain_all_content(base_url, api_key, vm_uuids)
|
||||
if domain_info:
|
||||
print("\n" , "=" * 14 , "Virtual Machine Info" , "=" * 15)
|
||||
print(f"\t VM: {domain_info['verbose_name']}")
|
||||
print(f"\t Power State: {power_state[domain_info['user_power_state']]}") #translating status code to "pretty name"
|
||||
print(f"\t vDisks: {domain_info['vdisks_count']}")
|
||||
print("-" * 19 , "vDisks Info" , "-" * 19)
|
||||
console = Console()
|
||||
vm_info_lines = f"[bold]Power State:[/] [bold red]{power_state[domain_info['user_power_state']]}[/bold red] \n[bold]vDisks:[/] {domain_info['vdisks_count']}"
|
||||
vm_info_renderable = Panel(vm_info_lines, title=f"[bold magenta]{domain_info['verbose_name']}" , expand=False , border_style="yellow")
|
||||
vm_info_renderable=Align.center(vm_info_renderable, vertical="middle")
|
||||
print("\n")
|
||||
console.rule(style="yellow")
|
||||
console.print(vm_info_renderable)
|
||||
console.rule(title = "[bold yellow]vDisks Info" , style="grey53" , align="center")
|
||||
get_disk_info(domain_all_content)
|
||||
console.rule(style="yellow")
|
||||
|
||||
|
||||
def vm_info_short(base_url , api_key): #output data pool info
|
||||
url= f"http://{base_url}//api/domains/"
|
||||
response = requests.get(url , headers={'Authorization' : api_key})
|
||||
|
||||
def vm_info_short(base_url, api_key):
|
||||
url = f"http://{base_url}/api/domains/"
|
||||
response = requests.get(url, headers={'Authorization': api_key})
|
||||
if response.status_code == 200:
|
||||
vm_info_short = response.json()
|
||||
results_vm_info_short = vm_info_short['results']
|
||||
print(f"\nShort VM overview | Total: {vm_info_short['count']}")
|
||||
print("=" * 43)
|
||||
os.system('cls' if os.name=='nt' else 'clear')
|
||||
console.print(Align.center(Panel(f"[bold magenta]Short VM overview | Total: {vm_info_short['count']}", expand=True , border_style="yellow") , vertical="middle"))
|
||||
console.rule(style="grey53")
|
||||
output_renderables = []
|
||||
for x in results_vm_info_short:
|
||||
print(f" VM: {x['verbose_name']}")
|
||||
print(f"UUID: {x['id']}")
|
||||
print("-" * 43)
|
||||
|
||||
output_string = f"VM: [bold]{x['verbose_name']}" + f"\nUUID: [italic]{x['id']}"
|
||||
output_renderable = Panel(output_string, expand=False, border_style="magenta")
|
||||
output_renderables.append(output_renderable) #adds current renderable
|
||||
console.print(Columns(output_renderables)) #print renderables by columns
|
||||
else:
|
||||
print(f"Failed to retrieve data {response.status_code}")
|
||||
print(f"Failed to retrieve data {response.status_code}")
|
||||
console.rule(style="grey53")
|
||||
Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.... :right_arrow_curving_down:")
|
||||
os.system('cls' if os.name=='nt' else 'clear')
|
||||
|
||||
def create_and_attach_disk(base_url , api_key , vm_id, data_pool_uuid, vdisk_size, preallocation):
|
||||
domain_name=get_domain_info(base_url , api_key , vm_id)
|
||||
@@ -143,7 +158,7 @@ def create_and_attach_disk(base_url , api_key , vm_id, data_pool_uuid, vdisk_siz
|
||||
}
|
||||
response = requests.post(url , headers=headers, json=payload)
|
||||
if response.status_code == 200:
|
||||
print(f"vDisk {disk_name} - {vdisk_size}GB has been created and attached to VM - {vm_id}")
|
||||
print(f"vDisk {disk_name} ({vdisk_size}GB) has been created and attached")
|
||||
return True
|
||||
else:
|
||||
print(f"ERROR creating vDisk :\n {response.status_code} - {response.text}")
|
||||
|
Reference in New Issue
Block a user