forked from ClusterLabs/pacemaker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrm_node.c
867 lines (720 loc) · 24.2 KB
/
crm_node.c
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
/*
* Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <crm_internal.h>
#include <sys/param.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h> /* for basename() */
#include <crm/crm.h>
#include <crm/cluster/internal.h>
#include <crm/common/mainloop.h>
#include <crm/msg_xml.h>
#include <crm/cib.h>
int command = 0;
int ccm_fd = 0;
gboolean do_quiet = FALSE;
char *target_uuid = NULL;
char *target_uname = NULL;
const char *standby_value = NULL;
const char *standby_scope = NULL;
int crmd_remove_node_cache(const char *id);
/* *INDENT-OFF* */
static struct crm_option long_options[] = {
/* Top-level Options */
{"help", 0, 0, '?', "\tThis text"},
{"version", 0, 0, '$', "\tVersion information" },
{"verbose", 0, 0, 'V', "\tIncrease debug output"},
{"quiet", 0, 0, 'Q', "\tEssential output only"},
{"-spacer-", 1, 0, '-', "\nStack:"},
#if SUPPORT_CMAN
{"cman", 0, 0, 'c', "\tOnly try connecting to a cman-based cluster"},
#endif
#if SUPPORT_COROSYNC
{"openais", 0, 0, 'A', "\tOnly try connecting to an OpenAIS-based cluster"},
#endif
#ifdef SUPPORT_HEARTBEAT
{"heartbeat", 0, 0, 'H', "Only try connecting to a Heartbeat-based cluster"},
#endif
{"-spacer-", 1, 0, '-', "\nCommands:"},
{"name", 0, 0, 'n', "\tDisplay the name used by the cluster for this node"},
{"name-for-id", 1, 0, 'N', "\tDisplay the name used by the cluster for the node with the specified id"},
{"epoch", 0, 0, 'e', "\tDisplay the epoch during which this node joined the cluster"},
{"quorum", 0, 0, 'q', "\tDisplay a 1 if our partition has quorum, 0 if not"},
{"list", 0, 0, 'l', "\tDisplay all known members (past and present) of this cluster (Not available for heartbeat clusters)"},
{"partition", 0, 0, 'p', "Display the members of this partition"},
{"cluster-id", 0, 0, 'i', "Display this node's cluster id"},
{"remove", 1, 0, 'R', "(Advanced) Remove the (stopped) node with the specified name from Pacemaker's configuration and caches"},
{"-spacer-", 1, 0, '-', "In the case of Heartbeat, CMAN and Corosync 2.0, requires that the node has already been removed from the underlying cluster"},
{"-spacer-", 1, 0, '-', "\nAdditional Options:"},
{"force", 0, 0, 'f'},
{0, 0, 0, 0}
};
/* *INDENT-ON* */
static int
cib_remove_node(uint32_t id, const char *name)
{
int rc;
cib_t *cib = NULL;
xmlNode *node = NULL;
xmlNode *node_state = NULL;
crm_trace("Removing %s from the CIB", name);
node = create_xml_node(NULL, XML_CIB_TAG_NODE);
node_state = create_xml_node(NULL, XML_CIB_TAG_STATE);
crm_xml_add(node, XML_ATTR_UNAME, name);
crm_xml_add(node_state, XML_ATTR_UNAME, name);
cib = cib_new();
cib->cmds->signon(cib, crm_system_name, cib_command);
rc = cib->cmds->delete(cib, XML_CIB_TAG_NODES, node, cib_sync_call);
if (rc != pcmk_ok) {
printf("Could not remove %s from " XML_CIB_TAG_NODES ": %s", name, pcmk_strerror(rc));
}
rc = cib->cmds->delete(cib, XML_CIB_TAG_STATUS, node_state, cib_sync_call);
if (rc != pcmk_ok) {
printf("Could not remove %s from " XML_CIB_TAG_STATUS ": %s", name, pcmk_strerror(rc));
}
cib->cmds->signoff(cib);
cib_delete(cib);
return rc;
}
int
crmd_remove_node_cache(const char *id)
{
int n = 0;
int rc = -1;
char *name = NULL;
char *admin_uuid = NULL;
crm_ipc_t *conn = crm_ipc_new(CRM_SYSTEM_CRMD, 0);
xmlNode *cmd = NULL;
xmlNode *hello = NULL;
xmlNode *msg_data = NULL;
char *endptr = NULL;
if (!conn) {
goto rm_node_cleanup;
}
if (!crm_ipc_connect(conn)) {
goto rm_node_cleanup;
}
admin_uuid = calloc(1, 11);
snprintf(admin_uuid, 10, "%d", getpid());
admin_uuid[10] = '\0';
hello = create_hello_message(admin_uuid, "crm_node", "0", "1");
rc = crm_ipc_send(conn, hello, 0, 0, NULL);
if (rc < 0) {
goto rm_node_cleanup;
}
errno = 0;
n = strtol(id, &endptr, 10);
if (errno != 0 || endptr == id || *endptr != '\0') {
/* Argument was not a nodeid */
n = 0;
name = strdup(id);
} else {
name = get_node_name(n);
}
crm_trace("Removing %s aka. %s from the membership cache", name, id);
msg_data = create_xml_node(NULL, XML_TAG_OPTIONS);
if (n) {
crm_xml_add_int(msg_data, XML_ATTR_ID, n);
}
crm_xml_add(msg_data, XML_ATTR_UNAME, name);
cmd = create_request(CRM_OP_RM_NODE_CACHE,
msg_data, NULL, CRM_SYSTEM_CRMD, "crm_node", admin_uuid);
rc = crm_ipc_send(conn, cmd, 0, 0, NULL);
if (rc > 0) {
rc = cib_remove_node(n, name);
}
rm_node_cleanup:
if (conn) {
crm_ipc_close(conn);
crm_ipc_destroy(conn);
}
free_xml(cmd);
free_xml(hello);
free(admin_uuid);
return rc > 0 ? 0 : rc;
}
#if SUPPORT_HEARTBEAT
# include <ocf/oc_event.h>
# include <ocf/oc_membership.h>
# include <clplumbing/cl_uuid.h>
# define UUID_LEN 16
oc_ev_t *ccm_token = NULL;
static void *ccm_library = NULL;
void oc_ev_special(const oc_ev_t *, oc_ev_class_t, int);
static gboolean
read_local_hb_uuid(void)
{
cl_uuid_t uuid;
char *buffer = NULL;
long start = 0, read_len = 0;
FILE *input = fopen(UUID_FILE, "r");
if (input == NULL) {
crm_info("Could not open UUID file %s\n", UUID_FILE);
return FALSE;
}
/* see how big the file is */
start = ftell(input);
fseek(input, 0L, SEEK_END);
if (UUID_LEN != ftell(input)) {
fprintf(stderr, "%s must contain exactly %d bytes\n", UUID_FILE, UUID_LEN);
abort();
}
fseek(input, 0L, start);
if (start != ftell(input)) {
fprintf(stderr, "fseek not behaving: %ld vs. %ld\n", start, ftell(input));
crm_exit(2);
}
buffer = malloc(50);
read_len = fread(uuid.uuid, 1, UUID_LEN, input);
fclose(input);
if (read_len != UUID_LEN) {
fprintf(stderr, "Expected and read bytes differ: %d vs. %ld\n", UUID_LEN, read_len);
crm_exit(3);
} else if (buffer != NULL) {
cl_uuid_unparse(&uuid, buffer);
fprintf(stdout, "%s\n", buffer);
return TRUE;
} else {
fprintf(stderr, "No buffer to unparse\n");
crm_exit(4);
}
free(buffer);
return FALSE;
}
static void
ccm_age_callback(oc_ed_t event, void *cookie, size_t size, const void *data)
{
int lpc;
int node_list_size;
const oc_ev_membership_t *oc = (const oc_ev_membership_t *)data;
int (*ccm_api_callback_done) (void *cookie) =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_callback_done", 1);
node_list_size = oc->m_n_member;
if (command == 'q') {
crm_debug("Processing \"%s\" event.",
event == OC_EV_MS_NEW_MEMBERSHIP ? "NEW MEMBERSHIP" :
event == OC_EV_MS_NOT_PRIMARY ? "NOT PRIMARY" :
event == OC_EV_MS_PRIMARY_RESTORED ? "PRIMARY RESTORED" :
event == OC_EV_MS_EVICTED ? "EVICTED" : "NO QUORUM MEMBERSHIP");
if (ccm_have_quorum(event)) {
fprintf(stdout, "1\n");
} else {
fprintf(stdout, "0\n");
}
} else if (command == 'e') {
crm_debug("Searching %d members for our birth", oc->m_n_member);
}
for (lpc = 0; lpc < node_list_size; lpc++) {
if (command == 'p') {
fprintf(stdout, "%s ", oc->m_array[oc->m_memb_idx + lpc].node_uname);
} else if (command == 'e') {
int (*ccm_api_is_my_nodeid) (const oc_ev_t * token, const oc_node_t * node) =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_is_my_nodeid", 1);
if ((*ccm_api_is_my_nodeid) (ccm_token, &(oc->m_array[lpc]))) {
crm_debug("MATCH: nodeid=%d, uname=%s, born=%d",
oc->m_array[oc->m_memb_idx + lpc].node_id,
oc->m_array[oc->m_memb_idx + lpc].node_uname,
oc->m_array[oc->m_memb_idx + lpc].node_born_on);
fprintf(stdout, "%d\n", oc->m_array[oc->m_memb_idx + lpc].node_born_on);
}
}
}
(*ccm_api_callback_done) (cookie);
if (command == 'p') {
fprintf(stdout, "\n");
}
fflush(stdout);
crm_exit(0);
}
static gboolean
ccm_age_connect(int *ccm_fd)
{
gboolean did_fail = FALSE;
int ret = 0;
int (*ccm_api_register) (oc_ev_t ** token) =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_register", 1);
int (*ccm_api_set_callback) (const oc_ev_t * token,
oc_ev_class_t class,
oc_ev_callback_t * fn,
oc_ev_callback_t ** prev_fn) =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_set_callback", 1);
void (*ccm_api_special) (const oc_ev_t *, oc_ev_class_t, int) =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_special", 1);
int (*ccm_api_activate) (const oc_ev_t * token, int *fd) =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_activate", 1);
crm_debug("Registering with CCM");
ret = (*ccm_api_register) (&ccm_token);
if (ret != 0) {
crm_info("CCM registration failed: %d", ret);
did_fail = TRUE;
}
if (did_fail == FALSE) {
crm_debug("Setting up CCM callbacks");
ret = (*ccm_api_set_callback) (ccm_token, OC_EV_MEMB_CLASS, ccm_age_callback, NULL);
if (ret != 0) {
crm_warn("CCM callback not set: %d", ret);
did_fail = TRUE;
}
}
if (did_fail == FALSE) {
(*ccm_api_special) (ccm_token, OC_EV_MEMB_CLASS, 0 /*don't care */ );
crm_debug("Activating CCM token");
ret = (*ccm_api_activate) (ccm_token, ccm_fd);
if (ret != 0) {
crm_warn("CCM Activation failed: %d", ret);
did_fail = TRUE;
}
}
return !did_fail;
}
static gboolean
try_heartbeat(int command, enum cluster_type_e stack)
{
crm_debug("Attempting to process %c command", command);
if (command == 'i') {
if (read_local_hb_uuid()) {
crm_exit(0);
}
} else if (command == 'R') {
if (crmd_remove_node_cache(target_uname)) {
crm_err("Failed to connect to crmd to remove node id %s", target_uname);
crm_exit(-pcmk_err_generic);
}
crm_exit(0);
} else if (ccm_age_connect(&ccm_fd)) {
int rc = 0;
fd_set rset;
int (*ccm_api_handle_event) (const oc_ev_t * token) =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_handle_event", 1);
while (1) {
sleep(1);
FD_ZERO(&rset);
FD_SET(ccm_fd, &rset);
errno = 0;
rc = select(ccm_fd + 1, &rset, NULL, NULL, NULL);
if (rc > 0 && (*ccm_api_handle_event) (ccm_token) != 0) {
crm_err("oc_ev_handle_event failed");
return FALSE;
} else if (rc < 0 && errno != EINTR) {
crm_perror(LOG_ERR, "select failed: %d", rc);
return FALSE;
}
}
}
return FALSE;
}
#endif
#if SUPPORT_CMAN
# include <libcman.h>
# define MAX_NODES 256
static gboolean
try_cman(int command, enum cluster_type_e stack)
{
int rc = -1, lpc = 0, node_count = 0;
cman_node_t node;
cman_cluster_t cluster;
cman_handle_t cman_handle = NULL;
cman_node_t cman_nodes[MAX_NODES];
memset(&cluster, 0, sizeof(cluster));
cman_handle = cman_init(NULL);
if (cman_handle == NULL || cman_is_active(cman_handle) == FALSE) {
crm_info("Couldn't connect to cman");
return FALSE;
}
switch (command) {
case 'R':
if (crmd_remove_node_cache(target_uname)) {
crm_err("Failed to connect to crmd to remove node id %s", target_uname);
}
break;
case 'e':
/* Age makes no sense (yet?) in a cman cluster */
fprintf(stdout, "1\n");
break;
case 'q':
fprintf(stdout, "%d\n", cman_is_quorate(cman_handle));
break;
case 'l':
case 'p':
rc = cman_get_nodes(cman_handle, MAX_NODES, &node_count, cman_nodes);
if (rc != 0) {
fprintf(stderr, "Couldn't query cman node list: %d %d", rc, errno);
goto cman_bail;
}
for (lpc = 0; lpc < node_count; lpc++) {
if (command == 'l') {
printf("%s ", cman_nodes[lpc].cn_name);
} else if (cman_nodes[lpc].cn_nodeid != 0 && cman_nodes[lpc].cn_member) {
/* Never allow node ID 0 to be considered a member #315711 */
printf("%s ", cman_nodes[lpc].cn_name);
}
}
printf("\n");
break;
case 'i':
rc = cman_get_node(cman_handle, CMAN_NODEID_US, &node);
if (rc != 0) {
fprintf(stderr, "Couldn't query cman node id: %d %d", rc, errno);
goto cman_bail;
}
fprintf(stdout, "%u\n", node.cn_nodeid);
break;
default:
fprintf(stderr, "Unknown option '%c'\n", command);
crm_help('?', EX_USAGE);
}
cman_finish(cman_handle);
crm_exit(0);
cman_bail:
cman_finish(cman_handle);
return crm_exit(EX_USAGE);
}
#endif
#if HAVE_CONFDB
static void
ais_membership_destroy(gpointer user_data)
{
crm_err("AIS connection terminated");
ais_fd_sync = -1;
crm_exit(1);
}
static gint
member_sort(gconstpointer a, gconstpointer b)
{
const crm_node_t *node_a = a;
const crm_node_t *node_b = b;
return strcmp(node_a->uname, node_b->uname);
}
static void
crm_add_member(gpointer key, gpointer value, gpointer user_data)
{
GList **list = user_data;
crm_node_t *node = value;
if (node->uname != NULL) {
*list = g_list_insert_sorted(*list, node, member_sort);
}
}
static gboolean
ais_membership_dispatch(int kind, const char *from, const char *data)
{
switch (kind) {
case crm_class_members:
case crm_class_notify:
case crm_class_quorum:
break;
default:
return TRUE;
break;
}
if (command == 'q') {
if (crm_have_quorum) {
fprintf(stdout, "1\n");
} else {
fprintf(stdout, "0\n");
}
} else if (command == 'l') {
GList *nodes = NULL;
GListPtr lpc = NULL;
g_hash_table_foreach(crm_peer_cache, crm_add_member, &nodes);
for (lpc = nodes; lpc != NULL; lpc = lpc->next) {
crm_node_t *node = (crm_node_t *) lpc->data;
fprintf(stdout, "%u %s %s\n", node->id, node->uname, node->state);
}
fprintf(stdout, "\n");
} else if (command == 'p') {
GList *nodes = NULL;
GListPtr lpc = NULL;
g_hash_table_foreach(crm_peer_cache, crm_add_member, &nodes);
for (lpc = nodes; lpc != NULL; lpc = lpc->next) {
crm_node_t *node = (crm_node_t *) lpc->data;
if (node->uname && safe_str_eq(node->state, CRM_NODE_MEMBER)) {
fprintf(stdout, "%s ", node->uname);
}
}
fprintf(stdout, "\n");
}
crm_exit(0);
return TRUE;
}
#endif
#ifdef SUPPORT_CS_QUORUM
# include <corosync/quorum.h>
# include <corosync/cpg.h>
static int
node_mcp_dispatch(const char *buffer, ssize_t length, gpointer userdata)
{
xmlNode *msg = string2xml(buffer);
if (msg) {
xmlNode *node = NULL;
crm_log_xml_trace(msg, "message");
for (node = __xml_first_child(msg); node != NULL; node = __xml_next(node)) {
const char *uname = crm_element_value(node, "uname");
if (command == 'l') {
int id = 0;
crm_element_value_int(node, "id", &id);
fprintf(stdout, "%u %s\n", id, uname);
} else if (command == 'p') {
fprintf(stdout, "%s ", uname);
}
}
free_xml(msg);
if (command == 'p') {
fprintf(stdout, "\n");
}
crm_exit(0);
}
return 0;
}
static void
node_mcp_destroy(gpointer user_data)
{
crm_exit(1);
}
static gboolean
try_corosync(int command, enum cluster_type_e stack)
{
int rc = 0;
int quorate = 0;
uint32_t quorum_type = 0;
unsigned int nodeid = 0;
cpg_handle_t c_handle = 0;
quorum_handle_t q_handle = 0;
mainloop_io_t *ipc = NULL;
GMainLoop *amainloop = NULL;
struct ipc_client_callbacks node_callbacks = {
.dispatch = node_mcp_dispatch,
.destroy = node_mcp_destroy
};
switch (command) {
case 'R':
if (crmd_remove_node_cache(target_uname)) {
crm_err("Failed to connect to crmd to remove node id %s", target_uname);
}
break;
case 'e':
/* Age makes no sense (yet) in an AIS cluster */
fprintf(stdout, "1\n");
crm_exit(0);
case 'q':
/* Go direct to the Quorum API */
rc = quorum_initialize(&q_handle, NULL, &quorum_type);
if (rc != CS_OK) {
crm_err("Could not connect to the Quorum API: %d\n", rc);
return FALSE;
}
rc = quorum_getquorate(q_handle, &quorate);
if (rc != CS_OK) {
crm_err("Could not obtain the current Quorum API state: %d\n", rc);
return FALSE;
}
if (quorate) {
fprintf(stdout, "1\n");
} else {
fprintf(stdout, "0\n");
}
quorum_finalize(q_handle);
crm_exit(0);
case 'i':
/* Go direct to the CPG API */
rc = cpg_initialize(&c_handle, NULL);
if (rc != CS_OK) {
crm_err("Could not connect to the Cluster Process Group API: %d\n", rc);
return FALSE;
}
rc = cpg_local_get(c_handle, &nodeid);
if (rc != CS_OK) {
crm_err("Could not get local node id from the CPG API");
return FALSE;
}
fprintf(stdout, "%u\n", nodeid);
cpg_finalize(c_handle);
crm_exit(0);
case 'l':
case 'p':
/* Go to pacemakerd */
amainloop = g_main_new(FALSE);
ipc =
mainloop_add_ipc_client(CRM_SYSTEM_MCP, G_PRIORITY_DEFAULT, 0, NULL,
&node_callbacks);
if (ipc != NULL) {
xmlNode *poke = create_xml_node(NULL, "poke");
crm_ipc_send(mainloop_get_ipc_client(ipc), poke, 0, 0, NULL);
free_xml(poke);
g_main_run(amainloop);
}
break;
}
return FALSE;
}
#endif
#if HAVE_CONFDB
static gboolean
try_openais(int command, enum cluster_type_e stack)
{
static crm_cluster_t cluster;
cluster.destroy = ais_membership_destroy;
cluster.cs_dispatch = ais_membership_dispatch;
if (init_cs_connection_once(&cluster)) {
GMainLoop *amainloop = NULL;
switch (command) {
case 'R':
send_ais_text(crm_class_rmpeer, target_uname, TRUE, NULL, crm_msg_ais);
cib_remove_node(0, target_uname);
crm_exit(0);
case 'e':
/* Age makes no sense (yet) in an AIS cluster */
fprintf(stdout, "1\n");
crm_exit(0);
case 'q':
send_ais_text(crm_class_quorum, NULL, TRUE, NULL, crm_msg_ais);
break;
case 'l':
case 'p':
crm_info("Requesting the list of configured nodes");
send_ais_text(crm_class_members, __FUNCTION__, TRUE, NULL, crm_msg_ais);
break;
case 'i':
printf("%u\n", cluster.nodeid);
crm_exit(0);
default:
fprintf(stderr, "Unknown option '%c'\n", command);
crm_help('?', EX_USAGE);
}
amainloop = g_main_new(FALSE);
g_main_run(amainloop);
}
return FALSE;
}
#endif
int set_cluster_type(enum cluster_type_e type);
int
main(int argc, char **argv)
{
int flag = 0;
int argerr = 0;
uint32_t nodeid = 0;
gboolean force_flag = FALSE;
gboolean dangerous_cmd = FALSE;
enum cluster_type_e try_stack = pcmk_cluster_unknown;
int option_index = 0;
crm_peer_init();
crm_log_cli_init("crm_node");
crm_set_options(NULL, "command [options]", long_options,
"Tool for displaying low-level node information");
while (flag >= 0) {
flag = crm_get_option(argc, argv, &option_index);
switch (flag) {
case -1:
break;
case 'V':
crm_bump_log_level(argc, argv);
break;
case '$':
case '?':
crm_help(flag, EX_OK);
break;
case 'Q':
do_quiet = TRUE;
break;
case 'H':
set_cluster_type(pcmk_cluster_heartbeat);
break;
case 'A':
set_cluster_type(pcmk_cluster_classic_ais);
break;
case 'C':
set_cluster_type(pcmk_cluster_corosync);
break;
case 'c':
set_cluster_type(pcmk_cluster_cman);
break;
case 'f':
force_flag = TRUE;
break;
case 'R':
dangerous_cmd = TRUE;
command = flag;
target_uname = optarg;
break;
case 'N':
command = flag;
nodeid = crm_parse_int(optarg, NULL);
break;
case 'p':
case 'e':
case 'q':
case 'i':
case 'l':
case 'n':
command = flag;
break;
default:
++argerr;
break;
}
}
if (optind > argc) {
++argerr;
}
if (argerr) {
crm_help('?', EX_USAGE);
}
if (command == 'n') {
fprintf(stdout, "%s\n", get_local_node_name());
crm_exit(0);
} else if (command == 'N') {
fprintf(stdout, "%s\n", get_node_name(nodeid));
crm_exit(0);
}
if (dangerous_cmd && force_flag == FALSE) {
fprintf(stderr, "The supplied command is considered dangerous."
" To prevent accidental destruction of the cluster,"
" the --force flag is required in order to proceed.\n");
fflush(stderr);
crm_exit(EX_USAGE);
}
try_stack = get_cluster_type();
crm_debug("Attempting to process -%c command for cluster type: %s", command,
name_for_cluster_type(try_stack));
#if SUPPORT_CMAN
if (try_stack == pcmk_cluster_cman) {
try_cman(command, try_stack);
}
#endif
#ifdef SUPPORT_CS_QUORUM
if (try_stack == pcmk_cluster_corosync) {
try_corosync(command, try_stack);
}
#endif
#if HAVE_CONFDB
/* Only an option if we're using the plugins */
if (try_stack == pcmk_cluster_classic_ais) {
try_openais(command, try_stack);
}
#endif
#if SUPPORT_HEARTBEAT
if (try_stack == pcmk_cluster_heartbeat) {
try_heartbeat(command, try_stack);
}
#endif
return (1);
}