diff --git a/utils/MachineData.py b/utils/MachineData.py index ed1a964..fd0a560 100644 --- a/utils/MachineData.py +++ b/utils/MachineData.py @@ -2,6 +2,7 @@ from getmac import get_mac_address as gma import socket import yaml import os +import stat import psutil from string import Template @@ -71,9 +72,9 @@ class MachineData(): def generate_runner_script(self, file_path: str): substitution_mapper = { - "CPU_CORES_COUNT": (self.cpu_count/2), - "MEMORY_ALLOCATION_SIZE": f"{(self.ram_size/2)}M", - "IMAGE_PATH": "image.qcow2" + "CPU_CORES_COUNT": int(self.cpu_count/2), + "MEMORY_ALLOCATION_SIZE": f"{int(self.ram_size/2)}M", + "IMAGE_PATH": f"{os.getcwd()}/{file_path}/image.qcow2" } try: with open("vm_run_script.template", "r") as file: @@ -84,5 +85,9 @@ class MachineData(): result_file.write(result) result_file.flush() result_file.close() + + st = os.stat(file_path) + os.chmod(file_path, st.st_mode | stat.S_IEXEC) + except Exception as ex: print(str(ex)) diff --git a/valhalla-client b/valhalla-client index a2b0407..e6a5129 100644 --- a/valhalla-client +++ b/valhalla-client @@ -6,6 +6,7 @@ from picotui.menu import * from picotui.context import Context import os import json +import subprocess machine_data = MachineData() @@ -33,100 +34,104 @@ def synchronize(): machine_data.generate_runner_script(run_script_path) def runner(): - 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): + 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)}") - 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 - # 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']}") - 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") - except Exception as ex: - print(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", diff --git a/vm_run_script.template b/vm_run_script.template index 370dff5..43096dd 100644 --- a/vm_run_script.template +++ b/vm_run_script.template @@ -2,6 +2,6 @@ ## QEMU (VM) command 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 \ --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 \ -hda $IMAGE_PATH & \ No newline at end of file diff --git a/vms_data.json b/vms_data.json index 366def4..abbc0fa 100644 --- a/vms_data.json +++ b/vms_data.json @@ -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"}} \ No newline at end of file +{"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"}} \ No newline at end of file