2019-10-25 06:42:40 +00:00
|
|
|
import argparse
|
2019-11-02 15:42:24 +00:00
|
|
|
import multiprocessing as mp
|
2019-11-18 17:39:57 +00:00
|
|
|
import time
|
2019-12-14 15:23:31 +00:00
|
|
|
import sys
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-03 10:40:41 +00:00
|
|
|
from ucloud.common.request import RequestEntry, RequestType
|
2019-12-22 07:26:48 +00:00
|
|
|
from ucloud.common.host import HostPool
|
|
|
|
from ucloud.shared import shared
|
|
|
|
from ucloud.settings import settings
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
from . import virtualmachine, logger
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
vmm = virtualmachine.VMM()
|
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
|
|
|
|
def update_heartbeat(hostname):
|
|
|
|
"""Update Last HeartBeat Time for :param hostname: in etcd"""
|
2019-12-21 09:36:55 +00:00
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
host_pool = shared.host_pool
|
2019-11-25 06:52:36 +00:00
|
|
|
this_host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
while True:
|
2019-11-02 15:42:24 +00:00
|
|
|
this_host.update_heartbeat()
|
|
|
|
host_pool.put(this_host)
|
2019-10-25 06:42:40 +00:00
|
|
|
time.sleep(10)
|
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
def main(hostname):
|
2019-12-23 07:58:04 +00:00
|
|
|
host_pool = shared.host_pool
|
2019-11-18 17:39:57 +00:00
|
|
|
host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
|
2019-11-27 14:19:57 +00:00
|
|
|
assert host is not None, "No such host with name = {}".format(hostname)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-11-02 15:42:24 +00:00
|
|
|
try:
|
2019-12-22 07:26:48 +00:00
|
|
|
heartbeat_updating_process = mp.Process(target=update_heartbeat, args=(hostname,))
|
2019-11-02 15:42:24 +00:00
|
|
|
heartbeat_updating_process.start()
|
|
|
|
except Exception as e:
|
2019-11-18 17:39:57 +00:00
|
|
|
logger.exception(e)
|
2019-12-22 07:26:48 +00:00
|
|
|
sys.exit("No Need To Go Further. ucloud-host heartbeat updating mechanism is not working")
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
for events_iterator in [
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.etcd_client.get_prefix(settings['etcd']['request_prefix'], value_in_json=True),
|
|
|
|
shared.etcd_client.watch_prefix(settings['etcd']['request_prefix'], timeout=10, value_in_json=True),
|
2019-10-25 06:42:40 +00:00
|
|
|
]:
|
|
|
|
for request_event in events_iterator:
|
|
|
|
request_event = RequestEntry(request_event)
|
|
|
|
|
|
|
|
if request_event.type == "TIMEOUT":
|
2019-12-22 07:26:48 +00:00
|
|
|
vmm.maintenance(host)
|
2019-10-25 06:42:40 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# If the event is directed toward me OR I am destination of a InitVMMigration
|
2019-11-18 17:39:57 +00:00
|
|
|
if request_event.hostname == host.key or request_event.destination == host.key:
|
2019-11-27 07:12:29 +00:00
|
|
|
logger.debug("VM Request: %s", request_event)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.request_pool.client.client.delete(request_event.key)
|
|
|
|
vm_entry = shared.vm_pool.get(request_event.uuid)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
if vm_entry:
|
|
|
|
if request_event.type == RequestType.StartVM:
|
2019-12-22 07:26:48 +00:00
|
|
|
vmm.start(vm_entry)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
elif request_event.type == RequestType.StopVM:
|
2019-12-22 07:26:48 +00:00
|
|
|
vmm.stop(vm_entry)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
elif request_event.type == RequestType.DeleteVM:
|
2019-12-22 07:26:48 +00:00
|
|
|
vmm.delete(vm_entry)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
elif request_event.type == RequestType.InitVMMigration:
|
2019-12-22 07:26:48 +00:00
|
|
|
vmm.start(vm_entry, host.key)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
elif request_event.type == RequestType.TransferVM:
|
2019-12-22 07:26:48 +00:00
|
|
|
vmm.transfer(request_event)
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2019-11-18 17:39:57 +00:00
|
|
|
logger.info("VM Entry missing")
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
logger.info("Running VMs %s", vmm.running_vms)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
2019-11-02 15:42:24 +00:00
|
|
|
if __name__ == "__main__":
|
2019-11-18 17:39:57 +00:00
|
|
|
argparser = argparse.ArgumentParser()
|
|
|
|
argparser.add_argument("hostname", help="Name of this host. e.g /v1/host/1")
|
|
|
|
args = argparser.parse_args()
|
|
|
|
mp.set_start_method('spawn')
|
|
|
|
main(args.hostname)
|