2025-05-16 17:53:57 +03:00
|
|
|
import os
|
2025-05-26 10:28:01 +03:00
|
|
|
import subprocess
|
2025-05-16 17:53:57 +03:00
|
|
|
|
2025-05-26 10:28:01 +03:00
|
|
|
from cluster_api import *
|
|
|
|
from data_pools_api import *
|
2025-05-16 17:53:57 +03:00
|
|
|
from rich import print
|
|
|
|
from rich.panel import Panel
|
|
|
|
from rich.console import Console , Align
|
|
|
|
from rich.prompt import Prompt
|
|
|
|
console = Console()
|
|
|
|
|
2025-05-16 12:17:51 +03:00
|
|
|
def config_menu(config_relative_path):
|
2025-05-16 17:53:57 +03:00
|
|
|
cls()
|
|
|
|
config_menu_options="[gold bold][1] [grey53 italic]Show current configuration\n[/grey53 italic] \
|
|
|
|
\n[gold bold][2] [grey53 italic]Change configuraion[/grey53 italic]\n \
|
2025-05-21 14:12:37 +03:00
|
|
|
\n\n[green_yellow bold]ENTER - return to Main Menu"
|
2025-05-16 17:53:57 +03:00
|
|
|
config_menu_options=Align.center(config_menu_options, vertical="middle")
|
|
|
|
console = Console()
|
2025-05-21 14:12:37 +03:00
|
|
|
console.print(Panel(config_menu_options, title="[gold bold]SpaceVM Utility - Utility Configuration" , border_style="magenta" , width=150 , padding = 2))
|
2025-05-16 12:17:51 +03:00
|
|
|
sub_choice=str(input("\n>>> "))
|
|
|
|
if sub_choice == "1":
|
|
|
|
config_show(config_relative_path)
|
|
|
|
if sub_choice == "2":
|
|
|
|
config_edit(config_relative_path)
|
|
|
|
|
|
|
|
def config_show(config_relative_path):
|
2025-05-16 17:53:57 +03:00
|
|
|
cls()
|
2025-05-21 14:12:37 +03:00
|
|
|
console.rule(title = "Current configuration" , align="center" , style="yellow")
|
2025-05-16 12:17:51 +03:00
|
|
|
with open(config_relative_path, "r") as f:
|
|
|
|
print(f.read())
|
2025-05-21 14:12:37 +03:00
|
|
|
console.rule(style="yellow")
|
|
|
|
Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.. :right_arrow_curving_down:")
|
2025-05-15 11:46:28 +03:00
|
|
|
|
|
|
|
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 import_threelines(config_relative_path):
|
2025-05-15 15:41:33 +03:00
|
|
|
threelines = [None] * 3
|
2025-05-15 11:46:28 +03:00
|
|
|
with open(config_relative_path, "r") as f:
|
2025-05-15 15:41:33 +03:00
|
|
|
all_lines = f.readlines()
|
|
|
|
if len(all_lines) < 3:
|
|
|
|
raise ValueError("Check config. Receiving less than 3 lines!")
|
2025-05-15 11:46:28 +03:00
|
|
|
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
|
2025-05-15 15:41:33 +03:00
|
|
|
|
|
|
|
def config_edit(config_relative_path):
|
|
|
|
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: ")
|
2025-05-26 10:28:01 +03:00
|
|
|
while ping(base_url) != True:
|
|
|
|
base_url = console.input("[bold red]No response.\nCheck and type SpaceVM Controller IP again: [/]")
|
2025-05-15 15:41:33 +03:00
|
|
|
api_key = input("Type your API Key: ")
|
2025-05-26 10:28:01 +03:00
|
|
|
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)
|
2025-05-16 12:17:51 +03:00
|
|
|
data_pool_uuid = input("Type Data Pool UUID you wish to use: ")
|
2025-05-15 15:41:33 +03:00
|
|
|
lines = [base_url, api_key, data_pool_uuid]
|
|
|
|
with open(config_relative_path, "w+") as file:
|
|
|
|
for line in lines:
|
|
|
|
file.write(line + '\n')
|
|
|
|
|
2025-05-16 12:17:51 +03:00
|
|
|
print("Type VM UUIDs one by one (input ENTER to stop)")
|
2025-05-15 15:41:33 +03:00
|
|
|
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')
|
2025-05-16 17:53:57 +03:00
|
|
|
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()
|
2025-05-16 12:17:51 +03:00
|
|
|
|
2025-05-16 17:53:57 +03:00
|
|
|
def cls():
|
2025-05-26 10:28:01 +03:00
|
|
|
os.system('cls' if os.name=='nt' else 'clear')
|
|
|
|
|
|
|
|
def ping(base_url):
|
|
|
|
DNULL = open(os.devnull, 'w')
|
|
|
|
if os.name == 'nt':
|
|
|
|
status = subprocess.call(["ping","-n","1",base_url],stdout = DNULL)
|
|
|
|
else:
|
|
|
|
status = subprocess.call(["ping","-c","1",base_url],stdout = DNULL)
|
|
|
|
|
|
|
|
if status == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|