set (at exit) hostname = '' of vms running on this host + hints of migration implementation

This commit is contained in:
ahmadbilalkhalid 2019-07-25 12:59:54 +05:00
parent 5429f2d168
commit 0ee9418fb9
1 changed files with 59 additions and 23 deletions

82
main.py
View File

@ -60,6 +60,13 @@ def goodbye(host):
host.value["status"] = "DEAD"
host.value["last_heartbeat"] = datetime.utcnow().isoformat()
client.put(host.key, json.dumps(host.value))
vms = client.get_prefix("/v1/vm", value_in_json=True)
vms = filter(lambda v: v.value["hostname"] == host.key, vms)
for vm in vms:
vm.value["hostname"] = ""
client.put(vm.key, vm.value, value_in_json=True)
logging.info(f"Host {host.key} dead! at {host.value['last_heartbeat']}")
os.kill(os.getpid(), signal.SIGKILL)
@ -203,9 +210,18 @@ def get_vm(vm_list: list, vm_key) -> Union[VM, None]:
def maintenence(e, host):
# VMs on this 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)
# TODO: Delete this. This was intended to start VMs that
# requested to be started when ucloud-vm is not running.
# This is no longer needed as we check for pending requests
# at the start and handle them.
# 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:
@ -215,10 +231,19 @@ def maintenence(e, host):
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)
# TODO: Delete this. This was intended to start VMs that
# requested to be started when ucloud-vm is not running.
# This is no longer needed as we check for pending requests
# at the start and handle them.
# for vm in should_be_running:
# vm_path = f"rbd:uservms/{vm.key.split('/')[-1]}"
# start_vm(vm_path, e)
# TODO: Check whether a vm is running on this host that
# is not supposed to be running on this host
host.value["status"] = "ALIVE"
host.value["last_heartbeat"] = datetime.utcnow().isoformat()
client.put(host.key, json.dumps(host.value))
@ -244,6 +269,9 @@ def main():
for events_iterator in [client.get_prefix("/v1/vm/"),
client.watch_prefix("/v1/vm/", timeout=10)]:
for e in events_iterator:
# TODO: Should we disable timeout alarm inside
# event handling code and enable it while going outside
try:
e.value = json.loads(e.value)
except json.JSONDecodeError:
@ -258,34 +286,42 @@ def main():
continue
e_hostname = e.value["hostname"]
if hasattr(e.value, "migration_destination"):
e_migration_destination = e.value["migration_destination"]
else:
e_migration_destination = ""
vm_uuid = e.key.split("/")[-1]
# If it is not for me then skip it
if e_hostname != args.hostname:
continue
# If the event is directed toward me or
# I am destination of a REQUESTED_MIGRATION
if e_hostname == host.key or\
e_migration_destination == host.key:
logging.debug(f"EVENT: {e}")
logging.debug(f"EVENT: {e}")
if e_status == "SCHEDULED_DEPLOY":
create_vm(vm_uuid, e)
if e_status == "SCHEDULED_DEPLOY":
create_vm(vm_uuid, e)
elif e_status == "REQUESTED_SUSPEND":
suspend_vm(e)
elif e_status == "REQUESTED_SUSPEND":
suspend_vm(e)
elif e_status == "REQUESTED_RESUME":
resume_vm(e)
elif e_status == "REQUESTED_RESUME":
resume_vm(e)
elif e_status == "REQUESTED_START":
vm_path = f"rbd:uservms/{vm_uuid}"
start_vm(vm_path, e)
elif e_status == "REQUESTED_START":
vm_path = f"rbd:uservms/{vm_uuid}"
start_vm(vm_path, e)
elif e_status == "REQUESTED_SHUTDOWN":
shutdown_vm(e)
elif e_status == "REQUESTED_SHUTDOWN":
shutdown_vm(e)
elif e_status == "DELETED":
delete_vm(e)
elif e_status == "DELETED":
delete_vm(e)
logging.info(f"Running VMs {running_vms}")
# elif e_status == "REQUESTED_MIGRATION":
# if e.value["migration_destination"]
logging.info(f"Running VMs {running_vms}")
main()