summaryrefslogtreecommitdiffstats
path: root/tools/crm_node.c
blob: 1e7ce6cb311207d83893cce1c6fa8e05763327a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
 * Copyright 2004-2023 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU General Public License version 2
 * or later (GPLv2+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>

#include <crm/crm.h>
#include <crm/common/cmdline_internal.h>
#include <crm/common/output_internal.h>
#include <crm/common/mainloop.h>
#include <crm/msg_xml.h>
#include <crm/cib.h>
#include <crm/cib/internal.h>
#include <crm/common/ipc_controld.h>
#include <crm/common/attrd_internal.h>

#include <pacemaker-internal.h>

#define SUMMARY "crm_node - Tool for displaying low-level node information"

struct {
    gboolean corosync;
    gboolean dangerous_cmd;
    gboolean force_flag;
    char command;
    int nodeid;
    char *target_uname;
} options = {
    .command = '\0',
    .force_flag = FALSE
};

gboolean command_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
gboolean name_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
gboolean remove_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);

static GError *error = NULL;
static GMainLoop *mainloop = NULL;
static crm_exit_t exit_code = CRM_EX_OK;
static pcmk__output_t *out = NULL;

#define INDENT "                           "

static GOptionEntry command_entries[] = {
    { "cluster-id", 'i', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
      "Display this node's cluster id",
      NULL },
    { "list", 'l', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
      "Display all known members (past and present) of this cluster",
      NULL },
    { "name", 'n', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
      "Display the name used by the cluster for this node",
      NULL },
    { "partition", 'p', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
      "Display the members of this partition",
      NULL },
    { "quorum", 'q', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
      "Display a 1 if our partition has quorum, 0 if not",
      NULL },
    { "name-for-id", 'N', 0, G_OPTION_ARG_CALLBACK, name_cb,
      "Display the name used by the cluster for the node with the specified ID",
      "ID" },
    { "remove", 'R', 0, G_OPTION_ARG_CALLBACK, remove_cb,
      "(Advanced) Remove the (stopped) node with the specified name from Pacemaker's\n"
      INDENT "configuration and caches (the node must already have been removed from\n"
      INDENT "the underlying cluster stack configuration",
      "NAME" },

    { NULL }
};

static GOptionEntry addl_entries[] = {
    { "force", 'f', 0, G_OPTION_ARG_NONE, &options.force_flag,
      NULL,
      NULL },
#if SUPPORT_COROSYNC
    /* Unused and deprecated */
    { "corosync", 'C', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &options.corosync,
      NULL,
      NULL },
#endif

    // @TODO add timeout option for when IPC replies are needed

    { NULL }
};

static pcmk__supported_format_t formats[] = {
    PCMK__SUPPORTED_FORMAT_NONE,
    PCMK__SUPPORTED_FORMAT_TEXT,
    PCMK__SUPPORTED_FORMAT_XML,
    { NULL, NULL, NULL }
};

gboolean
command_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
    if (pcmk__str_eq("-i", option_name, pcmk__str_casei) || pcmk__str_eq("--cluster-id", option_name, pcmk__str_casei)) {
        options.command = 'i';
    } else if (pcmk__str_eq("-l", option_name, pcmk__str_casei) || pcmk__str_eq("--list", option_name, pcmk__str_casei)) {
        options.command = 'l';
    } else if (pcmk__str_eq("-n", option_name, pcmk__str_casei) || pcmk__str_eq("--name", option_name, pcmk__str_casei)) {
        options.command = 'n';
    } else if (pcmk__str_eq("-p", option_name, pcmk__str_casei) || pcmk__str_eq("--partition", option_name, pcmk__str_casei)) {
        options.command = 'p';
    } else if (pcmk__str_eq("-q", option_name, pcmk__str_casei) || pcmk__str_eq("--quorum", option_name, pcmk__str_casei)) {
        options.command = 'q';
    } else {
        g_set_error(error, PCMK__EXITC_ERROR, CRM_EX_INVALID_PARAM, "Unknown param passed to command_cb: %s", option_name);
        return FALSE;
    }

    return TRUE;
}

