+def RestrictedCommand(opts, args):
+ """Runs a remote command on node(s).
+
+ @param opts: Command line options selected by user
+ @type args: list
+ @param args: Command line arguments
+ @rtype: int
+ @return: Exit code
+
+ """
+ cl = GetClient()
+
+ if len(args) > 1 or opts.nodegroup:
+ # Expand node names
+ nodes = GetOnlineNodes(nodes=args[1:], cl=cl, nodegroup=opts.nodegroup)
+ else:
+ raise errors.OpPrereqError("Node group or node names must be given",
+ errors.ECODE_INVAL)
+
+ op = opcodes.OpRestrictedCommand(command=args[0], nodes=nodes,
+ use_locking=opts.do_locking)
+ result = SubmitOrSend(op, opts, cl=cl)
+
+ exit_code = constants.EXIT_SUCCESS
+
+ for (node, (status, text)) in zip(nodes, result):
+ ToStdout("------------------------------------------------")
+ if status:
+ if opts.show_machine_names:
+ for line in text.splitlines():
+ ToStdout("%s: %s", node, line)
+ else:
+ ToStdout("Node: %s", node)
+ ToStdout(text)
+ else:
+ exit_code = constants.EXIT_FAILURE
+ ToStdout(text)
+
+ return exit_code
+
+
+class ReplyStatus(object):
+ """Class holding a reply status for synchronous confd clients.
+
+ """
+ def __init__(self):
+ self.failure = True
+ self.answer = False
+
+
+def ListDrbd(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
+
+ """
+ if len(args) != 1:
+ ToStderr("Please give one (and only one) node.")
+ return constants.EXIT_FAILURE
+
+ if not constants.ENABLE_CONFD:
+ ToStderr("Error: this command requires confd support, but it has not"
+ " been enabled at build time.")
+ return constants.EXIT_FAILURE
+
+ status = ReplyStatus()
+
+ def ListDrbdConfdCallback(reply):
+ """Callback for confd queries"""
+ if reply.type == confd_client.UPCALL_REPLY:
+ answer = reply.server_reply.answer
+ reqtype = reply.orig_request.type
+ if reqtype == constants.CONFD_REQ_NODE_DRBD:
+ if reply.server_reply.status != constants.CONFD_REPL_STATUS_OK:
+ ToStderr("Query gave non-ok status '%s': %s" %
+ (reply.server_reply.status,
+ reply.server_reply.answer))
+ status.failure = True
+ return
+ if not confd.HTNodeDrbd(answer):
+ ToStderr("Invalid response from server: expected %s, got %s",
+ confd.HTNodeDrbd, answer)
+ status.failure = True
+ else:
+ status.failure = False
+ status.answer = answer
+ else:
+ ToStderr("Unexpected reply %s!?", reqtype)
+ status.failure = True
+
+ node = args[0]
+ hmac = utils.ReadFile(pathutils.CONFD_HMAC_KEY)
+ filter_callback = confd_client.ConfdFilterCallback(ListDrbdConfdCallback)
+ counting_callback = confd_client.ConfdCountingCallback(filter_callback)
+ cf_client = confd_client.ConfdClient(hmac, [constants.IP4_ADDRESS_LOCALHOST],
+ counting_callback)
+ req = confd_client.ConfdClientRequest(type=constants.CONFD_REQ_NODE_DRBD,
+ query=node)
+
+ def DoConfdRequestReply(req):
+ counting_callback.RegisterQuery(req.rsalt)
+ cf_client.SendRequest(req, async=False)
+ while not counting_callback.AllAnswered():
+ if not cf_client.ReceiveReply():
+ ToStderr("Did not receive all expected confd replies")
+ break
+
+ DoConfdRequestReply(req)
+
+ if status.failure:
+ return constants.EXIT_FAILURE
+
+ fields = ["node", "minor", "instance", "disk", "role", "peer"]
+ if opts.no_headers:
+ headers = None
+ else:
+ headers = {"node": "Node", "minor": "Minor", "instance": "Instance",
+ "disk": "Disk", "role": "Role", "peer": "PeerNode"}
+
+ data = GenerateTable(separator=opts.separator, headers=headers,
+ fields=fields, data=sorted(status.answer),
+ numfields=["minor"])
+ for line in data:
+ ToStdout(line)
+
+ return constants.EXIT_SUCCESS
+
+