Files
SVMU/main.py

138 lines
6.2 KiB
Python
Raw Normal View History

2025-05-07 16:20:36 +03:00
import sys
import os
from cluster_api import *
from domain_api import *
2025-05-13 17:41:42 +03:00
from data_pools_api import *
from vm_info_short import *
2025-05-07 16:20:36 +03:00
power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown
#config.txt in the same directory with main.py
base_dir = os.getcwd() # Use the current directory as fallback
config_relative_path = os.path.join(base_dir, 'config.txt')
2025-05-07 16:20:36 +03:00
#config_relative_path = "Y:\\py\\SpaceVM_VM_Utility\\config.txt"
2025-05-07 16:20:36 +03:00
def config_edit():
read_input=input("Create new config file? (Y / N): ")
menu_choice=str(read_input)
if menu_choice == "Y" or menu_choice == "y":
base_url = input("Type SpaceVM Controller IP: ")
api_key = input("Type your API Key: ")
data_pool_uuid = input("Type Data pool uuid you wish to use: ")
lines = [base_url, api_key, data_pool_uuid]
with open(config_relative_path, "w+") as file:
for line in lines:
file.write(line + '\n')
2025-05-07 16:20:36 +03:00
print("Type VM-UUID (input ENTER to stop)")
with open(config_relative_path, "a") as file: #appends new content at the end without modifying the existing data
vm_input="test"
while (vm_input != ""):
vm_input = input(">> ")
file.write(vm_input + '\n')
2025-05-07 16:20:36 +03:00
if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if file exists and not empty
#importing API-KEY / IP / DATA POOL UUID from config
with open(config_relative_path, "r") as f: # using '\' (instead of '\\') throws syntax warning
all_lines = f.readlines()
base_url = all_lines[0].strip('\n')
api_key = "jwt " + all_lines[1].strip('\n') #actual format for api_key. That was realy obvious DACOM >:C
data_pool_uuid = all_lines[2].strip('\n')
else:
print("Config file was not found or empty.. ")
config_edit()
2025-05-07 16:20:36 +03:00
#importing VM-UUIDs
vm_uuids = []
with open(config_relative_path, "r") as f:
for i in range(3): # ignoring 2 first lines (IP, API-KEY)
next(f)
for line in f:
line = line.strip('\n')
if line: # checks if line is empty (EOF). ESSENTIAL, DO NOT REMOVE
vm_uuids.append(line)
2025-05-07 16:20:36 +03:00
#so-called INT MAIN
menu_choice=0
while(menu_choice != ""): #main menu loop
read_input=input("\nUitility Main Menu: \n1) Edit config \n2) Enter disk edit mode \n3) Show breif cluster overview \n4) Show VM info \n>>> ")
menu_choice=str(read_input)
if menu_choice == "1":
config_edit()
if menu_choice == "2":
print("\033[H\033[2J", end="") # clears cmd screen, but saves scrollback buffer
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™")
read_input=input(">> ")
menu_choice=int(read_input)
if menu_choice == 1:
read_input=input("Input vDisk uuid to delete: ")
vdisk_uuid=str(read_input)
delete_disk(base_url , api_key , vdisk_uuid)
2025-05-07 16:20:36 +03:00
if menu_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
print(f"actual selected uuid = {select_uuids}")
print(vm_uuids[select_uuids])
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)
2025-05-07 16:20:36 +03:00
for x in disk_uuids:
delete_disk(base_url , api_key , x)
2025-05-07 16:20:36 +03:00
print("All attached vDisks has been deleted!")
if menu_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"actual selected uuid = {select_uuids}")
print(vm_uuids[select_uuids])
create_and_attach_disk(vm_uuids[select_uuids] , data_pool_uuid, vdisk_size, "falloc")
if menu_choice == 4:
print("#" * 5 , "Preparing VMs for Courses" , "#" * 5)
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)
2025-05-07 16:20:36 +03:00
if domain_info:
print("=" * 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)
get_disk_info(domain_all_content)
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
2025-05-07 16:20:36 +03:00
for y in disk_uuids:
delete_disk(base_url , api_key , y)
2025-05-07 16:20:36 +03:00
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)
2025-05-07 16:20:36 +03:00
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")
2025-05-07 16:20:36 +03:00
if menu_choice == "3":
cluster_info(base_url , api_key)
2025-05-07 16:20:36 +03:00
if menu_choice == "4":
print("\033[H\033[2J", end="")
for x in vm_uuids:
vm_info(base_url , api_key , x)
2025-05-07 16:20:36 +03:00
2025-05-13 17:41:42 +03:00
if menu_choice == "5":
data_pools(base_url , api_key)
if menu_choice == "6":
vm_info_short(base_url , api_key)
2025-05-07 16:20:36 +03:00
print("Exiting Utility..")
sys.exit()