Blame SOURCES/001-feature-set.patch

23c78e
From d81282f1ac5e1226fbf6cfa1bd239d317e106def Mon Sep 17 00:00:00 2001
23c78e
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
23c78e
Date: Tue, 13 Oct 2020 10:08:21 +0200
23c78e
Subject: [PATCH 1/3] Feature: crmadmin: implement formatted output
23c78e
23c78e
---
23c78e
 include/crm/crm.h |   2 +-
23c78e
 tools/crmadmin.c  | 312 ++++++++++++++++++++++++++++++++++++++++--------------
23c78e
 2 files changed, 232 insertions(+), 82 deletions(-)
23c78e
23c78e
diff --git a/include/crm/crm.h b/include/crm/crm.h
23c78e
index 389b6aa..4eca278 100644
23c78e
--- a/include/crm/crm.h
23c78e
+++ b/include/crm/crm.h
23c78e
@@ -51,7 +51,7 @@ extern "C" {
23c78e
  * >=3.0.13: Fail counts include operation name and interval
23c78e
  * >=3.2.0:  DC supports PCMK_LRM_OP_INVALID and PCMK_LRM_OP_NOT_CONNECTED
23c78e
  */
23c78e
-#  define CRM_FEATURE_SET		"3.6.1"
23c78e
+#  define CRM_FEATURE_SET		"3.6.2"
23c78e
 
23c78e
 #  define EOS		'\0'
23c78e
 #  define DIMOF(a)	((int) (sizeof(a)/sizeof(a[0])) )
23c78e
diff --git a/tools/crmadmin.c b/tools/crmadmin.c
23c78e
index 0b87d01..14078e6 100644
23c78e
--- a/tools/crmadmin.c
23c78e
+++ b/tools/crmadmin.c
23c78e
@@ -20,6 +20,7 @@
23c78e
 #include <crm/cib.h>
23c78e
 #include <crm/msg_xml.h>
23c78e
 #include <crm/common/cmdline_internal.h>
23c78e
+#include <crm/common/output_internal.h>
23c78e
 #include <crm/common/xml.h>
23c78e
 #include <crm/common/iso8601.h>
23c78e
 #include <crm/common/ipc_controld.h>
23c78e
@@ -38,7 +39,6 @@ bool need_controld_api = true;
23c78e
 bool need_pacemakerd_api = false;
23c78e
 
23c78e
 bool do_work(pcmk_ipc_api_t *api);
23c78e
-void do_find_node_list(xmlNode *xml_node);
23c78e
 static char *ipc_name = NULL;
23c78e
 
23c78e
 gboolean admin_message_timeout(gpointer data);
23c78e
@@ -55,13 +55,12 @@ static enum {
23c78e
 
23c78e
 static gboolean BE_VERBOSE = FALSE;
23c78e
 static gboolean BASH_EXPORT = FALSE;
23c78e
-static gboolean BE_SILENT = FALSE;
23c78e
 static char *dest_node = NULL;
23c78e
 static crm_exit_t exit_code = CRM_EX_OK;
23c78e
+pcmk__output_t *out = NULL;
23c78e
 
23c78e
 
23c78e
 struct {
23c78e
-    gboolean quiet;
23c78e
     gboolean health;
23c78e
     gint timeout;
23c78e
 } options;
23c78e
@@ -168,6 +167,191 @@ command_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError
23c78e
     return TRUE;
23c78e
 }
23c78e
 
23c78e
+PCMK__OUTPUT_ARGS("health", "char *", "char *", "char *", "char *")
23c78e
+static int
23c78e
+health_text(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    char *sys_from = va_arg(args, char *);
23c78e
+    char *host_from = va_arg(args, char *);
23c78e
+    char *fsa_state = va_arg(args, char *);
23c78e
+    char *result = va_arg(args, char *);
23c78e
+
23c78e
+    if (!out->is_quiet(out)) {
23c78e
+        out->info(out, "Status of %s@%s: %s (%s)", crm_str(sys_from),
23c78e
+                       crm_str(host_from), crm_str(fsa_state), crm_str(result));
23c78e
+    } else if (fsa_state != NULL) {
23c78e
+        out->info(out, "%s", fsa_state);
23c78e
+    }
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("health", "char *", "char *", "char *", "char *")
23c78e
+static int
23c78e
+health_xml(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    char *sys_from = va_arg(args, char *);
23c78e
+    char *host_from = va_arg(args, char *);
23c78e
+    char *fsa_state = va_arg(args, char *);
23c78e
+    char *result = va_arg(args, char *);
23c78e
+
23c78e
+    xmlNodePtr node = pcmk__output_create_xml_node(out, crm_str(sys_from));
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "node_name", (pcmkXmlStr) crm_str(host_from));
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "state", (pcmkXmlStr) crm_str(fsa_state));
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "result", (pcmkXmlStr) crm_str(result));
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("pacemakerd-health", "char *", "char *", "char *")
23c78e
+static int
23c78e
+pacemakerd_health_text(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    char *sys_from = va_arg(args, char *);
23c78e
+    char *state = va_arg(args, char *);
23c78e
+    char *last_updated = va_arg(args, char *);
23c78e
+
23c78e
+    if (!out->is_quiet(out)) {
23c78e
+        out->info(out, "Status of %s: '%s' %s %s", crm_str(sys_from),
23c78e
+                  crm_str(state), (!pcmk__str_empty(last_updated))?
23c78e
+                  "last updated":"", crm_str(last_updated));
23c78e
+    } else {
23c78e
+        out->info(out, "%s", crm_str(state));
23c78e
+    }
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("pacemakerd-health", "char *", "char *", "char *")
23c78e
+static int
23c78e
+pacemakerd_health_xml(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    char *sys_from = va_arg(args, char *);
23c78e
+    char *state = va_arg(args, char *);
23c78e
+    char *last_updated = va_arg(args, char *);
23c78e
+
23c78e
+
23c78e
+    xmlNodePtr node = pcmk__output_create_xml_node(out, crm_str(sys_from));
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "state", (pcmkXmlStr) crm_str(state));
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "last_updated", (pcmkXmlStr) crm_str(last_updated));
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("dc", "char *")
23c78e
+static int
23c78e
+dc_text(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    char *dc = va_arg(args, char *);
23c78e
+
23c78e
+    if (!out->is_quiet(out)) {
23c78e
+        out->info(out, "Designated Controller is: %s", crm_str(dc));
23c78e
+    } else if (dc != NULL) {
23c78e
+        out->info(out, "%s", dc);
23c78e
+    }
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("dc", "char *")
23c78e
+static int
23c78e
+dc_xml(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    char *dc = va_arg(args, char *);
23c78e
+
23c78e
+    xmlNodePtr node = pcmk__output_create_xml_node(out, "dc");
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "node_name", (pcmkXmlStr) crm_str(dc));
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("crmadmin-node-list", "xmlNode *")
23c78e
+static int
23c78e
+crmadmin_node_list(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    xmlNode *xml_node = va_arg(args, xmlNode *);
23c78e
+    int found = 0;
23c78e
+    xmlNode *node = NULL;
23c78e
+    xmlNode *nodes = get_object_root(XML_CIB_TAG_NODES, xml_node);
23c78e
+
23c78e
+    out->begin_list(out, NULL, NULL, "Nodes");
23c78e
+
23c78e
+    for (node = first_named_child(nodes, XML_CIB_TAG_NODE); node != NULL;
23c78e
+         node = crm_next_same_xml(node)) {
23c78e
+        const char *node_type = BASH_EXPORT ? NULL :
23c78e
+                     crm_element_value(node, XML_ATTR_TYPE);
23c78e
+        out->message(out, "crmadmin-node", node_type,
23c78e
+                     crm_str(crm_element_value(node, XML_ATTR_UNAME)),
23c78e
+                     crm_str(crm_element_value(node, XML_ATTR_ID)));
23c78e
+
23c78e
+        found++;
23c78e
+    }
23c78e
+    // @TODO List Pacemaker Remote nodes that don't have a <node> entry
23c78e
+
23c78e
+    out->end_list(out);
23c78e
+
23c78e
+    if (found == 0) {
23c78e
+        out->info(out, "No nodes configured");
23c78e
+    }
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("crmadmin-node", "char *", "char *", "char *")
23c78e
+static int
23c78e
+crmadmin_node_text(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+        char *type = va_arg(args, char *);
23c78e
+        char *name = va_arg(args, char *);
23c78e
+        char *id = va_arg(args, char *);
23c78e
+
23c78e
+        if (BASH_EXPORT) {
23c78e
+            out->info(out, "export %s=%s", crm_str(name), crm_str(id));
23c78e
+        } else {
23c78e
+            out->info(out, "%s node: %s (%s)", type ? type : "member",
23c78e
+                      crm_str(name), crm_str(id));
23c78e
+        }
23c78e
+
23c78e
+        return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+PCMK__OUTPUT_ARGS("crmadmin-node", "char *", "char *", "char *")
23c78e
+static int
23c78e
+crmadmin_node_xml(pcmk__output_t *out, va_list args)
23c78e
+{
23c78e
+    char *type = va_arg(args, char *);
23c78e
+    char *name = va_arg(args, char *);
23c78e
+    char *id = va_arg(args, char *);
23c78e
+
23c78e
+    xmlNodePtr node = pcmk__output_create_xml_node(out, "crmadmin-node");
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "type", (pcmkXmlStr) (type ? type : "member"));
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "name", (pcmkXmlStr) crm_str(name));
23c78e
+    xmlSetProp(node, (pcmkXmlStr) "id", (pcmkXmlStr) crm_str(id));
23c78e
+
23c78e
+    return pcmk_rc_ok;
23c78e
+}
23c78e
+
23c78e
+static pcmk__message_entry_t fmt_functions[] = {
23c78e
+    {"health", "default", health_text },
23c78e
+    {"health", "xml", health_xml },
23c78e
+    {"pacemakerd-health", "default", pacemakerd_health_text },
23c78e
+    {"pacemakerd-health", "xml", pacemakerd_health_xml },
23c78e
+    {"dc", "default", dc_text },
23c78e
+    {"dc", "xml", dc_xml },
23c78e
+    {"crmadmin-node-list", "default", crmadmin_node_list },
23c78e
+    {"crmadmin-node", "default", crmadmin_node_text },
23c78e
+    {"crmadmin-node", "xml", crmadmin_node_xml },
23c78e
+
23c78e
+    { NULL, NULL, NULL }
23c78e
+};
23c78e
+
23c78e
+static pcmk__supported_format_t formats[] = {
23c78e
+    PCMK__SUPPORTED_FORMAT_TEXT,
23c78e
+    PCMK__SUPPORTED_FORMAT_XML,
23c78e
+    { NULL, NULL, NULL }
23c78e
+};
23c78e
+
23c78e
 static void
23c78e
 quit_main_loop(crm_exit_t ec)
23c78e
 {
23c78e
@@ -191,7 +375,7 @@ controller_event_cb(pcmk_ipc_api_t *controld_api,
23c78e
     switch (event_type) {
23c78e
         case pcmk_ipc_event_disconnect:
23c78e
             if (exit_code == CRM_EX_DISCONNECT) { // Unexpected
23c78e
-                fprintf(stderr, "error: Lost connection to controller\n");
23c78e
+                out->err(out, "error: Lost connection to controller");
23c78e
             }
23c78e
             goto done;
23c78e
             break;
23c78e
@@ -209,14 +393,14 @@ controller_event_cb(pcmk_ipc_api_t *controld_api,
23c78e
     }
23c78e
 
23c78e
     if (status != CRM_EX_OK) {
23c78e
-        fprintf(stderr, "error: Bad reply from controller: %s",
23c78e
+        out->err(out, "error: Bad reply from controller: %s",
23c78e
                 crm_exit_str(status));
23c78e
         exit_code = status;
23c78e
         goto done;
23c78e
     }
23c78e
 
23c78e
     if (reply->reply_type != pcmk_controld_reply_ping) {
23c78e
-        fprintf(stderr, "error: Unknown reply type %d from controller\n",
23c78e
+        out->err(out, "error: Unknown reply type %d from controller",
23c78e
                 reply->reply_type);
23c78e
         goto done;
23c78e
     }
23c78e
@@ -224,22 +408,16 @@ controller_event_cb(pcmk_ipc_api_t *controld_api,
23c78e
     // Parse desired information from reply
23c78e
     switch (command) {
23c78e
         case cmd_health:
23c78e
-            printf("Status of %s@%s: %s (%s)\n",
23c78e
+            out->message(out, "health",
23c78e
                    reply->data.ping.sys_from,
23c78e
                    reply->host_from,
23c78e
                    reply->data.ping.fsa_state,
23c78e
                    reply->data.ping.result);
23c78e
-            if (BE_SILENT && (reply->data.ping.fsa_state != NULL)) {
23c78e
-                fprintf(stderr, "%s\n", reply->data.ping.fsa_state);
23c78e
-            }
23c78e
             exit_code = CRM_EX_OK;
23c78e
             break;
23c78e
 
23c78e
         case cmd_whois_dc:
23c78e
-            printf("Designated Controller is: %s\n", reply->host_from);
23c78e
-            if (BE_SILENT && (reply->host_from != NULL)) {
23c78e
-                fprintf(stderr, "%s\n", reply->host_from);
23c78e
-            }
23c78e
+            out->message(out, "dc", reply->host_from);
23c78e
             exit_code = CRM_EX_OK;
23c78e
             break;
23c78e
 
23c78e
@@ -263,7 +441,7 @@ pacemakerd_event_cb(pcmk_ipc_api_t *pacemakerd_api,
23c78e
     switch (event_type) {
23c78e
         case pcmk_ipc_event_disconnect:
23c78e
             if (exit_code == CRM_EX_DISCONNECT) { // Unexpected
23c78e
-                fprintf(stderr, "error: Lost connection to pacemakerd\n");
23c78e
+                out->err(out, "error: Lost connection to pacemakerd");
23c78e
             }
23c78e
             goto done;
23c78e
             break;
23c78e
@@ -281,14 +459,14 @@ pacemakerd_event_cb(pcmk_ipc_api_t *pacemakerd_api,
23c78e
     }
23c78e
 
23c78e
     if (status != CRM_EX_OK) {
23c78e
-        fprintf(stderr, "error: Bad reply from pacemakerd: %s",
23c78e
+        out->err(out, "error: Bad reply from pacemakerd: %s",
23c78e
                 crm_exit_str(status));
23c78e
         exit_code = status;
23c78e
         goto done;
23c78e
     }
23c78e
 
23c78e
     if (reply->reply_type != pcmk_pacemakerd_reply_ping) {
23c78e
-        fprintf(stderr, "error: Unknown reply type %d from pacemakerd\n",
23c78e
+        out->err(out, "error: Unknown reply type %d from pacemakerd",
23c78e
                 reply->reply_type);
23c78e
         goto done;
23c78e
     }
23c78e
@@ -305,21 +483,12 @@ pacemakerd_event_cb(pcmk_ipc_api_t *pacemakerd_api,
23c78e
                     crm_time_log_date | crm_time_log_timeofday |
23c78e
                         crm_time_log_with_timezone);
23c78e
 
23c78e
-                printf("Status of %s: '%s' %s %s\n",
23c78e
+                out->message(out, "pacemakerd-health",
23c78e
                     reply->data.ping.sys_from,
23c78e
                     (reply->data.ping.status == pcmk_rc_ok)?
23c78e
                         pcmk_pacemakerd_api_daemon_state_enum2text(
23c78e
                             reply->data.ping.state):"query failed",
23c78e
-                    (reply->data.ping.status == pcmk_rc_ok)?"last updated":"",
23c78e
                     (reply->data.ping.status == pcmk_rc_ok)?pinged_buf:"");
23c78e
-                if (BE_SILENT &&
23c78e
-                    (reply->data.ping.state != pcmk_pacemakerd_state_invalid)) {
23c78e
-                    fprintf(stderr, "%s\n",
23c78e
-                        (reply->data.ping.status == pcmk_rc_ok)?
23c78e
-                        pcmk_pacemakerd_api_daemon_state_enum2text(
23c78e
-                            reply->data.ping.state):
23c78e
-                        "query failed");
23c78e
-                }
23c78e
                 exit_code = CRM_EX_OK;
23c78e
                 free(pinged_buf);
23c78e
             }
23c78e
@@ -354,7 +523,7 @@ list_nodes()
23c78e
     rc = the_cib->cmds->query(the_cib, NULL, &output,
23c78e
                               cib_scope_local | cib_sync_call);
23c78e
     if (rc == pcmk_ok) {
23c78e
-        do_find_node_list(output);
23c78e
+        out->message(out, "crmadmin-node-list", output);
23c78e
         free_xml(output);
23c78e
     }
23c78e
     the_cib->cmds->signoff(the_cib);
23c78e
@@ -362,20 +531,20 @@ list_nodes()
23c78e
 }
23c78e
 
23c78e
 static GOptionContext *
23c78e
-build_arg_context(pcmk__common_args_t *args) {
23c78e
+build_arg_context(pcmk__common_args_t *args, GOptionGroup **group) {
23c78e
     GOptionContext *context = NULL;
23c78e
 
23c78e
     const char *description = "Report bugs to users@clusterlabs.org";
23c78e
 
23c78e
     GOptionEntry extra_prog_entries[] = {
23c78e
-        { "quiet", 'q', 0, G_OPTION_ARG_NONE, &options.quiet,
23c78e
+        { "quiet", 'q', 0, G_OPTION_ARG_NONE, &(args->quiet),
23c78e
           "Display only the essential query information",
23c78e
           NULL },
23c78e
 
23c78e
         { NULL }
23c78e
     };
23c78e
 
23c78e
-    context = pcmk__build_arg_context(args, NULL, NULL, NULL);
23c78e
+    context = pcmk__build_arg_context(args, "text (default), xml", group, NULL);
23c78e
     g_option_context_set_description(context, description);
23c78e
 
23c78e
     /* Add the -q option, which cannot be part of the globally supported options
23c78e
@@ -402,9 +571,11 @@ main(int argc, char **argv)
23c78e
 
23c78e
     GError *error = NULL;
23c78e
     GOptionContext *context = NULL;
23c78e
+    GOptionGroup *output_group = NULL;
23c78e
     gchar **processed_args = NULL;
23c78e
 
23c78e
-    context = build_arg_context(args);
23c78e
+    context = build_arg_context(args, &output_group);
23c78e
+    pcmk__register_formats(output_group, formats);
23c78e
 
23c78e
     crm_log_cli_init("crmadmin");
23c78e
 
23c78e
@@ -421,9 +592,21 @@ main(int argc, char **argv)
23c78e
         crm_bump_log_level(argc, argv);
23c78e
     }
23c78e
 
23c78e
+    rc = pcmk__output_new(&out, args->output_ty, args->output_dest, argv);
23c78e
+    if (rc != pcmk_rc_ok) {
23c78e
+        fprintf(stderr, "Error creating output format %s: %s\n",
23c78e
+                args->output_ty, pcmk_rc_str(rc));
23c78e
+        exit_code = CRM_EX_ERROR;
23c78e
+        goto done;
23c78e
+    }
23c78e
+
23c78e
+    out->quiet = args->quiet;
23c78e
+
23c78e
+    pcmk__register_messages(out, fmt_functions);
23c78e
+
23c78e
     if (args->version) {
23c78e
-        /* FIXME:  When crmadmin is converted to use formatted output, this can go. */
23c78e
-        pcmk__cli_help('v', CRM_EX_USAGE);
23c78e
+        out->version(out, false);
23c78e
+        goto done;
23c78e
     }
23c78e
 
23c78e
     if (options.timeout) {
23c78e
@@ -433,12 +616,8 @@ main(int argc, char **argv)
23c78e
         }
23c78e
     }
23c78e
 
23c78e
-    if (options.quiet) {
23c78e
-        BE_SILENT = TRUE;
23c78e
-    }
23c78e
-
23c78e
     if (options.health) {
23c78e
-        fprintf(stderr, "Cluster-wide health option not supported\n");
23c78e
+        out->err(out, "Cluster-wide health option not supported");
23c78e
         ++argerr;
23c78e
     }
23c78e
 
23c78e
@@ -447,14 +626,14 @@ main(int argc, char **argv)
23c78e
     }
23c78e
 
23c78e
     if (command == cmd_none) {
23c78e
-        fprintf(stderr, "error: Must specify a command option\n\n");
23c78e
+        out->err(out, "error: Must specify a command option");
23c78e
         ++argerr;
23c78e
     }
23c78e
 
23c78e
     if (argerr) {
23c78e
         char *help = g_option_context_get_help(context, TRUE, NULL);
23c78e
 
23c78e
-        fprintf(stderr, "%s", help);
23c78e
+        out->err(out, "%s", help);
23c78e
         g_free(help);
23c78e
         exit_code = CRM_EX_USAGE;
23c78e
         goto done;
23c78e
@@ -464,7 +643,7 @@ main(int argc, char **argv)
23c78e
     if (need_controld_api) {
23c78e
         rc = pcmk_new_ipc_api(&controld_api, pcmk_ipc_controld);
23c78e
         if (controld_api == NULL) {
23c78e
-            fprintf(stderr, "error: Could not connect to controller: %s\n",
23c78e
+            out->err(out, "error: Could not connect to controller: %s",
23c78e
                     pcmk_rc_str(rc));
23c78e
             exit_code = pcmk_rc2exitc(rc);
23c78e
             goto done;
23c78e
@@ -472,7 +651,7 @@ main(int argc, char **argv)
23c78e
         pcmk_register_ipc_callback(controld_api, controller_event_cb, NULL);
23c78e
         rc = pcmk_connect_ipc(controld_api, pcmk_ipc_dispatch_main);
23c78e
         if (rc != pcmk_rc_ok) {
23c78e
-            fprintf(stderr, "error: Could not connect to controller: %s\n",
23c78e
+            out->err(out, "error: Could not connect to controller: %s",
23c78e
                     pcmk_rc_str(rc));
23c78e
             exit_code = pcmk_rc2exitc(rc);
23c78e
             goto done;
23c78e
@@ -483,7 +662,7 @@ main(int argc, char **argv)
23c78e
     if (need_pacemakerd_api) {
23c78e
         rc = pcmk_new_ipc_api(&pacemakerd_api, pcmk_ipc_pacemakerd);
23c78e
         if (pacemakerd_api == NULL) {
23c78e
-            fprintf(stderr, "error: Could not connect to pacemakerd: %s\n",
23c78e
+            out->err(out, "error: Could not connect to pacemakerd: %s",
23c78e
                     pcmk_rc_str(rc));
23c78e
             exit_code = pcmk_rc2exitc(rc);
23c78e
             goto done;
23c78e
@@ -491,7 +670,7 @@ main(int argc, char **argv)
23c78e
         pcmk_register_ipc_callback(pacemakerd_api, pacemakerd_event_cb, NULL);
23c78e
         rc = pcmk_connect_ipc(pacemakerd_api, pcmk_ipc_dispatch_main);
23c78e
         if (rc != pcmk_rc_ok) {
23c78e
-            fprintf(stderr, "error: Could not connect to pacemakerd: %s\n",
23c78e
+            out->err(out, "error: Could not connect to pacemakerd: %s",
23c78e
                     pcmk_rc_str(rc));
23c78e
             exit_code = pcmk_rc2exitc(rc);
23c78e
             goto done;
23c78e
@@ -528,6 +707,10 @@ done:
23c78e
     g_strfreev(processed_args);
23c78e
     g_clear_error(&error);
23c78e
     pcmk__free_arg_context(context);
23c78e
+    if (out != NULL) {
23c78e
+        out->finish(out, exit_code, true, NULL);
23c78e
+        pcmk__output_free(out);
23c78e
+    }
23c78e
     return crm_exit(exit_code);
23c78e
 
23c78e
 }
23c78e
@@ -567,7 +750,7 @@ do_work(pcmk_ipc_api_t *api)
23c78e
             break;
23c78e
     }
23c78e
     if (rc != pcmk_rc_ok) {
23c78e
-        fprintf(stderr, "error: Command failed: %s", pcmk_rc_str(rc));
23c78e
+        out->err(out, "error: Command failed: %s", pcmk_rc_str(rc));
23c78e
         exit_code = pcmk_rc2exitc(rc);
23c78e
     }
23c78e
     return need_reply;
23c78e
@@ -576,43 +759,10 @@ do_work(pcmk_ipc_api_t *api)
23c78e
 gboolean
23c78e
 admin_message_timeout(gpointer data)
23c78e
 {
23c78e
-    fprintf(stderr,
23c78e
-            "error: No reply received from controller before timeout (%dms)\n",
23c78e
+    out->err(out,
23c78e
+            "error: No reply received from controller before timeout (%dms)",
23c78e
             message_timeout_ms);
23c78e
     message_timer_id = 0;
23c78e
     quit_main_loop(CRM_EX_TIMEOUT);
23c78e
     return FALSE; // Tells glib to remove source
23c78e
 }
23c78e
-
23c78e
-void
23c78e
-do_find_node_list(xmlNode * xml_node)
23c78e
-{
23c78e
-    int found = 0;
23c78e
-    xmlNode *node = NULL;
23c78e
-    xmlNode *nodes = get_object_root(XML_CIB_TAG_NODES, xml_node);
23c78e
-
23c78e
-    for (node = first_named_child(nodes, XML_CIB_TAG_NODE); node != NULL;
23c78e
-         node = crm_next_same_xml(node)) {
23c78e
-
23c78e
-        if (BASH_EXPORT) {
23c78e
-            printf("export %s=%s\n",
23c78e
-                   crm_element_value(node, XML_ATTR_UNAME),
23c78e
-                   crm_element_value(node, XML_ATTR_ID));
23c78e
-        } else {
23c78e
-            const char *node_type = crm_element_value(node, XML_ATTR_TYPE);
23c78e
-
23c78e
-            if (node_type == NULL) {
23c78e
-                node_type = "member";
23c78e
-            }
23c78e
-            printf("%s node: %s (%s)\n", node_type,
23c78e
-                   crm_element_value(node, XML_ATTR_UNAME),
23c78e
-                   crm_element_value(node, XML_ATTR_ID));
23c78e
-        }
23c78e
-        found++;
23c78e
-    }
23c78e
-    // @TODO List Pacemaker Remote nodes that don't have a <node> entry
23c78e
-
23c78e
-    if (found == 0) {
23c78e
-        printf("No nodes configured\n");
23c78e
-    }
23c78e
-}
23c78e
-- 
23c78e
1.8.3.1
23c78e
23c78e
23c78e
From 2b121066c8eead96e85dc3bf6ecc1c2674cbdf32 Mon Sep 17 00:00:00 2001
23c78e
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
23c78e
Date: Tue, 20 Oct 2020 16:19:03 +0200
23c78e
Subject: [PATCH 2/3] Refactor: crmadmin: use simple XML lists
23c78e
23c78e
---
23c78e
 tools/crmadmin.c | 8 ++++++--
23c78e
 1 file changed, 6 insertions(+), 2 deletions(-)
23c78e
23c78e
diff --git a/tools/crmadmin.c b/tools/crmadmin.c
23c78e
index 14078e6..b80a31a 100644
23c78e
--- a/tools/crmadmin.c
23c78e
+++ b/tools/crmadmin.c
23c78e
@@ -275,7 +275,7 @@ crmadmin_node_list(pcmk__output_t *out, va_list args)
23c78e
     xmlNode *node = NULL;
23c78e
     xmlNode *nodes = get_object_root(XML_CIB_TAG_NODES, xml_node);
23c78e
 
23c78e
-    out->begin_list(out, NULL, NULL, "Nodes");
23c78e
+    out->begin_list(out, NULL, NULL, "nodes");
23c78e
 
23c78e
     for (node = first_named_child(nodes, XML_CIB_TAG_NODE); node != NULL;
23c78e
          node = crm_next_same_xml(node)) {
23c78e
@@ -324,7 +324,7 @@ crmadmin_node_xml(pcmk__output_t *out, va_list args)
23c78e
     char *name = va_arg(args, char *);
23c78e
     char *id = va_arg(args, char *);
23c78e
 
23c78e
-    xmlNodePtr node = pcmk__output_create_xml_node(out, "crmadmin-node");
23c78e
+    xmlNodePtr node = pcmk__output_create_xml_node(out, "node");
23c78e
     xmlSetProp(node, (pcmkXmlStr) "type", (pcmkXmlStr) (type ? type : "member"));
23c78e
     xmlSetProp(node, (pcmkXmlStr) "name", (pcmkXmlStr) crm_str(name));
23c78e
     xmlSetProp(node, (pcmkXmlStr) "id", (pcmkXmlStr) crm_str(id));
23c78e
@@ -604,6 +604,10 @@ main(int argc, char **argv)
23c78e
 
23c78e
     pcmk__register_messages(out, fmt_functions);
23c78e
 
23c78e
+    if (!pcmk__force_args(context, &error, "%s --xml-simple-list --xml-substitute", g_get_prgname())) {
23c78e
+        goto done;
23c78e
+    }
23c78e
+
23c78e
     if (args->version) {
23c78e
         out->version(out, false);
23c78e
         goto done;
23c78e
-- 
23c78e
1.8.3.1
23c78e
23c78e
23c78e
From 9b53a7eda078db736be5212aeb77daf8117e2f17 Mon Sep 17 00:00:00 2001
23c78e
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
23c78e
Date: Tue, 20 Oct 2020 16:21:12 +0200
23c78e
Subject: [PATCH 3/3] Feature: xml: add schema for new crmadmin output
23c78e
23c78e
---
23c78e
 xml/Makefile.am          |  2 +-
23c78e
 xml/api/crmadmin-2.4.rng | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
23c78e
 2 files changed, 69 insertions(+), 1 deletion(-)
23c78e
 create mode 100644 xml/api/crmadmin-2.4.rng
23c78e
23c78e
diff --git a/xml/Makefile.am b/xml/Makefile.am
23c78e
index c045522..892c811 100644
23c78e
--- a/xml/Makefile.am
23c78e
+++ b/xml/Makefile.am
23c78e
@@ -45,7 +45,7 @@ version_pairs_last = $(wordlist \
23c78e
 			  )
23c78e
 
23c78e
 # Names of API schemas that form the choices for pacemaker-result content
23c78e
-API_request_base	= command-output crm_mon stonith_admin version
23c78e
+API_request_base	= command-output crm_mon crmadmin stonith_admin version
23c78e
 
23c78e
 # Names of CIB schemas that form the choices for cib/configuration content
23c78e
 CIB_cfg_base		= options nodes resources constraints fencing acls tags alerts
23c78e
diff --git a/xml/api/crmadmin-2.4.rng b/xml/api/crmadmin-2.4.rng
23c78e
new file mode 100644
23c78e
index 0000000..34c9ca4
23c78e
--- /dev/null
23c78e
+++ b/xml/api/crmadmin-2.4.rng
23c78e
@@ -0,0 +1,68 @@
23c78e
+
23c78e
+
23c78e
+         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
23c78e
+
23c78e
+    <start>
23c78e
+        <ref name="element-crmadmin"/>
23c78e
+    </start>
23c78e
+
23c78e
+    <define name="element-crmadmin">
23c78e
+        <optional>
23c78e
+            <ref name="element-status" />
23c78e
+        </optional>
23c78e
+        <optional>
23c78e
+            <ref name="element-pacemakerd" />
23c78e
+        </optional>
23c78e
+        <optional>
23c78e
+            <ref name="element-dc" />
23c78e
+        </optional>
23c78e
+        <optional>
23c78e
+            <ref name="crmadmin-nodes-list" />
23c78e
+        </optional>
23c78e
+    </define>
23c78e
+
23c78e
+    <define name="element-status">
23c78e
+        <element name="crmd">
23c78e
+            <attribute name="node_name"> <text /> </attribute>
23c78e
+            <attribute name="state"> <text /> </attribute>
23c78e
+            <attribute name="result"> <text /> </attribute>
23c78e
+        </element>
23c78e
+    </define>
23c78e
+
23c78e
+    <define name="element-pacemakerd">
23c78e
+        <element name="pacemakerd">
23c78e
+            <attribute name="state"> <text /> </attribute>
23c78e
+            <attribute name="last_updated"> <text /> </attribute>
23c78e
+        </element>
23c78e
+    </define>
23c78e
+
23c78e
+    <define name="element-dc">
23c78e
+        <element name="dc">
23c78e
+            <attribute name="node_name"> <text /> </attribute>
23c78e
+        </element>
23c78e
+    </define>
23c78e
+
23c78e
+    <define name="crmadmin-nodes-list">
23c78e
+        <element name="nodes">
23c78e
+            <zeroOrMore>
23c78e
+                <ref name="element-crmadmin-node" />
23c78e
+            </zeroOrMore>
23c78e
+        </element>
23c78e
+    </define>
23c78e
+
23c78e
+    <define name="element-crmadmin-node">
23c78e
+        <element name="node">
23c78e
+            <attribute name="type">
23c78e
+                <choice>
23c78e
+                    <value>unknown</value>
23c78e
+                    <value>member</value>
23c78e
+                    <value>remote</value>
23c78e
+                    <value>ping</value>
23c78e
+                </choice>
23c78e
+            </attribute>
23c78e
+
23c78e
+            <attribute name="name"> <text/> </attribute>
23c78e
+            <attribute name="id"> <text/> </attribute>
23c78e
+        </element>
23c78e
+    </define>
23c78e
+</grammar>
23c78e
-- 
23c78e
1.8.3.1
23c78e