mirror of
https://github.com/OVERLORD7F/SVMU.git
synced 2025-10-01 21:52:47 +03:00
LEEETS GOOOO <50 lines !!1
- Ping / API key checks are back! #13 - Changes in config import - Moved everything in main to menu loop because of reasons #2 - New function check_config - Currently selected pools / vms .. are shown in main menu #12 - Renamed a few functions
This commit is contained in:
@@ -43,7 +43,7 @@ def check_api_key(base_url, api_key): # test api key and show spaceVM version
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
cluster_info = response.json()
|
cluster_info = response.json()
|
||||||
version = cluster_info['version']
|
version = cluster_info['version']
|
||||||
console.print(f"[bold green]Successfully conected to SpaceVM version {version}")
|
console.print(f"[bold green]Successfully conected to SpaceVM v{version}")
|
||||||
else:
|
else:
|
||||||
console.print(f"[bold red]{response.status_code}[/]")
|
console.print(f"[bold red]{response.status_code}[/]")
|
||||||
return response.status_code
|
return response.status_code
|
||||||
|
@@ -2,6 +2,7 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import configparser
|
import configparser
|
||||||
from cluster_api import *
|
from cluster_api import *
|
||||||
|
from domain_api import *
|
||||||
from data_pools_api import *
|
from data_pools_api import *
|
||||||
from rich import print
|
from rich import print
|
||||||
from rich.panel import Panel
|
from rich.panel import Panel
|
||||||
@@ -37,31 +38,37 @@ def config_import(config_relative_path):
|
|||||||
config.read(config_relative_path)
|
config.read(config_relative_path)
|
||||||
|
|
||||||
base_url = config.get('General', 'controller_ip')
|
base_url = config.get('General', 'controller_ip')
|
||||||
api_key = config.get('General', 'api_key')
|
api_key = "jwt " + config.get('General', 'api_key') #That was realy obvious DACOM >:C
|
||||||
data_pool_uuid = config.get('Data_Pool', 'data_pool_uuid')
|
data_pool_uuid = config.get('Data_Pool', 'data_pool_uuid')
|
||||||
|
|
||||||
vm_list = []
|
vm_list = []
|
||||||
if 'VM_List' in config:
|
if 'VM_List' in config:
|
||||||
for key, value in config['VM_List'].items():
|
for key, value in config['VM_List'].items():
|
||||||
vm_list.append(value)
|
vm_list.append(value)
|
||||||
|
#get pretty name for selected data pool
|
||||||
|
data_pool_name = get_data_pool_name(base_url , api_key , data_pool_uuid)
|
||||||
|
|
||||||
config_values = {
|
#get pretty name for selected VMs
|
||||||
'base_url': base_url,
|
vm_names=[]
|
||||||
'api_key': "jwt " + api_key,
|
for x in vm_list:
|
||||||
'data_pool_uuid': data_pool_uuid,
|
vm_names.append(get_vm_name(base_url, api_key, x))
|
||||||
'vm_list': vm_list
|
|
||||||
}
|
|
||||||
|
|
||||||
return config_values
|
return base_url, api_key, data_pool_uuid, data_pool_name, vm_list, vm_names
|
||||||
|
|
||||||
def config_edit(config_relative_path):
|
def config_edit(config_relative_path):
|
||||||
read_input = input("Create new config file? (Y / N): ")
|
read_input = input("Create new config file? (Y / N): ")
|
||||||
menu_choice = str(read_input)
|
menu_choice = str(read_input)
|
||||||
if menu_choice == "Y" or menu_choice == "y":
|
if menu_choice == "Y" or menu_choice == "y":
|
||||||
base_url = input("Type SpaceVM Controller IP: ")
|
base_url = input("Type SpaceVM Controller IP: ")
|
||||||
api_key = input("Type your API Key: ")
|
while check_ping(base_url) != True:
|
||||||
data_pool_uuid = input("Type Data Pool UUID you wish to use: ")
|
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: [/]")
|
||||||
|
show_data_pools(base_url, "jwt " + api_key)
|
||||||
|
|
||||||
|
data_pool_uuid = input("Type Data Pool UUID you wish to use: ")
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config["General"] = {
|
config["General"] = {
|
||||||
"controller_ip": base_url,
|
"controller_ip": base_url,
|
||||||
@@ -88,10 +95,17 @@ def config_edit(config_relative_path):
|
|||||||
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
||||||
cls()
|
cls()
|
||||||
|
|
||||||
|
def check_config(config_relative_path):
|
||||||
|
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:
|
||||||
|
console.print("[yellow bold italic]Config file was not found or empty.. ")
|
||||||
|
config_edit(config_relative_path)
|
||||||
|
|
||||||
def cls():
|
def cls():
|
||||||
os.system('cls' if os.name=='nt' else 'clear')
|
os.system('cls' if os.name=='nt' else 'clear')
|
||||||
|
|
||||||
def ping(base_url):
|
def check_ping(base_url):
|
||||||
DNULL = open(os.devnull, 'w')
|
DNULL = open(os.devnull, 'w')
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
status = subprocess.call(["ping","-n","1",base_url],stdout = DNULL)
|
status = subprocess.call(["ping","-n","1",base_url],stdout = DNULL)
|
||||||
|
@@ -5,7 +5,7 @@ from rich.console import Console
|
|||||||
from rich.panel import Panel
|
from rich.panel import Panel
|
||||||
from rich.align import Align
|
from rich.align import Align
|
||||||
|
|
||||||
def data_pools(base_url, api_key): # output data pool info
|
def show_data_pools(base_url, api_key): # output data pool info
|
||||||
url = f"http://{base_url}//api/data-pools/"
|
url = f"http://{base_url}//api/data-pools/"
|
||||||
response = requests.get(url, headers={'Authorization': api_key})
|
response = requests.get(url, headers={'Authorization': api_key})
|
||||||
console = Console()
|
console = Console()
|
||||||
|
@@ -55,7 +55,8 @@ def disk_edit_mode(base_url , api_key , data_pool_uuid , vm_uuids):
|
|||||||
console.print("[bold red]All attached vDisks has been deleted!")
|
console.print("[bold red]All attached vDisks has been deleted!")
|
||||||
for z in vm_uuids: # only for creating disks
|
for z in vm_uuids: # only for creating disks
|
||||||
domain_uuid = z.strip('\n')
|
domain_uuid = z.strip('\n')
|
||||||
console.print(f"\n[bold underline yellow]Creating and attaching disk to VM {domain_uuid}")
|
vm_name = get_vm_name(base_url, api_key, domain_uuid)
|
||||||
|
console.print(f"\n[bold underline yellow]Creating and attaching disk to VM {vm_name}")
|
||||||
domain_info = get_domain_info(base_url , api_key , domain_uuid)
|
domain_info = get_domain_info(base_url , api_key , domain_uuid)
|
||||||
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
|
domain_all_content = get_domain_all_content(base_url , api_key , domain_uuid)
|
||||||
if domain_info:
|
if domain_info:
|
||||||
|
@@ -108,7 +108,7 @@ def get_vm_name(base_url, api_key, vm_uuids):
|
|||||||
response = requests.get(url, headers={'Authorization': api_key})
|
response = requests.get(url, headers={'Authorization': api_key})
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
vm_name = response.json()
|
vm_name = response.json()
|
||||||
return (f"{vm_name['verbose_name']}")
|
return (f"{vm_name['verbose_name']}")
|
||||||
|
|
||||||
def vm_info(base_url, api_key, vm_uuids):
|
def vm_info(base_url, api_key, vm_uuids):
|
||||||
domain_info = get_domain_info(base_url, api_key, vm_uuids)
|
domain_info = get_domain_info(base_url, api_key, vm_uuids)
|
||||||
|
50
main.py
50
main.py
@@ -7,46 +7,26 @@ from disk_edit_mode import *
|
|||||||
from rich.panel import Panel
|
from rich.panel import Panel
|
||||||
from rich.console import Console , Align
|
from rich.console import Console , Align
|
||||||
|
|
||||||
config_relative_path = os.path.join(os.getcwd() , 'SpaceVM_Utility.conf') #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:
|
|
||||||
console.print("[yellow bold italic]Config file was not found or empty.. ")
|
|
||||||
config_edit(config_relative_path)
|
|
||||||
|
|
||||||
#importing API-KEY / IP / DATA POOL UUID / VM-UUIDs from config
|
|
||||||
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_choice=0
|
||||||
menu_options=f"[gold bold][1] [grey53 italic]Manage utility config\n[/grey53 italic] \
|
console = Console()
|
||||||
|
os.system('cls' if os.name=='nt' else 'clear')
|
||||||
|
while(menu_choice != ""): #main menu loop
|
||||||
|
check_config(config_relative_path)
|
||||||
|
base_url, api_key, data_pool_uuid, data_pool_name, vm_uuids, vm_names = config_import(config_relative_path) #importing API-KEY / IP / DATA POOL UUID / VM-UUIDs from config
|
||||||
|
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][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][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][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][5] [grey53 italic]Show data pools[/grey53 italic]\n \
|
||||||
\n[gold bold][6] [grey53 italic]Show VMs Name / UUID[/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 \
|
\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}"
|
[underline bold grey53]Currently imported config:[/]\n \
|
||||||
|
[bold grey53]Connected to Controller: [bright_yellow]{base_url}[/]\n Selected Data Pool: [bright_yellow]{data_pool_name}[/]\n Selected VMs:\n [bright_yellow]{vm_names}"
|
||||||
menu_options=Align.center(menu_options, vertical="middle")
|
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.print(Panel(menu_options, title="[bold magenta]SpaceVM Utility - Main Menu" , subtitle = menu_subtitle, subtitle_align="right" , style="yellow" , width=150 , padding = 2))
|
||||||
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')
|
|
||||||
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))
|
|
||||||
menu_choice=str(input("\n>>> "))
|
menu_choice=str(input("\n>>> "))
|
||||||
if menu_choice == "1":
|
if menu_choice == "1":
|
||||||
config_menu(config_relative_path)
|
config_menu(config_relative_path)
|
||||||
@@ -60,10 +40,10 @@ title="[bold magenta]SpaceVM Utility - Main Menu" , subtitle = menu_subtitle, su
|
|||||||
vm_info(base_url , api_key , x)
|
vm_info(base_url , api_key , x)
|
||||||
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
Prompt.ask("[green_yellow bold]Press ENTER to proceed.. :right_arrow_curving_down:")
|
||||||
if menu_choice == "5":
|
if menu_choice == "5":
|
||||||
data_pools(base_url , api_key)
|
show_data_pools(base_url , api_key)
|
||||||
if menu_choice == "6":
|
if menu_choice == "6":
|
||||||
vm_info_short(base_url , api_key)
|
vm_info_short(base_url , api_key)
|
||||||
if menu_choice == "7":
|
if menu_choice == "7":
|
||||||
check_api_key(base_url, api_key)
|
check_api_key(base_url, api_key)
|
||||||
os.system('cls' if os.name=='nt' else 'clear') #clears screen before looping back to main menu
|
os.system('cls' if os.name=='nt' else 'clear') #clears screen before looping back to main menu
|
||||||
console.print("[red bold]Exiting Utility ")
|
console.print("[red bold]Exiting Utility ")
|
Reference in New Issue
Block a user