- Changed "disk edit mode" into function and moved to new module

- Changed config sub menu into function and moved to config_data_import module
- New function config_show
- Fixed create disk option in disk edit mode
- A few formatting changes in menus
This commit is contained in:
OVERLORD7F
2025-05-16 12:17:51 +03:00
parent b6fb2b14e6
commit da756a8ba6
4 changed files with 90 additions and 64 deletions

View File

@@ -1,3 +1,20 @@
def config_menu(config_relative_path):
print("\033[H\033[2J", end="") # clears cmd screen, but saves scrollback buffer
print("### Utility Configuration ###\n")
print("1) Show current configuration")
print("2) Change configuraion")
print("\nENTER - return to Utility Main Menu ")
sub_choice=str(input("\n>>> "))
if sub_choice == "1":
config_show(config_relative_path)
if sub_choice == "2":
config_edit(config_relative_path)
def config_show(config_relative_path):
print("Current configuration:\n")
with open(config_relative_path, "r") as f:
print(f.read())
def import_vm_uuid(config_relative_path): def import_vm_uuid(config_relative_path):
vm_uuids = [] vm_uuids = []
@@ -27,17 +44,22 @@ def config_edit(config_relative_path):
if menu_choice == "Y" or menu_choice == "y": if menu_choice == "Y" or menu_choice == "y":
base_url = input("Type SpaceVM Controller IP: ") base_url = input("Type SpaceVM Controller IP: ")
api_key = input("Type your API Key: ") api_key = input("Type your API Key: ")
data_pool_uuid = input("Type Data pool uuid you wish to use: ") data_pool_uuid = input("Type Data Pool UUID you wish to use: ")
lines = [base_url, api_key, data_pool_uuid] lines = [base_url, api_key, data_pool_uuid]
with open(config_relative_path, "w+") as file: with open(config_relative_path, "w+") as file:
for line in lines: for line in lines:
file.write(line + '\n') file.write(line + '\n')
print("Type VM-UUID (input ENTER to stop)") print("Type VM UUIDs one by one (input ENTER to stop)")
with open(config_relative_path, "a") as file: #appends new content at the end without modifying the existing data with open(config_relative_path, "a") as file: #appends new content at the end without modifying the existing data
vm_input="test" vm_input="test"
while (vm_input != ""): while (vm_input != ""):
vm_input = input(">> ") vm_input = input(">> ")
file.write(vm_input + '\n') file.write(vm_input + '\n')
print("UUIDs has been written in config") print("VM UUIDs has been written in config.")
print("\nConfiguration completed!") print("\nConfiguration completed!")
input("Press ENTER to continue..")
print("\033[H\033[2J", end="")

58
disk_edit_mode.py Normal file
View File

@@ -0,0 +1,58 @@
import os
import requests
from domain_api import *
def disk_edit_mode(base_url , api_key , data_pool_uuid , vm_uuids):
print("\033[H\033[2J", end="")
print("### DISK EDIT MODE ###\n")
print("1) Delete vDisk by UUID")
print("2) Delete ALL vDisks on selected Virtual Machine")
print("3) Create Disk")
print("4) Prepare VMs for Courses™")
print("\nENTER - return to Utility Main Menu ")
sub_choice=str(input("\n>>> "))
if sub_choice == "1":
read_input=input("Input vDisk uuid to delete: ")
vdisk_uuid=str(read_input)
delete_disk(base_url , api_key , vdisk_uuid)
if sub_choice == "2":
print(vm_uuids)
select_uuids=int(input("Select VM to delete disks from. \n Type VM uuid index number (from list above) to select: ")) - 1
vm_check_power(base_url , api_key , vm_uuids[select_uuids]) #power on check
domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids[select_uuids])
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
for x in disk_uuids:
delete_disk(base_url , api_key , x)
print("All attached vDisks has been deleted!")
if sub_choice == "3":
vdisk_size=str(input("Enter disk size (GB): "))
print(vm_uuids)
select_uuids=int(input("Select VM to attach new disk. \n Type VM uuid index number (from list above) to select: ")) - 1
print(f"{vm_uuids[select_uuids]} - {data_pool_uuid} - {vdisk_size} ")
create_and_attach_disk(base_url , api_key , vm_uuids[select_uuids] , data_pool_uuid , vdisk_size , "falloc")
if sub_choice == "4":
print("#" * 5 , "Preparing VMs for Courses" , "#" * 5)
for y in vm_uuids: #power-on check
domain_uuid = y.strip('\n')
vm_check_power(base_url , api_key , domain_uuid)
for x in vm_uuids: # only for removing disks
domain_uuid = x.strip('\n')
domain_info = get_domain_info(base_url , api_key , domain_uuid)
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
vm_info(base_url , api_key , domain_uuid)
if domain_info:
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
for y in disk_uuids:
delete_disk(base_url , api_key , y)
print("All attached vDisks has been deleted!")
for z in vm_uuids: # only for creating disks
domain_uuid = z.strip('\n')
domain_info = get_domain_info(base_url , api_key , domain_uuid)
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
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, 20, "falloc")
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")

