More significant work
This commit is contained in:
parent
92ddec311b
commit
26a460c438
|
@ -3,17 +3,46 @@
|
||||||
import os, socket, time, uuid, threading, libvirt, kazoo.client
|
import os, socket, time, uuid, threading, libvirt, kazoo.client
|
||||||
|
|
||||||
class NodeInstance(threading.Thread):
|
class NodeInstance(threading.Thread):
|
||||||
def __init__(self, name, zk):
|
def __init__(self, name, node_list, zk):
|
||||||
super(NodeInstance, self).__init__()
|
super(NodeInstance, self).__init__()
|
||||||
# Passed-in variables on creation
|
# Passed-in variables on creation
|
||||||
self.zkey = '/nodes/%s' % name
|
self.zkey = '/nodes/%s' % name
|
||||||
self.zk = zk
|
self.zk = zk
|
||||||
self.name = name
|
self.name = name
|
||||||
self.stop_thread = threading.Event()
|
self.stop_thread = threading.Event()
|
||||||
|
self.node_list = node_list
|
||||||
|
self.domainlist = []
|
||||||
|
|
||||||
|
# Get value functions
|
||||||
|
def getfreemem():
|
||||||
|
return self.memfree
|
||||||
|
|
||||||
|
def getcpuload():
|
||||||
|
return self.cpuload
|
||||||
|
|
||||||
|
def getname():
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
# Shutdown the thread
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.stop_thread.set()
|
self.stop_thread.set()
|
||||||
|
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
self.zk.set('/domains/' + domain + '/state', b'migrate')
|
||||||
|
self.zk.set('/domains/' + domain + '/hypervisor', least_host.encode('ascii'))
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.name == socket.gethostname():
|
if self.name == socket.gethostname():
|
||||||
self.setup_local_node()
|
self.setup_local_node()
|
||||||
|
@ -42,10 +71,18 @@ class NodeInstance(threading.Thread):
|
||||||
self.zk.set(self.zkey + '/memfree', str(self.memfree).encode('ascii'))
|
self.zk.set(self.zkey + '/memfree', str(self.memfree).encode('ascii'))
|
||||||
self.zk.set(self.zkey + '/cpuload', str(self.cpuload).encode('ascii'))
|
self.zk.set(self.zkey + '/cpuload', str(self.cpuload).encode('ascii'))
|
||||||
print("Free memory: %s | Load: %s" % ( self.memfree, self.cpuload ))
|
print("Free memory: %s | Load: %s" % ( self.memfree, self.cpuload ))
|
||||||
time.sleep(1)
|
print("Active domains: %s" % self.domainlist)
|
||||||
if self.stop_thread.is_set():
|
for x in range(0,50):
|
||||||
break
|
time.sleep(0.1)
|
||||||
|
if self.stop_thread.is_set():
|
||||||
|
return
|
||||||
|
|
||||||
|
@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()
|
||||||
|
|
||||||
def setup_remote_node(self):
|
def setup_remote_node(self):
|
||||||
@zk.DataWatch(self.zkey + '/state')
|
@zk.DataWatch(self.zkey + '/state')
|
||||||
|
@ -54,17 +91,17 @@ class NodeInstance(threading.Thread):
|
||||||
print("Version: %s, data: %s" % (stat.version, self.state))
|
print("Version: %s, data: %s" % (stat.version, self.state))
|
||||||
|
|
||||||
@zk.DataWatch(self.zkey + '/cpucount')
|
@zk.DataWatch(self.zkey + '/cpucount')
|
||||||
def watch_state(data, stat):
|
def watch_cpucount(data, stat):
|
||||||
self.cpucount = data.decode('ascii')
|
self.cpucount = data.decode('ascii')
|
||||||
print("Version: %s, data: %s" % (stat.version, self.cpucount))
|
print("Version: %s, data: %s" % (stat.version, self.cpucount))
|
||||||
|
|
||||||
@zk.DataWatch(self.zkey + '/cpuload')
|
@zk.DataWatch(self.zkey + '/cpuload')
|
||||||
def watch_state(data, stat):
|
def watch_cpuload(data, stat):
|
||||||
self.cpuload = data.decode('ascii')
|
self.cpuload = data.decode('ascii')
|
||||||
print("Version: %s, data: %s" % (stat.version, self.cpuload))
|
print("Version: %s, data: %s" % (stat.version, self.cpuload))
|
||||||
|
|
||||||
@zk.DataWatch(self.zkey + '/memfree')
|
@zk.DataWatch(self.zkey + '/memfree')
|
||||||
def watch_state(data, stat):
|
def watch_memfree(data, stat):
|
||||||
self.memfree = data.decode('ascii')
|
self.memfree = data.decode('ascii')
|
||||||
print("Version: %s, data: %s" % (stat.version, self.memfree))
|
print("Version: %s, data: %s" % (stat.version, self.memfree))
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os, time, uuid, libvirt, kazoo.client
|
import os, time, uuid, threading, libvirt, kazoo.client
|
||||||
|
|
||||||
class VMInstance:
|
class VMInstance:
|
||||||
def __init__(self, domuuid, zk, thishypervisor):
|
def __init__(self, domuuid, zk, thishypervisor):
|
||||||
|
@ -36,17 +36,20 @@ class VMInstance:
|
||||||
if dom == None:
|
if dom == None:
|
||||||
print('Failed to create a domain from an XML definition.')
|
print('Failed to create a domain from an XML definition.')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
self.thishypervisor.domainlist.append(self.domuuid)
|
||||||
return dom
|
return dom
|
||||||
|
|
||||||
# Stop the VM forcibly
|
# Stop the VM forcibly
|
||||||
def stop_vm(self):
|
def stop_vm(self):
|
||||||
print("Forcibly stopping VM %s" % self.domuuid)
|
print("Forcibly stopping VM %s" % self.domuuid)
|
||||||
self.dom.destroy()
|
self.dom.destroy()
|
||||||
|
self.thishypervisor.domainlist.remove(self.domuuid)
|
||||||
|
|
||||||
# Shutdown the VM gracefully
|
# Shutdown the VM gracefully
|
||||||
def shutdown_vm(self):
|
def shutdown_vm(self):
|
||||||
print("Stopping VM %s" % self.domuuid)
|
print("Stopping VM %s" % self.domuuid)
|
||||||
self.dom.shutdown()
|
self.dom.shutdown()
|
||||||
|
self.thishypervisor.domainlist.remove(self.domuuid)
|
||||||
|
|
||||||
# Migrate the VM to a target host
|
# Migrate the VM to a target host
|
||||||
def migrate_vm(self):
|
def migrate_vm(self):
|
||||||
|
@ -61,6 +64,7 @@ class VMInstance:
|
||||||
print('Could not migrate to the new domain')
|
print('Could not migrate to the new domain')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
self.thishypervisor.domainlist.remove(self.domuuid)
|
||||||
print('Migrated successfully')
|
print('Migrated successfully')
|
||||||
dest_conn.close()
|
dest_conn.close()
|
||||||
|
|
||||||
|
@ -71,6 +75,7 @@ class VMInstance:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
self.zk.set(self.zkey + '/status', b'start')
|
self.zk.set(self.zkey + '/status', b'start')
|
||||||
|
self.thishypervisor.domainlist.append(self.domuuid)
|
||||||
break
|
break
|
||||||
#
|
#
|
||||||
# Main function to manage a VM (taking only self)
|
# Main function to manage a VM (taking only self)
|
||||||
|
@ -90,23 +95,26 @@ class VMInstance:
|
||||||
except:
|
except:
|
||||||
running = False
|
running = False
|
||||||
|
|
||||||
if running != False and self.state == "stop" and self.hypervisor == self.thishypervisor:
|
if running != False and self.state == "stop" and self.hypervisor == self.thishypervisor.name:
|
||||||
self.stop_vm()
|
self.stop_vm()
|
||||||
|
|
||||||
if running != False and self.state == "shutdown" and self.hypervisor == self.thishypervisor:
|
if running != False and self.state == "shutdown" and self.hypervisor == self.thishypervisor.name:
|
||||||
self.shutdown_vm()
|
self.shutdown_vm()
|
||||||
|
|
||||||
elif running == False and self.state == "migrate" and self.hypervisr == self.thishypervisor:
|
elif running == False and self.state == "migrate" and self.hypervisr == self.thishypervisor.name:
|
||||||
self.receive_migrate()
|
self.receive_migrate()
|
||||||
|
|
||||||
elif running != False and self.state == "migrate" and self.hypervisr != self.thishypervisor:
|
elif running != False and self.state == "migrate" and self.hypervisr != self.thishypervisor.name:
|
||||||
self.migrate_vm()
|
self.migrate_vm()
|
||||||
|
|
||||||
elif running == False and self.state == "start" and self.hypervisor == self.thishypervisor:
|
elif running == False and self.state == "start" and self.hypervisor == self.thishypervisor.name:
|
||||||
# Grab the domain information from Zookeeper
|
# Grab the domain information from Zookeeper
|
||||||
domxml, domxmlstat = self.zk.get(self.zkey + '/xml')
|
domxml, domxmlstat = self.zk.get(self.zkey + '/xml')
|
||||||
domxml = str(domxml.decode('ascii'))
|
domxml = str(domxml.decode('ascii'))
|
||||||
self.dom = self.start_vm(conn, domxml)
|
self.dom = self.start_vm(conn, domxml)
|
||||||
|
|
||||||
|
elif running != False and self.state == "start" and self.hypervisor == self.thishypervisor.name:
|
||||||
|
self.thishypervisor.domainlist.append(self.domuuid)
|
||||||
|
|
||||||
# The VM should now be running so return the domain and active connection
|
# The VM should now be running so return the domain and active connection
|
||||||
conn.close
|
conn.close
|
||||||
|
|
22
pvcd.py
22
pvcd.py
|
@ -28,19 +28,22 @@ except:
|
||||||
|
|
||||||
def zk_listener(state):
|
def zk_listener(state):
|
||||||
if state == KazooState.LOST:
|
if state == KazooState.LOST:
|
||||||
# Register somewhere that the session was lost
|
cleanup()
|
||||||
pass
|
exit(2)
|
||||||
elif state == KazooState.SUSPENDED:
|
elif state == KazooState.SUSPENDED:
|
||||||
# Handle being disconnected from Zookeeper
|
cleanup()
|
||||||
pass
|
exit(2)
|
||||||
else:
|
else:
|
||||||
# Handle being connected/reconnected to Zookeeper
|
# Handle being connected/reconnected to Zookeeper
|
||||||
pass
|
pass
|
||||||
|
|
||||||
zk.add_listener(zk_listener)
|
zk.add_listener(zk_listener)
|
||||||
|
|
||||||
|
myhostname = socket.gethostname()
|
||||||
|
|
||||||
def cleanup():
|
def cleanup():
|
||||||
t_node[socket.gethostname()].stop()
|
for node in node_list:
|
||||||
|
t_node[node].stop()
|
||||||
zk.stop()
|
zk.stop()
|
||||||
|
|
||||||
atexit.register(cleanup)
|
atexit.register(cleanup)
|
||||||
|
@ -55,16 +58,17 @@ t_node = dict()
|
||||||
s_domain = dict()
|
s_domain = dict()
|
||||||
|
|
||||||
for node in node_list:
|
for node in node_list:
|
||||||
t_node[node] = NodeInstance.NodeInstance(node, zk);
|
t_node[node] = NodeInstance.NodeInstance(node, node_list, zk);
|
||||||
t_node[node].start()
|
if t_node[node].name == myhostname:
|
||||||
|
t_node[node].start()
|
||||||
|
|
||||||
for domain in domain_list:
|
for domain in domain_list:
|
||||||
s_domain[domain] = VMInstance.VMInstance(domain, zk, socket.gethostname());
|
s_domain[domain] = VMInstance.VMInstance(domain, zk, t_node[myhostname]);
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Tick loop
|
# Tick loop
|
||||||
try:
|
try:
|
||||||
time.sleep(1)
|
time.sleep(0.1)
|
||||||
except:
|
except:
|
||||||
cleanup()
|
cleanup()
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
Loading…
Reference in New Issue