[host] main.py refactored from env_vars to config

This commit is contained in:
llnu 2019-12-08 13:41:32 +01:00
parent 537a5b01f1
commit 608d1eb280

View file

@ -8,7 +8,7 @@ from ucloud.common.request import RequestEntry, RequestType
from ucloud.config import (vm_pool, request_pool, from ucloud.config import (vm_pool, request_pool,
etcd_client, running_vms, etcd_client, running_vms,
etcd_wrapper_args, etcd_wrapper_kwargs, etcd_wrapper_args, etcd_wrapper_kwargs,
HostPool, env_vars) HostPool, config)
from .helper import find_free_port from .helper import find_free_port
from . import virtualmachine from . import virtualmachine
@ -18,7 +18,7 @@ from ucloud.host import logger
def update_heartbeat(hostname): def update_heartbeat(hostname):
"""Update Last HeartBeat Time for :param hostname: in etcd""" """Update Last HeartBeat Time for :param hostname: in etcd"""
client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs) client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
host_pool = HostPool(client, env_vars.get('HOST_PREFIX')) host_pool = HostPool(client, config['etcd']['HOST_PREFIX'])
this_host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None) this_host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
while True: while True:
@ -72,7 +72,7 @@ def maintenance(host):
running_vms.remove(_vm) running_vms.remove(_vm)
def check(): def check():
if env_vars.get('STORAGE_BACKEND') == 'filesystem' and not isdir(env_vars.get('VM_DIR')): if config['etcd']['STORAGE_BACKEND'] == 'filesystem' and not isdir(config['etcd']['VM_DIR']):
print("You have set STORAGE_BACKEND to filesystem. So, the vm directory mentioned" print("You have set STORAGE_BACKEND to filesystem. So, the vm directory mentioned"
" in .env file must exists. But, it don't.") " in .env file must exists. But, it don't.")
sys.exit(1) sys.exit(1)
@ -84,7 +84,7 @@ def main(hostname):
heartbeat_updating_process = mp.Process(target=update_heartbeat, args=(hostname,)) heartbeat_updating_process = mp.Process(target=update_heartbeat, args=(hostname,))
host_pool = HostPool(etcd_client, env_vars.get('HOST_PREFIX')) host_pool = HostPool(etcd_client, config['etcd']['HOST_PREFIX'])
host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None) host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
assert host is not None, "No such host with name = {}".format(hostname) assert host is not None, "No such host with name = {}".format(hostname)
@ -106,8 +106,8 @@ def main(hostname):
# beat updating mechanism in separated thread # beat updating mechanism in separated thread
for events_iterator in [ for events_iterator in [
etcd_client.get_prefix(env_vars.get('REQUEST_PREFIX'), value_in_json=True), etcd_client.get_prefix(config['etcd']['REQUEST_PREFIX'], value_in_json=True),
etcd_client.watch_prefix(env_vars.get('REQUEST_PREFIX'), timeout=10, value_in_json=True), etcd_client.watch_prefix(config['etcd']['REQUEST_PREFIX'], timeout=10, value_in_json=True),
]: ]:
for request_event in events_iterator: for request_event in events_iterator:
request_event = RequestEntry(request_event) request_event = RequestEntry(request_event)