Some more cleanup and avoid opening libvirt connections until they're really needed
This commit is contained in:
parent
ad4a9d8ea6
commit
8eb91a4ec3
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os, sys, socket, time, threading, libvirt, kazoo.client, pvcdomf
|
import os, sys, socket, time, threading, libvirt, kazoo.client, pvcf
|
||||||
|
|
||||||
class NodeInstance(threading.Thread):
|
class NodeInstance(threading.Thread):
|
||||||
def __init__(self, name, node_list, s_domain, zk):
|
def __init__(self, name, node_list, s_domain, zk):
|
||||||
|
@ -104,7 +104,7 @@ class NodeInstance(threading.Thread):
|
||||||
|
|
||||||
# Remove any non-running VMs from our list
|
# Remove any non-running VMs from our list
|
||||||
for domain in self.domain_list:
|
for domain in self.domain_list:
|
||||||
dom = pvcdomf.lookupByUUID(conn, domain)
|
dom = pvcdomf.lookupByUUID(domain)
|
||||||
if dom == None:
|
if dom == None:
|
||||||
self.domain_list.remove(domain)
|
self.domain_list.remove(domain)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os, sys, socket, time, threading, libvirt, kazoo.client, pvcdomf
|
import os, sys, socket, time, threading, libvirt, kazoo.client, pvcf
|
||||||
|
|
||||||
class VMInstance:
|
class VMInstance:
|
||||||
def __init__(self, domuuid, zk, thishypervisor):
|
def __init__(self, domuuid, zk, thishypervisor):
|
||||||
|
@ -19,15 +19,7 @@ class VMInstance:
|
||||||
self.inmigrate = False
|
self.inmigrate = False
|
||||||
self.inreceive = False
|
self.inreceive = False
|
||||||
|
|
||||||
# Start up a new Libvirt connection
|
self.dom = pvcdomf.lookupByUUID(self.domuuid)
|
||||||
libvirt_name = "qemu:///system"
|
|
||||||
conn = libvirt.open(libvirt_name)
|
|
||||||
if conn == None:
|
|
||||||
print('>>> %s - Failed to open local libvirt connection.' % self.domuuid)
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
self.dom = pvcdomf.lookupByUUID(conn, self.domuuid)
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
# Watch for changes to the hypervisor field in Zookeeper
|
# Watch for changes to the hypervisor field in Zookeeper
|
||||||
@zk.DataWatch(self.zkey + '/hypervisor')
|
@zk.DataWatch(self.zkey + '/hypervisor')
|
||||||
|
@ -51,9 +43,18 @@ class VMInstance:
|
||||||
return self.hypervisor
|
return self.hypervisor
|
||||||
|
|
||||||
# Start up the VM
|
# Start up the VM
|
||||||
def start_vm(self, conn, xmlconfig):
|
def start_vm(self, xmlconfig):
|
||||||
print(">>> %s - Starting VM" % self.domuuid)
|
print(">>> %s - Starting VM" % self.domuuid)
|
||||||
self.instart = True
|
self.instart = True
|
||||||
|
|
||||||
|
# Start up a new Libvirt connection
|
||||||
|
libvirt_name = "qemu:///system"
|
||||||
|
conn = libvirt.open(libvirt_name)
|
||||||
|
if conn == None:
|
||||||
|
print('>>> %s - Failed to open local libvirt connection.' % self.domuuid)
|
||||||
|
self.instart = False
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dom = conn.createXML(xmlconfig, 0)
|
dom = conn.createXML(xmlconfig, 0)
|
||||||
except libvirt.libvirtError as e:
|
except libvirt.libvirtError as e:
|
||||||
|
@ -63,6 +64,7 @@ class VMInstance:
|
||||||
if not self.domuuid in self.thishypervisor.domain_list:
|
if not self.domuuid in self.thishypervisor.domain_list:
|
||||||
self.thishypervisor.domain_list.append(self.domuuid)
|
self.thishypervisor.domain_list.append(self.domuuid)
|
||||||
|
|
||||||
|
conn.close()
|
||||||
self.dom = dom
|
self.dom = dom
|
||||||
self.instart = False
|
self.instart = False
|
||||||
|
|
||||||
|
@ -144,11 +146,11 @@ class VMInstance:
|
||||||
self.inmigrate = False
|
self.inmigrate = False
|
||||||
|
|
||||||
# Receive the migration from another host (wait until VM is running)
|
# Receive the migration from another host (wait until VM is running)
|
||||||
def receive_migrate(self, conn):
|
def receive_migrate(self):
|
||||||
print('>>> %s - Receiving migration' % self.domuuid)
|
print('>>> %s - Receiving migration' % self.domuuid)
|
||||||
self.inreceive = True
|
self.inreceive = True
|
||||||
while True:
|
while True:
|
||||||
self.dom = pvcdomf.lookupByUUID(conn, self.domuuid)
|
self.dom = pvcdomf.lookupByUUID(self.domuuid)
|
||||||
if self.dom == None:
|
if self.dom == None:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
continue
|
continue
|
||||||
|
@ -166,13 +168,6 @@ class VMInstance:
|
||||||
# Main function to manage a VM (taking only self)
|
# Main function to manage a VM (taking only self)
|
||||||
#
|
#
|
||||||
def manage_vm_state(self):
|
def manage_vm_state(self):
|
||||||
# Start up a new Libvirt connection
|
|
||||||
libvirt_name = "qemu:///system"
|
|
||||||
conn = libvirt.open(libvirt_name)
|
|
||||||
if conn == None:
|
|
||||||
print('>>> %s - Failed to open local libvirt connection.' % self.domuuid)
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Check the current state of the VM
|
# Check the current state of the VM
|
||||||
try:
|
try:
|
||||||
if self.dom != None:
|
if self.dom != None:
|
||||||
|
@ -192,7 +187,7 @@ class VMInstance:
|
||||||
|
|
||||||
# VM should be migrated to this hypervisor
|
# VM should be migrated to this hypervisor
|
||||||
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor == self.thishypervisor.name and self.inreceive == False:
|
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor == self.thishypervisor.name and self.inreceive == False:
|
||||||
self.receive_migrate(conn)
|
self.receive_migrate()
|
||||||
|
|
||||||
# VM should be migrated away from this hypervisor
|
# VM should be migrated away from this hypervisor
|
||||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor != self.thishypervisor.name and self.inmigrate == False:
|
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor != self.thishypervisor.name and self.inmigrate == False:
|
||||||
|
@ -208,7 +203,4 @@ class VMInstance:
|
||||||
# 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.start_vm(conn, domxml)
|
self.start_vm(domxml)
|
||||||
|
|
||||||
# The VM should now be running so return the domain and active connection
|
|
||||||
conn.close()
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ import os, sys, libvirt, uuid
|
||||||
# 1. Takes a text UUID and handles converting it to bytes
|
# 1. Takes a text UUID and handles converting it to bytes
|
||||||
# 2. Disables stdout to avoid stupid printouts
|
# 2. Disables stdout to avoid stupid printouts
|
||||||
# 3. Try's it and returns a sensible value if not
|
# 3. Try's it and returns a sensible value if not
|
||||||
def lookupByUUID(conn, tuuid):
|
def lookupByUUID(tuuid):
|
||||||
dom = None
|
dom = None
|
||||||
|
|
||||||
# Convert the text UUID to bytes
|
# Convert the text UUID to bytes
|
||||||
|
@ -22,12 +22,23 @@ def lookupByUUID(conn, tuuid):
|
||||||
|
|
||||||
# Try
|
# Try
|
||||||
try:
|
try:
|
||||||
|
# Open a libvirt connection
|
||||||
|
libvirt_name = "qemu:///system"
|
||||||
|
conn = libvirt.open(libvirt_name)
|
||||||
|
if conn == None:
|
||||||
|
print('>>> %s - Failed to open local libvirt connection.' % self.domuuid)
|
||||||
|
return dom
|
||||||
|
|
||||||
# Lookup the UUID
|
# Lookup the UUID
|
||||||
dom = conn.lookupByUUID(buuid)
|
dom = conn.lookupByUUID(buuid)
|
||||||
|
|
||||||
|
# Close the libvirt connection
|
||||||
|
conn.close()
|
||||||
# Fail
|
# Fail
|
||||||
except:
|
except:
|
||||||
# Just pass
|
# Close the libvirt connection
|
||||||
pass
|
if conn != None:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
# Enable stdout
|
# Enable stdout
|
||||||
sys.stdout = sys.__stdout__
|
sys.stdout = sys.__stdout__
|
Loading…
Reference in New Issue