Numerous tweaks to make migration work well
This commit is contained in:
parent
b84f6ff62e
commit
a297f9dbcf
|
@ -13,7 +13,11 @@ class VMInstance:
|
||||||
# These will all be set later
|
# These will all be set later
|
||||||
self.hypervisor = None
|
self.hypervisor = None
|
||||||
self.state = None
|
self.state = None
|
||||||
|
self.instart = False
|
||||||
|
self.instop = False
|
||||||
self.inshutdown = False
|
self.inshutdown = False
|
||||||
|
self.inmigrate = False
|
||||||
|
self.inreceive = False
|
||||||
|
|
||||||
# Start up a new Libvirt connection
|
# Start up a new Libvirt connection
|
||||||
libvirt_name = "qemu:///system"
|
libvirt_name = "qemu:///system"
|
||||||
|
@ -52,6 +56,7 @@ class VMInstance:
|
||||||
# Start up the VM
|
# Start up the VM
|
||||||
def start_vm(self, conn, xmlconfig):
|
def start_vm(self, conn, xmlconfig):
|
||||||
print(">>> Starting VM %s" % self.domuuid)
|
print(">>> Starting VM %s" % self.domuuid)
|
||||||
|
self.instart = True
|
||||||
try:
|
try:
|
||||||
dom = conn.createXML(xmlconfig, 0)
|
dom = conn.createXML(xmlconfig, 0)
|
||||||
except libvirt.libvirtError as e:
|
except libvirt.libvirtError as e:
|
||||||
|
@ -62,10 +67,12 @@ class VMInstance:
|
||||||
self.thishypervisor.domain_list.append(self.domuuid)
|
self.thishypervisor.domain_list.append(self.domuuid)
|
||||||
|
|
||||||
self.dom = dom
|
self.dom = dom
|
||||||
|
self.instart = False
|
||||||
|
|
||||||
# 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.instop = True
|
||||||
self.dom.destroy()
|
self.dom.destroy()
|
||||||
if self.domuuid in self.thishypervisor.domain_list:
|
if self.domuuid in self.thishypervisor.domain_list:
|
||||||
try:
|
try:
|
||||||
|
@ -74,13 +81,14 @@ class VMInstance:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.dom = None
|
self.dom = None
|
||||||
|
self.instop = False
|
||||||
|
|
||||||
# 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.inshutdown = True
|
||||||
self.dom.shutdown()
|
self.dom.shutdown()
|
||||||
try:
|
try:
|
||||||
self.inshutdown = True
|
|
||||||
tick = 0
|
tick = 0
|
||||||
while self.dom.state()[0] == libvirt.VIR_DOMAIN_RUNNING and tick < 60:
|
while self.dom.state()[0] == libvirt.VIR_DOMAIN_RUNNING and tick < 60:
|
||||||
tick += 1
|
tick += 1
|
||||||
|
@ -88,6 +96,7 @@ class VMInstance:
|
||||||
|
|
||||||
if tick >= 60:
|
if tick >= 60:
|
||||||
self.stop_vm()
|
self.stop_vm()
|
||||||
|
self.inshutdown = False
|
||||||
return
|
return
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
@ -99,9 +108,12 @@ class VMInstance:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.dom = None
|
self.dom = None
|
||||||
|
self.inshutdown = False
|
||||||
|
|
||||||
# Migrate the VM to a target host
|
# Migrate the VM to a target host
|
||||||
def migrate_vm(self):
|
def migrate_vm(self):
|
||||||
|
print('>>> Migrating %s to %s' % (self.domuuid, self.hypervisor))
|
||||||
|
self.inmigrate = True
|
||||||
try:
|
try:
|
||||||
dest_conn = libvirt.open('qemu+tcp://%s/system' % self.hypervisor)
|
dest_conn = libvirt.open('qemu+tcp://%s/system' % self.hypervisor)
|
||||||
if dest_conn == None:
|
if dest_conn == None:
|
||||||
|
@ -129,22 +141,26 @@ class VMInstance:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
dest_conn.close()
|
dest_conn.close()
|
||||||
|
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):
|
def receive_migrate(self):
|
||||||
|
print('>>> Receiving migration of %s' % self.domuuid)
|
||||||
|
self.inreceive = True
|
||||||
while True:
|
while True:
|
||||||
if self.dom == None or self.dom.state()[0] != libvirt.VIR_DOMAIN_RUNNING:
|
try:
|
||||||
try:
|
if self.dom == None or self.dom.state()[0] != libvirt.VIR_DOMAIN_RUNNING:
|
||||||
time.sleep(0.2)
|
|
||||||
print("derp")
|
|
||||||
self.dom = conn.lookupByUUID(uuid.UUID(self.domuuid).bytes)
|
self.dom = conn.lookupByUUID(uuid.UUID(self.domuuid).bytes)
|
||||||
except:
|
else:
|
||||||
pass
|
self.zk.set(self.zkey + '/state', 'start'.encode('ascii'))
|
||||||
else:
|
if not self.domuuid in self.thishypervisor.domain_list:
|
||||||
self.zk.set(self.zkey + '/status', 'start'.encode('ascii'))
|
self.thishypervisor.domain_list.append(self.domuuid)
|
||||||
if not self.domuuid in self.thishypervisor.domain_list:
|
break
|
||||||
self.thishypervisor.domain_list.append(self.domuuid)
|
except:
|
||||||
break
|
pass
|
||||||
|
time.sleep(0.2)
|
||||||
|
print('>>> Migrated successfully' % self.domuuid)
|
||||||
|
self.inreceive = False
|
||||||
|
|
||||||
#
|
#
|
||||||
# Main function to manage a VM (taking only self)
|
# Main function to manage a VM (taking only self)
|
||||||
|
@ -167,19 +183,19 @@ class VMInstance:
|
||||||
running = libvirt.VIR_DOMAIN_NOSTATE
|
running = libvirt.VIR_DOMAIN_NOSTATE
|
||||||
|
|
||||||
# VM should be stopped
|
# VM should be stopped
|
||||||
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "stop" and self.hypervisor == self.thishypervisor.name:
|
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "stop" and self.hypervisor == self.thishypervisor.name and self.instop == False:
|
||||||
self.stop_vm()
|
self.stop_vm()
|
||||||
|
|
||||||
# VM should be shut down
|
# VM should be shut down
|
||||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "shutdown" and self.hypervisor == self.thishypervisor.name:
|
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "shutdown" and self.hypervisor == self.thishypervisor.name and self.inshutdown == False:
|
||||||
self.shutdown_vm()
|
self.shutdown_vm()
|
||||||
|
|
||||||
# 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:
|
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor == self.thishypervisor.name and self.inreceive == False:
|
||||||
self.receive_migrate()
|
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:
|
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor != self.thishypervisor.name and self.inmigrate == False:
|
||||||
self.migrate_vm()
|
self.migrate_vm()
|
||||||
|
|
||||||
# VM is already running and should be
|
# VM is already running and should be
|
||||||
|
@ -188,7 +204,7 @@ class VMInstance:
|
||||||
self.thishypervisor.domain_list.append(self.domuuid)
|
self.thishypervisor.domain_list.append(self.domuuid)
|
||||||
|
|
||||||
# VM should be started
|
# VM should be started
|
||||||
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "start" and self.hypervisor == self.thishypervisor.name:
|
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "start" and self.hypervisor == self.thishypervisor.name and self.instart == False:
|
||||||
# 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'))
|
||||||
|
|
Loading…
Reference in New Issue