Make the ansiiprint more portable (give colour functions), have pvcd.py use it too

This commit is contained in:
Joshua Boniface 2018-06-06 23:57:25 -04:00
parent c6c058d832
commit 3e51988e9a
2 changed files with 30 additions and 25 deletions

View File

@ -22,9 +22,26 @@
import os, sys, socket, time, libvirt, kazoo.client, threading, fencenode, ansiiprint import os, sys, socket, time, libvirt, kazoo.client, threading, fencenode, ansiiprint
# ANSII colours for output
def red():
return '\033[91m'
def blue():
return '\033[94m'
def green():
return '\033[92m'
def yellow():
return '\033[93m'
def purple():
return '\033[95m'
def bold():
return '\033[1m'
def end():
return '\033[0m'
# Print function # Print function
def echo(message, prefix, state): def echo(message, prefix, state):
date = '{} - '.format(time.strftime('%Y/%m/%d %H:%M:%S')) date = '{} - '.format(time.strftime('%Y/%m/%d %H:%M:%S'))
endc = end()
# Continuation # Continuation
if state == 'c': if state == 'c':
date = '' date = ''
@ -32,26 +49,25 @@ def echo(message, prefix, state):
prompt = ' ' prompt = ' '
# OK # OK
elif state == 'o': elif state == 'o':
colour = '\033[92m' # Green colour = green()
prompt = '>>> ' prompt = '>>> '
# Error # Error
elif state == 'e': elif state == 'e':
colour = '\033[91m' # Red colour = red()
prompt = '>>> ' prompt = '>>> '
# Warning # Warning
elif state == 'w': elif state == 'w':
colour = '\033[93m' # Yellow colour = yellow()
prompt = '>>> ' prompt = '>>> '
# Tick # Tick
elif state == 't': elif state == 't':
colour = '\033[95m' # Purple colour = purple()
prompt = '>>> ' prompt = '>>> '
# Information # Information
elif state == 'i': elif state == 'i':
colour = '\033[94m' # Blue colour = blue()
prompt = '>>> ' prompt = '>>> '
else: else:
colour = '\033[1m' # Bold colour = bold()
prompt = '>>> ' prompt = '>>> '
end = '\033[0m' print(colour + prompt + endc + date + prefix + message)
print(colour + prompt + end + date + prefix + message)

23
pvcd.py
View File

@ -32,19 +32,8 @@ import atexit
import apscheduler.schedulers.background import apscheduler.schedulers.background
import ansiiprint import 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'
def help(): def help():
print(bcolours.BOLD + "pvcd - Parallel Virtual Cluster management daemon" + bcolours.ENDC) print(ansiiprint.bold() + "pvcd - Parallel Virtual Cluster management daemon" + ansiiprint.end())
# exit(0) # exit(0)
help() help()
@ -54,7 +43,7 @@ zk = kazoo.client.KazooClient(hosts='127.0.0.1:2181')
try: try:
zk.start() zk.start()
except: except:
print(bcolours.RED + "Failed to connect to local Zookeeper instance" + bcolours.ENDC) print(ansiiprint.red() + "Failed to connect to local Zookeeper instance" + ansiiprint.end())
exit(1) exit(1)
def zk_listener(state): def zk_listener(state):
@ -86,9 +75,9 @@ atexit.register(cleanup)
# Check if our node exists in Zookeeper, and create it if not # Check if our node exists in Zookeeper, and create it if not
if zk.exists('/nodes/{}'.format(myhostname)): if zk.exists('/nodes/{}'.format(myhostname)):
print("> Node is " + bcolours.GREEN + "present" + bcolours.ENDC + " in Zookeeper.") print("Node is " + ansiiprint.green() + "present" + ansiiprint.end() + " in Zookeeper")
else: else:
print("> Node is " + bcolours.RED + "absent" + bcolours.ENDC + " in Zookeeper; adding new node.") print("Node is " + ansiiprint.red() + "absent" + ansiiprint.end() + " in Zookeeper; adding new node")
keepalive_time = int(time.time()) keepalive_time = int(time.time())
zk.create('/domains/{}'.format(myhostname), 'hypervisor'.encode('ascii')) zk.create('/domains/{}'.format(myhostname), 'hypervisor'.encode('ascii'))
zk.create('/domains/{}/state'.format(myhostname), 'stop'.encode('ascii')) zk.create('/domains/{}/state'.format(myhostname), 'stop'.encode('ascii'))
@ -107,7 +96,7 @@ domain_list = []
def updatenodes(new_node_list): def updatenodes(new_node_list):
global node_list global node_list
node_list = new_node_list node_list = new_node_list
print('Node list: ' + bcolours.BLUE + '{}'.format(' '.join(node_list)) + bcolours.ENDC) print(ansiiprint.blue() + 'Node list: ' + ansiiprint.end() + '{}'.format(' '.join(node_list)))
for node in node_list: for node in node_list:
if node in t_node: if node in t_node:
t_node[node].updatenodelist(t_node) t_node[node].updatenodelist(t_node)
@ -118,7 +107,7 @@ def updatenodes(new_node_list):
def updatedomains(new_domain_list): def updatedomains(new_domain_list):
global domain_list global domain_list
domain_list = new_domain_list domain_list = new_domain_list
print('Domain list: ' + bcolours.BLUE + '{}'.format(' '.join(domain_list)) + bcolours.ENDC) print(ansiiprint.blue() + 'Domain list: ' + ansiiprint.end() + '{}'.format(' '.join(domain_list)))
for domain in domain_list: for domain in domain_list:
if not domain in s_domain: if not domain in s_domain:
s_domain[domain] = VMInstance.VMInstance(domain, zk, t_node[myhostname]); s_domain[domain] = VMInstance.VMInstance(domain, zk, t_node[myhostname]);