diff --git a/main.py b/main.py index a4a67ef..b89ce0a 100755 --- a/main.py +++ b/main.py @@ -100,16 +100,12 @@ def main(): continue # If the event is directed toward me OR I am destination of a InitVMMigration - if ((hasattr(request_event, "hostname") and - request_event.hostname == host.key) or - (hasattr(request_event, "destination") and - request_event.destination == host.key)): + if (request_event.hostname == host.key or request_event.destination == host.key): + logging.debug("EVENT: %s", request_event) request_pool.client.client.delete(request_event.key) vm_entry = vm_pool.get(request_event.uuid) - logging.debug("EVENT: %s", request_event) - if request_event.type == RequestType.StartVM: virtualmachine.start(vm_entry) diff --git a/virtualmachine.py b/virtualmachine.py index ae8b56a..26cdd40 100755 --- a/virtualmachine.py +++ b/virtualmachine.py @@ -9,7 +9,7 @@ import os import subprocess import tempfile import time -import traceback + from functools import wraps from os.path import join from typing import Union @@ -169,22 +169,7 @@ def start(vm_entry: VMEntry): return else: create(vm_entry) - - logging.info("Starting %s", vm_entry.key) - - vm = create_vm_object(vm_entry) - try: - vm.handle.launch() - except (qmp.QEMUMachineError, TypeError, Exception): - vm_entry.declare_killed() - vm_entry.add_log("Machine Error occurred | %s", traceback.format_exc()) - vm_entry.vnc_socket = vm.vnc_socket_file - vm_pool.put(vm_entry) - else: - running_vms.append(vm) - vm_entry.status = VMStatus.running - vm_entry.add_log("Started successfully") - vm_pool.put(vm_entry) + launch_vm(vm_entry) @need_running_vm @@ -276,29 +261,45 @@ def init_migration(vm_entry, destination_host_key): logging.info("%s Already running", _vm.key) return + launch_vm(vm_entry, migration=True, migration_port=4444, + destination_host_key=destination_host_key) + + +def launch_vm(vm_entry, migration=False, migration_port=None, destination_host_key=None): logging.info("Starting %s", vm_entry.key) - vm = create_vm_object(vm_entry, migration=True, migration_port=4444) - + vm = create_vm_object(vm_entry, migration=migration, migration_port=migration_port) try: vm.handle.launch() except Exception as e: - # We don't care whether MachineError or any other error occurred logging.exception(e) - vm.handle.shutdown() + + if migration: + # We don't care whether MachineError or any other error occurred + vm.handle.shutdown() + else: + # Error during typical launch of a vm + vm_entry.add_log("Error Occurred while starting VM") + vm_entry.declare_killed() + vm_pool.put(vm_entry) else: - vm_entry.in_migration = True vm_entry.vnc_socket = vm.vnc_socket_file - - vm_pool.put(vm_entry) - running_vms.append(vm) - r = RequestEntry.from_scratch( - type=RequestType.TransferVM, - hostname=vm_entry.hostname, - parameters={"host": get_ipv4_address(), "port": 4444}, - uuid=vm_entry.uuid, - destination_host_key=destination_host_key, - ) - request_pool.put(r) + if migration: + vm_entry.in_migration = True + r = RequestEntry.from_scratch( + type=RequestType.TransferVM, + hostname=vm_entry.hostname, + parameters={"host": get_ipv4_address(), "port": 4444}, + uuid=vm_entry.uuid, + destination_host_key=destination_host_key, + ) + request_pool.put(r) + else: + # Typical launching of a vm + vm_entry.status = VMStatus.running + vm_entry.add_log("Started successfully") + + vm_pool.put(vm_entry) + \ No newline at end of file