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-16 23:32:34 -04:00
|
|
|
import os, sys, psutil, socket, time, libvirt, kazoo.client, threading, subprocess
|
2018-06-17 14:55:13 -04:00
|
|
|
import pvcd.ansiiprint as ansiiprint
|
2018-06-06 22:56:03 -04:00
|
|
|
|
2018-06-06 14:16:52 -04:00
|
|
|
class NodeInstance():
|
2018-06-06 22:59:31 -04:00
|
|
|
# Initialization function
|
2018-06-17 21:55:39 -04:00
|
|
|
def __init__(self, this_node, name, t_node, s_domain, zk_conn, config):
|
2018-05-31 21:49:23 -04:00
|
|
|
# Passed-in variables on creation
|
2018-06-17 21:55:39 -04:00
|
|
|
self.zk_conn = zk_conn
|
2018-06-08 12:19:48 -04:00
|
|
|
self.config = config
|
2018-06-11 02:20:03 -04:00
|
|
|
self.this_node = this_node
|
2018-05-31 21:49:23 -04:00
|
|
|
self.name = name
|
2018-06-11 02:46:24 -04:00
|
|
|
self.daemon_state = 'stop'
|
|
|
|
self.domain_state = 'ready'
|
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 = []
|
2018-06-08 12:44:47 -04:00
|
|
|
self.ipmi_hostname = self.config['ipmi_hostname']
|
2018-06-11 01:50:06 -04:00
|
|
|
self.domains_count = 0
|
|
|
|
self.memused = 0
|
|
|
|
self.memfree = 0
|
2018-06-11 18:05:35 -04:00
|
|
|
self.inflush = False
|
2018-06-01 12:21:58 -04:00
|
|
|
|
|
|
|
# Zookeeper handlers for changed states
|
2018-06-17 21:55:39 -04:00
|
|
|
@zk_conn.DataWatch('/nodes/{}/daemonstate'.format(self.name))
|
2018-06-11 02:46:24 -04:00
|
|
|
def watch_hypervisor_daemonstate(data, stat, event=""):
|
2018-06-06 01:24:28 -04:00
|
|
|
try:
|
2018-06-11 02:46:24 -04:00
|
|
|
self.daemon_state = data.decode('ascii')
|
2018-06-06 01:24:28 -04:00
|
|
|
except AttributeError:
|
2018-06-11 02:46:24 -04:00
|
|
|
self.daemon_state = 'stop'
|
|
|
|
|
2018-06-17 21:55:39 -04:00
|
|
|
@zk_conn.DataWatch('/nodes/{}/domainstate'.format(self.name))
|
2018-06-11 02:46:24 -04:00
|
|
|
def watch_hypervisor_domainstate(data, stat, event=""):
|
|
|
|
try:
|
|
|
|
self.domain_state = data.decode('ascii')
|
|
|
|
except AttributeError:
|
2018-06-11 22:45:06 -04:00
|
|
|
self.domain_state = 'unknown'
|
|
|
|
|
2018-06-12 22:07:40 -04:00
|
|
|
# toggle state management of this node
|
2018-06-13 12:08:11 -04:00
|
|
|
if self.name == self.this_node:
|
|
|
|
if self.domain_state == 'flush' and self.inflush == False:
|
|
|
|
self.flush()
|
|
|
|
if self.domain_state == 'unflush' and self.inflush == False:
|
|
|
|
self.unflush()
|
2018-06-11 22:45:06 -04:00
|
|
|
|
2018-06-06 01:24:28 -04:00
|
|
|
|
2018-06-17 21:55:39 -04:00
|
|
|
@zk_conn.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-17 21:55:39 -04:00
|
|
|
@zk_conn.DataWatch('/nodes/{}/memused'.format(self.name))
|
2018-06-11 02:46:24 -04:00
|
|
|
def watch_hypervisor_memused(data, stat, event=""):
|
|
|
|
try:
|
|
|
|
self.memused = data.decode('ascii')
|
|
|
|
except AttributeError:
|
|
|
|
self.memused = 0
|
|
|
|
|
2018-06-17 21:55:39 -04:00
|
|
|
@zk_conn.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-06-17 21:55:39 -04:00
|
|
|
@zk_conn.DataWatch('/nodes/{}/domainscount'.format(self.name))
|
2018-06-11 02:46:24 -04:00
|
|
|
def watch_hypervisor_domainscount(data, stat, event=""):
|
|
|
|
try:
|
|
|
|
self.domains_count = data.decode('ascii')
|
|
|
|
except AttributeError:
|
|
|
|
self.domains_count = 0
|
|
|
|
|
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-06-11 02:46:24 -04:00
|
|
|
def getdaemonstate(self):
|
|
|
|
return self.daemon_state
|
|
|
|
|
|
|
|
def getdomainstate(self):
|
|
|
|
return self.domain_state
|
2018-05-31 23:40:21 -04:00
|
|
|
|
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-11 18:05:35 -04:00
|
|
|
self.inflush = True
|
2018-06-06 22:59:31 -04:00
|
|
|
ansiiprint.echo('Flushing node "{}" of running VMs'.format(self.name), '', 'i')
|
2018-06-11 22:50:52 -04:00
|
|
|
ansiiprint.echo('Domain list: {}'.format(', '.join(self.domain_list)), '', 'c')
|
2018-06-06 20:36:03 -04:00
|
|
|
for dom_uuid in self.domain_list:
|
|
|
|
most_memfree = 0
|
2018-06-06 21:31:20 -04:00
|
|
|
target_hypervisor = None
|
2018-06-26 21:52:22 -04:00
|
|
|
hypervisor_list = zkhander.listchildren(self.zk_conn, '/nodes')
|
|
|
|
current_hypervisor = zkhandler.readdata(self.zk_conn, '/domains/{}/hypervisor'.format(dom_uuid))
|
2018-06-13 12:12:00 -04:00
|
|
|
if current_hypervisor != self.this_node:
|
|
|
|
continue
|
|
|
|
|
2018-06-06 20:36:03 -04:00
|
|
|
for hypervisor in hypervisor_list:
|
2018-06-26 21:52:22 -04:00
|
|
|
daemon_state = zkhandler.readdata(self.zk_conn, '/nodes/{}/daemonstate'.format(hypervisor))
|
|
|
|
domain_state = zkhandler.readdata(self.zk_conn, '/nodes/{}/domainstate'.format(hypervisor))
|
2018-06-11 02:46:24 -04:00
|
|
|
if hypervisor == current_hypervisor:
|
|
|
|
continue
|
|
|
|
|
2018-06-12 21:56:34 -04:00
|
|
|
if daemon_state != 'run' or domain_state != 'ready':
|
2018-06-04 02:22:59 -04:00
|
|
|
continue
|
2018-06-06 20:36:03 -04:00
|
|
|
|
2018-06-26 21:52:22 -04:00
|
|
|
memfree = int(zkhandler.readdata(self.zk_conn, '/nodes/{}/memfree'.format(hypervisor)))
|
2018-06-06 20:36:03 -04:00
|
|
|
if memfree > most_memfree:
|
|
|
|
most_memfree = memfree
|
|
|
|
target_hypervisor = hypervisor
|
2018-06-11 21:23:57 -04:00
|
|
|
|
2018-06-06 21:28:58 -04:00
|
|
|
if target_hypervisor == None:
|
2018-06-06 22:59:31 -04:00
|
|
|
ansiiprint.echo('Failed to find migration target for VM "{}"; shutting down'.format(dom_uuid), '', 'e')
|
2018-06-17 21:55:39 -04:00
|
|
|
transaction = self.zk_conn.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-11 02:10:04 -04:00
|
|
|
ansiiprint.echo('Migrating VM "{}" to hypervisor "{}"'.format(dom_uuid, target_hypervisor), '', 'i')
|
2018-06-17 21:55:39 -04:00
|
|
|
transaction = self.zk_conn.transaction()
|
2018-06-06 21:35:32 -04:00
|
|
|
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
|
2018-06-06 20:36:03 -04:00
|
|
|
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-12 02:12:36 -04:00
|
|
|
transaction.commit()
|
2018-06-04 02:22:59 -04:00
|
|
|
|
2018-06-17 21:55:39 -04:00
|
|
|
self.zk_conn.set('/nodes/{}/runningdomains'.format(self.name), ''.encode('ascii'))
|
|
|
|
self.zk_conn.set('/nodes/{}/domainstate'.format(self.name), 'flushed'.encode('ascii'))
|
2018-06-11 18:05:35 -04:00
|
|
|
self.inflush = False
|
|
|
|
|
2018-06-04 03:09:51 -04:00
|
|
|
def unflush(self):
|
2018-06-11 18:05:35 -04:00
|
|
|
self.inflush = True
|
2018-06-06 22:59:31 -04:00
|
|
|
ansiiprint.echo('Restoring node {} to active service.'.format(self.name), '', 'i')
|
2018-06-17 21:55:39 -04:00
|
|
|
self.zk_conn.set('/nodes/{}/domainstate'.format(self.name), 'ready'.encode('ascii'))
|
2018-06-06 20:36:03 -04:00
|
|
|
for dom_uuid in self.s_domain:
|
2018-06-26 21:52:22 -04:00
|
|
|
last_hypervisor = zkhandler.readdata(self.zk_conn, '/domains/{}/lasthypervisor'.format(dom_uuid))
|
2018-06-06 20:36:03 -04:00
|
|
|
if last_hypervisor != self.name:
|
|
|
|
continue
|
|
|
|
|
2018-06-06 22:59:31 -04:00
|
|
|
ansiiprint.echo('Setting unmigration for VM "{}"'.format(dom_uuid), '', 'i')
|
2018-06-17 21:55:39 -04:00
|
|
|
transaction = self.zk_conn.transaction()
|
2018-06-06 20:36:03 -04:00
|
|
|
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'))
|
2018-06-12 02:12:36 -04:00
|
|
|
transaction.commit()
|
2018-06-04 03:09:51 -04:00
|
|
|
|
2018-06-11 18:05:35 -04:00
|
|
|
self.inflush = False
|
|
|
|
|
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"
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn = libvirt.open(libvirt_name)
|
|
|
|
if lv_conn == None:
|
2018-06-06 22:59:31 -04:00
|
|
|
ansiiprint.echo('Failed to open connection to "{}"'.format(libvirt_name), '', 'e')
|
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-26 21:52:22 -04:00
|
|
|
past_state = zkhandler.readdata(self.zk_conn, '/nodes/{}/daemonstate'.format(self.name))
|
2018-06-12 21:56:34 -04:00
|
|
|
if past_state != 'run':
|
|
|
|
self.daemon_state = 'run'
|
2018-06-17 21:55:39 -04:00
|
|
|
self.zk_conn.set('/nodes/{}/daemonstate'.format(self.name), 'run'.encode('ascii'))
|
2018-06-04 02:34:03 -04:00
|
|
|
else:
|
2018-06-12 21:56:34 -04:00
|
|
|
self.daemon_state = 'run'
|
2018-06-04 02:34:03 -04:00
|
|
|
|
2018-06-12 01:54:01 -04:00
|
|
|
# Toggle state management of dead VMs to restart them
|
2018-06-06 14:16:52 -04:00
|
|
|
for domain, instance in self.s_domain.items():
|
|
|
|
if instance.inshutdown == False and domain in self.domain_list:
|
2018-06-19 19:52:08 -04:00
|
|
|
if instance.getstate() == 'start' and instance.gethypervisor() == self.name:
|
|
|
|
if instance.getdom() != None:
|
|
|
|
try:
|
|
|
|
if instance.getdom().state()[0] != libvirt.VIR_DOMAIN_RUNNING:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
# Toggle a state "change"
|
|
|
|
self.zk_conn.set('/domains/{}/state'.format(domain), instance.getstate().encode('ascii'))
|
2018-06-02 14:37:49 -04:00
|
|
|
|
2018-06-06 14:16:52 -04:00
|
|
|
# Set our information in zookeeper
|
2018-06-17 21:55:39 -04:00
|
|
|
self.name = lv_conn.getHostname()
|
2018-06-11 01:55:36 -04:00
|
|
|
self.memused = int(psutil.virtual_memory().used / 1024 / 1024)
|
|
|
|
self.memfree = int(psutil.virtual_memory().free / 1024 / 1024)
|
2018-06-06 14:16:52 -04:00
|
|
|
self.cpuload = os.getloadavg()[0]
|
2018-06-17 21:55:39 -04:00
|
|
|
self.domains_count = len(lv_conn.listDomainsID())
|
2018-06-06 15:41:06 -04:00
|
|
|
keepalive_time = int(time.time())
|
2018-06-06 14:16:52 -04:00
|
|
|
try:
|
2018-06-17 21:55:39 -04:00
|
|
|
transaction = self.zk_conn.transaction()
|
2018-06-12 02:12:36 -04:00
|
|
|
transaction.set_data('/nodes/{}/memused'.format(self.name), str(self.memused).encode('ascii'))
|
|
|
|
transaction.set_data('/nodes/{}/memfree'.format(self.name), str(self.memfree).encode('ascii'))
|
|
|
|
transaction.set_data('/nodes/{}/cpuload'.format(self.name), str(self.cpuload).encode('ascii'))
|
|
|
|
transaction.set_data('/nodes/{}/runningdomains'.format(self.name), ' '.join(self.domain_list).encode('ascii'))
|
|
|
|
transaction.set_data('/nodes/{}/domainscount'.format(self.name), str(self.domains_count).encode('ascii'))
|
|
|
|
transaction.set_data('/nodes/{}/keepalive'.format(self.name), str(keepalive_time).encode('ascii'))
|
|
|
|
transaction.commit()
|
2018-06-06 14:16:52 -04:00
|
|
|
except:
|
|
|
|
return
|
|
|
|
|
2018-06-06 15:16:39 -04:00
|
|
|
# Close the Libvirt connection
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn.close()
|
2018-06-06 15:16:39 -04:00
|
|
|
|
2018-06-06 14:53:28 -04:00
|
|
|
# Display node information to the terminal
|
2018-06-07 00:39:09 -04:00
|
|
|
ansiiprint.echo('{}{} keepalive{}'.format(ansiiprint.purple(), self.name, ansiiprint.end()), '', 't')
|
2018-06-12 21:21:22 -04:00
|
|
|
ansiiprint.echo('{0}Active domains:{1} {2} {0}Free memory [MiB]:{1} {3} {0}Used memory [MiB]:{1} {4} {0}Load:{1} {5}'.format(ansiiprint.bold(), ansiiprint.end(), self.domains_count, self.memfree, self.memused, self.cpuload), '', 'c')
|
2018-06-06 14:53:28 -04:00
|
|
|
|
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-26 21:52:22 -04:00
|
|
|
node_daemon_state = zkhandler.readdata(self.zk_conn, '/nodes/{}/daemonstate'.format(node_name))
|
|
|
|
node_domain_state = zkhandler.readdata(self.zk_conn, '/nodes/{}/domainstate'.format(node_name))
|
|
|
|
node_keepalive = int(zkhandler.readdata(self.zk_conn, '/nodes/{}/keepalive'.format(node_name)))
|
2018-06-01 01:00:55 -04:00
|
|
|
except:
|
2018-06-11 19:04:59 -04:00
|
|
|
node_daemon_state = 'unknown'
|
|
|
|
node_domain_state = 'unknown'
|
2018-06-06 15:41:06 -04:00
|
|
|
node_keepalive = 0
|
|
|
|
|
2018-06-06 18:18:58 -04:00
|
|
|
# Handle deadtime and fencng if needed
|
2018-06-08 12:44:47 -04:00
|
|
|
# (A node is considered dead when its keepalive timer is >6*keepalive_interval seconds
|
|
|
|
# out-of-date while in 'start' state)
|
|
|
|
node_deadtime = int(time.time()) - ( int(self.config['keepalive_interval']) * 6 )
|
2018-06-12 21:56:34 -04:00
|
|
|
if node_keepalive < node_deadtime and node_daemon_state == 'run':
|
2018-06-22 12:37:45 -04:00
|
|
|
# CHECK VERSIONING HERE
|
2018-06-11 19:04:59 -04:00
|
|
|
ansiiprint.echo('Node {} seems dead - starting monitor for fencing'.format(node_name), '', 'w')
|
2018-06-17 21:55:39 -04:00
|
|
|
self.zk_conn.set('/nodes/{}/daemonstate'.format(node_name), 'dead'.encode('ascii'))
|
|
|
|
fence_thread = threading.Thread(target=fenceNode, args=(node_name, self.zk_conn), kwargs={})
|
2018-06-06 15:41:06 -04:00
|
|
|
fence_thread.start()
|
|
|
|
|
|
|
|
# Update the arrays
|
2018-06-12 21:56:34 -04:00
|
|
|
if node_daemon_state == 'run' and node_domain_state != 'flushed' and node_name not in self.active_node_list:
|
2018-06-06 14:16:52 -04:00
|
|
|
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
|
2018-06-12 21:56:34 -04:00
|
|
|
if node_daemon_state != 'run' and node_domain_state != 'flushed' and node_name not in self.inactive_node_list:
|
2018-06-11 19:04:59 -04:00
|
|
|
self.inactive_node_list.append(node_name)
|
2018-06-06 14:16:52 -04:00
|
|
|
try:
|
|
|
|
self.active_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
2018-06-11 19:04:59 -04:00
|
|
|
self.flushed_node_list.remove(node_name)
|
2018-06-06 14:16:52 -04:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2018-06-11 19:07:10 -04:00
|
|
|
if node_domain_state == 'flushed' and node_name not in self.flushed_node_list:
|
2018-06-11 19:04:59 -04:00
|
|
|
self.flushed_node_list.append(node_name)
|
2018-06-06 14:16:52 -04:00
|
|
|
try:
|
|
|
|
self.active_node_list.remove(node_name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
2018-06-11 19:04:59 -04:00
|
|
|
self.inactive_node_list.remove(node_name)
|
2018-06-06 14:16:52 -04:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2018-06-06 14:53:28 -04:00
|
|
|
# Display cluster information to the terminal
|
2018-06-07 00:39:09 -04:00
|
|
|
ansiiprint.echo('{}Cluster status{}'.format(ansiiprint.purple(), ansiiprint.end()), '', 't')
|
|
|
|
ansiiprint.echo('{}Active nodes:{} {}'.format(ansiiprint.bold(), ansiiprint.end(), ' '.join(self.active_node_list)), '', 'c')
|
|
|
|
ansiiprint.echo('{}Inactive nodes:{} {}'.format(ansiiprint.bold(), ansiiprint.end(), ' '.join(self.inactive_node_list)), '', 'c')
|
2018-06-11 19:04:59 -04:00
|
|
|
ansiiprint.echo('{}Flushed nodes:{} {}'.format(ansiiprint.bold(), ansiiprint.end(), ' '.join(self.flushed_node_list)), '', 'c')
|
2018-06-14 12:00:48 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Fence thread entry function
|
|
|
|
#
|
2018-06-17 21:55:39 -04:00
|
|
|
def fenceNode(node_name, zk_conn):
|
2018-06-14 12:00:48 -04:00
|
|
|
failcount = 0
|
|
|
|
while failcount < 3:
|
|
|
|
# Wait 5 seconds
|
|
|
|
time.sleep(5)
|
|
|
|
# Get the state
|
2018-06-26 21:52:22 -04:00
|
|
|
node_daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node_name))
|
2018-06-14 12:00:48 -04:00
|
|
|
# Is it still 'dead'
|
|
|
|
if node_daemon_state == 'dead':
|
|
|
|
failcount += 1
|
|
|
|
ansiiprint.echo('Node "{}" failed {} saving throws'.format(node_name, failcount), '', 'w')
|
|
|
|
# It changed back to something else so it must be alive
|
|
|
|
else:
|
|
|
|
ansiiprint.echo('Node "{}" passed a saving throw; canceling fence'.format(node_name), '', 'o')
|
|
|
|
return
|
|
|
|
|
|
|
|
ansiiprint.echo('Fencing node "{}" via IPMI reboot signal'.format(node_name), '', 'e')
|
|
|
|
|
2018-06-26 21:52:22 -04:00
|
|
|
ipmi_hostname = zkhandler.readdata(zk_conn, '/nodes/{}/ipmihostname'.format(node_name))
|
|
|
|
ipmi_username = zkhandler.readdata(zk_conn, '/nodes/{}/ipmiusername'.format(node_name))
|
|
|
|
ipmi_password = zkhandler.readdata(zk_conn, '/nodes/{}/ipmipassword'.format(node_name))
|
2018-06-16 23:29:28 -04:00
|
|
|
rebootViaIPMI(ipmi_hostname, ipmi_username, ipmi_password)
|
2018-06-17 02:19:08 -04:00
|
|
|
time.sleep(5)
|
2018-06-14 12:00:48 -04:00
|
|
|
|
|
|
|
ansiiprint.echo('Moving VMs from dead hypervisor "{}" to new hosts'.format(node_name), '', 'i')
|
2018-06-26 21:52:22 -04:00
|
|
|
dead_node_running_domains = zkhandler.readdata(zk_conn, '/nodes/{}/runningdomains'.format(node_name)).split()
|
2018-06-14 12:00:48 -04:00
|
|
|
for dom_uuid in dead_node_running_domains:
|
|
|
|
most_memfree = 0
|
2018-06-26 21:52:22 -04:00
|
|
|
hypervisor_list = zkhandler.listchildren(zk_conn, '/nodes')
|
|
|
|
current_hypervisor = zkhandler.readdata(zk_conn, '/domains/{}/hypervisor'.format(dom_uuid))
|
2018-06-14 12:00:48 -04:00
|
|
|
for hypervisor in hypervisor_list:
|
2018-06-17 01:55:11 -04:00
|
|
|
print(hypervisor)
|
2018-06-26 21:52:22 -04:00
|
|
|
daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(hypervisor))
|
|
|
|
domain_state = zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(hypervisor))
|
2018-06-17 02:08:40 -04:00
|
|
|
if daemon_state != 'run' or domain_state != 'ready':
|
2018-06-14 12:00:48 -04:00
|
|
|
continue
|
|
|
|
|
2018-06-26 21:52:22 -04:00
|
|
|
memfree = int(zkhandler.readdata(zk_conn, '/nodes/{}/memfree'.format(hypervisor)))
|
2018-06-14 12:00:48 -04:00
|
|
|
if memfree > most_memfree:
|
|
|
|
most_memfree = memfree
|
|
|
|
target_hypervisor = hypervisor
|
|
|
|
|
|
|
|
ansiiprint.echo('Moving VM "{}" to hypervisor "{}"'.format(dom_uuid, target_hypervisor), '', 'i')
|
2018-06-17 21:55:39 -04:00
|
|
|
transaction = zk_conn.transaction()
|
2018-06-14 12:00:48 -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'))
|
|
|
|
transaction.commit()
|
|
|
|
|
2018-06-17 02:08:40 -04:00
|
|
|
# Set node in flushed state for easy remigrating when it comes back
|
2018-06-17 21:55:39 -04:00
|
|
|
zk_conn.set('/nodes/{}/domainstate'.format(node_name), 'flushed'.encode('ascii'))
|
2018-06-17 02:08:40 -04:00
|
|
|
|
2018-06-14 12:00:48 -04:00
|
|
|
#
|
|
|
|
# Perform an IPMI fence
|
|
|
|
#
|
|
|
|
def rebootViaIPMI(ipmi_hostname, ipmi_user, ipmi_password):
|
2018-06-16 23:34:12 -04:00
|
|
|
ipmi_command = ['/usr/bin/ipmitool', '-H', ipmi_hostname, '-U', ipmi_user, '-P', ipmi_password, 'chassis', 'power', 'reset']
|
2018-06-14 12:00:48 -04:00
|
|
|
ipmi_command_output = subprocess.run(ipmi_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
if ipmi_command_output == 0:
|
|
|
|
ansiiprint.echo('Successfully rebooted dead node', '', 'o')
|
|
|
|
else:
|
|
|
|
ansiiprint.echo('Failed to reboot dead node', '', 'e')
|