From f3734432a72e459728af7e43f1563010ff6ed730 Mon Sep 17 00:00:00 2001 From: OVERLORD7F Date: Fri, 30 May 2025 12:12:33 +0300 Subject: [PATCH] =?UTF-8?q?Added=20option=20to=20write=20selected=20VMs=20?= =?UTF-8?q?by=20tag=20(#10)=20to=20=D1=81onfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config_data_import.py | 5 ++--- domain_api.py | 31 ++++++++++++++++++++++++------- main.py | 4 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/config_data_import.py b/config_data_import.py index 7990106..70f4bf7 100644 --- a/config_data_import.py +++ b/config_data_import.py @@ -119,14 +119,13 @@ def config_import(config_relative_path): vm_names.append(get_vm_name(base_url, api_key, x)) return base_url, api_key, data_pool_uuid, data_pool_name, vm_list, vm_names, disk1_size, disk2_size, disk3_size -def change_data_pool(base_url, api_key, config_relative_path): +def change_data_pool(base_url, api_key, config_relative_path): #change selected data pool in config cls() show_data_pools(base_url, api_key) new_data_pool_uuid = input("Type NEW Data Pool UUID: ") config = configparser.ConfigParser() config.read(config_relative_path) if config.has_section('Data_Pool'): - # update data_pool_uuid config.set('Data_Pool', 'data_pool_uuid', new_data_pool_uuid) with open(config_relative_path, 'w') as config_file: config.write(config_file) @@ -135,7 +134,7 @@ def change_data_pool(base_url, api_key, config_relative_path): config_show(config_relative_path) -def change_vm_uuids(config_relative_path): +def change_vm_uuids(config_relative_path): #change selected VM uuids in config config = configparser.ConfigParser() config.read(config_relative_path) # Remove old VM_List section if it exists, then add a fresh one diff --git a/domain_api.py b/domain_api.py index 764edb6..cca2d3a 100644 --- a/domain_api.py +++ b/domain_api.py @@ -2,11 +2,12 @@ import requests import secrets #for generating unique names import os +import configparser from config_data_import import * from rich.console import Console , Align from rich.columns import Columns from rich.panel import Panel -from rich.prompt import Prompt +from rich.prompt import Prompt, Confirm from rich.progress import Progress, SpinnerColumn, TextColumn console = Console() #necessary for pretty menus & output @@ -195,11 +196,11 @@ def vm_check_power(base_url , api_key , vm_uuids): if domain_info['user_power_state'] == 1: pass -def vm_tags(base_url, api_key): +def select_vm_by_tags(base_url, api_key, config_relative_path): url = f"http://{base_url}/api/domains/" response = requests.get(url, headers={'Authorization': api_key}) if response.status_code == 200: - verbose_name_input = input("Write tag:") + verbose_name_input = input("Specify tag: ") output_renderables = [] vm_info_short = response.json() y= vm_info_short @@ -214,16 +215,32 @@ def vm_tags(base_url, api_key): console.print(Columns(output_renderables)) #print renderables by columns else: print(f"Failed to retrieve data {response.status_code}") - console.rule(style="grey53") + console.rule(style="grey53") + + if vm_id_list: # promt to write found VM UUIDs to config + write_to_config = Confirm.ask("[bold yellow]Write these VM UUIDs to config file?") + if write_to_config: + config = configparser.ConfigParser() + config.read(config_relative_path) + # Remove old VM_List section if it exists, then add a fresh one + if config.has_section('VM_List'): + config.remove_section('VM_List') + config.add_section('VM_List') + for idx, vm_id in enumerate(vm_id_list, 1): + config.set('VM_List', f'uuid_{idx}', vm_id) + with open(config_relative_path, 'w') as configfile: + config.write(configfile) + console.print(f"[green bold]VM UUIDs have been written in config :pencil:") + Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.... :right_arrow_curving_down:") os.system('cls' if os.name=='nt' else 'clear') return(vm_id_list) -def vm_menu(base_url, api_key, vm_uuids): +def vm_menu(base_url, api_key, vm_uuids, config_relative_path): os.system('cls' if os.name=='nt' else 'clear') config_menu_options="[gold bold][1] [grey53 italic]Show VM info \n (for selected VMs in config)[/grey53 italic]\n \ \n[gold bold][2] [grey53 italic]Show VMs Name / UUID[/grey53 italic]\n \ -\n[gold bold][3] [grey53 italic]Show VMs by tags / UUID[/grey53 italic]\n \ +\n[gold bold][3] [grey53 italic]Select VMs by tag / UUID[/grey53 italic]\n \ \n\n[green_yellow bold]ENTER - return to Main Menu[/]" config_menu_options=Align.center(config_menu_options, vertical="middle") console = Console() @@ -239,4 +256,4 @@ def vm_menu(base_url, api_key, vm_uuids): vm_info_short(base_url , api_key) if sub_choice == "3": os.system('cls' if os.name=='nt' else 'clear') - vm_tags(base_url , api_key) \ No newline at end of file + select_vm_by_tags(base_url , api_key, config_relative_path) \ No newline at end of file diff --git a/main.py b/main.py index b714635..478a0b6 100644 --- a/main.py +++ b/main.py @@ -22,7 +22,7 @@ while(menu_choice != ""): #main menu loop menu_options=f"[gold bold][1] [grey53 italic]Manage utility config\n[/grey53 italic] \ \n[gold bold][2] [grey53 italic]Enter disk edit mode[/grey53 italic]\n \ \n[gold bold][3] [grey53 italic]Show breif cluster overview[/grey53 italic]\n \ -\n[gold bold][4] [grey53 italic]Show VM info[/grey53 italic]\n \ +\n[gold bold][4] [grey53 italic]Enter VM menu[/grey53 italic]\n \ \n[gold bold][5] [grey53 italic]Show data pools[/grey53 italic]\n \ \n\n[green_yellow bold]ENTER - exit Utility[/]\n\n \ [underline bold grey53]Currently imported config:[/]\n \ @@ -38,7 +38,7 @@ while(menu_choice != ""): #main menu loop if menu_choice == "3": cluster_info(base_url , api_key) if menu_choice == "4": - vm_menu(base_url , api_key, vm_uuids) + vm_menu(base_url , api_key, vm_uuids, config_relative_path) if menu_choice == "5": show_data_pools(base_url , api_key) os.system('cls' if os.name=='nt' else 'clear') #clears screen before looping back to main menu