mirror of
https://github.com/OVERLORD7F/SVMU.git
synced 2025-10-01 21:52:47 +03:00
- Beggining of implementing new menus and text using "Rich" library
btw main.py is ~ 50 lines now 0_o
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
|
|
||||||
#from main import base_url , api_key , requests
|
#from main import base_url , api_key , requests
|
||||||
import requests
|
import requests
|
||||||
|
import os
|
||||||
|
from rich.prompt import Prompt
|
||||||
|
|
||||||
def cluster_info(base_url , api_key): #output short clusters overview
|
def cluster_info(base_url , api_key): #output short clusters overview
|
||||||
url= f"http://{base_url}/api/clusters"
|
url= f"http://{base_url}/api/clusters"
|
||||||
@@ -24,3 +26,5 @@ def cluster_info(base_url , api_key): #output short clusters overview
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
print(f"Failed to retrieve data {response.status_code}")
|
print(f"Failed to retrieve data {response.status_code}")
|
||||||
|
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
||||||
|
os.system('cls' if os.name=='nt' else 'clear')
|
@@ -1,9 +1,23 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from rich import print
|
||||||
|
from rich.panel import Panel
|
||||||
|
from rich.console import Console , Align
|
||||||
|
from rich.prompt import Prompt
|
||||||
|
console = Console()
|
||||||
|
|
||||||
def config_menu(config_relative_path):
|
def config_menu(config_relative_path):
|
||||||
print("\033[H\033[2J", end="") # clears cmd screen, but saves scrollback buffer
|
cls()
|
||||||
print("### Utility Configuration ###\n")
|
config_menu_options="[gold bold][1] [grey53 italic]Show current configuration\n[/grey53 italic] \
|
||||||
print("1) Show current configuration")
|
\n[gold bold][2] [grey53 italic]Change configuraion[/grey53 italic]\n \
|
||||||
print("2) Change configuraion")
|
\n\n[green_yellow bold]ENTER - exit Utility"
|
||||||
print("\nENTER - return to Utility Main Menu ")
|
config_menu_options=Align.center(config_menu_options, vertical="middle")
|
||||||
|
console = Console()
|
||||||
|
|
||||||
|
console.print(Panel(config_menu_options,
|
||||||
|
title="[gold bold]SpaceVM Utility - Utility Configuration" , style="dark_orange" , width=150 , padding = 2))
|
||||||
|
|
||||||
|
|
||||||
sub_choice=str(input("\n>>> "))
|
sub_choice=str(input("\n>>> "))
|
||||||
|
|
||||||
if sub_choice == "1":
|
if sub_choice == "1":
|
||||||
@@ -12,9 +26,11 @@ def config_menu(config_relative_path):
|
|||||||
config_edit(config_relative_path)
|
config_edit(config_relative_path)
|
||||||
|
|
||||||
def config_show(config_relative_path):
|
def config_show(config_relative_path):
|
||||||
|
cls()
|
||||||
print("Current configuration:\n")
|
print("Current configuration:\n")
|
||||||
with open(config_relative_path, "r") as f:
|
with open(config_relative_path, "r") as f:
|
||||||
print(f.read())
|
print(f.read())
|
||||||
|
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
||||||
|
|
||||||
def import_vm_uuid(config_relative_path):
|
def import_vm_uuid(config_relative_path):
|
||||||
vm_uuids = []
|
vm_uuids = []
|
||||||
@@ -56,10 +72,10 @@ def config_edit(config_relative_path):
|
|||||||
while (vm_input != ""):
|
while (vm_input != ""):
|
||||||
vm_input = input(">> ")
|
vm_input = input(">> ")
|
||||||
file.write(vm_input + '\n')
|
file.write(vm_input + '\n')
|
||||||
print("VM UUIDs has been written in config.")
|
console.print("[green bold]VM UUIDs has been written in config :pencil:")
|
||||||
print("\nConfiguration completed!")
|
console.print("[green bold]Configuration completed ! :white_check_mark:")
|
||||||
input("Press ENTER to continue..")
|
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
||||||
print("\033[H\033[2J", end="")
|
cls()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def cls():
|
||||||
|
os.system('cls' if os.name=='nt' else 'clear')
|
@@ -1,4 +1,6 @@
|
|||||||
import requests
|
import requests
|
||||||
|
import os
|
||||||
|
from rich.prompt import Prompt
|
||||||
|
|
||||||
def data_pools(base_url , api_key): #output data pool info
|
def data_pools(base_url , api_key): #output data pool info
|
||||||
url= f"http://{base_url}//api/data-pools/"
|
url= f"http://{base_url}//api/data-pools/"
|
||||||
@@ -17,3 +19,5 @@ def data_pools(base_url , api_key): #output data pool info
|
|||||||
print("-" * 44)
|
print("-" * 44)
|
||||||
else:
|
else:
|
||||||
print(f"Failed to retrieve data {response.status_code} ")
|
print(f"Failed to retrieve data {response.status_code} ")
|
||||||
|
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
||||||
|
os.system('cls' if os.name=='nt' else 'clear')
|
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
from domain_api import *
|
from domain_api import *
|
||||||
|
from rich.prompt import Prompt
|
||||||
|
|
||||||
def disk_edit_mode(base_url , api_key , data_pool_uuid , vm_uuids):
|
def disk_edit_mode(base_url , api_key , data_pool_uuid , vm_uuids):
|
||||||
print("\033[H\033[2J", end="")
|
print("\033[H\033[2J", end="")
|
||||||
@@ -50,9 +51,12 @@ def disk_edit_mode(base_url , api_key , data_pool_uuid , vm_uuids):
|
|||||||
print("All attached vDisks has been deleted!")
|
print("All attached vDisks has been deleted!")
|
||||||
for z in vm_uuids: # only for creating disks
|
for z in vm_uuids: # only for creating disks
|
||||||
domain_uuid = z.strip('\n')
|
domain_uuid = z.strip('\n')
|
||||||
|
print(f"\nCreating and attaching disk to VM {domain_uuid}")
|
||||||
domain_info = get_domain_info(base_url , api_key , domain_uuid)
|
domain_info = get_domain_info(base_url , api_key , domain_uuid)
|
||||||
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
|
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
|
||||||
if domain_info:
|
if domain_info:
|
||||||
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 10, "falloc")
|
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 10, "falloc")
|
||||||
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
|
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
|
||||||
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
|
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
|
||||||
|
print("Done. Happy virtualization :D")
|
||||||
|
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
@@ -1,9 +1,10 @@
|
|||||||
# functions for working with domain-api
|
# functions for working with domain-api
|
||||||
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import secrets #for generating unique names
|
import secrets #for generating unique names
|
||||||
|
|
||||||
|
from rich.console import Console
|
||||||
|
from rich.columns import Columns
|
||||||
|
from rich.panel import Panel
|
||||||
|
|
||||||
power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown
|
power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown
|
||||||
|
|
||||||
@@ -50,6 +51,7 @@ def get_disk_uuids(base_url , api_key , domain_all_content):
|
|||||||
print("ERROR: unexpected data format")
|
print("ERROR: unexpected data format")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def delete_disk(base_url , api_key , vdisk_uuid):
|
def delete_disk(base_url , api_key , vdisk_uuid):
|
||||||
url = f"http://{base_url}/api/vdisks/{vdisk_uuid}/remove/"
|
url = f"http://{base_url}/api/vdisks/{vdisk_uuid}/remove/"
|
||||||
headers={
|
headers={
|
||||||
@@ -70,6 +72,7 @@ def delete_disk(base_url , api_key , vdisk_uuid):
|
|||||||
print(f"ERROR deleting disk {vdisk_uuid} :\n {response.status_code} - {response.text}")
|
print(f"ERROR deleting disk {vdisk_uuid} :\n {response.status_code} - {response.text}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_disk_info(domain_all_content):
|
def get_disk_info(domain_all_content):
|
||||||
# check for "vdisks" field in recieved json response
|
# check for "vdisks" field in recieved json response
|
||||||
if 'vdisks' not in domain_all_content:
|
if 'vdisks' not in domain_all_content:
|
||||||
@@ -83,7 +86,6 @@ def get_disk_info(domain_all_content):
|
|||||||
if not disks:
|
if not disks:
|
||||||
print("No 'disks' field in recieved data. \nProbably VM does not have any attached disks?")
|
print("No 'disks' field in recieved data. \nProbably VM does not have any attached disks?")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Print info for each disk
|
# Print info for each disk
|
||||||
for disk in disks:
|
for disk in disks:
|
||||||
# check for requiered fileds
|
# check for requiered fileds
|
||||||
@@ -96,11 +98,9 @@ def get_disk_info(domain_all_content):
|
|||||||
print("ERROR: failed to retrieve vdisk data.")
|
print("ERROR: failed to retrieve vdisk data.")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def vm_info(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_info = get_domain_info(base_url , api_key , vm_uuids)
|
||||||
domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids)
|
domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids)
|
||||||
|
|
||||||
if domain_info:
|
if domain_info:
|
||||||
print("\n" , "=" * 14 , "Virtual Machine Info" , "=" * 15)
|
print("\n" , "=" * 14 , "Virtual Machine Info" , "=" * 15)
|
||||||
print(f"\t VM: {domain_info['verbose_name']}")
|
print(f"\t VM: {domain_info['verbose_name']}")
|
||||||
@@ -110,16 +110,12 @@ def vm_info(base_url , api_key , vm_uuids):
|
|||||||
get_disk_info(domain_all_content)
|
get_disk_info(domain_all_content)
|
||||||
|
|
||||||
|
|
||||||
import requests
|
|
||||||
|
|
||||||
def vm_info_short(base_url , api_key): #output data pool info
|
def vm_info_short(base_url , api_key): #output data pool info
|
||||||
url= f"http://{base_url}//api/domains/"
|
url= f"http://{base_url}//api/domains/"
|
||||||
response = requests.get(url , headers={'Authorization' : api_key})
|
response = requests.get(url , headers={'Authorization' : api_key})
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
vm_info_short = response.json()
|
vm_info_short = response.json()
|
||||||
results_vm_info_short = vm_info_short['results']
|
results_vm_info_short = vm_info_short['results']
|
||||||
|
|
||||||
print(f"\nShort VM overview | Total: {vm_info_short['count']}")
|
print(f"\nShort VM overview | Total: {vm_info_short['count']}")
|
||||||
print("=" * 43)
|
print("=" * 43)
|
||||||
for x in results_vm_info_short:
|
for x in results_vm_info_short:
|
||||||
|
27
main.py
27
main.py
@@ -4,12 +4,15 @@ from cluster_api import *
|
|||||||
from domain_api import *
|
from domain_api import *
|
||||||
from data_pools_api import *
|
from data_pools_api import *
|
||||||
from disk_edit_mode import *
|
from disk_edit_mode import *
|
||||||
|
from rich import print
|
||||||
|
from rich.panel import Panel
|
||||||
|
from rich.console import Console , Align
|
||||||
|
|
||||||
config_relative_path = os.path.join(os.getcwd() , 'config.txt') #config.txt in the same directory with main.py
|
config_relative_path = os.path.join(os.getcwd() , 'config.txt') #config.txt in the same directory with main.py
|
||||||
if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if config exists and not empty
|
if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if config exists and not empty
|
||||||
pass #do nothing
|
pass #do nothing
|
||||||
else:
|
else:
|
||||||
print("Config file was not found or empty.. ")
|
console.print("[yellow bold italic]Config file was not found or empty.. ")
|
||||||
config_edit(config_relative_path)
|
config_edit(config_relative_path)
|
||||||
|
|
||||||
#importing API-KEY / IP / DATA POOL UUID / VM-UUIDs from config
|
#importing API-KEY / IP / DATA POOL UUID / VM-UUIDs from config
|
||||||
@@ -18,15 +21,19 @@ base_url, api_key, data_pool_uuid = import_threelines(config_relative_path)
|
|||||||
vm_uuids = import_vm_uuid(config_relative_path)
|
vm_uuids = import_vm_uuid(config_relative_path)
|
||||||
|
|
||||||
menu_choice=0
|
menu_choice=0
|
||||||
|
menu_options="[gold bold][1] [grey53 italic]Manage utility config\n[/grey53 italic] \
|
||||||
|
\n[gold bold][2] [grey53 italic]Enter disk edit mode[/grey53 italic]\n \
|
||||||
|
\n[gold bold][3] [grey53 italic]Show breif cluster overview[/grey53 italic]\n \
|
||||||
|
\n[gold bold][4] [grey53 italic]Show VM info[/grey53 italic]\n \
|
||||||
|
\n[gold bold][5] [grey53 italic]Show data pools[/grey53 italic]\n \
|
||||||
|
\n[gold bold][6] [grey53 italic]Show VMs Name / UUID[/grey53 italic]\n \
|
||||||
|
\n\n[green_yellow bold]ENTER - exit Utility"
|
||||||
|
menu_options=Align.center(menu_options, vertical="middle")
|
||||||
|
menu_subtitle = "[blue bold][link=github.com/OVERLORD7F]:wrench: Project_GitHub[/link] [yellow]| [magenta bold][link=spacevm.ru/docs/]:books: SpaceVM_Docs[/link] [yellow]| [red bold][link=comptek.ru]:briefcase: Comptek[/link]"
|
||||||
|
console = Console()
|
||||||
while(menu_choice != ""): #main menu loop
|
while(menu_choice != ""): #main menu loop
|
||||||
print("\n*** Uitility Main Menu: ***\n")
|
console.print(Panel(menu_options,
|
||||||
print("1) Manage utility config")
|
title="[bold magenta]SpaceVM Utility - Main Menu" , subtitle = menu_subtitle, subtitle_align="right" , style="yellow" , width=150 , padding = 2))
|
||||||
print("2) Enter disk edit mode")
|
|
||||||
print("3) Show breif cluster overview")
|
|
||||||
print("4) Show VM info")
|
|
||||||
print("5) Show data pools")
|
|
||||||
print("6) Show VMs Name / UUID")
|
|
||||||
print("\nENTER - exit Utility ")
|
|
||||||
menu_choice=str(input("\n>>> "))
|
menu_choice=str(input("\n>>> "))
|
||||||
if menu_choice == "1":
|
if menu_choice == "1":
|
||||||
config_menu(config_relative_path)
|
config_menu(config_relative_path)
|
||||||
@@ -42,4 +49,4 @@ while(menu_choice != ""): #main menu loop
|
|||||||
data_pools(base_url , api_key)
|
data_pools(base_url , api_key)
|
||||||
if menu_choice == "6":
|
if menu_choice == "6":
|
||||||
vm_info_short(base_url , api_key)
|
vm_info_short(base_url , api_key)
|
||||||
print("Exiting Utility..")
|
console.print("[red bold]Exiting Utility ")
|
Reference in New Issue
Block a user