- Finally, proper config file is here!

- Small changes in menus
This commit is contained in:
OVERLORD7F
2025-05-26 17:52:12 +03:00
parent 96c6e29c00
commit 2ccde25b6e
5 changed files with 87 additions and 52 deletions

View File

@@ -47,3 +47,4 @@ def check_api_key(base_url, api_key): # test api key and show spaceVM version
else:
console.print(f"[bold red]{response.status_code}[/]")
return response.status_code

View File

@@ -1,6 +1,6 @@
import os
import subprocess
import configparser
from cluster_api import *
from data_pools_api import *
from rich import print
@@ -20,6 +20,7 @@ def config_menu(config_relative_path):
sub_choice=str(input("\n>>> "))
if sub_choice == "1":
config_show(config_relative_path)
config_menu(config_relative_path)
if sub_choice == "2":
config_edit(config_relative_path)
@@ -29,57 +30,63 @@ def config_show(config_relative_path):
with open(config_relative_path, "r") as f:
print(f.read())
console.rule(style="yellow")
Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.. :right_arrow_curving_down:")
Prompt.ask("[green_yellow bold]ENTER - return to Utility Configuration.. :right_arrow_curving_down:")
def import_vm_uuid(config_relative_path):
vm_uuids = []
with open(config_relative_path, "r") as f:
for i in range(3): # ignoring 2 first lines (IP, API-KEY)
next(f)
for line in f:
line = line.strip('\n')
if line: # checks if line is empty (EOF). ESSENTIAL, DO NOT REMOVE
vm_uuids.append(line)
return vm_uuids
def config_import(config_relative_path):
config = configparser.ConfigParser()
config.read(config_relative_path)
def import_threelines(config_relative_path):
threelines = [None] * 3
with open(config_relative_path, "r") as f:
all_lines = f.readlines()
if len(all_lines) < 3:
raise ValueError("Check config. Receiving less than 3 lines!")
threelines[0] = all_lines[0].strip('\n')
threelines[1] = "jwt " + all_lines[1].strip('\n') #actual format for api_key. That was realy obvious DACOM >:C
threelines[2] = all_lines[2].strip('\n')
return threelines
base_url = config.get('General', 'controller_ip')
api_key = config.get('General', 'api_key')
data_pool_uuid = config.get('Data_Pool', 'data_pool_uuid')
vm_list = []
if 'VM_List' in config:
for key, value in config['VM_List'].items():
vm_list.append(value)
config_values = {
'base_url': base_url,
'api_key': "jwt " + api_key,
'data_pool_uuid': data_pool_uuid,
'vm_list': vm_list
}
return config_values
def config_edit(config_relative_path):
read_input=input("Create new config file? (Y / N): ")
menu_choice=str(read_input)
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 SpaceVM Controller IP: ")
while ping(base_url) != True:
base_url = console.input("[bold red]No response.\nCheck and type SpaceVM Controller IP again: [/]")
api_key = input("Type your API Key: ")
while check_api_key(base_url, "jwt " + api_key) != 200:
api_key = console.input("[bold red]Check and type SpaceVM Controller API Key again: [/]")
data_pools(base_url,"jwt " + api_key)
data_pool_uuid = input("Type Data Pool UUID you wish to use: ")
lines = [base_url, api_key, data_pool_uuid]
with open(config_relative_path, "w+") as file:
for line in lines:
file.write(line + '\n')
config = configparser.ConfigParser()
config["General"] = {
"controller_ip": base_url,
"api_key": api_key,
}
config["Data_Pool"] = {"data_pool_uuid": data_pool_uuid}
with open(config_relative_path, "w") as configfile:
config.write(configfile)
print("Type VM UUIDs one by one (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 != ""):
with open(config_relative_path, "a") as file:
file.write("[VM_List]\n") #manually writing section for VMs
vm_input = []
x = 0
while vm_input != "":
vm_input = input(">> ")
file.write(vm_input + '\n')
console.print("[green bold]VM UUIDs has been written in config :pencil:")
console.print("[green bold]Configuration completed ! :white_check_mark:")
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
cls()
if vm_input:
x += 1
file.write(f"UUID_{x} = {vm_input}\n")
console.print("[green bold]VM UUIDs have been written in config :pencil:")
console.print("[green bold]Configuration completed ! :white_check_mark:")
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
cls()
def cls():
os.system('cls' if os.name=='nt' else 'clear')

View File

@@ -32,4 +32,12 @@ def data_pools(base_url, api_key): # output data pool info
console.print(*panels, sep="\n")
else:
console.print(f"[red]Failed to retrieve data {response.status_code}[/]")
Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.. :right_arrow_curving_down:")
Prompt.ask("[green_yellow bold]ENTER - to proceed.. :right_arrow_curving_down:")
#translates data pool uuid to verbose_name
def get_data_pool_name(base_url, api_key, data_pool_uuid):
url = f"http://{base_url}//api/data-pools/{data_pool_uuid}/"
response = requests.get(url, headers={'Authorization': api_key})
if response.status_code == 200:
data_pool_name = response.json()
return (f"{data_pool_name['verbose_name']}")

View File

@@ -103,6 +103,13 @@ def get_disk_info(domain_all_content):
console.print(Columns(disk_info_renderables))
def get_vm_name(base_url, api_key, vm_uuids):
url = f"http://{base_url}//api/domains/{vm_uuids}/"
response = requests.get(url, headers={'Authorization': api_key})
if response.status_code == 200:
vm_name = response.json()
return (f"{vm_name['verbose_name']}")
def vm_info(base_url, api_key, vm_uuids):
domain_info = get_domain_info(base_url, api_key, vm_uuids)
domain_all_content = get_domain_all_content(base_url, api_key, vm_uuids)

28
main.py
View File

@@ -4,11 +4,10 @@ from cluster_api import *
from domain_api import *
from data_pools_api import *
from disk_edit_mode import *
from rich import print
from rich.panel import Panel
from rich.console import Console , Align
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() , 'SpaceVM_Utility.conf') #config.txt in the same directory with main.py
if os.path.exists(config_relative_path) and os.path.getsize(config_relative_path) > 0: #check if config exists and not empty
pass #do nothing
else:
@@ -16,22 +15,35 @@ else:
config_edit(config_relative_path)
#importing API-KEY / IP / DATA POOL UUID / VM-UUIDs from config
#base_url=threelines[0] api_key=threelines[1] data_pool_uuid=threelines[2]
base_url, api_key, data_pool_uuid = import_threelines(config_relative_path)
vm_uuids = import_vm_uuid(config_relative_path)
config_data = config_import(config_relative_path)
base_url = config_data['base_url']
api_key = config_data['api_key']
data_pool_uuid = config_data['data_pool_uuid']
vm_uuids = config_data['vm_list']
data_pool_name = get_data_pool_name(base_url , api_key , data_pool_uuid)
#for x in vm_uuids:
# vm_names = get_vm_name(base_url , api_key , x)
# print(vm_names)
menu_choice=0
menu_options="[gold bold][1] [grey53 italic]Manage utility config\n[/grey53 italic] \
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 \n (for selected VMs in config)[/grey53 italic]\n \
\n[gold bold][5] [grey53 italic]Show data pools[/grey53 italic]\n \
\n[gold bold][6] [grey53 italic]Show VMs Name / UUID[/grey53 italic]\n \
\n\n[green_yellow bold]ENTER - exit Utility"
\n\n[green_yellow bold]ENTER - exit Utility\n\n \
[grey53]Connected to Controller: {base_url} \n Selected Data Pool: {data_pool_name} \n Selected VMs:\n {vm_uuids}"
menu_options=Align.center(menu_options, vertical="middle")
menu_subtitle = "[blue bold][link=https://github.com/OVERLORD7F/SpaceVM_VM_Utility]:wrench: Project_GitHub[/link] [yellow]| [magenta bold][link=https://spacevm.ru/docs/]:books: SpaceVM_Docs[/link] [yellow]| [red bold][link=https://comptek.ru]:briefcase: Comptek[/link]"
console = Console()
os.system('cls' if os.name=='nt' else 'clear')
#os.system('cls' if os.name=='nt' else 'clear')
while(menu_choice != ""): #main menu loop
console.print(Panel(menu_options,
title="[bold magenta]SpaceVM Utility - Main Menu" , subtitle = menu_subtitle, subtitle_align="right" , style="yellow" , width=150 , padding = 2))