master
Wojciech Janota 2 years ago
parent e0f1eac159
commit 2263749d86

@ -2,6 +2,7 @@ from getmac import get_mac_address as gma
import socket import socket
import yaml import yaml
import os import os
import stat
import psutil import psutil
from string import Template from string import Template
@ -71,9 +72,9 @@ class MachineData():
def generate_runner_script(self, file_path: str): def generate_runner_script(self, file_path: str):
substitution_mapper = { substitution_mapper = {
"CPU_CORES_COUNT": (self.cpu_count/2), "CPU_CORES_COUNT": int(self.cpu_count/2),
"MEMORY_ALLOCATION_SIZE": f"{(self.ram_size/2)}M", "MEMORY_ALLOCATION_SIZE": f"{int(self.ram_size/2)}M",
"IMAGE_PATH": "image.qcow2" "IMAGE_PATH": f"{os.getcwd()}/{file_path}/image.qcow2"
} }
try: try:
with open("vm_run_script.template", "r") as file: with open("vm_run_script.template", "r") as file:
@ -84,5 +85,9 @@ class MachineData():
result_file.write(result) result_file.write(result)
result_file.flush() result_file.flush()
result_file.close() result_file.close()
st = os.stat(file_path)
os.chmod(file_path, st.st_mode | stat.S_IEXEC)
except Exception as ex: except Exception as ex:
print(str(ex)) print(str(ex))

