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, socket, time, libvirt, kazoo.client, threading, fencenode
# 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, socket, time, libvirt, kazoo.client, threading, fencenode, ansiiprint
class NodeInstance():
# Initialization function
def __init__(self, name, t_node, s_domain, zk):
# Passed-in variables on creation
self.zk = zk
@ -98,7 +88,7 @@ class NodeInstance():
# Flush all VMs on the host
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:
most_memfree = 0
target_hypervisor = None
@ -115,12 +105,12 @@ class NodeInstance():
target_hypervisor = hypervisor
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.set_data('/domains/{}/state'.format(dom_uuid), 'shutdown'.encode('ascii'))
transaction.commit()
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.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
@ -131,14 +121,14 @@ class NodeInstance():
time.sleep(1)
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'))
for dom_uuid in self.s_domain:
last_hypervisor = self.zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
if last_hypervisor != self.name:
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.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), self.name.encode('ascii'))
@ -153,7 +143,7 @@ class NodeInstance():
libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name)
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
# Get past state and update if needed
@ -204,9 +194,9 @@ class NodeInstance():
conn.close()
# Display node information to the terminal
print(bcolours.PURPLE + '>>> ' + bcolours.ENDC + '{} - {} keepalive'.format(time.strftime('%d/%m/%Y %H:%M:%S'), self.name))
print(' CPUs: {} | Free memory: {} | Load: {}'.format(self.cpucount, self.memfree, self.cpuload))
print(' Active domains: {}'.format(' '.join(self.domain_list)))
ansiiprint.echo('{} keepalive'.format(self.name), '', 't')
ansiiprint.echo('CPUs: {} | Free memory: {} | Load: {}'.format(self.cpucount, self.memfree, self.cpuload), '', 'c')
ansiiprint.echo('Active domains: {}'.format(' '.join(self.domain_list)), '', 'c')
# Update our local node lists
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)
node_deadtime = int(time.time()) - 30
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'))
fence_thread = threading.Thread(target=fencenode.fence, args=(node_name, self.zk), kwargs={})
fence_thread.start()
@ -259,7 +249,7 @@ class NodeInstance():
pass
# Display cluster information to the terminal
print(bcolours.PURPLE + '>>> ' + bcolours.ENDC + '{} - Cluster status'.format(time.strftime('%d/%m/%Y %H:%M:%S')))
print(' Active nodes: {}'.format(' '.join(self.active_node_list)))
print(' Flushed nodes: {}'.format(' '.join(self.flushed_node_list)))
print(' Inactive nodes: {}'.format(' '.join(self.inactive_node_list)))
ansiiprint.echo('Cluster status', '', 't')
ansiiprint.echo('Active nodes: {}'.format(' '.join(self.active_node_list)), '', 'c')
ansiiprint.echo('Flushed nodes: {}'.format(' '.join(self.flushed_node_list)), '', 'c')
ansiiprint.echo('Inactive nodes: {}'.format(' '.join(self.inactive_node_list)), '', 'c')