def del_node(addr): ''' Remove node from corosync ''' f = open(conf()).read() p = Parser(f) nth = p.remove_section_where('nodelist.node', 'ring0_addr', addr) if nth == -1: return if p.count('nodelist.node') <= 2: p.set('quorum.two_node', '1') f = open(conf(), 'w') f.write(p.to_string()) f.close() # check for running config try: nodes = utils.list_cluster_nodes() except Exception: nodes = [] if nodes: utils.ext_cmd(["corosync-cmapctl", "-D", "nodelist.node.%s.nodeid" % (nth)], shell=False) utils.ext_cmd(["corosync-cmapctl", "-D", "nodelist.node.%s.ring0_addr" % (nth)], shell=False)
def _push_completer(args): try: n = utils.list_cluster_nodes() n.remove(utils.this_node()) return n except: n = []
def del_node(addr): ''' Remove node from corosync ''' f = open(conf()).read() p = Parser(f) nth = p.remove_section_where('nodelist.node', 'ring0_addr', addr) if nth == -1: return if p.count('nodelist.node') <= 2: p.set('quorum.two_node', '1') f = open(conf(), 'w') f.write(p.to_string()) f.close() # check for running config try: nodes = utils.list_cluster_nodes() except Exception: nodes = [] if nodes: utils.ext_cmd( ["corosync-cmapctl", "-D", "nodelist.node.%s.nodeid" % (nth)], shell=False) utils.ext_cmd( ["corosync-cmapctl", "-D", "nodelist.node.%s.ring0_addr" % (nth)], shell=False)
def do_push(self, context, *nodes): ''' Push corosync configuration to other cluster nodes. If no nodes are provided, configuration is pushed to all other cluster nodes. ''' if not nodes: nodes = utils.list_cluster_nodes() nodes.remove(utils.this_node()) return corosync.push_configuration(nodes)
def _filter_nodes(nodes, user, port): 'filter function for the nodes element' if nodes: nodes = nodes.replace(',', ' ').split() else: nodes = utils.list_cluster_nodes() if not nodes: raise ValueError("No hosts") nodes = [(node, port or None, user or None) for node in nodes] return nodes
def do_diff(self, context, *nodes): ''' Compare corosync configuration between nodes. ''' checksum = False if nodes and nodes[0] == '--checksum': checksum = True nodes = nodes[1:] if not nodes: nodes = utils.list_cluster_nodes() return corosync.diff_configuration(nodes, checksum=checksum)
def add_node(name): ''' Add node to corosync.conf ''' coronodes = None nodes = None coronodes = utils.list_corosync_nodes() try: nodes = utils.list_cluster_nodes() except Exception: nodes = [] ipaddr = get_ip(name) if name in coronodes or (ipaddr and ipaddr in coronodes): err_buf.warning("%s already in corosync.conf" % (name)) return if name in nodes: err_buf.warning("%s already in configuration" % (name)) return f = open(conf()).read() p = Parser(f) node_addr = name node_id = next_nodeid(p) p.add( 'nodelist', make_section( 'nodelist.node', make_value('nodelist.node.ring0_addr', node_addr) + make_value('nodelist.node.nodeid', str(node_id)))) num_nodes = p.count('nodelist.node') if num_nodes > 2: p.remove('quorum.two_node') f = open(conf(), 'w') f.write(p.to_string()) f.close() # update running config (if any) if nodes: utils.ext_cmd([ "corosync-cmapctl", "-s", "nodelist.node.%s.nodeid" % (num_nodes - 1), "u32", str(node_id) ], shell=False) utils.ext_cmd([ "corosync-cmapctl", "-s", "nodelist.node.%s.ring0_addr" % (num_nodes - 1), "str", node_addr ], shell=False)
def add_node(name): ''' Add node to corosync.conf ''' coronodes = None nodes = None coronodes = utils.list_corosync_nodes() try: nodes = utils.list_cluster_nodes() except Exception: nodes = [] ipaddr = get_ip(name) if name in coronodes or (ipaddr and ipaddr in coronodes): err_buf.warning("%s already in corosync.conf" % (name)) return if name in nodes: err_buf.warning("%s already in configuration" % (name)) return f = open(conf()).read() p = Parser(f) node_addr = name node_id = next_nodeid(p) p.add('nodelist', make_section('nodelist.node', make_value('nodelist.node.ring0_addr', node_addr) + make_value('nodelist.node.nodeid', str(node_id)))) num_nodes = p.count('nodelist.node') if num_nodes > 2: p.remove('quorum.two_node') f = open(conf(), 'w') f.write(p.to_string()) f.close() # update running config (if any) if nodes: utils.ext_cmd(["corosync-cmapctl", "-s", "nodelist.node.%s.nodeid" % (num_nodes - 1), "u32", str(node_id)], shell=False) utils.ext_cmd(["corosync-cmapctl", "-s", "nodelist.node.%s.ring0_addr" % (num_nodes - 1), "str", node_addr], shell=False)
def do_diff(self, context, filename, *nodes): "usage: diff <filename> [--checksum] [nodes...]. Diff file across cluster." this_node = utils.this_node() checksum = False if len(nodes) and nodes[0] == '--checksum': nodes = nodes[1:] checksum = True if not nodes: nodes = utils.list_cluster_nodes() if checksum: utils.remote_checksum(filename, nodes, this_node) elif len(nodes) == 1: utils.remote_diff_this(filename, nodes, this_node) elif this_node in nodes: nodes.remove(this_node) utils.remote_diff_this(filename, nodes, this_node) elif len(nodes): utils.remote_diff(filename, nodes)
def do_add(self, context, *args): ''' Add the given node(s) to the cluster. Installs packages, sets up corosync and pacemaker, etc. Must be executed from a node in the existing cluster. ''' params = self._args_implicit(context, args, 'node') paramdict = utils.nvpairs2dict(params) node = paramdict.get('node') if node: node = node.replace(',', ' ').split() else: node = [] nodes = paramdict.get('nodes') if not nodes: nodes = utils.list_cluster_nodes() nodes += node params += ['nodes=%s' % (','.join(nodes))] return scripts.run('add', params)
def do_run(self, context, cmd): ''' Execute the given command on all nodes, report outcome ''' try: from psshlib import api as pssh _has_pssh = True except ImportError: _has_pssh = False if not _has_pssh: context.fatal_error("PSSH not found") hosts = utils.list_cluster_nodes() opts = pssh.Options() for host, result in pssh.call(hosts, cmd, opts).iteritems(): if isinstance(result, pssh.Error): err_buf.error("[%s]: %s" % (host, result)) else: if result[0] != 0: err_buf.error("[%s]: rc=%s\n%s\n%s" % (host, result[0], result[1], result[2])) else: err_buf.ok("[%s]\n%s" % (host, result[1]))
def _node_in_cluster(self, node): return node in utils.list_cluster_nodes()
def _all_nodes(args): try: return utils.list_cluster_nodes() except: return []
def _remove_completer(args): try: n = utils.list_cluster_nodes() except: n = [] return scripts.param_completion_list('remove') + n