Formatting cleanup and colours!

This commit is contained in:
Joshua Boniface 2018-06-06 22:13:16 -04:00
parent c124cae24b
commit ad1bf35837
1 changed files with 25 additions and 14 deletions

39
pvcd.py
View File

@ -31,8 +31,18 @@ import time
import atexit import atexit
import apscheduler.schedulers.background import apscheduler.schedulers.background
class bcolours:
HEADER = '\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("pvcd - Parallel Virtual Cluster management daemon") print(bcolours.HEADER + bcolours.BOLD + bcolours.RED + "pvcd - Parallel Virtual Cluster management daemon" + bcolours.ENDC)
# exit(0) # exit(0)
help() help()
@ -42,7 +52,7 @@ zk = kazoo.client.KazooClient(hosts='127.0.0.1:2181')
try: try:
zk.start() zk.start()
except: except:
print("Failed to connect to local Zookeeper instance") print(bcolours.RED + "Failed to connect to local Zookeeper instance" + bcolours.ENDC)
exit(1) exit(1)
def zk_listener(state): def zk_listener(state):
@ -64,7 +74,7 @@ def cleanup():
try: try:
update_timer.shutdown() update_timer.shutdown()
if t_node[myhostname].getstate() != 'flush': if t_node[myhostname].getstate() != 'flush':
zk.set('/nodes/' + myhostname + '/state', 'stop'.encode('ascii')) zk.set('/nodes/{}/state'.format(myhostname), 'stop'.encode('ascii'))
zk.stop() zk.stop()
zk.close() zk.close()
except: except:
@ -73,17 +83,18 @@ def cleanup():
atexit.register(cleanup) 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('%s' % mynodestring): if zk.exists('/nodes/{}'.format(myhostname)):
print("Node is present in Zookeeper") print("> Node is " + bcolours.GREEN + "present" + bcolours.ENDC + " in Zookeeper.")
else: else:
print("> Node is " + bcolours.RED + "absent" + bcolours.ENDC + " in Zookeeper; adding new node.")
keepalive_time = int(time.time()) keepalive_time = int(time.time())
zk.create('%s' % mynodestring, 'hypervisor'.encode('ascii')) zk.create('/domains/{}'.format(myhostname), 'hypervisor'.encode('ascii'))
zk.create('%s/state' % mynodestring, 'stop'.encode('ascii')) zk.create('/domains/{}/state'.format(myhostname), 'stop'.encode('ascii'))
zk.create('%s/cpucount' % mynodestring, '0'.encode('ascii')) zk.create('/domains/{}/cpucount'.format(myhostname), '0'.encode('ascii'))
zk.create('%s/memfree' % mynodestring, '0'.encode('ascii')) zk.create('/domains/{}/memfree'.format(myhostname), '0'.encode('ascii'))
zk.create('%s/cpuload' % mynodestring, '0.0'.encode('ascii')) zk.create('/domains/{}/cpuload'.format(myhostname), '0.0'.encode('ascii'))
zk.create('%s/runningdomains' % mynodestring, ''.encode('ascii')) zk.create('/domains/{}/runningdomains'.format(myhostname), ''.encode('ascii'))
zk.create('%s/keepalive' % mynodestring, str(keepalive_time).encode('ascii')) zk.create('/domains/{}/keepalive'.format(myhostname), str(keepalive_time).encode('ascii'))
t_node = dict() t_node = dict()
s_domain = dict() s_domain = dict()
@ -94,7 +105,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: %s' % node_list) print('Node list: ' + bcolours.BLUE + '{}'.format(' '.join(node_list)) + bcolours.ENDC)
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)
@ -105,7 +116,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: %s' % domain_list) print('Domain list: ' + bcolours.BLUE + '{}'.format(' '.join(domain_list)) + bcolours.ENDC)
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]);