From 5429f2d1688e96cd168adfe8032513194d2966be Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Khalid Date: Tue, 23 Jul 2019 10:25:50 +0500 Subject: [PATCH] TIMEOUT event handling shifted to function ;maintenence' --- main.py | 63 +++++++++++++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/main.py b/main.py index 216b95d..a00cb71 100644 --- a/main.py +++ b/main.py @@ -94,25 +94,11 @@ def create_vm(vm_uuid, e): _command_to_create = f"rbd clone images/{image_uuid}@protected uservms/{vm_uuid}" subprocess.call(_command_to_create.split(" ")) - # DELETEME: Delete when CEPH integeration is complete - - # os.makedirs(f"{owner_dir}/.vm", exist_ok=True) - - # if not os.path.isfile(f"{owner_dir}/.vm/{vm_uuid}.raw"): - # shutil.copy( - # f"/var/vm/{image_uuid}.raw", f"{owner_dir}/.vm/{vm_uuid}.raw" - # ) - e.value["status"] = "REQUESTED_START" client.put(e.key, json.dumps(e.value)) def start_vm(vm_path, e): - # FIXME: Assume for the moment that the image exists - # Use librados to list files that exists in - # uservms pool then checkwhether the e.key.split("/").pop() - # exists in rbd_ls(uservms_pool) - if not vm_path.split("/")[-1] in RBD.ls("uservms"): logging.info(f"Image file of vm {e.key} does not exists") logging.info(f"Setting vm {e.key} status to DELETED") @@ -216,6 +202,29 @@ def get_vm(vm_list: list, vm_key) -> Union[VM, None]: return next((vm for vm in vm_list if vm.key == vm_key), None) +def maintenence(e, host): + _vms = filter(lambda v: v.value["hostname"] == host.key, client.get_prefix("/v1/vm", value_in_json=True)) + alleged_running_vms = filter(lambda v: v.value["status"] == "RUNNING", _vms) + should_be_running = filter(lambda v: v.value["status"] == "REQUESTED_START", _vms) + for vm in alleged_running_vms: + _vm = get_vm(running_vms, vm.key) + if (_vm and not _vm.vm.is_running()) or _vm is None: + logging.debug(f"{_vm} {vm.key}") + logging.info(f"{vm.key} is not running but is said to be running") + logging.info(f"Updating {vm.key} status to KILLED") + vm.value["status"] = "KILLED" + client.put(vm.key, json.dumps(vm.value)) + + for vm in should_be_running: + vm_path = f"rbd:uservms/{vm.key.split('/')[-1]}" + start_vm(vm_path, e) + + host.value["status"] = "ALIVE" + host.value["last_heartbeat"] = datetime.utcnow().isoformat() + client.put(host.key, json.dumps(host.value)) + logging.info(f"Updated last heartbeat time {host.value['last_heartbeat']}") + + def main(): argparser = argparse.ArgumentParser() argparser.add_argument("hostname", help="Name of this host. e.g /v1/host/1") @@ -228,6 +237,7 @@ def main(): host.value["status"] = "ALIVE" host.value["last_heartbeat"] = datetime.utcnow().isoformat() + client.put(host.key, host.value, value_in_json=True) atexit.register(goodbye, host=host) @@ -244,26 +254,7 @@ def main(): if e_status == "TIMEOUT": logging.info("Timeout") - _vms = filter(lambda v: v.value["hostname"] == args.hostname, client.get_prefix("/v1/vm", value_in_json=True)) - alleged_running_vms = filter(lambda v: v.value["status"] == "RUNNING", _vms) - should_be_running = filter(lambda v: v.value["status"] == "REQUESTED_START", _vms) - for vm in alleged_running_vms: - _vm = get_vm(running_vms, vm.key) - if (_vm and not _vm.vm.is_running()) or _vm is None: - logging.debug(f"{_vm} {vm.key}") - logging.info(f"{vm.key} is not running but is said to be running") - logging.info(f"Updating {vm.key} status to KILLED") - vm.value["status"] = "KILLED" - client.put(vm.key, json.dumps(vm.value)) - - for vm in should_be_running: - vm_path = f"rbd:uservms/{vm.key.split('/')[-1]}" - start_vm(vm_path, e) - - host.value["status"] = "ALIVE" - host.value["last_heartbeat"] = datetime.utcnow().isoformat() - client.put(host.key, json.dumps(host.value)) - logging.info(f"Updated last heartbeat time {host.value['last_heartbeat']}") + maintenence(e, host) continue e_hostname = e.value["hostname"] @@ -285,10 +276,6 @@ def main(): resume_vm(e) elif e_status == "REQUESTED_START": - - # DELETEME: Delete when CEPH integeration is complete - # vm_path = f"{owner_dir}/.vm/{vm_uuid}.raw" - vm_path = f"rbd:uservms/{vm_uuid}" start_vm(vm_path, e)