diff --git a/pyth-api1.py b/pyth-api1.py index 4a85ea0..a88b6f4 100644 --- a/pyth-api1.py +++ b/pyth-api1.py @@ -4,38 +4,77 @@ import copy import sys import secrets #for generating unique names -power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown -config_relative_path = "C:\\Users\\n.danilov\\config.txt" # absolute path to cluster config file -VM_UUID_relative_path = "C:\\scripts\\VM-UUIDs.txt" # absolute path to cluster config file -# edit config or use old -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 ip address: ") - api_key = input("Type integration key: ") - lines = [base_url, api_key] - with open(config_relative_path, "w+") as file: - for line in lines: - file.write(line + '\n') +power_state = ["Unknown" , "Off" , "Suspend" , "On"] #3 - on; 2 - suspend; 1 - off; 0 - unknown +config_relative_path = "Y:\\py\\config.txt" # absolute path to cluster config file +VM_UUID_relative_path = "Y:\\py\\VM-UUIDs.txt" + + + #importing API-KEY and ip address from config file 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') #importing ONLY 1st entry in VM-UUIDs file with open(VM_UUID_relative_path, "r") as f: - vm_uuids = f.readline().strip('\n') + vm_uuids = f.readline().strip('\n') #importing data_pool_uuid from file #selected data pool will be used for new disks and alike -with open("C:\\scripts\\data-pool.txt", "r") as f: +with open("Y:\\py\\data-pool.txt", "r") as f: data_pool_uuid = f.readline() +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 ip address: ") + api_key = input("Type integration key: ") + lines = [base_url, api_key] + with open(config_relative_path, "w+") as file: + 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 + vm_input="test" + while (vm_input != ""): + vm_input = input(">> ") + file.write(vm_input + '\n') + + + +def cluster_info(): #output short clusters overview + url= f"http://{base_url}/api/clusters" + response = requests.get(url , headers={'Authorization' : api_key}) + + if response.status_code == 200: + cluster_info = response.json() + results_cluster_info = cluster_info['results'] + + print("\nShort clusters overview:") + print(f"\nClusters total: {cluster_info['count']}") + print("-" * 51) + for x in results_cluster_info: + + print(f"\nCluster Name: {x['verbose_name']} ({x['status']})") + print(f"Nodes: {x['nodes_count']}") + print(f"Total CPU: {x['cpu_count']} Cores || CPU Usage: {round(x['cpu_used_percent_user'] , 2)}%")#output is rounded by 2 + print(f"Total RAM: {int(x['memory_count']/1024)}GB || RAM Usage: {round(x['mem_used_percent_user'] , 2)}%") #RAM pretty output = mb-to-gb + set 'int' to remove .0 + + print("-" * 51) + + else: + print(f"Failed to retrieve data {response.status_code}") + + + #get domain info "http://10.2.1.52/api/domains/uuid OR /domains/{id}/all-content/" def get_domain_info(domain_uuid): url= f"http://{base_url}/api/domains/{domain_uuid}" @@ -150,77 +189,91 @@ def create_and_attach_disk(vm_id, data_pool_uuid, vdisk_size, preallocation): return False +def vm_info (vm_uuids): + domain_uuid = vm_uuids + #print(domain_uuid) + domain_info = get_domain_info(domain_uuid) + domain_all_content = get_domain_all_content(domain_uuid) + + 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) + #so-called INT MAIN +menu_choice=0 +while(menu_choice != ""): + 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) -domain_uuid = vm_uuids -print(domain_uuid) -print((domain_uuid)) -domain_info = get_domain_info(domain_uuid) -domain_all_content = get_domain_all_content(domain_uuid) - - -#print(domain_info) -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) - - -read_input=input("Enter disk edit mode Y / N: ") -menu_choice=str(read_input) -if menu_choice == "Y" or menu_choice == "y": - 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(vdisk_uuid) - if menu_choice == 2: - disk_uuids = get_disk_uuids(domain_all_content) - for x in disk_uuids: - delete_disk(x) - print("All attached vDisks has been deleted!") - if menu_choice == 3: - read_input=input("Enter disk size (GB): ") - menu_choice=str(read_input) - create_and_attach_disk(vm_uuids , data_pool_uuid, menu_choice, "falloc") - if menu_choice == 4: - print("#" * 5 , "Preparing VMs for Courses" , "#" * 5) - with open(VM_UUID_relative_path, "r") as f: - for x in f: # only for removing disks + 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(vdisk_uuid) + if menu_choice == 2: + disk_uuids = get_disk_uuids(domain_all_content) + for x in disk_uuids: + delete_disk(x) + print("All attached vDisks has been deleted!") + if menu_choice == 3: + read_input=input("Enter disk size (GB): ") + menu_choice=str(read_input) + create_and_attach_disk(vm_uuids , data_pool_uuid, menu_choice, "falloc") + if menu_choice == 4: + print("#" * 5 , "Preparing VMs for Courses" , "#" * 5) + with open(VM_UUID_relative_path, "r") as f: + for x in f: # only for removing disks - domain_uuid = x.strip('\n') - domain_info = get_domain_info(domain_uuid) - domain_all_content = get_domain_all_content(domain_uuid) + domain_uuid = x.strip('\n') + domain_info = get_domain_info(domain_uuid) + domain_all_content = get_domain_all_content(domain_uuid) - 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(domain_all_content) - for y in disk_uuids: - delete_disk(y) - print("All attached vDisks has been deleted!") - with open(VM_UUID_relative_path, "r") as f: - for z in f: # only for creating disks - domain_uuid = z.strip('\n') - domain_info = get_domain_info(domain_uuid) - domain_all_content = get_domain_all_content(domain_uuid) + 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(domain_all_content) + for y in disk_uuids: + delete_disk(y) + print("All attached vDisks has been deleted!") + with open(VM_UUID_relative_path, "r") as f: + for z in f: # only for creating disks + domain_uuid = z.strip('\n') + domain_info = get_domain_info(domain_uuid) + domain_all_content = get_domain_all_content(domain_uuid) - if domain_info: - create_and_attach_disk(domain_uuid , data_pool_uuid, 10, "falloc") - create_and_attach_disk(domain_uuid , data_pool_uuid, 20, "falloc") - create_and_attach_disk(domain_uuid , data_pool_uuid, 20, "falloc") + if domain_info: + create_and_attach_disk(domain_uuid , data_pool_uuid, 10, "falloc") + create_and_attach_disk(domain_uuid , data_pool_uuid, 20, "falloc") + create_and_attach_disk(domain_uuid , data_pool_uuid, 20, "falloc") - + if menu_choice == "3": + cluster_info() + + if menu_choice == "4": + with open(config_relative_path , 'r') as f: + for i in range(2): # ignoring 2 first lines (IP, API-KEY) + next(f) + for line in f: + #print(line.strip('\n')) + vm_info(line.strip()) # strip with no args - removes '\n' and spaces + + + # HOW - TO EOF ????? + @@ -229,3 +282,7 @@ if menu_choice == "Y" or menu_choice == "y": print("Exiting Utility..") sys.exit() + + + +