You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.4 KiB
Plaintext

from network.ValhallaServer import ValhallaServer
from utils.MachineData import MachineData
import argparse
from picotui.widgets import *
from picotui.menu import *
from picotui.context import Context
import os
import json
import subprocess
machine_data = MachineData()
def synchronize():
server = ValhallaServer(
server_address=machine_data.server_url,
server_port=machine_data.server_port,
server_user=machine_data.server_access_username,
server_access_password=machine_data.server_password,
logging_level=machine_data.client_loglevel,
)
server.authenticate()
server.get_server_data()
server.update_machine_data_on_server(machine_data=machine_data)
vm_list = server.get_vm_list_from_server(machine_data=machine_data)
print(vm_list)
vm_data = server.get_vms_data_from_server()
with open("vms_data.json", "w") as vm_data_file:
vm_data_file.write(json.dumps(vm_data))
vm_data_file.flush()
server.update_vms_images_from_server()
for image_id in vm_data:
run_script_path = f"images/valhalla/{vm_data[image_id]['image_name']}/{vm_data[image_id]['image_version']}"
print(run_script_path)
machine_data.generate_runner_script(run_script_path)
def runner():
while (True):
d = None
image_choice_vm_id = None
vm_data = {}
with open("vms_data.json", "r") as vm_data_file:
try:
vm_data = json.loads(vm_data_file.read())
except Exception as ex:
print(f"ERROR: {str(ex)}")
# This routine is called to redraw screen "in menu's background"
def screen_redraw(s, allow_cursor=False):
with open("vms_data.json", "r") as vm_data_file:
try:
vm_data = json.loads(vm_data_file.read())
except Exception as ex:
print(f"ERROR: {str(ex)}")
s.attr_color(C_WHITE, C_BLUE)
s.cls()
s.attr_reset()
d.redraw()
def main_loop():
while 1:
key = m.get_input()
if isinstance(key, list):
# Mouse click
x, y = key
if m.inside(x, y):
m.focus = True
if m.focus:
# If menu is focused, it gets events. If menu is cancelled,
# it loses focus. Otherwise, if menu selection is made, we
# quit with with menu result.
res = m.handle_input(key)
if res == ACTION_CANCEL:
m.focus = False
elif res is not None and res is not True:
return res
else:
# If menu isn't focused, it can be focused by pressing F9.
if key == KEY_F9:
m.focus = True
m.redraw()
continue
if key == KEY_ESC:
exit(0)
# Otherwise, dialog gets input
res = d.handle_input(key)
if res is not None and res is not True:
return res
with Context():
d = Dialog(10, 5, 80, 24)
d.add(12, 1, WLabel("Virtual Machine selection screen"))
d.add(1, 2, WLabel("VMs:"))
vm_names_table = []
for vm_id in vm_data:
vm_names_table.append(f"{vm_id}: {vm_data[vm_id]['image_name']} - ver. {vm_data[vm_id]['image_version']}")
w_listbox = WListBox(64, 4, vm_names_table)
d.add(1,3, w_listbox)
d.add(1, 6, "Selected listbox value:")
w_listbox_val = WLabel("", w=64)
d.add(1, 7, w_listbox_val)
def listbox_changed(w):
val = w.items[w.choice]
w_listbox_val.t = val
w_listbox_val.redraw()
w_listbox.on("changed", listbox_changed)
b = WButton(8, "OK")
d.add(10, 11, b)
b.finish_dialog = ACTION_OK
b = WButton(8, "Cancel")
d.add(23, 11, b)
b.finish_dialog = ACTION_CANCEL
screen_redraw(Screen)
Screen.set_screen_redraw(screen_redraw)
menu_file = WMenuBox([("Exit", "ex")])
m = WMenuBar([("File", menu_file), ("About", "About")])
m.permanent = True
m.redraw()
res = main_loop()
image_choice_vm_id = int(str(vm_names_table[int(w_listbox.choice)]).split(":")[0])
print(image_choice_vm_id)
try:
print(f"Running VM: {vm_data[f'{image_choice_vm_id}']['image_name']}:{vm_data[f'{image_choice_vm_id}']['image_version']}")
run_res = subprocess.check_call([f"images/valhalla/{vm_data[f'{image_choice_vm_id}']['image_name']}/{vm_data[f'{image_choice_vm_id}']['image_version']}/run.sh"])
except Exception as ex:
print(str(ex))
parser = argparse.ArgumentParser(
prog="valhalla-client",
description="Client for running VM machines",
)
function_mapper = {
"runner": runner,
"synchronize": synchronize,
}
parser.add_argument("command", choices=function_mapper)
args = parser.parse_args()
fun = function_mapper[args.command]
try:
fun()
except Exception as ex:
print(f"Invalid command: {str(ex)}")
# vim:ft=py