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

82
main.py
View file

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