@ -6,6 +6,7 @@ from picotui.menu import *
from picotui.context import Context from picotui.context import Context
import os import os
import json import json
import subprocess
machine_data = MachineData() machine_data = MachineData()
@ -33,100 +34,104 @@ def synchronize():
machine_data.generate_runner_script(run_script_path) machine_data.generate_runner_script(run_script_path)
def runner(): def runner():
d = None while (True):
image_choice_vm_id = None d = None
vm_data = {} image_choice_vm_id = None
with open("vms_data.json", "r") as vm_data_file: vm_data = {}
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: with open("vms_data.json", "r") as vm_data_file:
try: try:
vm_data = json.loads(vm_data_file.read()) vm_data = json.loads(vm_data_file.read())
except Exception as ex: except Exception as ex:
print(f"ERROR: {str(ex)}") print(f"ERROR: {str(ex)}")
s.attr_color(C_WHITE, C_BLUE) # This routine is called to redraw screen "in menu's background"
s.cls() def screen_redraw(s, allow_cursor=False):
s.attr_reset() with open("vms_data.json", "r") as vm_data_file:
d.redraw() try:
def main_loop(): vm_data = json.loads(vm_data_file.read())
while 1: except Exception as ex:
key = m.get_input() print(f"ERROR: {str(ex)}")
s.attr_color(C_WHITE, C_BLUE)
if isinstance(key, list): s.cls()
# Mouse click s.attr_reset()
x, y = key d.redraw()
if m.inside(x, y): def main_loop():
m.focus = True while 1:
key = m.get_input()
if m.focus:
# If menu is focused, it gets events. If menu is cancelled, if isinstance(key, list):
# it loses focus. Otherwise, if menu selection is made, we # Mouse click
# quit with with menu result. x, y = key
res = m.handle_input(key) if m.inside(x, y):
if res == ACTION_CANCEL: m.focus = True
m.focus = False
elif res is not None and res is not True: if m.focus:
return res # If menu is focused, it gets events. If menu is cancelled,
else: # it loses focus. Otherwise, if menu selection is made, we
# If menu isn't focused, it can be focused by pressing F9. # quit with with menu result.
if key == KEY_F9: res = m.handle_input(key)
m.focus = True if res == ACTION_CANCEL:
m.redraw() m.focus = False
continue elif res is not None and res is not True:
# Otherwise, dialog gets input return res
res = d.handle_input(key) else:
if res is not None and res is not True: # If menu isn't focused, it can be focused by pressing F9.
return res if key == KEY_F9:
with Context(): m.focus = True
m.redraw()
d = Dialog(10, 5, 80, 24) continue
d.add(12, 1, WLabel("Virtual Machine selection screen")) if key == KEY_ESC:
d.add(1, 2, WLabel("VMs:")) exit(0)
vm_names_table = [] # Otherwise, dialog gets input
for vm_id in vm_data: res = d.handle_input(key)
vm_names_table.append(f"{vm_id}: {vm_data[vm_id]['image_name']} - ver. {vm_data[vm_id]['image_version']}") if res is not None and res is not True:
w_listbox = WListBox(64, 4, vm_names_table) return res
d.add(1,3, w_listbox) with Context():
d.add(1, 6, "Selected listbox value:") d = Dialog(10, 5, 80, 24)
w_listbox_val = WLabel("", w=64) d.add(12, 1, WLabel("Virtual Machine selection screen"))
d.add(1, 7, w_listbox_val) d.add(1, 2, WLabel("VMs:"))
vm_names_table = []
def listbox_changed(w): for vm_id in vm_data:
val = w.items[w.choice] vm_names_table.append(f"{vm_id}: {vm_data[vm_id]['image_name']} - ver. {vm_data[vm_id]['image_version']}")
w_listbox_val.t = val w_listbox = WListBox(64, 4, vm_names_table)
w_listbox_val.redraw() d.add(1,3, w_listbox)
w_listbox.on("changed", listbox_changed) d.add(1, 6, "Selected listbox value:")
w_listbox_val = WLabel("", w=64)
b = WButton(8, "OK") d.add(1, 7, w_listbox_val)
d.add(10, 11, b)
b.finish_dialog = ACTION_OK def listbox_changed(w):
val = w.items[w.choice]
b = WButton(8, "Cancel") w_listbox_val.t = val
d.add(23, 11, b) w_listbox_val.redraw()
b.finish_dialog = ACTION_CANCEL
w_listbox.on("changed", listbox_changed)
screen_redraw(Screen)
Screen.set_screen_redraw(screen_redraw) b = WButton(8, "OK")
d.add(10, 11, b)
menu_file = WMenuBox([("Exit", "ex")]) b.finish_dialog = ACTION_OK
m = WMenuBar([("File", menu_file), ("About", "About")])
m.permanent = True b = WButton(8, "Cancel")
m.redraw() d.add(23, 11, b)
b.finish_dialog = ACTION_CANCEL
res = main_loop()
image_choice_vm_id = int(str(vm_names_table[int(w_listbox.choice)]).split(":")[0]) screen_redraw(Screen)
print(image_choice_vm_id) Screen.set_screen_redraw(screen_redraw)
try:
print(f"Running VM: {vm_data[f'{image_choice_vm_id}']['image_name']}:{vm_data[f'{image_choice_vm_id}']['image_version']}") menu_file = WMenuBox([("Exit", "ex")])
os.execvp(f"images/valhalla/{vm_data[f'{image_choice_vm_id}']['image_name']}/{vm_data[f'{image_choice_vm_id}']['image_version']}/run.sh") m = WMenuBar([("File", menu_file), ("About", "About")])
except Exception as ex: m.permanent = True
print(str(ex)) 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( parser = argparse.ArgumentParser(
prog="valhalla-client", prog="valhalla-client",

@ -2,6 +2,6 @@
## QEMU (VM) command ## QEMU (VM) command
qemu-system-x86_64 -enable-kvm -m $MEMORY_ALLOCATION_SIZE \ qemu-system-x86_64 -enable-kvm -m $MEMORY_ALLOCATION_SIZE \
-cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_time,hv_vapic,hv_vendor_id=0xDEADBEEFFF \ -cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_time,hv_vapic,hv_vendor_id=0xDEADBEEFFF \
-rtc clock=host,base=localtime -smp 4,sockets=1,cores=$CPU_CORES_COUNT,threads=2 \ -rtc clock=host,base=localtime -smp $CPU_CORES_COUNT,sockets=1,cores=$CPU_CORES_COUNT,threads=1 \
-device virtio-net-pci,netdev=n1 -netdev user,id=n1 \ -device virtio-net-pci,netdev=n1 -netdev user,id=n1 \
-hda $IMAGE_PATH & -hda $IMAGE_PATH &

@ -1 +1 @@
{"4": {"image_file": "temp_file.qcow2", "image_hash": "d41d8cd98f00b204e9800998ecf8427e", "image_id": "4", "image_name": "cool-image-name", "image_name_version_combo": "cool-image-name@v0.0.2alpha", "image_version": "v0.0.2alpha"}, "3": {"image_file": "temp_file.qcow2", "image_hash": "d41d8cd98f00b204e9800998ecf8427e", "image_id": "3", "image_name": "cool-image-name", "image_name_version_combo": "cool-image-name@v0.0.1alpha", "image_version": "v0.0.1alpha"}} {"4": {"image_file": "temp_file.qcow2", "image_hash": "d41d8cd98f00b204e9800998ecf8427e", "image_id": "4", "image_name": "cool-image-name", "image_name_version_combo": "cool-image-name@v0.0.2alpha", "image_version": "v0.0.2alpha"}, "3": {"image_file": "temp_file.qcow2", "image_hash": "d41d8cd98f00b204e9800998ecf8427e", "image_id": "3", "image_name": "cool-image-name", "image_name_version_combo": "cool-image-name@v0.0.1alpha", "image_version": "v0.0.1alpha"}, "5": {"image_file": "/home/nixen/Projects/College/engineering-degree/images/ubuntu_base/image.qcow2", "image_hash": "f734423d6aaccd7e97c9561f127dd72d", "image_id": "5", "image_name": "xubuntu-22.04", "image_name_version_combo": "xubuntu-22.04@v0.0.1", "image_version": "v0.0.1"}}
Loading…
Cancel
Save