Fixed issue #2 , Expanded config_edit function

This commit is contained in:
OVERLORD
2025-05-14 10:53:34 +03:00
committed by GitHub
parent 013d352bde
commit 22f3d3fe64

272
main.py
View File

@@ -1,130 +1,142 @@
import sys import sys
import os import os
from cluster_api import * from cluster_api import *
from domain_api import * from domain_api import *
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
#config.txt in the same directory with main.py #config.txt in the same directory with main.py
base_dir = os.getcwd() # Use the current directory as fallback base_dir = os.getcwd() # Use the current directory as fallback
config_relative_path = os.path.join(base_dir, 'config.txt') config_relative_path = os.path.join(base_dir, 'config.txt')
#config_relative_path = "Y:\\py\\SpaceVM_VM_Utility\\config.txt" #config_relative_path = "Y:\\py\\SpaceVM_VM_Utility\\config.txt"
def config_edit(): def config_edit():
read_input=input("Create new config file? (Y / N): ") read_input=input("Create new config file? (Y / N): ")
menu_choice=str(read_input) menu_choice=str(read_input)
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-UUID (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")
if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if file exists and not empty print("\nConfiguration completed!")
#importing API-KEY / IP / DATA POOL UUID from config if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if file exists and not empty
with open(config_relative_path, "r") as f: # using '\' (instead of '\\') throws syntax warning pass #do nothing
all_lines = f.readlines() else:
base_url = all_lines[0].strip('\n') print("Config file was not found or empty.. ")
api_key = "jwt " + all_lines[1].strip('\n') #actual format for api_key. That was realy obvious DACOM >:C config_edit()
data_pool_uuid = all_lines[2].strip('\n')
else: #importing API-KEY / IP / DATA POOL UUID from config
print("Config file was not found or empty.. ") with open(config_relative_path, "r") as f:
config_edit() all_lines = f.readlines()
base_url = all_lines[0].strip('\n')
#importing VM-UUIDs api_key = "jwt " + all_lines[1].strip('\n') #actual format for api_key. That was realy obvious DACOM >:C
vm_uuids = [] data_pool_uuid = all_lines[2].strip('\n')
with open(config_relative_path, "r") as f:
for i in range(3): # ignoring 2 first lines (IP, API-KEY) #importing VM-UUIDs
next(f) vm_uuids = []
for line in f: with open(config_relative_path, "r") as f:
line = line.strip('\n') for i in range(3): # ignoring 2 first lines (IP, API-KEY)
if line: # checks if line is empty (EOF). ESSENTIAL, DO NOT REMOVE next(f)
vm_uuids.append(line) for line in f:
line = line.strip('\n')
if line: # checks if line is empty (EOF). ESSENTIAL, DO NOT REMOVE
#so-called INT MAIN vm_uuids.append(line)
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>>> ") #so-called INT MAIN
menu_choice=str(read_input) menu_choice=0
while(menu_choice != ""): #main menu loop
if menu_choice == "1": read_input=input("\nUitility Main Menu: \n1) Manage utility config \n2) Enter disk edit mode \n3) Show breif cluster overview \n4) Show VM info \n>>> ")
config_edit() menu_choice=str(read_input)
if menu_choice == "2":
print("\033[H\033[2J", end="") # clears cmd screen, but saves scrollback buffer if menu_choice == "1":
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™") print("\033[H\033[2J", end="") # clears cmd screen, but saves scrollback buffer
read_input=input(">> ") print("1) Show current configuration \n2) Change configuraion")
menu_choice=int(read_input) read_input=input(">> ")
if menu_choice == 1: menu_choice=int(read_input)
read_input=input("Input vDisk uuid to delete: ") if menu_choice == 1:
vdisk_uuid=str(read_input) print("Current configuration:\n")
delete_disk(base_url , api_key , vdisk_uuid) with open(config_relative_path, "r") as f:
if menu_choice == 2: print(f.read())
print(vm_uuids) if menu_choice == 2:
select_uuids=int(input("Select VM to delete disks from. \n Type VM uuid index number (from list above) to select: ")) - 1 config_edit()
print(f"actual selected uuid = {select_uuids}") if menu_choice == "2":
print(vm_uuids[select_uuids]) print("\033[H\033[2J", end="")
domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids[select_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™")
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content) read_input=input(">> ")
for x in disk_uuids: menu_choice=int(read_input)
delete_disk(base_url , api_key , x) if menu_choice == 1:
print("All attached vDisks has been deleted!") read_input=input("Input vDisk uuid to delete: ")
if menu_choice == 3: vdisk_uuid=str(read_input)
vdisk_size=str(input("Enter disk size (GB): ")) delete_disk(base_url , api_key , vdisk_uuid)
print(vm_uuids) if menu_choice == 2:
select_uuids=int(input("Select VM to attach new disk. \n Type VM uuid index number (from list above) to select: ")) - 1 print(vm_uuids)
print(f"actual selected uuid = {select_uuids}") select_uuids=int(input("Select VM to delete disks from. \n Type VM uuid index number (from list above) to select: ")) - 1
print(vm_uuids[select_uuids]) print(f"actual selected uuid = {select_uuids}")
create_and_attach_disk(vm_uuids[select_uuids] , data_pool_uuid, vdisk_size, "falloc") print(vm_uuids[select_uuids])
if menu_choice == 4: domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids[select_uuids])
print("#" * 5 , "Preparing VMs for Courses" , "#" * 5) disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
for x in vm_uuids: # only for removing disks for x in disk_uuids:
domain_uuid = x.strip('\n') delete_disk(base_url , api_key , x)
domain_info = get_domain_info(base_url , api_key , domain_uuid) print("All attached vDisks has been deleted!")
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid) if menu_choice == 3:
if domain_info: vdisk_size=str(input("Enter disk size (GB): "))
print("=" * 14 , "Virtual Machine Info" , "=" * 15) print(vm_uuids)
print(f"\t VM: {domain_info['verbose_name']}") select_uuids=int(input("Select VM to attach new disk. \n Type VM uuid index number (from list above) to select: ")) - 1
print(f"\t Power State: {power_state[domain_info['user_power_state']]}") #translating status code to "pretty name" print(f"actual selected uuid = {select_uuids}")
print(f"\t vDisks: {domain_info['vdisks_count']}") print(vm_uuids[select_uuids])
print("-" * 19 , "vDisks Info" , "-" * 19) create_and_attach_disk(vm_uuids[select_uuids] , data_pool_uuid, vdisk_size, "falloc")
get_disk_info(domain_all_content) if menu_choice == 4:
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content) print("#" * 5 , "Preparing VMs for Courses" , "#" * 5)
for y in disk_uuids: for x in vm_uuids: # only for removing disks
delete_disk(base_url , api_key , y) domain_uuid = x.strip('\n')
print("All attached vDisks has been deleted!") domain_info = get_domain_info(base_url , api_key , domain_uuid)
for z in vm_uuids: # only for creating disks domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
domain_uuid = z.strip('\n') if domain_info:
domain_info = get_domain_info(base_url , api_key , domain_uuid) print("=" * 14 , "Virtual Machine Info" , "=" * 15)
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid) print(f"\t VM: {domain_info['verbose_name']}")
if domain_info: print(f"\t Power State: {power_state[domain_info['user_power_state']]}") #translating status code to "pretty name"
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 10, "falloc") print(f"\t vDisks: {domain_info['vdisks_count']}")
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc") print("-" * 19 , "vDisks Info" , "-" * 19)
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc") get_disk_info(domain_all_content)
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
if menu_choice == "3": for y in disk_uuids:
cluster_info(base_url , api_key) delete_disk(base_url , api_key , y)
print("All attached vDisks has been deleted!")
if menu_choice == "4": for z in vm_uuids: # only for creating disks
print("\033[H\033[2J", end="") domain_uuid = z.strip('\n')
for x in vm_uuids: domain_info = get_domain_info(base_url , api_key , domain_uuid)
vm_info(base_url , api_key , x) 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")
print("Exiting Utility..") create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
sys.exit() create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
if menu_choice == "3":
cluster_info(base_url , api_key)
if menu_choice == "4":
print("\033[H\033[2J", end="")
for x in vm_uuids:
vm_info(base_url , api_key , x)
print("Exiting Utility..")
sys.exit()