2018-10-22 20:20:27 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# fencing.py - PVC daemon function library, node fencing functions
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2020-01-08 19:38:02 -05:00
|
|
|
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
2018-10-22 20:20:27 -04:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2020-02-08 19:16:19 -05:00
|
|
|
import pvcnoded.zkhandler as zkhandler
|
|
|
|
import pvcnoded.common as common
|
|
|
|
import pvcnoded.VMInstance as VMInstance
|
2018-10-22 20:20:27 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Fence thread entry function
|
|
|
|
#
|
|
|
|
def fenceNode(node_name, zk_conn, config, logger):
|
2020-08-05 22:36:28 -04:00
|
|
|
# We allow exactly 6 saving throws (30 seconds) for the host to come back online or we kill it
|
|
|
|
failcount_limit = 6
|
2018-10-22 20:20:27 -04:00
|
|
|
failcount = 0
|
2020-08-05 22:36:28 -04:00
|
|
|
while failcount < failcount_limit:
|
2018-10-22 20:20:27 -04:00
|
|
|
# Wait 5 seconds
|
2020-08-15 12:38:03 -04:00
|
|
|
time.sleep(config.keepalive_interval)
|
2018-10-22 20:20:27 -04:00
|
|
|
# Get the state
|
|
|
|
node_daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node_name))
|
|
|
|
# Is it still 'dead'
|
|
|
|
if node_daemon_state == 'dead':
|
|
|
|
failcount += 1
|
2020-08-05 22:36:28 -04:00
|
|
|
logger.out('Node "{}" failed {}/{} saving throws'.format(node_name, failcount, failcount_limit), state='w')
|
2018-10-22 20:20:27 -04:00
|
|
|
# It changed back to something else so it must be alive
|
|
|
|
else:
|
|
|
|
logger.out('Node "{}" passed a saving throw; canceling fence'.format(node_name), state='o')
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.out('Fencing node "{}" via IPMI reboot signal'.format(node_name), state='w')
|
|
|
|
|
|
|
|
# Get IPMI information
|
|
|
|
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))
|
|
|
|
|
|
|
|
# Shoot it in the head
|
|
|
|
fence_status = rebootViaIPMI(ipmi_hostname, ipmi_username, ipmi_password, logger)
|
2020-08-15 12:38:03 -04:00
|
|
|
# Hold to ensure the fence takes effect and system stabilizes
|
|
|
|
time.sleep(config.keepalive_interval * 2)
|
2018-10-22 20:20:27 -04:00
|
|
|
|
|
|
|
# Force into secondary network state if needed
|
2019-03-13 19:26:08 -04:00
|
|
|
if node_name in config['coordinators']:
|
2020-08-11 12:40:35 -04:00
|
|
|
logger.out('Forcing secondary status for node "{}"'.format(node_name), state='i')
|
2018-10-22 20:20:27 -04:00
|
|
|
zkhandler.writedata(zk_conn, { '/nodes/{}/routerstate'.format(node_name): 'secondary' })
|
|
|
|
if zkhandler.readdata(zk_conn, '/primary_node') == node_name:
|
|
|
|
zkhandler.writedata(zk_conn, { '/primary_node': 'none' })
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2018-10-22 20:20:27 -04:00
|
|
|
# If the fence succeeded and successful_fence is migrate
|
2020-08-05 21:57:36 -04:00
|
|
|
if fence_status and config['successful_fence'] == 'migrate':
|
2019-10-12 01:17:39 -04:00
|
|
|
migrateFromFencedNode(zk_conn, node_name, config, logger)
|
2018-10-22 20:20:27 -04:00
|
|
|
|
|
|
|
# If the fence failed and failed_fence is migrate
|
2020-08-05 21:57:36 -04:00
|
|
|
if not fence_status and config['failed_fence'] == 'migrate' and config['suicide_intervals'] != '0':
|
2019-10-12 01:17:39 -04:00
|
|
|
migrateFromFencedNode(zk_conn, node_name, config, logger)
|
2018-10-22 20:20:27 -04:00
|
|
|
|
|
|
|
# Migrate hosts away from a fenced node
|
2019-10-12 01:17:39 -04:00
|
|
|
def migrateFromFencedNode(zk_conn, node_name, config, logger):
|
2019-07-10 11:54:56 -04:00
|
|
|
logger.out('Migrating VMs from dead node "{}" to new hosts'.format(node_name), state='i')
|
2020-08-05 22:26:01 -04:00
|
|
|
|
|
|
|
# Get the list of VMs
|
2018-10-22 20:20:27 -04:00
|
|
|
dead_node_running_domains = zkhandler.readdata(zk_conn, '/nodes/{}/runningdomains'.format(node_name)).split()
|
2020-08-05 22:26:01 -04:00
|
|
|
|
|
|
|
# Set the node to a custom domainstate so we know what's happening
|
|
|
|
zkhandler.writedata(zk_conn, { '/nodes/{}/domainstate'.format(node_name): 'fence-flush' })
|
|
|
|
|
|
|
|
# Migrate a VM after a flush
|
|
|
|
def fence_migrate_vm(dom_uuid):
|
2019-08-07 13:36:56 -04:00
|
|
|
VMInstance.flush_locks(zk_conn, logger, dom_uuid)
|
2019-07-09 19:17:53 -04:00
|
|
|
|
2019-10-12 01:59:08 -04:00
|
|
|
target_node = common.findTargetNode(zk_conn, config, dom_uuid)
|
2018-11-23 20:02:31 -05:00
|
|
|
|
2019-10-12 01:17:39 -04:00
|
|
|
if target_node is not None:
|
|
|
|
logger.out('Migrating VM "{}" to node "{}"'.format(dom_uuid, target_node), state='i')
|
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}/state'.format(dom_uuid): 'start',
|
|
|
|
'/domains/{}/node'.format(dom_uuid): target_node,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): node_name
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
logger.out('No target node found for VM "{}"; VM will autostart on next unflush/ready of current node'.format(dom_uuid), state='i')
|
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}/state'.format(dom_uuid): 'stopped',
|
|
|
|
'/domains/{}/node_autostart'.format(dom_uuid): 'True'
|
2019-10-12 01:36:50 -04:00
|
|
|
})
|
2018-10-22 20:20:27 -04:00
|
|
|
|
2020-08-05 22:26:01 -04:00
|
|
|
# Loop through the VMs
|
|
|
|
for dom_uuid in dead_node_running_domains:
|
|
|
|
fence_migrate_vm(dom_uuid)
|
|
|
|
|
2018-10-22 20:20:27 -04:00
|
|
|
# Set node in flushed state for easy remigrating when it comes back
|
|
|
|
zkhandler.writedata(zk_conn, { '/nodes/{}/domainstate'.format(node_name): 'flushed' })
|
|
|
|
|
|
|
|
#
|
|
|
|
# Perform an IPMI fence
|
|
|
|
#
|
|
|
|
def rebootViaIPMI(ipmi_hostname, ipmi_user, ipmi_password, logger):
|
2018-11-23 20:02:31 -05:00
|
|
|
# Forcibly reboot the node
|
|
|
|
ipmi_command_reset = '/usr/bin/ipmitool -I lanplus -H {} -U {} -P {} chassis power reset'.format(
|
2018-10-22 20:20:27 -04:00
|
|
|
ipmi_hostname, ipmi_user, ipmi_password
|
|
|
|
)
|
2018-11-23 20:02:31 -05:00
|
|
|
ipmi_reset_retcode, ipmi_reset_stdout, ipmi_reset_stderr = common.run_os_command(ipmi_command_reset)
|
|
|
|
|
2019-08-07 11:35:49 -04:00
|
|
|
time.sleep(2)
|
2018-11-23 20:02:31 -05:00
|
|
|
|
|
|
|
# Ensure the node is powered on
|
|
|
|
ipmi_command_status = '/usr/bin/ipmitool -I lanplus -H {} -U {} -P {} chassis power status'.format(
|
|
|
|
ipmi_hostname, ipmi_user, ipmi_password
|
|
|
|
)
|
|
|
|
ipmi_status_retcode, ipmi_status_stdout, ipmi_status_stderr = common.run_os_command(ipmi_command_status)
|
|
|
|
|
|
|
|
# Trigger a power start if needed
|
|
|
|
if ipmi_status_stdout != "Chassis Power is on":
|
2018-12-07 12:36:53 -05:00
|
|
|
ipmi_command_start = '/usr/bin/ipmitool -I lanplus -H {} -U {} -P {} chassis power on'.format(
|
2018-11-23 20:02:31 -05:00
|
|
|
ipmi_hostname, ipmi_user, ipmi_password
|
|
|
|
)
|
|
|
|
ipmi_start_retcode, ipmi_start_stdout, ipmi_start_stderr = common.run_os_command(ipmi_command_start)
|
|
|
|
|
|
|
|
# Declare success or failure
|
|
|
|
if ipmi_reset_retcode == 0:
|
2018-10-22 20:20:27 -04:00
|
|
|
logger.out('Successfully rebooted dead node', state='o')
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
logger.out('Failed to reboot dead node', state='e')
|
2019-08-07 11:35:49 -04:00
|
|
|
print(ipmi_reset_stderr)
|
2018-10-22 20:20:27 -04:00
|
|
|
return False
|
2020-08-13 14:38:05 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Verify that IPMI connectivity to this host exists (used during node init)
|
|
|
|
#
|
|
|
|
def verifyIPMI(ipmi_hostname, ipmi_user, ipmi_password):
|
|
|
|
ipmi_command_status = '/usr/bin/ipmitool -I lanplus -H {} -U {} -P {} chassis power status'.format(
|
|
|
|
ipmi_hostname, ipmi_user, ipmi_password
|
|
|
|
)
|
|
|
|
ipmi_status_retcode, ipmi_status_stdout, ipmi_status_stderr = common.run_os_command(ipmi_command_status, timeout=2)
|
|
|
|
if ipmi_status_retcode == 0 and ipmi_status_stdout != "Chassis Power is on":
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|