2019-06-25 11:39:29 +00:00
|
|
|
# TODO
|
2019-07-25 10:29:35 +00:00
|
|
|
# 1. send an email to an email address defined by env['admin-email']
|
2019-06-30 16:30:17 +00:00
|
|
|
# if resources are finished
|
2019-07-25 10:29:35 +00:00
|
|
|
# 2. v3) Introduce a status endpoint of the scheduler -
|
2019-06-30 16:30:17 +00:00
|
|
|
# maybe expose a prometheus compatible output
|
2019-06-25 11:39:29 +00:00
|
|
|
|
2019-06-30 16:30:17 +00:00
|
|
|
import argparse
|
2019-07-18 13:46:49 +00:00
|
|
|
import logging
|
2019-06-25 11:39:29 +00:00
|
|
|
|
|
|
|
from decouple import config
|
2019-08-12 12:38:34 +00:00
|
|
|
|
2019-09-03 16:06:41 +00:00
|
|
|
from config import etcd_client as client
|
2019-07-27 08:57:51 +00:00
|
|
|
|
2019-08-12 12:38:34 +00:00
|
|
|
from ucloud_common.vm import VmPool
|
|
|
|
from ucloud_common.host import HostPool
|
|
|
|
from ucloud_common.request import RequestEntry, RequestPool, RequestType
|
|
|
|
from helper import (get_suitable_host, dead_host_mitigation, dead_host_detection,
|
|
|
|
assign_host)
|
2019-07-18 13:46:49 +00:00
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.DEBUG,
|
|
|
|
filename="log.txt",
|
|
|
|
filemode="a",
|
|
|
|
format="%(asctime)s: %(levelname)s - %(message)s",
|
|
|
|
datefmt="%d-%b-%y %H:%M:%S",
|
|
|
|
)
|
2019-06-25 11:39:29 +00:00
|
|
|
|
|
|
|
|
2019-08-12 12:38:34 +00:00
|
|
|
def main(vm_prefix, host_prefix, request_prefix):
|
2019-07-27 08:57:51 +00:00
|
|
|
logging.info(f"{'*' * 5} SESSION STARTED {'*' * 5}")
|
2019-09-03 16:06:41 +00:00
|
|
|
|
2019-08-12 12:38:34 +00:00
|
|
|
vm_pool = VmPool(client, vm_prefix)
|
|
|
|
host_pool = HostPool(client, host_prefix)
|
|
|
|
request_pool = RequestPool(client, request_prefix)
|
2019-07-27 08:57:51 +00:00
|
|
|
|
|
|
|
PENDING_VMS = []
|
2019-07-25 08:45:34 +00:00
|
|
|
for events_iterator in [
|
2019-08-12 12:38:34 +00:00
|
|
|
client.get_prefix(request_prefix, value_in_json=True),
|
|
|
|
client.watch_prefix(request_prefix, timeout=5, value_in_json=True),
|
2019-07-25 08:45:34 +00:00
|
|
|
]:
|
2019-07-20 09:50:08 +00:00
|
|
|
for e in events_iterator:
|
2019-08-12 12:38:34 +00:00
|
|
|
if not e.value:
|
|
|
|
continue
|
|
|
|
e = RequestEntry(e)
|
2019-07-27 08:57:51 +00:00
|
|
|
logging.debug(f"{e.key}, {e.value}")
|
|
|
|
|
|
|
|
# Never Run time critical mechanism inside timeout
|
|
|
|
# mechanism because timeout mechanism only comes
|
|
|
|
# when no other event is happening. It means under
|
2019-08-12 12:38:34 +00:00
|
|
|
# heavy load there would not be a timeout<
|
|
|
|
if e.type == "TIMEOUT":
|
|
|
|
logging.debug("TIMEOUT event occured")
|
2019-07-27 08:57:51 +00:00
|
|
|
dead_hosts = dead_host_detection()
|
|
|
|
logging.debug(f"Dead hosts: {dead_hosts}")
|
|
|
|
dead_host_mitigation(dead_hosts)
|
2019-08-12 12:38:34 +00:00
|
|
|
#
|
|
|
|
# vm_scheduled = []
|
|
|
|
# for vm in PENDING_VMS:
|
|
|
|
# if assign_host(vm) is not None:
|
|
|
|
# vm_scheduled.append(vm)
|
|
|
|
#
|
|
|
|
# for vm in vm_scheduled:
|
|
|
|
# PENDING_VMS.remove(vm)
|
|
|
|
# logging.debug(f"Remaining Pending: {PENDING_VMS}")
|
|
|
|
|
|
|
|
elif e.type == RequestType.ScheduleVM:
|
|
|
|
if hasattr(e, "migration") and e.migration and\
|
|
|
|
hasattr(e, "destination") and e.destination:
|
|
|
|
client.client.delete(e.key)
|
|
|
|
vm = vm_pool.get(e.uuid)
|
|
|
|
host = get_suitable_host(vm.specs, [host_pool.get(e.destination)])
|
|
|
|
if host:
|
|
|
|
r = RequestEntry.from_scratch(type=RequestType.InitVMMigration,
|
|
|
|
uuid=e.uuid, destination=e.destination)
|
|
|
|
request_pool.put(r)
|
|
|
|
print(host, e)
|
|
|
|
else:
|
|
|
|
logging.info("Requested destination host doesn't have enough capacity"
|
|
|
|
f"to hold {vm.uuid}")
|
|
|
|
else:
|
|
|
|
client.client.delete(e.key)
|
|
|
|
vm = vm_pool.get(e.uuid)
|
|
|
|
if assign_host(vm) is None:
|
|
|
|
vm.log.append("Can't schedule VM. No Resource Left.")
|
|
|
|
vm_pool.put(vm)
|
|
|
|
|
|
|
|
PENDING_VMS.append(vm)
|
|
|
|
logging.info("No Resource Left. Emailing admin....")
|
|
|
|
logging.debug(f"Pending VMS: {PENDING_VMS}")
|
2019-06-30 16:30:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
argparser = argparse.ArgumentParser()
|
|
|
|
argparser.add_argument(
|
|
|
|
"--vm_prefix", required=False, default=config("VM_PREFIX")
|
|
|
|
)
|
|
|
|
argparser.add_argument(
|
|
|
|
"--host_prefix", required=False, default=config("HOST_PREFIX")
|
|
|
|
)
|
2019-08-12 12:38:34 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
"--request_prefix", required=False, default=config("REQUEST_PREFIX")
|
|
|
|
)
|
2019-06-30 16:30:17 +00:00
|
|
|
args = argparser.parse_args()
|
|
|
|
|
2019-08-12 12:38:34 +00:00
|
|
|
main(args.vm_prefix, args.host_prefix, args.request_prefix)
|