2018-05-31 21:49:23 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-06-06 01:47:53 -04:00
|
|
|
# NodeInstance.py - Class implementing a PVC node and run by pvcd
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2018-06-06 15:41:06 -04:00
|
|
|
import os, sys, socket, time, libvirt, kazoo.client, threading, fencenode
|
2018-05-31 21:49:23 -04:00
|
|
|
|
2018-06-06 14:16:52 -04:00
|
|
|
class NodeInstance():
|
2018-06-04 01:52:26 -04:00
|
|
|
def __init__(self, name, t_node, s_domain, zk):
|
2018-05-31 21:49:23 -04:00
|
|
|
# Passed-in variables on creation
|
|
|
|
self.zk = zk
|
|
|
|
self.name = name
|
2018-06-01 01:00:55 -04:00
|
|
|
self.state = 'stop'
|
2018-06-04 01:52:26 -04:00
|
|
|
self.t_node = t_node
|
2018-06-04 16:34:41 -04:00
|
|
|
self.active_node_list = []
|
|
|
|
self.flushed_node_list = []
|
|
|
|
self.inactive_node_list = []
|
2018-06-02 15:03:44 -04:00
|
|
|
self.s_domain = s_domain
|
2018-06-01 12:21:58 -04:00
|
|
|
self.domain_list = []
|
|
|
|
|
|
|
|
# Zookeeper handlers for changed states
|
2018-06-06 20:39:21 -04:00
|
|
|
@zk.DataWatch('/nodes/{}/state'.format(self.name))
|
2018-06-01 12:21:58 -04:00
|
|
|
def watch_hypervisor_state(data, stat, event=""):
|
2018-06-06 01:24:28 -04:00
|
|
|
try:
|
|
|
|
self.state = data.decode('ascii')
|
|
|
|
except AttributeError:
|
|
|
|
self.state = 'stop'
|
|
|
|
|
2018-06-04 16:34:41 -04:00
|
|
|
if self.state == 'flush':
|
|
|
|
self.flush()
|
2018-06-04 16:46:45 -04:00
|
|
|
if self.state == 'unflush':
|
2018-06-04 16:34:41 -04:00
|
|
|
self.unflush()
|
2018-06-01 12:21:58 -04:00
|
|
|
|
2018-06-06 20:39:21 -04:00
|
|
|
@zk.DataWatch('/nodes/{}/memfree'.format(self.name))
|
2018-06-01 12:21:58 -04:00
|
|
|
def watch_hypervisor_memfree(data, stat, event=""):
|
2018-06-06 01:24:28 -04:00
|
|
|
try:
|
|
|
|
self.memfree = data.decode('ascii')
|
|
|
|
except AttributeError:
|
|
|
|
self.memfree = 0
|
2018-06-01 12:21:58 -04:00
|
|
|
|
2018-06-06 20:39:21 -04:00
|
|
|
@zk.DataWatch('/nodes/{}/runningdomains'.format(self.name))
|
2018-06-01 12:21:58 -04:00
|
|
|
def watch_hypervisor_runningdomains(data, stat, event=""):
|
2018-06-06 01:24:28 -04:00
|
|
|
try:
|
|
|
|
self.domain_list = data.decode('ascii').split()
|
|
|
|
except AttributeError:
|
|
|
|
self.domain_list = []
|
2018-05-31 21:49:23 -04:00
|
|
|
|
2018-05-31 22:31:20 -04:00
|
|
|
# Get value functions
|
2018-05-31 23:41:52 -04:00
|
|
|
def getfreemem(self):
|
2018-05-31 22:31:20 -04:00
|
|
|
return self.memfree
|
|
|
|
|
2018-05-31 23:41:52 -04:00
|
|
|
def getcpuload(self):
|
2018-05-31 22:31:20 -04:00
|
|
|
return self.cpuload
|
|
|
|
|
2018-05-31 23:41:52 -04:00
|
|
|
def getname(self):
|
2018-05-31 22:31:20 -04:00
|
|
|
return self.name
|
|
|
|
|
2018-05-31 23:41:52 -04:00
|
|
|
def getstate(self):
|
2018-05-31 23:40:21 -04:00
|
|
|
return self.state
|
|
|
|
|
2018-06-01 12:21:58 -04:00
|
|
|
def getdomainlist(self):
|
|
|
|
return self.domain_list
|
|
|
|
|
2018-05-31 23:01:22 -04:00
|
|
|
# Update value functions
|
2018-06-04 01:52:26 -04:00
|
|
|
def updatenodelist(self, t_node):
|
|
|
|
self.t_node = t_node
|
2018-05-31 23:01:22 -04:00
|
|
|
|
2018-06-02 15:03:44 -04:00
|
|
|
def updatedomainlist(self, s_domain):
|
|
|
|
self.s_domain = s_domain
|
|
|
|
|
2018-05-31 22:31:20 -04:00
|
|
|
# Flush all VMs on the host
|
2018-06-04 16:34:41 -04:00
|
|
|
def flush(self):
|
2018-06-06 20:36:03 -04:00
|
|
|
print('>>> Flushing node {} of running VMs.'.format(self.name))
|
|
|
|
for dom_uuid in self.domain_list:
|
|
|
|
most_memfree = 0
|
|
|
|
hypervisor_list = zk.get_children('/nodes')
|
|
|
|
current_hypervisor = zk.get('/dom_uuids/{}/hypervisor'.format(dom_uuid))[0].decode('ascii')
|
|
|
|
for hypervisor in hypervisor_list:
|
|
|
|
state = zk.get('/nodes/{}/state'.format(hypervisor))[0].decode('ascii')
|
|
|
|
if state != 'start' or hypervisor == current_hypervisor:
|
2018-06-04 02:22:59 -04:00
|
|
|
continue
|
2018-06-06 20:36:03 -04:00
|
|
|
|
|
|
|
memfree = int(zk.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii'))
|
|
|
|
if memfree > most_memfree:
|
|
|
|
most_memfree = memfree
|
|
|
|
target_hypervisor = hypervisor
|
|
|
|
|
2018-06-04 02:22:59 -04:00
|
|
|
if least_host == None:
|
2018-06-06 20:39:54 -04:00
|
|
|
print('>>> Failed to find migration target for VM "{}"; shutting down.'.format(dom_uuid))
|
2018-06-04 02:22:59 -04:00
|
|
|
transaction = self.zk.transaction()
|
2018-06-06 20:36:03 -04:00
|
|
|
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'shutdown'.encode('ascii'))
|
2018-06-04 02:22:59 -04:00
|
|
|
transaction.commit()
|
|
|
|
else:
|
2018-06-06 20:36:03 -04:00
|
|
|
print('>>> Moving VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
|
2018-06-04 02:22:59 -04:00
|
|
|
transaction = self.zk.transaction()
|
2018-06-06 20:36:03 -04:00
|
|
|
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'start'.encode('ascii'))
|
|
|
|
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
|
|
|
|
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii'))
|
2018-06-04 03:00:17 -04:00
|
|
|
result = transaction.commit()
|
2018-06-04 02:22:59 -04:00
|
|
|
|
|
|
|
# Wait 1s between migrations
|
|
|
|
time.sleep(1)
|
2018-05-31 22:31:20 -04:00
|
|
|
|
2018-06-04 03:09:51 -04:00
|
|
|
def unflush(self):
|
2018-06-06 20:36:03 -04:00
|
|
|
print('>>> Restoring node {} to active service.'.format(self.name))
|
|
|
|
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('>>> Setting unmigration for VM "{}".'.format(domain))
|
|
|
|
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'))
|
|
|
|
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
|
|
|
|
result = transaction.commit()
|
2018-06-04 03:09:51 -04:00
|
|
|
|
2018-06-06 20:36:03 -04:00
|
|
|
# Wait 1s between migrations
|
|
|
|
time.sleep(1)
|
2018-06-04 03:09:51 -04:00
|
|
|
|
2018-06-06 14:16:52 -04:00
|
|
|
def update_zookeeper(self):
|
2018-05-31 21:49:23 -04:00
|
|
|
# Connect to libvirt
|
|
|
|
libvirt_name = "qemu:///system"
|
|
|
|
conn = libvirt.open(libvirt_name)
|
|
|
|
if conn == None:
|
2018-06-06 20:39:21 -04:00
|
|
|
print('>>> Failed to open connection to {}'.format(libvirt_name))
|
2018-06-06 14:53:28 -04:00
|
|
|
return
|
2018-05-31 21:49:23 -04:00
|
|
|
|
2018-06-04 02:34:03 -04:00
|
|
|
# Get past state and update if needed
|
2018-06-06 20:42:33 -04:00
|
|
|
past_state = self.zk.get('/nodes/{}/state'.format(self.name))[0].decode('ascii')
|
2018-06-04 02:34:03 -04:00
|
|
|
if past_state != 'flush':
|
|
|
|
self.state = 'start'
|
2018-06-06 20:42:33 -04:00
|
|
|
self.zk.set('/nodes/{}/state'.format(self.name), 'start'.encode('ascii'))
|
2018-06-04 02:34:03 -04:00
|
|
|
else:
|
|
|
|
self.state = 'flush'
|
|
|
|
|
2018-06-06 14:16:52 -04:00
|
|
|
# Toggle state management of all VMs and remove any non-running VMs
|
|
|
|
for domain, instance in self.s_domain.items():
|
|
|
|
if instance.inshutdown == False and domain in self.domain_list:
|
|
|
|
instance.manage_vm_state()
|
|
|
|
if instance.dom == None:
|
|
|
|
try:
|
|
|
|
self.domain_list.remove(domain)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
state = instance.dom.state()[0]
|
|
|
|
except:
|
|
|
|
state = libvirt.VIR_DOMAIN_NOSTATE
|
|
|
|
|
|
|
|
if state != libvirt.VIR_DOMAIN_RUNNING:
|
2018-06-04 01:26:23 -04:00
|
|
|
try:
|
|
|
|
self.domain_list.remove(domain)
|
|
|
|
except:
|
|
|
|
pass
|
2018-06-02 14:37:49 -04:00
|
|
|
|
2018-06-06 14:16:52 -04:00
|
|
|
# Set our information in zookeeper
|
2018-06-06 14:53:28 -04:00
|
|
|
self.name = conn.getHostname()
|
|
|
|
self.cpucount = conn.getCPUMap()[0]
|
2018-06-06 14:16:52 -04:00
|
|
|
self.memfree = conn.getFreeMemory()
|
|
|
|
self.cpuload = os.getloadavg()[0]
|
2018-06-06 15:41:06 -04:00
|
|
|
keepalive_time = int(time.time())
|
2018-06-06 14:16:52 -04:00
|
|
|
try:
|
2018-06-06 20:42:33 -04:00
|
|
|
self.zk.set('/nodes/{}/cpucount'.format(self.name), str(self.cpucount).encode('ascii'))
|
|
|
|
self.zk.set('/nodes/{}/memfree'.format(self.name), str(self.memfree).encode('ascii'))
|
|
|
|
self.zk.set('/nodes/{}/cpuload'.format(self.name), str(self.cpuload).encode('ascii'))
|
|
|
|
self.zk.set('/nodes/{}/runningdomains'.format(self.name), ' '.join(self.domain_list).encode('ascii'))
|
|
|
|
self.zk.set('/nodes/{}/keepalive'.format(self.name), str(keepalive_time).encode('ascii'))
|
2018-06-06 14:16:52 -04:00
|
|
|
except:
|
|
|
|
return
|
|
|
|
|
2018-06-06 15:16:39 -04:00
|
|
|
# Close the Libvirt connection
|
|
|
|
conn.close()
|
|
|
|
|
2018-06-06 14:53:28 -04:00
|
|
|
# Display node information to the terminal
|
|
|
|
print('>>> {} - {} 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)))
|
|
|
|
|
2018-06-06 14:16:52 -04:00
|
|
|
# Update our local node lists
|
|
|
|
for node_name in self.t_node:
|
2018-06-01 01:00:55 -04:00
|
|
|
try:
|
2018-06-06 20:39:21 -04:00
|
|
|
node_state = self.zk.get('/nodes/{}/state'.format(node_name))[0].decode('ascii')
|
|
|
|
node_keepalive = int(self.zk.get('/nodes/{}/keepalive'.format(node_name))[0].decode('ascii'))
|
2018-06-01 01:00:55 -04:00
|
|
|
except:
|
2018-06-06 15:41:06 -04:00
|
|
|
node_state = 'unknown'
|
|
|
|
node_keepalive = 0
|
|
|
|
|
2018-06-06 18:18:58 -04:00
|
|
|
# Handle deadtime and fencng if needed
|
|
|
|
# (A node is considered dead when its keepalive timer is >30s out-of-date while in 'start' state)
|
2018-06-06 15:41:06 -04:00
|
|
|
node_deadtime = int(time.time()) - 30
|
2018-06-06 18:18:58 -04:00
|
|
|
if node_keepalive < node_deadtime and node_state == 'start':
|
2018-06-06 15:41:06 -04:00
|
|
|
print('>>> Node {} is dead! Performing fence operation in 3 seconds.'.format(node_name))
|
2018-06-06 18:18:58 -04:00
|
|
|
self.zk.set('/nodes/{}/state'.format(node_name), 'dead'.encode('ascii'))
|
|
|
|
fence_thread = threading.Thread(target=fencenode.fence, args=(node_name, self.zk), kwargs={})
|
2018-06-06 15:41:06 -04:00
|
|
|
fence_thread.start()
|
|
|
|
|
|
|
|
# Update the arrays
|
2018-06-06 14:16:52 -04:00
|
|
|
if node_state == 'start' and node_name not in self.active_node_list:
|
|
|
|
self.active_node_list.append(node_name)
|
2018-06-06 00:49:34 -04:00
|
|
|
try:
|
2018-06-06 14:16:52 -04:00
|
|
|
self.flushed_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.inactive_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if node_state == 'flush' and node_name not in self.flushed_node_list:
|
|
|
|
self.flushed_node_list.append(node_name)
|
|
|
|
try:
|
|
|
|
self.active_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.inactive_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if node_state != 'start' and node_state != 'flush' and node_name not in self.inactive_node_list:
|
|
|
|
self.inactive_node_list.append(node_name)
|
|
|
|
try:
|
|
|
|
self.active_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.flushed_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2018-06-06 14:53:28 -04:00
|
|
|
# Display cluster information to the terminal
|
|
|
|
print('>>> {} - 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)))
|