diff --git a/fencenode.py b/fencenode.py index cf910711..03cce0ae 100644 --- a/fencenode.py +++ b/fencenode.py @@ -20,29 +20,21 @@ # ############################################################################### -import os, sys, libvirt, uuid, kazoo.client, time - -# ANSII colours for output -class bcolours: - PURPLE = '\033[95m' - BLUE = '\033[94m' - GREEN = '\033[92m' - YELLOW = '\033[93m' - RED = '\033[91m' - ENDC = '\033[0m' - BOLD = '\033[1m' - UNDERLINE = '\033[4m' +import os, sys, libvirt, uuid, kazoo.client, time, subprocess, re, ansiiprint # # Trigger function # def fence(node_name, zk): time.sleep(3) - print(bcolours.YELLOW + '>>> ' + bcolours.ENDC + 'Fencing node {} via IPMI reboot signal.'.format(node_name)) + ansiiprint.echo('Fencing node "{}" via IPMI reboot signal'.format(node_name), '', 'w') - # DO IPMI FENCE HERE + ipmi_address = zk.get('/nodes/{}/ipmiaddress'.format(node_name))[0].decode('ascii') + ipmi_username = zk.get('/nodes/{}/ipmiusername'.format(node_name))[0].decode('ascii') + ipmi_password = zk.get('/nodes/{}/ipmipassword'.format(node_name))[0].decode('ascii') + rebootViaIPMI(ipmi_address, ipmi_user, ipmi_password) - print(bcolours.BLUE + '>>> ' + bcolours.ENDC + 'Moving VMs from dead hypervisor {} to new hosts.'.format(node_name)) + ansiiprint.echo('Moving VMs from dead hypervisor "{}" to new hosts'.format(node_name), '', 'i') dead_node_running_domains = zk.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split() for dom_uuid in dead_node_running_domains: most_memfree = 0 @@ -58,9 +50,26 @@ def fence(node_name, zk): most_memfree = memfree target_hypervisor = hypervisor - print(bcolours.BLUE + '>>> ' + bcolours.ENDC + 'Moving VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor)) + ansiiprint.echo('Moving VM "{}" to hypervisor "{}"'.format(dom_uuid, target_hypervisor), '', 'i') transaction = zk.transaction() 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() + +def getIPMIAddress(): + ipmi_command = ['bash', '/fakeipmi.sh'] + + # Get the IPMI address + ipmi_lan_output = subprocess.run(ipmi_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + ipmi_lan_parsed = ipmi_lan_output.stdout.decode('ascii').split('\n') + ipmi_lan_address = [s for s in ipmi_lan_parsed if re.search('IP Address[ ]*:', s)][0].split(':')[-1].strip() + return ipmi_lan_address + +def rebootViaIPMI(ipmi_address, ipmi_user, ipmi_password): + ipmi_command = ['ipmitool', '-H', ipmi_address, '-U', ipmi_user, '-P', ipmi_password, 'chassis', 'power', 'reset'] + 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')