Some more cleanup and avoid opening libvirt connections until they're really needed

This commit is contained in:
Joshua Boniface 2018-06-04 01:22:18 -04:00
parent ad4a9d8ea6
commit 8eb91a4ec3
3 changed files with 33 additions and 30 deletions

View File

@ -1,6 +1,6 @@
#!/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):
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
for domain in self.domain_list:
dom = pvcdomf.lookupByUUID(conn, domain)
dom = pvcdomf.lookupByUUID(domain)
if dom == None:
self.domain_list.remove(domain)
else:

View File

@ -1,6 +1,6 @@
#!/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:
def __init__(self, domuuid, zk, thishypervisor):
@ -19,15 +19,7 @@ class VMInstance:
self.inmigrate = False
self.inreceive = False
# 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)
self.dom = pvcdomf.lookupByUUID(conn, self.domuuid)
conn.close()
self.dom = pvcdomf.lookupByUUID(self.domuuid)
# Watch for changes to the hypervisor field in Zookeeper
@zk.DataWatch(self.zkey + '/hypervisor')
@ -51,9 +43,18 @@ class VMInstance:
return self.hypervisor
# Start up the VM
def start_vm(self, conn, xmlconfig):
def start_vm(self, xmlconfig):
print(">>> %s - Starting VM" % self.domuuid)
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:
dom = conn.createXML(xmlconfig, 0)
except libvirt.libvirtError as e:
@ -63,6 +64,7 @@ class VMInstance:
if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid)
conn.close()
self.dom = dom
self.instart = False
@ -144,11 +146,11 @@ class VMInstance:
self.inmigrate = False
# 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)
self.inreceive = True
while True:
self.dom = pvcdomf.lookupByUUID(conn, self.domuuid)
self.dom = pvcdomf.lookupByUUID(self.domuuid)
if self.dom == None:
time.sleep(0.2)
continue
@ -166,13 +168,6 @@ class VMInstance:
# Main function to manage a VM (taking only 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
try:
if self.dom != None:
@ -192,7 +187,7 @@ class VMInstance:
# 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:
self.receive_migrate(conn)
self.receive_migrate()
# 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:
@ -208,7 +203,4 @@ class VMInstance:
# Grab the domain information from Zookeeper
domxml, domxmlstat = self.zk.get(self.zkey + '/xml')
domxml = str(domxml.decode('ascii'))
self.start_vm(conn, domxml)
# The VM should now be running so return the domain and active connection
conn.close()
self.start_vm(domxml)

View File

@ -11,7 +11,7 @@ import os, sys, libvirt, uuid
# 1. Takes a text UUID and handles converting it to bytes
# 2. Disables stdout to avoid stupid printouts
# 3. Try's it and returns a sensible value if not
def lookupByUUID(conn, tuuid):
def lookupByUUID(tuuid):
dom = None
# Convert the text UUID to bytes
@ -22,12 +22,23 @@ def lookupByUUID(conn, tuuid):
# 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
dom = conn.lookupByUUID(buuid)
# Close the libvirt connection
conn.close()
# Fail
except:
# Just pass
pass
# Close the libvirt connection
if conn != None:
conn.close()
# Enable stdout
sys.stdout = sys.__stdout__