- Introducing power-on-check function.

All disk-related operations are available ONLY if VM is powered off.
This commit is contained in:
OVERLORD7F
2025-05-16 11:09:27 +03:00
parent 6b56b75189
commit b6fb2b14e6
3 changed files with 24 additions and 13 deletions

View File

@@ -7,15 +7,13 @@ def data_pools(base_url , api_key): #output data pool info
if response.status_code == 200: if response.status_code == 200:
data_pools = response.json() data_pools = response.json()
results_data_pools_info = data_pools['results'] results_data_pools_info = data_pools['results']
print("\nData pools overview:") print("\nData pools overview:")
print(f"\nData pools total: {data_pools['count']}") print(f"\nData pools total: {data_pools['count']}")
print("-" * 44) print("-" * 44)
for x in results_data_pools_info: for x in results_data_pools_info:
print(f"\nData pool: {x['verbose_name']} ({x['status']})") print(f"\nData pool: {x['verbose_name']} ({x['status']})")
print(f"type: {x['type']} | Used: {round((x['free_space']/1024), 1)} Gb/{round((x['size'] / 1024), 1)} Gb") print(f"Type: {x['type']} | Used: {round((x['free_space']/1024), 1)} Gb / {round((x['size'] / 1024), 1)} Gb")
print(f"UID: {x['id']}") print(f"UUID: {x['id']}")
print("-" * 44) print("-" * 44)
else: else:
print(f"Failed to retrieve data {response.status_code} ") print(f"Failed to retrieve data {response.status_code} ")

View File

@@ -3,7 +3,6 @@
import requests import requests
import secrets #for generating unique names import secrets #for generating unique names
#from main import power_state
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
@@ -153,3 +152,16 @@ def create_and_attach_disk(base_url , api_key , vm_id, data_pool_uuid, vdisk_siz
else: else:
print(f"ERROR creating vDisk :\n {response.status_code} - {response.text}") print(f"ERROR creating vDisk :\n {response.status_code} - {response.text}")
return False return False
#checks for power on.
def vm_check_power(base_url , api_key , vm_uuids):
domain_info = get_domain_info(base_url , api_key , vm_uuids)
if domain_info:
#3 - on; 2 - suspend; 1 - off; 0 - unknown
if domain_info['user_power_state'] == 3 or domain_info['user_power_state'] == 2 : #if ON or SUSPEND
raise Exception(f"VM - {vm_uuids} IS POWERED ON! \n Turn it off and relaunch Utility.")
if domain_info['user_power_state'] == 0:
raise Exception(f"VM - {vm_uuids} is UNAVAILABLE! \n Have fun figuring that out D:")
if domain_info['user_power_state'] == 1:
print(f"VM - {vm_uuids} Power check passed!")

11
main.py
View File

@@ -4,11 +4,8 @@ from cluster_api import *
from domain_api import * from domain_api import *
from data_pools_api import * from data_pools_api import *
#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
config_relative_path = os.path.join(os.getcwd() , 'config.txt') if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if config exists and not empty
print(config_relative_path)
#check if config exists and not empty
if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0:
pass #do nothing pass #do nothing
else: else:
print("Config file was not found or empty.. ") print("Config file was not found or empty.. ")
@@ -53,6 +50,7 @@ while(menu_choice != ""): #main menu loop
if sub_choice == "2": if sub_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
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]) 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) disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
for x in disk_uuids: for x in disk_uuids:
@@ -66,6 +64,9 @@ while(menu_choice != ""): #main menu loop
if sub_choice == "4": if sub_choice == "4":
print("#" * 5 , "Preparing VMs for Courses" , "#" * 5) 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 for x in vm_uuids: # only for removing disks
domain_uuid = x.strip('\n') domain_uuid = x.strip('\n')
domain_info = get_domain_info(base_url , api_key , domain_uuid) domain_info = get_domain_info(base_url , api_key , domain_uuid)