View File

@@ -147,7 +147,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) response = requests.post(url , headers=headers, json=payload)
if response.status_code == 200: if response.status_code == 200:
print(f"\nvDisk {disk_name} - {vdisk_size}GB has been created") print(f"vDisk {disk_name} - {vdisk_size}GB has been created and attached to VM - {vm_id}")
return True return True
else: else:
print(f"ERROR creating vDisk :\n {response.status_code} - {response.text}") print(f"ERROR creating vDisk :\n {response.status_code} - {response.text}")

64
main.py
View File

@@ -3,6 +3,7 @@ from config_data_import import *
from cluster_api import * 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 *
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
@@ -18,74 +19,19 @@ vm_uuids = import_vm_uuid(config_relative_path)
menu_choice=0 menu_choice=0
while(menu_choice != ""): #main menu loop while(menu_choice != ""): #main menu loop
print("\nUitility Main Menu:") print("\n*** Uitility Main Menu: ***\n")
print("1) Manage utility config") print("1) Manage utility config")
print("2) Enter disk edit mode") print("2) Enter disk edit mode")
print("3) Show breif cluster overview") print("3) Show breif cluster overview")
print("4) Show VM info") print("4) Show VM info")
print("5) Show data pools") print("5) Show data pools")
print("6) Show VMs Name / UUID") 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":
print("\033[H\033[2J", end="") # clears cmd screen, but saves scrollback buffer config_menu(config_relative_path)
print("1) Show current configuration \n2) Change configuraion")
sub_choice=str(input("\n>>> "))
if sub_choice == "1":
print("Current configuration:\n")
with open(config_relative_path, "r") as f:
print(f.read())
if sub_choice == "2":
config_edit(config_relative_path)
if menu_choice == "2": if menu_choice == "2":
print("\033[H\033[2J", end="") disk_edit_mode(base_url , api_key , data_pool_uuid , vm_uuids)
print("Select option: \n 1) Delete vDisk by UUID \n 2) Delete ALL vDisks on selected Virtual Machine \n 3) Create Disk \n 4) Prepare VMs for Courses™")
sub_choice=str(input("\n>>> "))
if sub_choice == "1":
read_input=input("Input vDisk uuid to delete: ")
vdisk_uuid=str(read_input)
delete_disk(base_url , api_key , vdisk_uuid)
if sub_choice == "2":
print(vm_uuids)
select_uuids=int(input("Select VM to delete disks from. \n Type VM uuid index number (from list above) to select: ")) - 1
vm_check_power(vm_uuids[select_uuids]) #power on check
domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids[select_uuids])
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
for x in disk_uuids:
delete_disk(base_url , api_key , x)
print("All attached vDisks has been deleted!")
if sub_choice == "3":
vdisk_size=str(input("Enter disk size (GB): "))
print(vm_uuids)
select_uuids=int(input("Select VM to attach new disk. \n Type VM uuid index number (from list above) to select: ")) - 1
create_and_attach_disk(vm_uuids[select_uuids] , data_pool_uuid, vdisk_size, "falloc")
if sub_choice == "4":
print("#" * 5 , "Preparing VMs for Courses" , "#" * 5)
for y in vm_uuids: #power-on check
domain_uuid = y.strip('\n')
vm_check_power(base_url , api_key , domain_uuid)
for x in vm_uuids: # only for removing disks
domain_uuid = x.strip('\n')
domain_info = get_domain_info(base_url , api_key , domain_uuid)
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
vm_info(base_url , api_key , domain_uuid)
if domain_info:
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
for y in disk_uuids:
delete_disk(base_url , api_key , y)
print("All attached vDisks has been deleted!")
for z in vm_uuids: # only for creating disks
domain_uuid = z.strip('\n')
domain_info = get_domain_info(base_url , api_key , domain_uuid)
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
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, 20, "falloc")
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
if menu_choice == "3": if menu_choice == "3":
cluster_info(base_url , api_key) cluster_info(base_url , api_key)
if menu_choice == "4": if menu_choice == "4":