Use new ansiiprint setup

This commit is contained in:
Joshua Boniface 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, socket, time, libvirt, kazoo.client, threading, fencenode import os, sys, socket, time, libvirt, kazoo.client, threading, fencenode, ansiiprint
# 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'
class NodeInstance(): class NodeInstance():
# Initialization function
def __init__(self, name, t_node, s_domain, zk): def __init__(self, name, t_node, s_domain, zk):
# Passed-in variables on creation # Passed-in variables on creation
self.zk = zk self.zk = zk
@ -98,7 +88,7 @@ class NodeInstance():
# Flush all VMs on the host # Flush all VMs on the host
def flush(self): def flush(self):
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + 'Flushing node {} of running VMs.'.format(self.name)) ansiiprint.echo('Flushing node "{}" of running VMs'.format(self.name), '', 'i')
for dom_uuid in self.domain_list: for dom_uuid in self.domain_list:
most_memfree = 0 most_memfree = 0
target_hypervisor = None target_hypervisor = None
@ -115,12 +105,12 @@ class NodeInstance():
target_hypervisor = hypervisor target_hypervisor = hypervisor
if target_hypervisor == None: if target_hypervisor == None:
print(bcolours.RED + '>>> ' + bcolours.ENDC + 'Failed to find migration target for VM "{}"; shutting down.'.format(dom_uuid)) ansiiprint.echo('Failed to find migration target for VM "{}"; shutting down'.format(dom_uuid), '', 'e')
transaction = self.zk.transaction() transaction = self.zk.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'shutdown'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'shutdown'.encode('ascii'))
transaction.commit() transaction.commit()
else: else:
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + 'Migrating VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor)) ansiiprint.echo('Migrating VM "{}" to hypervisor "{}"'.format(dom_uuid, '', target_hypervisor), 'i')
transaction = self.zk.transaction() transaction = self.zk.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
@ -131,14 +121,14 @@ class NodeInstance():
time.sleep(1) time.sleep(1)
def unflush(self): def unflush(self):
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + 'Restoring node {} to active service.'.format(self.name)) ansiiprint.echo('Restoring node {} to active service.'.format(self.name), '', 'i')
self.zk.set('/nodes/{}/state'.format(self.name), 'start'.encode('ascii')) self.zk.set('/nodes/{}/state'.format(self.name), 'start'.encode('ascii'))
for dom_uuid in self.s_domain: for dom_uuid in self.s_domain:
last_hypervisor = self.zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii') last_hypervisor = self.zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
if last_hypervisor != self.name: if last_hypervisor != self.name:
continue continue
print(bcolours.BLUE + '>>> ' + bcolours.ENDC + 'Setting unmigration for VM "{}".'.format(dom_uuid)) ansiiprint.echo('Setting unmigration for VM "{}"'.format(dom_uuid), '', 'i')
transaction = self.zk.transaction() transaction = self.zk.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), self.name.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), self.name.encode('ascii'))
@ -153,7 +143,7 @@ class NodeInstance():
libvirt_name = "qemu:///system" libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name) conn = libvirt.open(libvirt_name)
if conn == None: if conn == None:
print(bcolours.RED + '>>> ' + bcolours.ENDC + 'Failed to open connection to {}'.format(libvirt_name)) ansiiprint.echo('Failed to open connection to "{}"'.format(libvirt_name), '', 'e')
return return
# Get past state and update if needed # Get past state and update if needed
@ -204,9 +194,9 @@ class NodeInstance():
conn.close() conn.close()
# Display node information to the terminal # Display node information to the terminal
print(bcolours.PURPLE + '>>> ' + bcolours.ENDC + '{} - {} keepalive'.format(time.strftime('%d/%m/%Y %H:%M:%S'), self.name)) ansiiprint.echo('{} keepalive'.format(self.name), '', 't')
print(' CPUs: {} | Free memory: {} | Load: {}'.format(self.cpucount, self.memfree, self.cpuload)) ansiiprint.echo('CPUs: {} | Free memory: {} | Load: {}'.format(self.cpucount, self.memfree, self.cpuload), '', 'c')
print(' Active domains: {}'.format(' '.join(self.domain_list))) ansiiprint.echo('Active domains: {}'.format(' '.join(self.domain_list)), '', 'c')
# Update our local node lists # Update our local node lists
for node_name in self.t_node: for node_name in self.t_node:
@ -221,7 +211,7 @@ class NodeInstance():
# (A node is considered dead when its keepalive timer is >30s out-of-date while in 'start' state) # (A node is considered dead when its keepalive timer is >30s out-of-date while in 'start' state)
node_deadtime = int(time.time()) - 30 node_deadtime = int(time.time()) - 30
if node_keepalive < node_deadtime and node_state == 'start': if node_keepalive < node_deadtime and node_state == 'start':
print(bcolours.RED + '>>> ' + bcolours.ENDC + 'Node {} is dead! Performing fence operation in 3 seconds.'.format(node_name)) ansiiprint.echo('Node {} is dead - performing fence operation in 3 seconds'.format(node_name), '', 'w')
self.zk.set('/nodes/{}/state'.format(node_name), 'dead'.encode('ascii')) self.zk.set('/nodes/{}/state'.format(node_name), 'dead'.encode('ascii'))
fence_thread = threading.Thread(target=fencenode.fence, args=(node_name, self.zk), kwargs={}) fence_thread = threading.Thread(target=fencenode.fence, args=(node_name, self.zk), kwargs={})
fence_thread.start() fence_thread.start()
@ -259,7 +249,7 @@ class NodeInstance():
pass pass
# Display cluster information to the terminal # Display cluster information to the terminal
print(bcolours.PURPLE + '>>> ' + bcolours.ENDC + '{} - Cluster status'.format(time.strftime('%d/%m/%Y %H:%M:%S'))) ansiiprint.echo('Cluster status', '', 't')
print(' Active nodes: {}'.format(' '.join(self.active_node_list))) ansiiprint.echo('Active nodes: {}'.format(' '.join(self.active_node_list)), '', 'c')
print(' Flushed nodes: {}'.format(' '.join(self.flushed_node_list))) ansiiprint.echo('Flushed nodes: {}'.format(' '.join(self.flushed_node_list)), '', 'c')
print(' Inactive nodes: {}'.format(' '.join(self.inactive_node_list))) ansiiprint.echo('Inactive nodes: {}'.format(' '.join(self.inactive_node_list)), '', 'c')

