def make_bindnetaddr(): host = crm_script.host() hostinfo = crm_script.output(2)[host] ba = crm_script.param('bindnetaddr') if ba: return ba # if not, try to figure it out based # on preferred interface iface = crm_script.param('iface') if isinstance(iface, dict): iface = iface[host] interfaces = hostinfo['net']['interfaces'] if not iface: for info in interfaces: if info.get('Destination') == '0.0.0.0': iface = info.get('Iface') break try: for info in interfaces: if info.get('Iface') != iface: continue dst = info.get('Destination') if dst != '0.0.0.0': return info.get('Destination') except: pass crm_script.fail_exit("Could not discover appropriate bindnetaddr")
def run_corosync(): # create corosync.conf nodelist = crm_script.output(2).keys() nodelist_txt = "" for i, node in enumerate(nodelist): nodelist_txt += """ node { ring0_addr: %s nodeid: %s } """ % (node, i + 1) quorum_txt = "" if len(nodelist) == 1: quorum_txt = '' if len(nodelist) == 2: quorum_txt = """ two_node: 1 """ else: quorum_txt = """ provider: corosync_votequorum expected_votes: %s """ % ((len(nodelist) / 2) + 1) try: crm_script.save_template('./corosync.conf.template', '/etc/corosync/corosync.conf', bindnetaddr=make_bindnetaddr(), mcastaddr=crm_script.param('mcastaddr'), mcastport=crm_script.param('mcastport'), transport=crm_script.param('transport'), nodelist=nodelist_txt, quorum=quorum_txt) except Exception, e: crm_script.exit_fail(str(e))
def make_bindnetaddr(): host = crm_script.host() hostinfo = crm_script.output(1)[host] ba = crm_script.param('bindnetaddr') if ba: return ba # if not, try to figure it out based # on preferred interface iface = crm_script.param('iface') if isinstance(iface, dict): iface = iface[host] interfaces = hostinfo['net']['interfaces'] if not iface: for info in interfaces: if info.get('Destination') == '0.0.0.0': iface = info.get('Iface') break try: for info in interfaces: if info.get('Iface') != iface: continue dst = info.get('Destination') if dst != '0.0.0.0': return info.get('Destination') except: pass crm_script.fail_exit("Could not discover appropriate bindnetaddr")
def run_corosync(): # create corosync.conf nodelist = crm_script.output(1).keys() nodelist_txt = "" for i, node in enumerate(nodelist): nodelist_txt += """ node { ring0_addr: %s nodeid: %s } """ % (node, i + 1) quorum_txt = "" if len(nodelist) == 1: quorum_txt = '' if len(nodelist) == 2: quorum_txt = """ two_node: 1 """ else: quorum_txt = """ provider: corosync_votequorum expected_votes: %s """ % ((len(nodelist) / 2) + 1) try: crm_script.save_template('./corosync.conf.template', '/etc/corosync/corosync.conf', bindnetaddr=make_bindnetaddr(), mcastaddr=crm_script.param('mcastaddr'), mcastport=crm_script.param('mcastport'), transport=crm_script.param('transport'), nodelist=nodelist_txt, quorum=quorum_txt) except Exception, e: crm_script.exit_fail(str(e))
def configure_firewall(): _SUSE_FW_TEMPLATE = """## Name: HAE cluster ports ## Description: opens ports for HAE cluster services TCP="%(tcp)s" UDP="%(udp)s" """ corosync_mcastport = crm_script.param('mcastport') if not corosync_mcastport: rc, out, err = crm_script.call(['crm', 'corosync', 'get', 'totem.interface.mcastport']) if rc == 0: corosync_mcastport = out.strip() FW = '/etc/sysconfig/SuSEfirewall2' FW_CLUSTER = '/etc/sysconfig/SuSEfirewall2.d/services/cluster' tcp_ports = '30865 5560 7630 21064' udp_ports = '%s %s' % (corosync_mcastport, int(corosync_mcastport) - 1) if is_service_enabled('SuSEfirewall2'): if os.path.isfile(FW_CLUSTER): tmpl = open(FW_CLUSTER).read() tmpl = re.sub(r'^TCP="(.*)"', 'TCP="%s"' % (tcp_ports), tmpl, flags=re.M) tmpl = re.sub(r'^UDP="(.*)"', 'UDP="%s"' % (udp_ports), tmpl, flags=re.M) with open(FW_CLUSTER, 'w') as f: f.write(tmpl) elif os.path.isdir(os.path.dirname(FW_CLUSTER)): with open(FW_CLUSTER, 'w') as fwc: fwc.write(_SUSE_FW_TEMPLATE % {'tcp': tcp_ports, 'udp': udp_ports}) else: # neither the cluster file nor the services # directory exists crm_script.exit_fail("SUSE firewall is configured but %s does not exist" % os.path.dirname(FW_CLUSTER)) # add cluster to FW_CONFIGURATIONS_EXT if os.path.isfile(FW): txt = open(FW).read() m = re.search(r'^FW_CONFIGURATIONS_EXT="(.*)"', txt, re.M) if m: services = m.group(1).split() if 'cluster' not in services: services.append('cluster') txt = re.sub(r'^FW_CONFIGURATIONS_EXT="(.*)"', r'FW_CONFIGURATIONS_EXT="%s"' % (' '.join(services)), txt, flags=re.M) else: txt += '\nFW_CONFIGURATIONS_EXT="cluster"' with open(FW, 'w') as fw: fw.write(txt) if is_service_active('SuSEfirewall2'): crm_script.service('SuSEfirewall2', 'restart')
#!/usr/bin/env python import sys import crm_script host = crm_script.host() remove_nodes = crm_script.param('node').split(',') def run_collect(): crm_script.exit_ok(host) def run_validate(): data = crm_script.output(1) for node in remove_nodes: if data.get(node) != node: crm_script.exit_fail("%s not found or not responding: %s" % (node, data.get(node))) if host == node: crm_script.exit_fail("Call from another node: %s = %s" % (node, host)) crm_script.exit_ok(host) def run_apply(): for node in remove_nodes: rc, out, err = crm_script.call([ 'ssh', '-o', 'PasswordAuthentication=no', 'root@%s' % (node), 'systemctl stop corosync.service' ]) if rc != 0:
import crm_script rc, _, err = crm_script.call(['crm', 'cluster', 'wait_for_startup', '30']) if rc != 0: crm_script.exit_fail("Cluster not responding") def check_for_primitives(): rc, out, err = crm_script.call("crm configure show type:primitive | grep primitive", shell=True) if rc == 0 and out: return True return False if check_for_primitives(): crm_script.debug("Joined existing cluster - will not reconfigure") crm_script.exit_ok(True) try: nodelist = crm_script.param('nodes') crm_script.save_template('./basic.cib.template', './basic.cib') except IOError, e: crm_script.exit_fail("IO error: %s" % (str(e))) except ValueError, e: crm_script.exit_fail("Value error: %s" % (str(e))) rc, _, err = crm_script.call(['crm', 'configure', 'load', 'replace', './basic.cib']) if rc != 0: crm_script.exit_fail("Failed to load CIB configuration: %s" % (err)) crm_script.exit_ok(True)
#!/usr/bin/python3 import crm_script show_all = crm_script.is_true(crm_script.param('show_all')) uptimes = list(crm_script.output(1).items()) max_uptime = '', 0.0 for host, uptime in uptimes: if float(uptime) > max_uptime[1]: max_uptime = host, float(uptime) if show_all: print("Uptimes: %s" % (', '.join("%s: %s" % v for v in uptimes))) print("Longest uptime is %s seconds on host %s" % (max_uptime[1], max_uptime[0]))
for host, iface in selections.iteritems(): if not iface or invalid(host, iface): crm_script.exit_fail("No usable network interface on %s: %s" % (host, iface)) return user_iface def make_mcastaddr(): import random random.seed() b, c, d = random.randint(1, 254), random.randint(1, 254), random.randint(1, 254) return "%d.%d.%d.%d" % (239, b, c, d) try: data = crm_script.output(2) crm_init.verify(data) ret = {} ret["iface"] = select_interfaces(crm_script.param("iface"), data) if not crm_script.param("mcastaddr"): ret["mcastaddr"] = make_mcastaddr() crm_script.exit_ok(ret) except Exception, e: crm_script.exit_fail("Verification failed: %s" % (e))
if rc != 0: crm_script.exit_fail("Cluster not responding") def check_for_primitives(): rc, out, err = crm_script.call( "crm configure show type:primitive | grep primitive", shell=True) if rc == 0 and out: return True return False if check_for_primitives(): crm_script.debug("Joined existing cluster - will not reconfigure") crm_script.exit_ok(True) try: nodelist = crm_script.param('nodes') crm_script.save_template('./basic.cib.template', './basic.cib') except IOError, e: crm_script.exit_fail("IO error: %s" % (str(e))) except ValueError, e: crm_script.exit_fail("Value error: %s" % (str(e))) rc, _, err = crm_script.call( ['crm', 'configure', 'load', 'replace', './basic.cib']) if rc != 0: crm_script.exit_fail("Failed to load CIB configuration: %s" % (err)) crm_script.exit_ok(True)
return True for host, iface in selections.iteritems(): if not iface or invalid(host, iface): crm_script.exit_fail("No usable network interface on %s: %s" % (host, iface)) return user_iface def make_mcastaddr(): import random random.seed() b, c, d = random.randint(1, 254), random.randint(1, 254), random.randint(1, 254) return "%d.%d.%d.%d" % (239, b, c, d) try: data = crm_script.output(1) crm_init.verify(data) ret = {} ret['iface'] = select_interfaces(crm_script.param('iface'), data) if not crm_script.param('mcastaddr'): ret['mcastaddr'] = make_mcastaddr() crm_script.exit_ok(ret) except Exception, e: crm_script.exit_fail("Verification failed: %s" % (e))
crm_script.exit_fail("No usable network interface on %s: %s" % (host, iface)) return user_iface def make_mcastaddr(): import random random.seed() b, c, d = random.randint(1, 254), random.randint(1, 254), random.randint(1, 254) return "%d.%d.%d.%d" % (239, b, c, d) try: data = crm_script.output(1) crm_init.verify(data) ret = {} ret['iface'] = select_interfaces(crm_script.param('iface'), data) if not crm_script.param('mcastaddr'): ret['mcastaddr'] = make_mcastaddr() crm_script.exit_ok(ret) except Exception, e: crm_script.exit_fail("Verification failed: %s" % (e))
#!/usr/bin/env python import sys import crm_script host = crm_script.host() remove_nodes = crm_script.param('node').split(',') def run_collect(): crm_script.exit_ok(host) def run_validate(): data = crm_script.output(1) for node in remove_nodes: if data.get(node) != node: crm_script.exit_fail("%s not found or not responding: %s" % (node, data.get(node))) if host == node: crm_script.exit_fail("Call from another node: %s = %s" % (node, host)) crm_script.exit_ok(host) def run_apply(): for node in remove_nodes: rc, out, err = crm_script.call(['ssh', '-o', 'PasswordAuthentication=no', 'root@%s' % (node), 'systemctl stop corosync.service']) if rc != 0: crm_script.exit_fail("Failed to stop corosync on %s: %s" % (node, err))