gboolean
name_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
    options.command = 'N';
    pcmk__scan_min_int(optarg, &(options.nodeid), 0);
    return TRUE;
}

gboolean
remove_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
    if (optarg == NULL) {
        g_set_error(error, PCMK__EXITC_ERROR, CRM_EX_INVALID_PARAM, "-R option requires an argument");
        return FALSE;
    }

    options.command = 'R';
    options.dangerous_cmd = TRUE;
    pcmk__str_update(&options.target_uname, optarg);
    return TRUE;
}

PCMK__OUTPUT_ARGS("node-id", "uint32_t")
static int
node_id_default(pcmk__output_t *out, va_list args) {
    uint32_t node_id = va_arg(args, uint32_t);

    out->info(out, "%" PRIu32, node_id);
    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("node-id", "uint32_t")
static int
node_id_xml(pcmk__output_t *out, va_list args) {
    uint32_t node_id = va_arg(args, uint32_t);

    char *id_s = crm_strdup_printf("%" PRIu32, node_id);

    pcmk__output_create_xml_node(out, "node-info",
                                 "nodeid", id_s,
                                 NULL);

    free(id_s);
    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("node-list", "GList *")
static int
node_list_default(pcmk__output_t *out, va_list args)
{
    GList *nodes = va_arg(args, GList *);

    for (GList *node_iter = nodes; node_iter != NULL; node_iter = node_iter->next) {
        pcmk_controld_api_node_t *node = node_iter->data;
        out->info(out, "%" PRIu32 " %s %s", node->id, pcmk__s(node->uname, ""),
                  pcmk__s(node->state, ""));
    }

    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("node-list", "GList *")
static int
node_list_xml(pcmk__output_t *out, va_list args)
{
    GList *nodes = va_arg(args, GList *);

    out->begin_list(out, NULL, NULL, "nodes");

    for (GList *node_iter = nodes; node_iter != NULL; node_iter = node_iter->next) {
        pcmk_controld_api_node_t *node = node_iter->data;
        char *id_s = crm_strdup_printf("%" PRIu32, node->id);

        pcmk__output_create_xml_node(out, "node",
                                     "id", id_s,
                                     "name", node->uname,
                                     "state", node->state,
                                     NULL);

        free(id_s);
    }

    out->end_list(out);

    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("node-name", "uint32_t", "const char *")
static int
node_name_default(pcmk__output_t *out, va_list args) {
    uint32_t node_id G_GNUC_UNUSED = va_arg(args, uint32_t);
    const char *node_name = va_arg(args, const char *);

    out->info(out, "%s", node_name);
    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("node-name", "uint32_t", "const char *")
static int
node_name_xml(pcmk__output_t *out, va_list args) {
    uint32_t node_id = va_arg(args, uint32_t);
    const char *node_name = va_arg(args, const char *);

    char *id_s = crm_strdup_printf("%" PRIu32, node_id);

    pcmk__output_create_xml_node(out, "node-info",
                                 "nodeid", id_s,
                                 XML_ATTR_UNAME, node_name,
                                 NULL);

    free(id_s);
    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("partition-list", "GList *")
static int
partition_list_default(pcmk__output_t *out, va_list args)
{
    GList *nodes = va_arg(args, GList *);

    GString *buffer = NULL;

    for (GList *node_iter = nodes; node_iter != NULL; node_iter = node_iter->next) {
        pcmk_controld_api_node_t *node = node_iter->data;
        if (pcmk__str_eq(node->state, "member", pcmk__str_none)) {
            pcmk__add_separated_word(&buffer, 128, pcmk__s(node->uname, ""), " ");
        }
    }

    if (buffer != NULL) {
        out->info(out, "%s", buffer->str);
        g_string_free(buffer, TRUE);
        return pcmk_rc_ok;
    }

    return pcmk_rc_no_output;
}

PCMK__OUTPUT_ARGS("partition-list", "GList *")
static int
partition_list_xml(pcmk__output_t *out, va_list args)
{
    GList *nodes = va_arg(args, GList *);

    out->begin_list(out, NULL, NULL, "nodes");

    for (GList *node_iter = nodes; node_iter != NULL; node_iter = node_iter->next) {
        pcmk_controld_api_node_t *node = node_iter->data;

        if (pcmk__str_eq(node->state, "member", pcmk__str_none)) {
            char *id_s = crm_strdup_printf("%" PRIu32, node->id);

            pcmk__output_create_xml_node(out, "node",
                                         "id", id_s,
                                         "name", node->uname,
                                         "state", node->state,
                                         NULL);
            free(id_s);
        }
    }

    out->end_list(out);
    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("quorum", "bool")
static int
quorum_default(pcmk__output_t *out, va_list args) {
    bool have_quorum = va_arg(args, int);

    out->info(out, "%d", have_quorum);
    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("quorum", "bool")
static int
quorum_xml(pcmk__output_t *out, va_list args) {
    bool have_quorum = va_arg(args, int);

    pcmk__output_create_xml_node(out, "cluster-info",
                                 "quorum", have_quorum ? "true" : "false",
                                 NULL);
    return pcmk_rc_ok;
}

static pcmk__message_entry_t fmt_functions[] = {
    { "node-id", "default", node_id_default },
    { "node-id", "xml", node_id_xml },
    { "node-list", "default", node_list_default },
    { "node-list", "xml", node_list_xml },
    { "node-name", "default", node_name_default },
    { "node-name", "xml", node_name_xml },
    { "quorum", "default", quorum_default },
    { "quorum", "xml", quorum_xml },
    { "partition-list", "default", partition_list_default },
    { "partition-list", "xml", partition_list_xml },

    { NULL, NULL, NULL }
};

static gint
sort_node(gconstpointer a, gconstpointer b)
{
    const pcmk_controld_api_node_t *node_a = a;
    const pcmk_controld_api_node_t *node_b = b;

    return pcmk__numeric_strcasecmp((node_a->uname? node_a->uname : ""),
                                    (node_b->uname? node_b->uname : ""));
}

static void
controller_event_cb(pcmk_ipc_api_t *controld_api,
                    enum pcmk_ipc_event event_type, crm_exit_t status,
                    void *event_data, void *user_data)
{
    pcmk_controld_api_reply_t *reply = event_data;

    switch (event_type) {
        case pcmk_ipc_event_disconnect:
            if (exit_code == CRM_EX_DISCONNECT) { // Unexpected
                g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
                            "Lost connection to controller");
            }
            goto done;
            break;

        case pcmk_ipc_event_reply:
            break;

        default:
            return;
    }

    if (status != CRM_EX_OK) {
        exit_code = status;
        g_set_error(&error, PCMK__EXITC_ERROR, status,
                    "Bad reply from controller: %s",
                    crm_exit_str(status));
        goto done;
    }

    if (reply->reply_type != pcmk_controld_reply_nodes) {
        g_set_error(&error, PCMK__EXITC_ERROR, CRM_EX_INDETERMINATE,
                    "Unknown reply type %d from controller",
                    reply->reply_type);
        goto done;
    }

    reply->data.nodes = g_list_sort(reply->data.nodes, sort_node);

    if (options.command == 'p') {
        out->message(out, "partition-list", reply->data.nodes);
    } else if (options.command == 'l') {
        out->message(out, "node-list", reply->data.nodes);
    }

    // Success
    exit_code = CRM_EX_OK;
done:
    pcmk_disconnect_ipc(controld_api);
    pcmk_quit_main_loop(mainloop, 10);
}

static void
run_controller_mainloop(void)
{
    pcmk_ipc_api_t *controld_api = NULL;
    int rc;

    // Set disconnect exit code to handle unexpected disconnects
    exit_code = CRM_EX_DISCONNECT;

    // Create controller IPC object
    rc = pcmk_new_ipc_api(&controld_api, pcmk_ipc_controld);
    if (rc != pcmk_rc_ok) {
        g_set_error(&error, PCMK__RC_ERROR, rc,
                    "Could not connect to controller: %s",
                    pcmk_rc_str(rc));
        return;
    }
    pcmk_register_ipc_callback(controld_api, controller_event_cb, NULL);

    // Connect to controller
    rc = pcmk__connect_ipc(controld_api, pcmk_ipc_dispatch_main, 5);
    if (rc != pcmk_rc_ok) {
        exit_code = pcmk_rc2exitc(rc);
        g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
                    "Could not connect to %s: %s",
                    pcmk_ipc_name(controld_api, true), pcmk_rc_str(rc));
        return;
    }

    rc = pcmk_controld_api_list_nodes(controld_api);

    if (rc != pcmk_rc_ok) {
        pcmk_disconnect_ipc(controld_api);
        exit_code = pcmk_rc2exitc(rc);
        g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
                    "Could not ping controller: %s", pcmk_rc_str(rc));
        return;
    }

    // Run main loop to get controller reply via controller_event_cb()
    mainloop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(mainloop);
    g_main_loop_unref(mainloop);
    mainloop = NULL;
    pcmk_free_ipc_api(controld_api);
}

static void
print_node_id(void)
{
    uint32_t nodeid;
    int rc = pcmk__query_node_info(out, &nodeid, NULL, NULL, NULL, NULL, NULL,
                                   false, 0);

    if (rc != pcmk_rc_ok) {
        /* pcmk__query_node_info already sets an error message on the output object,
         * so there's no need to call g_set_error here.  That would just create a
         * duplicate error message in the output.
         */
        exit_code = pcmk_rc2exitc(rc);
        return;
    }

    rc = out->message(out, "node-id", nodeid);

    if (rc != pcmk_rc_ok) {
        g_set_error(&error, PCMK__RC_ERROR, rc, "Could not print node ID: %s",
                    pcmk_rc_str(rc));
    }

    exit_code = pcmk_rc2exitc(rc);
}

static void
print_node_name(uint32_t nodeid)
{
    int rc = pcmk_rc_ok;
    char *node_name = NULL;

    if (nodeid == 0) {
        // Check environment first (i.e. when called by resource agent)
        const char *name = getenv("OCF_RESKEY_" CRM_META "_" XML_LRM_ATTR_TARGET);

        if (name != NULL) {
            rc = out->message(out, "node-name", 0, name);
            goto done;
        }
    }

    // Otherwise ask the controller

    /* pcmk__query_node_name already sets an error message on the output object,
     * so there's no need to call g_set_error here.  That would just create a
     * duplicate error message in the output.
     */
    rc = pcmk__query_node_name(out, nodeid, &node_name, 0);
    if (rc != pcmk_rc_ok) {
        exit_code = pcmk_rc2exitc(rc);
        return;
    }

    rc = out->message(out, "node-name", 0, node_name);

done:
    if (node_name != NULL) {
        free(node_name);
    }

    if (rc != pcmk_rc_ok) {
        g_set_error(&error, PCMK__RC_ERROR, rc, "Could not print node name: %s",
                    pcmk_rc_str(rc));
    }

    exit_code = pcmk_rc2exitc(rc);
}

static void
print_quorum(void)
{
    bool quorum;
    int rc = pcmk__query_node_info(out, NULL, NULL, NULL, NULL, &quorum, NULL,
                                   false, 0);

    if (rc != pcmk_rc_ok) {
        /* pcmk__query_node_info already sets an error message on the output object,
         * so there's no need to call g_set_error here.  That would just create a
         * duplicate error message in the output.
         */
        exit_code = pcmk_rc2exitc(rc);
        return;
    }

    rc = out->message(out, "quorum", quorum);

    if (rc != pcmk_rc_ok) {
        g_set_error(&error, PCMK__RC_ERROR, rc, "Could not print quorum status: %s",
                    pcmk_rc_str(rc));
    }

    exit_code = pcmk_rc2exitc(rc);
}

/*!
 * \internal
 * \brief Extend a transaction by removing a node from a CIB section
 *
 * \param[in,out] cib        Active CIB connection
 * \param[in]     element    CIB element containing node name and/or ID
 * \param[in]     section    CIB section that \p element is in
 * \param[in]     node_name  Name of node to purge (NULL to leave unspecified)
 * \param[in]     node_id    Node ID of node to purge (0 to leave unspecified)
 *
 * \note At least one of node_name and node_id must be specified.
 * \return Standard Pacemaker return code
 */
static int
remove_from_section(cib_t *cib, const char *element, const char *section,
                    const char *node_name, long node_id)
{
    xmlNode *xml = NULL;
    int rc = pcmk_rc_ok;

    xml = create_xml_node(NULL, element);
    if (xml == NULL) {
        return pcmk_rc_error;
    }
    crm_xml_add(xml, XML_ATTR_UNAME, node_name);
    if (node_id > 0) {
        crm_xml_set_id(xml, "%ld", node_id);
    }
    rc = cib->cmds->remove(cib, section, xml, cib_transaction);
    free_xml(xml);
    return (rc >= 0)? pcmk_rc_ok : pcmk_legacy2rc(rc);
}

/*!
 * \internal
 * \brief Purge a node from CIB
 *
 * \param[in] node_name  Name of node to purge (or NULL to leave unspecified)
 * \param[in] node_id    Node ID of node to purge (or 0 to leave unspecified)
 *
 * \note At least one of node_name and node_id must be specified.
 * \return Standard Pacemaker return code
 */
static int
purge_node_from_cib(const char *node_name, long node_id)
{
    int rc = pcmk_rc_ok;
    int commit_rc = pcmk_rc_ok;
    cib_t *cib = NULL;

    // Connect to CIB and start a transaction
    cib = cib_new();
    if (cib == NULL) {
        return ENOTCONN;
    }
    rc = cib->cmds->signon(cib, crm_system_name, cib_command);
    if (rc == pcmk_ok) {
        rc = cib->cmds->init_transaction(cib);
    }
    if (rc != pcmk_ok) {
        rc = pcmk_legacy2rc(rc);
        cib__clean_up_connection(&cib);
        return rc;
    }

    // Remove from configuration and status
    rc = remove_from_section(cib, XML_CIB_TAG_NODE, XML_CIB_TAG_NODES,
                             node_name, node_id);
    if (rc == pcmk_rc_ok) {
        rc = remove_from_section(cib, XML_CIB_TAG_STATE, XML_CIB_TAG_STATUS,
                                 node_name, node_id);
    }

    // Commit the transaction
    commit_rc = cib->cmds->end_transaction(cib, (rc == pcmk_rc_ok),
                                           cib_sync_call);
    cib__clean_up_connection(&cib);

    if ((rc == pcmk_rc_ok) && (commit_rc == pcmk_ok)) {
        crm_debug("Purged node %s (%ld) from CIB",
                  pcmk__s(node_name, "by ID"), node_id);
    }
    return rc;
}

/*!
 * \internal
 * \brief Purge a node from a single server's peer cache
 *
 * \param[in] server     IPC server to send request to
 * \param[in] node_name  Name of node to purge (or NULL to leave unspecified)
 * \param[in] node_id    Node ID of node to purge (or 0 to leave unspecified)
 *
 * \note At least one of node_name and node_id must be specified.
 * \return Standard Pacemaker return code
 */
static int
purge_node_from(enum pcmk_ipc_server server, const char *node_name,
                long node_id)
{
    pcmk_ipc_api_t *api = NULL;
    int rc;

    rc = pcmk_new_ipc_api(&api, server);
    if (rc != pcmk_rc_ok) {
        goto done;
    }

    rc = pcmk__connect_ipc(api, pcmk_ipc_dispatch_sync, 5);
    if (rc != pcmk_rc_ok) {
        goto done;
    }

    rc = pcmk_ipc_purge_node(api, node_name, node_id);
done:
    if (rc != pcmk_rc_ok) { // Debug message already logged on success
        g_set_error(&error, PCMK__RC_ERROR, rc,
                    "Could not purge node %s from %s: %s",
                    pcmk__s(node_name, "by ID"), pcmk_ipc_name(api, true),
                    pcmk_rc_str(rc));
    }
    pcmk_free_ipc_api(api);
    return rc;
}

/*!
 * \internal
 * \brief Purge a node from the fencer's peer cache
 *
 * \param[in] node_name  Name of node to purge (or NULL to leave unspecified)
 * \param[in] node_id    Node ID of node to purge (or 0 to leave unspecified)
 *
 * \note At least one of node_name and node_id must be specified.
 * \return Standard Pacemaker return code
 */
static int
purge_node_from_fencer(const char *node_name, long node_id)
{
    int rc = pcmk_rc_ok;
    crm_ipc_t *conn = NULL;
    xmlNode *cmd = NULL;

    conn = crm_ipc_new("stonith-ng", 0);
    if (conn == NULL) {
        rc = ENOTCONN;
        exit_code = pcmk_rc2exitc(rc);
        g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
                    "Could not connect to fencer to purge node %s",
                    pcmk__s(node_name, "by ID"));
        return rc;
    }

    rc = pcmk__connect_generic_ipc(conn);
    if (rc != pcmk_rc_ok) {
        exit_code = pcmk_rc2exitc(rc);
        g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
                    "Could not connect to fencer to purge node %s: %s",
                    pcmk__s(node_name, "by ID"), pcmk_rc_str(rc));
        crm_ipc_destroy(conn);
        return rc;
    }

    cmd = create_request(CRM_OP_RM_NODE_CACHE, NULL, NULL, "stonith-ng",
                         crm_system_name, NULL);
    if (node_id > 0) {
        crm_xml_set_id(cmd, "%ld", node_id);
    }
    crm_xml_add(cmd, XML_ATTR_UNAME, node_name);

    rc = crm_ipc_send(conn, cmd, 0, 0, NULL);
    if (rc >= 0) {
        rc = pcmk_rc_ok;
        crm_debug("Purged node %s (%ld) from fencer",
                  pcmk__s(node_name, "by ID"), node_id);
    } else {
        rc = pcmk_legacy2rc(rc);
        fprintf(stderr, "Could not purge node %s from fencer: %s\n",
                pcmk__s(node_name, "by ID"), pcmk_rc_str(rc));
    }
    free_xml(cmd);
    crm_ipc_close(conn);
    crm_ipc_destroy(conn);
    return rc;
}

static void
remove_node(const char *target_uname)
{
    int rc = pcmk_rc_ok;
    long nodeid = 0;
    const char *node_name = NULL;
    char *endptr = NULL;
    const enum pcmk_ipc_server servers[] = {
        pcmk_ipc_controld,
        pcmk_ipc_attrd,
    };

    // Check whether node was specified by name or numeric ID
    errno = 0;
    nodeid = strtol(target_uname, &endptr, 10);
    if ((errno != 0) || (endptr == target_uname) || (*endptr != '\0')
        || (nodeid <= 0)) {
        // It's not a positive integer, so assume it's a node name
        nodeid = 0;
        node_name = target_uname;
    }

    for (int i = 0; i < PCMK__NELEM(servers); ++i) {
        rc = purge_node_from(servers[i], node_name, nodeid);
        if (rc != pcmk_rc_ok) {
            exit_code = pcmk_rc2exitc(rc);
            return;
        }
    }

    // The fencer hasn't been converted to pcmk_ipc_api_t yet
    rc = purge_node_from_fencer(node_name, nodeid);
    if (rc != pcmk_rc_ok) {
        exit_code = pcmk_rc2exitc(rc);
        return;
    }

    // Lastly, purge the node from the CIB itself
    rc = purge_node_from_cib(node_name, nodeid);
    exit_code = pcmk_rc2exitc(rc);
}

static GOptionContext *
build_arg_context(pcmk__common_args_t *args, GOptionGroup **group) {
    GOptionContext *context = NULL;

    GOptionEntry extra_prog_entries[] = {
        { "quiet", 'Q', 0, G_OPTION_ARG_NONE, &(args->quiet),
          "Be less descriptive in output.",
          NULL },

        { NULL }
    };

    context = pcmk__build_arg_context(args, "text (default), xml", group, NULL);

    /* Add the -q option, which cannot be part of the globally supported options
     * because some tools use that flag for something else.
     */
    pcmk__add_main_args(context, extra_prog_entries);

    pcmk__add_arg_group(context, "commands", "Commands:",
                        "Show command help", command_entries);
    pcmk__add_arg_group(context, "additional", "Additional Options:",
                        "Show additional options", addl_entries);
    return context;
}

int
main(int argc, char **argv)
{
    int rc = pcmk_rc_ok;

    GOptionGroup *output_group = NULL;
    pcmk__common_args_t *args = pcmk__new_common_args(SUMMARY);
    gchar **processed_args = pcmk__cmdline_preproc(argv, "NR");
    GOptionContext *context = build_arg_context(args, &output_group);

    pcmk__register_formats(output_group, formats);
    if (!g_option_context_parse_strv(context, &processed_args, &error)) {
        exit_code = CRM_EX_USAGE;
        goto done;
    }

    pcmk__cli_init_logging("crm_node", args->verbosity);

    rc = pcmk__output_new(&out, args->output_ty, args->output_dest, argv);
    if (rc != pcmk_rc_ok) {
        exit_code = pcmk_rc2exitc(rc);
        g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
                    "Error creating output format %s: %s", args->output_ty,
                    pcmk_rc_str(rc));
        goto done;
    }

    if (!pcmk__force_args(context, &error, "%s --xml-simple-list", g_get_prgname())) {
        exit_code = CRM_EX_SOFTWARE;
        goto done;
    }

    if (args->version) {
        out->version(out, false);
        goto done;
    }

    if (options.command == 0) {
        char *help = g_option_context_get_help(context, TRUE, NULL);

        out->err(out, "%s", help);
        g_free(help);
        exit_code = CRM_EX_USAGE;
        goto done;
    }

    if (options.dangerous_cmd && options.force_flag == FALSE) {
        exit_code = CRM_EX_USAGE;
        g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
                    "The supplied command is considered dangerous."
                    "  To prevent accidental destruction of the cluster,"
                    " the --force flag is required in order to proceed.");
        goto done;
    }

    pcmk__register_lib_messages(out);
    pcmk__register_messages(out, fmt_functions);

    switch (options.command) {
        case 'i':
            print_node_id();
            break;

        case 'n':
            print_node_name(0);
            break;

        case 'q':
            print_quorum();
            break;

        case 'N':
            print_node_name(options.nodeid);
            break;

        case 'R':
            remove_node(options.target_uname);
            break;

        case 'l':
        case 'p':
            run_controller_mainloop();
            break;

        default:
            break;
    }

done:
    g_strfreev(processed_args);
    pcmk__free_arg_context(context);

    pcmk__output_and_clear_error(&error, out);

    if (out != NULL) {
        out->finish(out, exit_code, true, NULL);
        pcmk__output_free(out);
    }
    pcmk__unregister_formats();
    return crm_exit(exit_code);
}