View File

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

57
ansiiprint.py Normal file
View File

@ -0,0 +1,57 @@
#!/usr/bin/env python3
# ansiprint.py - Printing function for formatted daemon messages
# Part of the Parallel Virtual Cluster (PVC) system
#
# Copyright (C) 2018 Joshua M. Boniface <joshua@boniface.me>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
###############################################################################
import os, sys, socket, time, libvirt, kazoo.client, threading, fencenode, ansiiprint
# Print function
def echo(message, prefix, state):
date = '{} - '.format(time.strftime('%Y/%m/%d %H:%M:%S'))
# Continuation
if state == 'c':
date = ''
colour = ''
prompt = ' '
# OK
elif state == 'o':
colour = '\033[92m' # Green
prompt = '>>> '
# Error
elif state == 'e':
colour = '\033[91m' # Red
prompt = '>>> '
# Warning
elif state == 'w':
colour = '\033[93m' # Yellow
prompt = '>>> '
# Tick
elif state == 't':
colour = '\033[95m' # Purple
prompt = '>>> '
# Information
elif state == 'i':
colour = '\033[94m' # Blue
prompt = '>>> '
else:
colour = '\033[1m' # Bold
prompt = '>>> '
end = '\033[0m'
print(colour + prompt + end + date + prefix + '{}'.format(message))

View File

@ -30,6 +30,7 @@ import NodeInstance
import time import time
import atexit import atexit
import apscheduler.schedulers.background import apscheduler.schedulers.background
import ansiiprint
# ANSII colours for output # ANSII colours for output
class bcolours: class bcolours: