pvc/NodeInstance.py

115 lines
3.8 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
2018-05-31 23:45:07 -04:00
self.state = 'stop'
2018-05-31 21:49:23 -04:00
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
2018-05-31 23:41:52 -04:00
def getfreemem(self):
2018-05-31 22:31:20 -04:00
return self.memfree
2018-05-31 23:41:52 -04:00
def getcpuload(self):
2018-05-31 22:31:20 -04:00
return self.cpuload
2018-05-31 23:41:52 -04:00
def getname(self):
2018-05-31 22:31:20 -04:00
return self.name
2018-05-31 23:41:52 -04:00
def getstate(self):
2018-05-31 23:40:21 -04:00
return self.state
2018-05-31 23:01:22 -04:00
# Update value functions
2018-05-31 23:41:52 -04:00
def updatenodelist(self, node_list):
2018-05-31 23:01:22 -04:00
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():
2018-06-01 00:53:53 -04:00
conn = self.setup_local_node()
while True:
if self.name == socket.gethostname():
self.memfree = conn.getFreeMemory()
self.cpuload = os.getloadavg()[0]
try:
self.zk.set(self.zkey + '/memfree', str(self.memfree).encode('ascii'))
self.zk.set(self.zkey + '/cpuload', str(self.cpuload).encode('ascii'))
except:
if self.stop_thread.is_set():
return
print("Free memory: %s | Load: %s" % ( self.memfree, self.cpuload ))
print("Active domains: %s" % self.domainlist)
for x in range(0,100):
time.sleep(0.1)
if self.stop_thread.is_set():
return
2018-06-01 00:51: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))
@zk.DataWatch(self.zkey + '/cpucount')
def watch_cpucount(data, stat):
self.cpucount = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.cpucount))
@zk.DataWatch(self.zkey + '/cpuload')
def watch_cpuload(data, stat):
self.cpuload = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.cpuload))
@zk.DataWatch(self.zkey + '/memfree')
def watch_memfree(data, stat):
self.memfree = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.memfree))
2018-05-31 21:49:23 -04:00
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)
2018-05-31 23:45:07 -04:00
2018-05-31 21:49:23 -04:00
# Gather data about hypervisor
self.name = conn.getHostname()
self.cpucount = conn.getCPUMap()[0]
2018-05-31 23:48:12 -04:00
self.state = 'start'
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)
2018-06-01 00:53:53 -04:00
return conn