Use new ansiiprint setup

This commit is contained in:
2018-06-06 22:59:31 -04:00
parent 640f38c3aa
commit b6b7bb86b6
4 changed files with 130 additions and 91 deletions

View File

@ -20,20 +20,10 @@
#
###############################################################################
import os, sys, uuid, socket, time, threading, libvirt, kazoo.client
# ANSII colours for output
class bcolours:
PURPLE = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
import os, sys, uuid, socket, time, threading, libvirt, kazoo.client, ansiiprint
class VMInstance:
# Initialization function
def __init__(self, domuuid, zk, thishypervisor):
# Passed-in variables on creation
self.domuuid = domuuid
@ -49,7 +39,7 @@ class VMInstance:
self.inmigrate = False
self.inreceive = False
self.dom = lookupByUUID(self.domuuid)
self.dom = self.lookupByUUID(self.domuuid)
# Watch for changes to the hypervisor field in Zookeeper
@zk.DataWatch('/domains/{}/hypervisor'.format(self.domuuid))
@ -78,54 +68,54 @@ class VMInstance:
# Start up the VM
def start_vm(self, xmlconfig):
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + '{} - Starting VM.'.format(self.domuuid))
ansiiprint.echo('Starting VM', '{}:'.format(self.domuuid), 'i')
self.instart = True
# Start up a new Libvirt connection
libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name)
if conn == None:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Failed to open local libvirt connection.'.format(self.domuuid))
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
self.instart = False
return
try:
dom = conn.createXML(xmlconfig, 0)
except libvirt.libvirtError as e:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Failed to create VM.'.format(self.domuuid))
ansiiprint.echo('Failed to create VM', '{}:'.format(self.domuuid), 'e')
self.zk.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii'))
if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid)
conn.close()
print(bcolours.GREEN + '>>> ' + bcolours.ENDC + '{} - Successfully started VM.'.format(self.domuuid))
ansiiprint.echo('Successfully started VM', '{}:'.format(self.domuuid), 'o')
self.dom = dom
self.instart = False
# Stop the VM forcibly without updating state
def terminate_vm(self):
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + '{} - Terminating VM.'.format(self.domuuid))
ansiiprint.echo('Terminating VM', '{}:'.format(self.domuuid), 'i')
self.instop = True
try:
self.dom.destroy()
except AttributeError:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Failed to terminate VM.'.format(self.domuuid))
ansiiprint.echo('Failed to terminate VM', '{}:'.format(self.domuuid), 'e')
if self.domuuid in self.thishypervisor.domain_list:
try:
self.thishypervisor.domain_list.remove(self.domuuid)
except ValueError:
pass
print(bcolours.GREEN + '>>> ' + bcolours.ENDC + '{} - Successfully terminated VM.'.format(self.domuuid))
ansiiprint.echo('Successfully terminated VM', '{}:'.format(self.domuuid), 'o')
# Stop the VM forcibly
def stop_vm(self):
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + '{} - Forcibly stopping VM.'.format(self.domuuid))
ansiiprint.echo('Forcibly stopping VM', '{}:'.format(self.domuuid), 'i')
self.instop = True
try:
self.dom.destroy()
except AttributeError:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Failed to stop VM.'.format(self.domuuid))
ansiiprint.echo('Failed to stop VM', '{}:'.format(self.domuuid), 'e')
if self.domuuid in self.thishypervisor.domain_list:
try:
self.thishypervisor.domain_list.remove(self.domuuid)
@ -133,13 +123,13 @@ class VMInstance:
pass
self.zk.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii'))
print(bcolours.GREEN + '>>> ' + bcolours.ENDC + '{} - Successfully stopped VM.'.format(self.domuuid))
ansiiprint.echo('Successfully stopped VM', '{}:'.format(self.domuuid), 'o')
self.dom = None
self.instop = False
# Shutdown the VM gracefully
def shutdown_vm(self):
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + '{} - Gracefully stopping VM.'.format(self.domuuid))
ansiiprint.echo('Gracefully stopping VM', '{}:'.format(self.domuuid), 'i')
self.inshutdown = True
self.dom.shutdown()
try:
@ -149,7 +139,7 @@ class VMInstance:
time.sleep(0.5)
if tick >= 60:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Shutdown timeout expired.'.format(self.domuuid))
ansiiprint.echo('Shutdown timeout expired', '{}:'.format(self.domuuid), 'e')
self.stop_vm()
self.inshutdown = False
return
@ -163,7 +153,7 @@ class VMInstance:
pass
self.zk.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii'))
print(bcolours.GREEN + '>>> ' + bcolours.ENDC + '{} - Successfully shutdown VM.'.format(self.domuuid))
ansiiprint.echo('Successfully shutdown VM', '{}:'.format(self.domuuid), 'o')
self.dom = None
self.inshutdown = False
@ -173,14 +163,15 @@ class VMInstance:
if dest_conn == None:
raise
except:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Failed to open connection to qemu+tcp://{}/system; aborting migration.'.format(self.dom_uuid, self.hypervisor))
ansiiprint.echo('Failed to open connection to qemu+tcp://{}/system; aborting migration.'.format(self.hypervisor), '{}:'.format(self.domuuid), 'e')
return 1
try:
target_dom = self.dom.migrate(dest_conn, libvirt.VIR_MIGRATE_LIVE, None, None, 0)
if target_dom == None:
raise
print(bcolours.GREEN + '>>> ' + bcolours.ENDC + '{} - Migrated successfully.'.format(self.domuuid))
ansiiprint.echo('Successfully migrated VM', '{}:'.format(self.domuuid), 'o')
except:
dest_conn.close()
return 1
@ -192,10 +183,10 @@ class VMInstance:
def migrate_vm(self):
self.inmigrate = True
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + '{} - Migrating VM to hypervisor "{}".'.format(self.domuuid, self.hypervisor))
ansiiprint.echo('Migrating VM to hypervisor "{}"'.format(self.hypervisor), '{}:'.format(self.domuuid), 'i')
migrate_ret = self.live_migrate_vm(self.hypervisor)
if migrate_ret != 0:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Could not live migrate VM; shutting down to migrate instead.'.format(self.domuuid))
ansiiprint.echo('Could not live migrate VM; shutting down to migrate instead', '{}:'.format(self.domuuid), 'e')
self.shutdown_vm()
time.sleep(1)
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
@ -209,7 +200,7 @@ class VMInstance:
# Receive the migration from another host (wait until VM is running)
def receive_migrate(self):
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + '{} - Receiving migration.'.format(self.domuuid))
ansiiprint.echo('Receiving migration', '{}:'.format(self.domuuid), 'i')
self.inreceive = True
while True:
self.dom = lookupByUUID(self.domuuid)
@ -224,7 +215,7 @@ class VMInstance:
if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid)
print(bcolours.GREEN + '>>> ' + bcolours.ENDC + '{} - Migrated successfully.'.format(self.domuuid))
ansiiprint.echo('Successfully migrated VM', '{}:'.format(self.domuuid), 'o')
self.inreceive = False
#
@ -273,37 +264,37 @@ class VMInstance:
self.start_vm(domxml)
# This function is a wrapper for libvirt.lookupByUUID which fixes some problems
# 1. Takes a text UUID and handles converting it to bytes
# 2. Try's it and returns a sensible value if not
def lookupByUUID(tuuid):
conn = None
dom = None
libvirt_name = "qemu:///system"
# Convert the text UUID to bytes
buuid = uuid.UUID(tuuid).bytes
# Try
try:
# Open a libvirt connection
conn = libvirt.open(libvirt_name)
if conn == None:
print(bcolours.RED + '>>> ' + bcolours.ENDC + '{} - Failed to open local libvirt connection.'.format(self.domuuid))
return dom
# This function is a wrapper for libvirt.lookupByUUID which fixes some problems
# 1. Takes a text UUID and handles converting it to bytes
# 2. Try's it and returns a sensible value if not
def lookupByUUID(self, tuuid):
conn = None
dom = None
libvirt_name = "qemu:///system"
# Lookup the UUID
dom = conn.lookupByUUID(buuid)
# Fail
except:
pass
# After everything
finally:
# Close the libvirt connection
if conn != None:
conn.close()
# Return the dom object (or None)
return dom
# Convert the text UUID to bytes
buuid = uuid.UUID(tuuid).bytes
# Try
try:
# Open a libvirt connection
conn = libvirt.open(libvirt_name)
if conn == None:
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
return dom
# Lookup the UUID
dom = conn.lookupByUUID(buuid)
# Fail
except:
pass
# After everything
finally:
# Close the libvirt connection
if conn != None:
conn.close()
# Return the dom object (or None)
return dom