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

296
main.py
View File

@@ -1,143 +1,153 @@
import sys import sys
import os import os
from config_data_import import * 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 *
power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown from vm_info_short import *
#config.txt in the same directory with main.py power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown
base_dir = os.getcwd() # Use the current directory as fallback
config_relative_path = os.path.join(base_dir, 'config.txt') #config.txt in the same directory with main.py
base_dir = os.getcwd() # Use the current directory as fallback
#config_relative_path = "Y:\\py\\SpaceVM_VM_Utility\\config.txt" config_relative_path = os.path.join(base_dir, 'config.txt')
def config_edit():
read_input=input("Create new config file? (Y / N): ") def config_edit():
menu_choice=str(read_input) read_input=input("Create new config file? (Y / N): ")
if menu_choice == "Y" or menu_choice == "y": menu_choice=str(read_input)
base_url = input("Type SpaceVM Controller IP: ") if menu_choice == "Y" or menu_choice == "y":
api_key = input("Type your API Key: ") base_url = input("Type SpaceVM Controller IP: ")
data_pool_uuid = input("Type Data pool uuid you wish to use: ") api_key = input("Type your API Key: ")
lines = [base_url, api_key, data_pool_uuid] data_pool_uuid = input("Type Data pool uuid you wish to use: ")
with open(config_relative_path, "w+") as file: lines = [base_url, api_key, data_pool_uuid]
for line in lines: with open(config_relative_path, "w+") as file:
file.write(line + '\n') for line in lines:
file.write(line + '\n')
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 print("Type VM-UUID (input ENTER to stop)")
vm_input="test" with open(config_relative_path, "a") as file: #appends new content at the end without modifying the existing data
while (vm_input != ""): vm_input="test"
vm_input = input(">> ") while (vm_input != ""):
file.write(vm_input + '\n') vm_input = input(">> ")
print("UUIDs has been written in config") file.write(vm_input + '\n')
print("\nConfiguration completed!") 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!")
pass #do nothing if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if file exists and not empty
else: pass #do nothing
print("Config file was not found or empty.. ") else:
config_edit() print("Config file was not found or empty.. ")
config_edit()
#importing API-KEY / IP / DATA POOL UUID from config
threelines = import_threelines(config_relative_path) #importing API-KEY / IP / DATA POOL UUID from config
print(f"3 lines: {threelines}") threelines = import_threelines(config_relative_path)
base_url=threelines[0] print(f"3 lines: {threelines}")
api_key=threelines[1] base_url=threelines[0]
data_pool_uuid=threelines[2] api_key=threelines[1]
data_pool_uuid=threelines[2]
'''
with open(config_relative_path, "r") as f: '''
all_lines = f.readlines() with open(config_relative_path, "r") as f:
base_url = all_lines[0].strip('\n') all_lines = f.readlines()
api_key = "jwt " + all_lines[1].strip('\n') #actual format for api_key. That was realy obvious DACOM >:C base_url = all_lines[0].strip('\n')
data_pool_uuid = all_lines[2].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')
'''
#importing VM-UUIDs
vm_uuids = import_vm_uuid(config_relative_path) #importing VM-UUIDs
print(f"vm uuids: {vm_uuids}") vm_uuids = import_vm_uuid(config_relative_path)
print(f"vm uuids: {vm_uuids}")
#so-called INT MAIN
menu_choice=0 #so-called INT MAIN
while(menu_choice != ""): #main menu loop menu_choice=0
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>>> ") while(menu_choice != ""): #main menu loop
menu_choice=str(read_input) 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>>> ")
menu_choice=str(read_input)
if menu_choice == "1":
print("\033[H\033[2J", end="") # clears cmd screen, but saves scrollback buffer if menu_choice == "1":
print("1) Show current configuration \n2) Change configuraion") 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)
print("Current configuration:\n") if menu_choice == 1:
with open(config_relative_path, "r") as f: print("Current configuration:\n")
print(f.read()) with open(config_relative_path, "r") as f:
if menu_choice == 2: print(f.read())
config_edit() if menu_choice == 2:
if menu_choice == "2": config_edit()
print("\033[H\033[2J", end="") if menu_choice == "2":
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="")
read_input=input(">> ") 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™")
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: ")
vdisk_uuid=str(read_input) if menu_choice == 1:
delete_disk(base_url , api_key , vdisk_uuid) read_input=input("Input vDisk uuid to delete: ")
if menu_choice == 2: vdisk_uuid=str(read_input)
print(vm_uuids) delete_disk(base_url , api_key , vdisk_uuid)
select_uuids=int(input("Select VM to delete disks from. \n Type VM uuid index number (from list above) to select: ")) - 1
domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids[select_uuids]) if menu_choice == 2:
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content) print(vm_uuids)
for x in disk_uuids: select_uuids=int(input("Select VM to delete disks from. \n Type VM uuid index number (from list above) to select: ")) - 1
delete_disk(base_url , api_key , x) domain_all_content = get_domain_all_content(base_url , api_key , vm_uuids[select_uuids])
print("All attached vDisks has been deleted!") disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
if menu_choice == 3: for x in disk_uuids:
vdisk_size=str(input("Enter disk size (GB): ")) delete_disk(base_url , api_key , x)
print(vm_uuids) print("All attached vDisks has been deleted!")
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 menu_choice == 3:
if menu_choice == 4: vdisk_size=str(input("Enter disk size (GB): "))
print("#" * 5 , "Preparing VMs for Courses" , "#" * 5) print(vm_uuids)
for x in vm_uuids: # only for removing disks select_uuids=int(input("Select VM to attach new disk. \n Type VM uuid index number (from list above) to select: ")) - 1
domain_uuid = x.strip('\n') create_and_attach_disk(vm_uuids[select_uuids] , data_pool_uuid, vdisk_size, "falloc")
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 menu_choice == 4:
if domain_info: print("#" * 5 , "Preparing VMs for Courses" , "#" * 5)
print("=" * 14 , "Virtual Machine Info" , "=" * 15) for x in vm_uuids: # only for removing disks
print(f"\t VM: {domain_info['verbose_name']}") domain_uuid = x.strip('\n')
print(f"\t Power State: {power_state[domain_info['user_power_state']]}") #translating status code to "pretty name" domain_info = get_domain_info(base_url , api_key , domain_uuid)
print(f"\t vDisks: {domain_info['vdisks_count']}") domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
print("-" * 19 , "vDisks Info" , "-" * 19) if domain_info:
get_disk_info(domain_all_content) print("=" * 14 , "Virtual Machine Info" , "=" * 15)
disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content) print(f"\t VM: {domain_info['verbose_name']}")
for y in disk_uuids: print(f"\t Power State: {power_state[domain_info['user_power_state']]}") #translating status code to "pretty name"
delete_disk(base_url , api_key , y) print(f"\t vDisks: {domain_info['vdisks_count']}")
print("All attached vDisks has been deleted!") print("-" * 19 , "vDisks Info" , "-" * 19)
for z in vm_uuids: # only for creating disks get_disk_info(domain_all_content)
domain_uuid = z.strip('\n') disk_uuids = get_disk_uuids(base_url , api_key , domain_all_content)
domain_info = get_domain_info(base_url , api_key , domain_uuid) for y in disk_uuids:
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid) delete_disk(base_url , api_key , y)
if domain_info: print("All attached vDisks has been deleted!")
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 10, "falloc") for z in vm_uuids: # only for creating disks
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc") domain_uuid = z.strip('\n')
create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc") 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 menu_choice == "3": if domain_info:
cluster_info(base_url , api_key) 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")
if menu_choice == "4": create_and_attach_disk(base_url , api_key , domain_uuid , data_pool_uuid, 20, "falloc")
print("\033[H\033[2J", end="")
for x in vm_uuids: if menu_choice == "3":
vm_info(base_url , api_key , x) cluster_info(base_url , api_key)
if menu_choice == "4":
print("Exiting Utility..") print("\033[H\033[2J", end="")
sys.exit() for x in vm_uuids:
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..")
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}")