def SetNodeParams(opts, args): """Modifies a node. @param opts: the command line options selected by the user @type args: list @param args: should contain only one element, the node name @rtype: int @return: the desired exit code """ all_changes = [opts.master_candidate, opts.drained, opts.offline, opts.master_capable, opts.vm_capable, opts.secondary_ip, opts.ndparams] if (all_changes.count(None) == len(all_changes) and not (opts.hv_state or opts.disk_state)): ToStderr("Please give at least one of the parameters.") return 1 if opts.disk_state: disk_state = utils.FlatToDict(opts.disk_state) else: disk_state = {} # Comparing explicitly to false to distinguish between a parameter # modification that doesn't set the node online (where the value will be None) # and modifying the node to bring it online. if opts.offline is False: usertext = ("You are setting this node online manually. If the" " configuration has changed, this can cause issues such as" " split brain. To safely bring a node back online, please use" " --readd instead. If you are confident that the configuration" " hasn't changed, continue?") if not AskUser(usertext): return 1 hv_state = dict(opts.hv_state) op = opcodes.OpNodeSetParams(node_name=args[0], master_candidate=opts.master_candidate, offline=opts.offline, drained=opts.drained, master_capable=opts.master_capable, vm_capable=opts.vm_capable, secondary_ip=opts.secondary_ip, force=opts.force, ndparams=opts.ndparams, auto_promote=opts.auto_promote, powered=opts.node_powered, hv_state=hv_state, disk_state=disk_state) # even if here we process the result, we allow submit only result = SubmitOrSend(op, opts) if result: ToStdout("Modified node %s", args[0]) for param, data in result: ToStdout(" - %-5s -> %s", param, data) return 0
def testSetSecondaryIpNoLock(self): self.instance = self.cfg.AddNewInstance(primary_node=self.node, secondary_node=self.snode, disk_template='file') op = opcodes.OpNodeSetParams(node_name=self.node.name, secondary_ip='254.254.254.254') self.ExecOpCode(op) self.assertEqual('254.254.254.254', self.node.secondary_ip) self.assertEqual(sorted(self.wconfd.all_locks.items()), [('cluster/BGL', 'shared'), ('node-res/blue_bunny', 'exclusive'), ('node/blue_bunny', 'exclusive')])
def SetNodeParams(opts, args): """Modifies a node. @param opts: the command line options selected by the user @type args: list @param args: should contain only one element, the node name @rtype: int @return: the desired exit code """ all_changes = [ opts.master_candidate, opts.drained, opts.offline, opts.master_capable, opts.vm_capable, opts.secondary_ip, opts.ndparams ] if (all_changes.count(None) == len(all_changes) and not (opts.hv_state or opts.disk_state)): ToStderr("Please give at least one of the parameters.") return 1 if opts.disk_state: disk_state = utils.FlatToDict(opts.disk_state) else: disk_state = {} hv_state = dict(opts.hv_state) op = opcodes.OpNodeSetParams(node_name=args[0], master_candidate=opts.master_candidate, offline=opts.offline, drained=opts.drained, master_capable=opts.master_capable, vm_capable=opts.vm_capable, secondary_ip=opts.secondary_ip, force=opts.force, ndparams=opts.ndparams, auto_promote=opts.auto_promote, powered=opts.node_powered, hv_state=hv_state, disk_state=disk_state, verbose=opts.verbose, debug=opts.debug > 0) # even if here we process the result, we allow submit only result = SubmitOrSend(op, opts) if result: ToStdout("Modified node %s", args[0]) for param, data in result: ToStdout(" - %-5s -> %s", param, data) return 0
def testSetSecondaryIp(self): self.instance = self.cfg.AddNewInstance(primary_node=self.node, secondary_node=self.snode, disk_template='drbd') op = opcodes.OpNodeSetParams(node_name=self.node.name, secondary_ip='254.254.254.254') self.ExecOpCode(op) self.assertEqual('254.254.254.254', self.node.secondary_ip) self.assertEqual( sorted(self.wconfd.all_locks.items()), [('cluster/BGL', 'shared'), ('instance/mock_inst_1.example.com', 'shared'), ('node-res/00000000-dead-beef-dead-beefdeadbeef', 'exclusive'), ('node/00000000-dead-beef-dead-beefdeadbeef', 'exclusive')])
def PowerNode(opts, args): """Change/ask power state of a node. @param opts: the command line options selected by the user @type args: list @param args: should contain only one element, the name of the node to be removed @rtype: int @return: the desired exit code """ command = args.pop(0) if opts.no_headers: headers = None else: headers = {"node": "Node", "status": "Status"} if command not in _LIST_POWER_COMMANDS: ToStderr("power subcommand %s not supported." % command) return constants.EXIT_FAILURE oob_command = "power-%s" % command if oob_command in _OOB_COMMAND_ASK: if not args: ToStderr("Please provide at least one node for this command") return constants.EXIT_FAILURE elif not opts.force and not ConfirmOperation(args, "nodes", "power %s" % command): return constants.EXIT_FAILURE assert len(args) > 0 opcodelist = [] if not opts.ignore_status and oob_command == constants.OOB_POWER_OFF: # TODO: This is a little ugly as we can't catch and revert for node in args: opcodelist.append(opcodes.OpNodeSetParams(node_name=node, offline=True, auto_promote=opts.auto_promote)) opcodelist.append(opcodes.OpOobCommand(node_names=args, command=oob_command, ignore_status=opts.ignore_status, timeout=opts.oob_timeout, power_delay=opts.power_delay)) cli.SetGenericOpcodeOpts(opcodelist, opts) job_id = cli.SendJob(opcodelist) # We just want the OOB Opcode status # If it fails PollJob gives us the error message in it result = cli.PollJob(job_id)[-1] errs = 0 data = [] for node_result in result: (node_tuple, data_tuple) = node_result (_, node_name) = node_tuple (data_status, data_node) = data_tuple if data_status == constants.RS_NORMAL: if oob_command == constants.OOB_POWER_STATUS: if data_node[constants.OOB_POWER_STATUS_POWERED]: text = "powered" else: text = "unpowered" data.append([node_name, text]) else: # We don't expect data here, so we just say, it was successfully invoked data.append([node_name, "invoked"]) else: errs += 1 data.append([node_name, cli.FormatResultError(data_status, True)]) data = GenerateTable(separator=opts.separator, headers=headers, fields=["node", "status"], data=data) for line in data: ToStdout(line) if errs: return constants.EXIT_FAILURE else: return constants.EXIT_SUCCESS