+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[0]
+ node = args[1]
+
+ if command not in _LIST_POWER_COMMANDS:
+ ToStderr("power subcommand %s not supported." % command)
+ return constants.EXIT_FAILURE
+
+ oob_command = "power-%s" % command
+
+ opcodelist = []
+ if oob_command == constants.OOB_POWER_OFF:
+ opcodelist.append(opcodes.OpSetNodeParams(node_name=node, offline=True,
+ auto_promote=opts.auto_promote))
+
+ opcodelist.append(opcodes.OpOobCommand(node_name=node, command=oob_command))
+
+ 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]
+
+ if result:
+ if oob_command == constants.OOB_POWER_STATUS:
+ text = "The machine is %spowered"
+ if result[constants.OOB_POWER_STATUS_POWERED]:
+ result = text % ""
+ else:
+ result = text % "not "
+ ToStderr(result)
+
+ return constants.EXIT_SUCCESS
+
+