pvc/NodeInstance.py

113 lines
3.9 KiB
Python
Raw Normal View History

2018-05-31 21:49:23 -04:00
#!/usr/bin/env python3
import os, socket, time, uuid, threading, libvirt, kazoo.client
class NodeInstance(threading.Thread):
2018-05-31 22:31:20 -04:00
def __init__(self, name, node_list, zk):
2018-05-31 21:49:23 -04:00
super(NodeInstance, self).__init__()
# Passed-in variables on creation
self.zkey = '/nodes/%s' % name
self.zk = zk
self.name = name
self.stop_thread = threading.Event()
2018-05-31 22:31:20 -04:00
self.node_list = node_list
self.domainlist = []
2018-05-31 21:49:23 -04:00
2018-05-31 22:31:20 -04:00
# Get value functions
def getfreemem():
return self.memfree
def getcpuload():
return self.cpuload
def getname():
return self.name
2018-05-31 23:01:22 -04:00
# Update value functions
def updatenodelist(node_list):
self.node_list = node_list
2018-05-31 22:31:20 -04:00
# Shutdown the thread
2018-05-31 21:49:23 -04:00
def stop(self):
self.stop_thread.set()
2018-05-31 22:31:20 -04:00
# Flush all VMs on the host
def flush(self):
for domain in self.domainlist:
# Determine the best target hypervisor
least_mem = (2^64)/8
least_load = 999.0
least_host = ""
for node in node_list:
node_freemem = node.getfreemem()
if node_freemem < least_mem:
least_mem = node_freemem
least_host = node.getname()
2018-05-31 22:55:44 -04:00
self.zk.set('/domains/' + domain + '/state', 'flush'.encode('ascii'))
2018-05-31 22:31:20 -04:00
self.zk.set('/domains/' + domain + '/hypervisor', least_host.encode('ascii'))
2018-05-31 21:49:23 -04:00
def run(self):
if self.name == socket.gethostname():
self.setup_local_node()
else:
self.setup_remote_node()
def setup_local_node(self):
# Connect to libvirt
libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name)
if conn == None:
print('Failed to open connection to %s' % libvirt_name)
exit(1)
# Gather data about hypervisor
self.name = conn.getHostname()
self.cpucount = conn.getCPUMap()[0]
2018-05-31 22:55:44 -04:00
self.zk.set(self.zkey + '/state', 'start'.encode('ascii'))
2018-05-31 21:49:23 -04:00
self.zk.set(self.zkey + '/cpucount', str(self.cpucount).encode('ascii'))
print("Node hostname: %s" % self.name)
print("CPUs: %s" % self.cpucount)
while True:
self.memfree = conn.getFreeMemory()
self.cpuload = os.getloadavg()[0]
self.zk.set(self.zkey + '/memfree', str(self.memfree).encode('ascii'))
self.zk.set(self.zkey + '/cpuload', str(self.cpuload).encode('ascii'))
print("Free memory: %s | Load: %s" % ( self.memfree, self.cpuload ))
2018-05-31 23:01:22 -04:00
print("Node list: %s" % self.node_list)
2018-05-31 22:31:20 -04:00
print("Active domains: %s" % self.domainlist)
for x in range(0,50):
time.sleep(0.1)
if self.stop_thread.is_set():
return
2018-05-31 21:49:23 -04:00
2018-05-31 22:31:20 -04:00
@zk.DataWatch(self.zkey + '/state')
def watch_state(data, stat):
self.state = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.state))
if self.state == 'flush':
self.flush()
2018-05-31 21:49:23 -04:00
def setup_remote_node(self):
@zk.DataWatch(self.zkey + '/state')
def watch_state(data, stat):
self.state = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.state))
@zk.DataWatch(self.zkey + '/cpucount')
2018-05-31 22:31:20 -04:00
def watch_cpucount(data, stat):
2018-05-31 21:49:23 -04:00
self.cpucount = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.cpucount))
@zk.DataWatch(self.zkey + '/cpuload')
2018-05-31 22:31:20 -04:00
def watch_cpuload(data, stat):
2018-05-31 21:49:23 -04:00
self.cpuload = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.cpuload))
@zk.DataWatch(self.zkey + '/memfree')
2018-05-31 22:31:20 -04:00
def watch_memfree(data, stat):
2018-05-31 21:49:23 -04:00
self.memfree = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.memfree))