TIMEOUT event handling shifted to function ;maintenence'

This commit is contained in:
ahmadbilalkhalid 2019-07-23 10:25:50 +05:00
parent 6b14a3462e
commit 5429f2d168

63
main.py
View file

@ -94,25 +94,11 @@ def create_vm(vm_uuid, e):
_command_to_create = f"rbd clone images/{image_uuid}@protected uservms/{vm_uuid}" _command_to_create = f"rbd clone images/{image_uuid}@protected uservms/{vm_uuid}"
subprocess.call(_command_to_create.split(" ")) 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" e.value["status"] = "REQUESTED_START"
client.put(e.key, json.dumps(e.value)) client.put(e.key, json.dumps(e.value))
def start_vm(vm_path, e): 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"): 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"Image file of vm {e.key} does not exists")
logging.info(f"Setting vm {e.key} status to DELETED") 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) 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(): def main():
argparser = argparse.ArgumentParser() argparser = argparse.ArgumentParser()
argparser.add_argument("hostname", help="Name of this host. e.g /v1/host/1") 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["status"] = "ALIVE"
host.value["last_heartbeat"] = datetime.utcnow().isoformat() host.value["last_heartbeat"] = datetime.utcnow().isoformat()
client.put(host.key, host.value, value_in_json=True)
atexit.register(goodbye, host=host) atexit.register(goodbye, host=host)
@ -244,26 +254,7 @@ def main():
if e_status == "TIMEOUT": if e_status == "TIMEOUT":
logging.info("Timeout") logging.info("Timeout")
_vms = filter(lambda v: v.value["hostname"] == args.hostname, client.get_prefix("/v1/vm", value_in_json=True)) maintenence(e, host)
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']}")
continue continue
e_hostname = e.value["hostname"] e_hostname = e.value["hostname"]
@ -285,10 +276,6 @@ def main():
resume_vm(e) resume_vm(e)
elif e_status == "REQUESTED_START": 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}" vm_path = f"rbd:uservms/{vm_uuid}"
start_vm(vm_path, e) start_vm(vm_path, e)