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
|
2020-01-03 13:38:59 +00:00
|
|
|
|
2019-12-31 13:06:51 +00:00
|
|
|
from uuid import uuid4
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-31 10:30:02 +00:00
|
|
|
from uncloud.common.request import RequestEntry, RequestType
|
2020-01-08 19:40:05 +00:00
|
|
|
from uncloud.common.shared import shared
|
2020-01-06 07:25:59 +00:00
|
|
|
from uncloud.common.settings import settings
|
2019-12-31 10:30:02 +00:00
|
|
|
from uncloud.common.vm import VMStatus
|
|
|
|
from uncloud.vmm import VMM
|
2019-12-28 10:39:11 +00:00
|
|
|
from os.path import join as join_path
|
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
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
arg_parser = argparse.ArgumentParser('host', add_help=False)
|
|
|
|
arg_parser.add_argument('--hostname', required=True)
|
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
|
|
|
|
def update_heartbeat(hostname):
|
|
|
|
"""Update Last HeartBeat Time for :param hostname: in etcd"""
|
2019-12-23 07:58:04 +00:00
|
|
|
host_pool = shared.host_pool
|
2019-12-30 09:35:07 +00:00
|
|
|
this_host = next(
|
|
|
|
filter(lambda h: h.hostname == hostname, host_pool.hosts), None
|
|
|
|
)
|
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-12-30 10:18:25 +00:00
|
|
|
time.sleep(10)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-12-30 09:35:07 +00:00
|
|
|
def maintenance(host):
|
2019-12-28 10:39:11 +00:00
|
|
|
vmm = VMM()
|
|
|
|
running_vms = vmm.discover()
|
|
|
|
for vm_uuid in running_vms:
|
2020-01-05 15:20:00 +00:00
|
|
|
if vmm.is_running(vm_uuid) and vmm.get_status(vm_uuid) == 'running':
|
2019-12-30 15:05:12 +00:00
|
|
|
logger.debug('VM {} is running on {}'.format(vm_uuid, host))
|
2019-12-30 09:35:07 +00:00
|
|
|
vm = shared.vm_pool.get(
|
2020-01-05 15:20:00 +00:00
|
|
|
join_path(settings['etcd']['vm_prefix'], vm_uuid)
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
vm.status = VMStatus.running
|
2019-12-30 09:35:07 +00:00
|
|
|
vm.vnc_socket = vmm.get_vnc(vm_uuid)
|
|
|
|
vm.hostname = host
|
2019-12-28 10:39:11 +00:00
|
|
|
shared.vm_pool.put(vm)
|
|
|
|
|
|
|
|
|
2020-01-10 10:45:48 +00:00
|
|
|
def main(arguments):
|
|
|
|
hostname = arguments['hostname']
|
2019-12-23 07:58:04 +00:00
|
|
|
host_pool = shared.host_pool
|
2019-12-30 15:05:12 +00:00
|
|
|
host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
|
2019-12-31 13:06:51 +00:00
|
|
|
|
|
|
|
# Does not yet exist, create it
|
|
|
|
if not host:
|
|
|
|
host_key = join_path(
|
2020-01-05 15:20:00 +00:00
|
|
|
settings['etcd']['host_prefix'], uuid4().hex
|
2019-12-31 13:06:51 +00:00
|
|
|
)
|
|
|
|
host_entry = {
|
2020-01-05 15:20:00 +00:00
|
|
|
'specs': '',
|
|
|
|
'hostname': hostname,
|
|
|
|
'status': 'DEAD',
|
|
|
|
'last_heartbeat': '',
|
2019-12-31 13:06:51 +00:00
|
|
|
}
|
|
|
|
shared.etcd_client.put(
|
|
|
|
host_key, host_entry, value_in_json=True
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-31 13:13:08 +00:00
|
|
|
# update, get ourselves now for sure
|
|
|
|
host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
|
|
|
|
|
2019-11-02 15:42:24 +00:00
|
|
|
try:
|
2019-12-30 15:05:12 +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:
|
2020-01-05 15:20:00 +00:00
|
|
|
raise Exception('uncloud-host heartbeat updating mechanism is not working') from e
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-08 19:40:05 +00:00
|
|
|
# The below while True is neccessary for gracefully handling leadership transfer and temporary
|
|
|
|
# unavailability in etcd. Why does it work? It works because the get_prefix,watch_prefix return
|
|
|
|
# iter([]) that is iterator of empty list on exception (that occur due to above mentioned reasons)
|
|
|
|
# which ends the loop immediately. So, having it inside infinite loop we try again and again to
|
|
|
|
# get prefix until either success or deamon death comes.
|
|
|
|
while True:
|
|
|
|
for events_iterator in [
|
|
|
|
shared.etcd_client.get_prefix(settings['etcd']['request_prefix'], value_in_json=True,
|
|
|
|
raise_exception=False),
|
|
|
|
shared.etcd_client.watch_prefix(settings['etcd']['request_prefix'], value_in_json=True,
|
|
|
|
raise_exception=False)
|
|
|
|
]:
|
|
|
|
for request_event in events_iterator:
|
|
|
|
request_event = RequestEntry(request_event)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-30 09:35:07 +00:00
|
|
|
maintenance(host.key)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-08 19:40:05 +00:00
|
|
|
if request_event.hostname == host.key:
|
|
|
|
logger.debug('VM Request: %s on Host %s', request_event, host.hostname)
|
|
|
|
|
|
|
|
shared.request_pool.client.client.delete(request_event.key)
|
|
|
|
vm_entry = shared.etcd_client.get(
|
|
|
|
join_path(settings['etcd']['vm_prefix'], request_event.uuid)
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug('VM hostname: {}'.format(vm_entry.value))
|
|
|
|
|
|
|
|
vm = virtualmachine.VM(vm_entry)
|
|
|
|
if request_event.type == RequestType.StartVM:
|
|
|
|
vm.start()
|
|
|
|
|
|
|
|
elif request_event.type == RequestType.StopVM:
|
|
|
|
vm.stop()
|
|
|
|
|
|
|
|
elif request_event.type == RequestType.DeleteVM:
|
|
|
|
vm.delete()
|
|
|
|
|
|
|
|
elif request_event.type == RequestType.InitVMMigration:
|
|
|
|
vm.start(destination_host_key=host.key)
|
|
|
|
|
|
|
|
elif request_event.type == RequestType.TransferVM:
|
|
|
|
destination_host = host_pool.get(request_event.destination_host_key)
|
|
|
|
if destination_host:
|
|
|
|
vm.migrate(
|
|
|
|
destination_host=destination_host.hostname,
|
|
|
|
destination_sock_path=request_event.destination_sock_path,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.error('Host %s not found!', request_event.destination_host_key)
|