This commit is contained in:
OVERLORD7F
2025-05-15 11:51:06 +03:00
4 changed files with 201 additions and 144 deletions

26
data_pools_api.py Normal file
View File

@@ -0,0 +1,26 @@
#from main import base_url , api_key , requests
import requests
def data_pools(base_url , api_key): #output data pool info
url= f"http://{base_url}//api/data-pools/"
response = requests.get(url , headers={'Authorization' : api_key})
if response.status_code == 200:
data_pools = response.json()
results_data_pools_info = data_pools['results']
print("\nData pools overview:")
print(f"\nData pools total: {data_pools['count']}")
print("-" * 44)
for x in results_data_pools_info:
print(" "*14,"Data pool info")
print(f"\nName: {x['verbose_name']}")
print(f"UID: {x['id']}")
print(f"type: {x['type']}")
print(f"status: {x['status']}")
print(f"size: {round((x['size'] / 1024), 1)}Gb")
print("-" * 44)
else:
print(f"Failed to retrieve data {response.status_code}")

View File

@@ -111,7 +111,6 @@ def vm_info(base_url , api_key , vm_uuids):
get_disk_info(domain_all_content) get_disk_info(domain_all_content)
def create_and_attach_disk(base_url , api_key , vm_id, data_pool_uuid, vdisk_size, preallocation): 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) domain_name=get_domain_info(base_url , api_key , vm_id)
disk_name=domain_name["verbose_name"]+"_"+secrets.token_hex(5) #generates unique hex id. this method can generate ~million unique ids disk_name=domain_name["verbose_name"]+"_"+secrets.token_hex(5) #generates unique hex id. this method can generate ~million unique ids

12
main.py
View File

@@ -5,6 +5,8 @@ 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 vm_info_short 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
@@ -12,7 +14,6 @@ power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - o
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"
def config_edit(): def config_edit():
read_input=input("Create new config file? (Y / N): ") read_input=input("Create new config file? (Y / N): ")
@@ -82,10 +83,12 @@ while(menu_choice != ""): #main menu loop
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("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(">> ") read_input=input(">> ")
menu_choice=int(read_input) menu_choice=int(read_input)
if menu_choice == 1: if menu_choice == 1:
read_input=input("Input vDisk uuid to delete: ") read_input=input("Input vDisk uuid to delete: ")
vdisk_uuid=str(read_input) vdisk_uuid=str(read_input)
delete_disk(base_url , api_key , vdisk_uuid) delete_disk(base_url , api_key , vdisk_uuid)
if menu_choice == 2: if menu_choice == 2:
print(vm_uuids) 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 select_uuids=int(input("Select VM to delete disks from. \n Type VM uuid index number (from list above) to select: ")) - 1
@@ -94,11 +97,13 @@ while(menu_choice != ""): #main menu loop
for x in disk_uuids: for x in disk_uuids:
delete_disk(base_url , api_key , x) delete_disk(base_url , api_key , x)
print("All attached vDisks has been deleted!") print("All attached vDisks has been deleted!")
if menu_choice == 3: if menu_choice == 3:
vdisk_size=str(input("Enter disk size (GB): ")) vdisk_size=str(input("Enter disk size (GB): "))
print(vm_uuids) 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 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") create_and_attach_disk(vm_uuids[select_uuids] , data_pool_uuid, vdisk_size, "falloc")
if menu_choice == 4: if menu_choice == 4:
print("#" * 5 , "Preparing VMs for Courses" , "#" * 5) print("#" * 5 , "Preparing VMs for Courses" , "#" * 5)
for x in vm_uuids: # only for removing disks for x in vm_uuids: # only for removing disks
@@ -133,6 +138,11 @@ while(menu_choice != ""): #main menu loop
for x in vm_uuids: for x in vm_uuids:
vm_info(base_url , api_key , x) vm_info(base_url , api_key , x)
if menu_choice == "5":
data_pools(base_url , api_key)
if menu_choice == "6":
vm_info_short(base_url , api_key)
print("Exiting Utility..") print("Exiting Utility..")
sys.exit() sys.exit()

22
vm_info_short.py Normal file
View File

@@ -0,0 +1,22 @@
#from main import base_url , api_key , requests
import requests
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})
if response.status_code == 200:
vm_info_short = response.json()
results_vm_info_short = vm_info_short['results']
print("\nShort VM overview")
print(f"\nVM total: {vm_info_short['count']}")
print("-" * 41)
for x in results_vm_info_short:
print(" "*16,f"VM {x['verbose_name']}")
print(f"UID: {x['id']}")
print("-" * 41)
else:
print(f"Failed to retrieve data {response.status_code}")