pvc/pvcd.py

109 lines
2.7 KiB
Python
Raw Normal View History

2018-05-31 20:26:44 -04:00
#!/usr/bin/env python3
from kazoo.client import KazooClient
from kazoo.client import KazooState
import libvirt
import sys
2018-05-31 21:49:23 -04:00
import socket
2018-05-31 20:26:44 -04:00
import uuid
import VMInstance
2018-05-31 21:49:23 -04:00
import NodeInstance
2018-05-31 20:26:44 -04:00
import time
2018-05-31 21:49:23 -04:00
import threading
import atexit
2018-05-31 20:26:44 -04:00
def help():
print("pvcd - Parallel Virtual Cluster management daemon")
# exit(0)
help()
# Connect to zookeeper
zk = KazooClient(hosts='127.0.0.1:2181')
try:
zk.start()
except:
print("Failed to connect to local Zookeeper instance")
exit(1)
def zk_listener(state):
if state == KazooState.LOST:
2018-05-31 22:31:20 -04:00
cleanup()
exit(2)
2018-05-31 20:26:44 -04:00
elif state == KazooState.SUSPENDED:
2018-05-31 22:31:20 -04:00
cleanup()
exit(2)
2018-05-31 20:26:44 -04:00
else:
# Handle being connected/reconnected to Zookeeper
pass
zk.add_listener(zk_listener)
2018-05-31 22:31:20 -04:00
myhostname = socket.gethostname()
2018-05-31 23:33:07 -04:00
mynodestring = '/nodes/%s' % myhostname
2018-05-31 22:31:20 -04:00
2018-05-31 21:49:23 -04:00
def cleanup():
2018-06-01 00:35:39 -04:00
t_node[myhostname].stop()
try:
zk.set('/nodes/' + myhostname + '/state', 'stop'.encode('ascii'))
except:
pass
2018-05-31 21:49:23 -04:00
zk.stop()
2018-05-31 20:26:44 -04:00
2018-05-31 21:49:23 -04:00
atexit.register(cleanup)
2018-05-31 20:26:44 -04:00
2018-05-31 22:55:44 -04:00
# Check if our node exists in Zookeeper, and create it if not
2018-05-31 23:05:27 -04:00
if zk.exists('%s' % mynodestring):
2018-05-31 23:04:34 -04:00
print("Node is present in Zookeeper")
else:
2018-05-31 22:55:44 -04:00
zk.create('%s' % mynodestring, 'hypervisor'.encode('ascii'))
zk.create('%s/state' % mynodestring, 'stop'.encode('ascii'))
zk.create('%s/cpucount' % mynodestring, '0'.encode('ascii'))
zk.create('%s/memfree' % mynodestring, '0'.encode('ascii'))
zk.create('%s/cpuload' % mynodestring, '0.0'.encode('ascii'))
2018-05-31 23:28:26 -04:00
t_node = dict()
s_domain = dict()
node_list = []
2018-05-31 22:55:44 -04:00
2018-05-31 23:28:26 -04:00
@zk.ChildrenWatch('/nodes')
def updatenodes(new_node_list):
2018-05-31 23:28:26 -04:00
node_list = new_node_list
2018-05-31 23:37:48 -04:00
active_node_list = []
flushed_node_list = []
inactive_node_list = []
2018-05-31 23:28:26 -04:00
print('Node list: %s' % node_list)
2018-05-31 23:01:22 -04:00
for node in node_list:
2018-05-31 23:28:26 -04:00
if node in t_node:
2018-05-31 23:01:22 -04:00
t_node[node].updatenodelist(node_list)
2018-05-31 23:28:26 -04:00
else:
t_node[node] = NodeInstance.NodeInstance(node, node_list, zk);
2018-06-01 00:44:23 -04:00
t_node[node].start()
2018-05-31 20:26:44 -04:00
2018-05-31 23:40:21 -04:00
node_state = t_node[node].getstate()
2018-06-01 00:41:26 -04:00
print(node_state)
2018-05-31 23:40:21 -04:00
if node_state == 'start':
2018-05-31 23:37:48 -04:00
active_node_list.append(t_node[node].getname())
2018-05-31 23:40:21 -04:00
elif node_state == 'flush':
2018-05-31 23:37:48 -04:00
flushed_node_list.append(t_node[node].getname())
else:
inactive_node_list.append(t_node[node].getname())
print('Active nodes: %s' % active_node_list)
print('Flushed nodes: %s' % flushed_node_list)
print('Inactive nodes: %s' % inactive_node_list)
2018-05-31 21:49:23 -04:00
domain_list = zk.get_children('/domains')
2018-05-31 23:28:26 -04:00
print('Domain list: %s' % domain_list)
2018-05-31 22:55:44 -04:00
2018-05-31 21:49:23 -04:00
for domain in domain_list:
2018-05-31 22:31:20 -04:00
s_domain[domain] = VMInstance.VMInstance(domain, zk, t_node[myhostname]);
2018-05-31 20:26:44 -04:00
while True:
# Tick loop
2018-05-31 21:49:23 -04:00
try:
2018-05-31 22:31:20 -04:00
time.sleep(0.1)
2018-05-31 21:49:23 -04:00
except:
cleanup()
exit(0)