This source file includes following definitions.
- attr_value_cb
- command_cb
- delete_attr_cb
- get_attr_cb
- grant_standby_cb
- set_attr_cb
- find_ticket
- print_date
- print_ticket
- print_ticket_list
- find_ticket_state
- find_ticket_constraints
- dump_ticket_xml
- dump_constraints
- get_ticket_state_attr
- ticket_warning
- allow_modification
- modify_ticket_state
- delete_ticket_state
- build_arg_context
- main
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <sys/param.h>
13
14 #include <crm/crm.h>
15
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <libgen.h>
24
25 #include <crm/msg_xml.h>
26 #include <crm/common/xml.h>
27 #include <crm/common/ipc.h>
28 #include <crm/common/cmdline_internal.h>
29
30 #include <crm/cib.h>
31 #include <crm/cib/internal.h>
32 #include <crm/pengine/rules.h>
33 #include <crm/pengine/status.h>
34
35 #include <pacemaker-internal.h>
36
37 GError *error = NULL;
38
39 #define SUMMARY "Perform tasks related to cluster tickets\n\n" \
40 "Allows ticket attributes to be queried, modified and deleted."
41
42 struct {
43 gchar *attr_default;
44 gchar *attr_id;
45 char *attr_name;
46 char *attr_value;
47 gboolean force;
48 char *get_attr_name;
49 gboolean quiet;
50 gchar *set_name;
51 char ticket_cmd;
52 gchar *ticket_id;
53 gchar *xml_file;
54 } options = {
55 .ticket_cmd = 'S'
56 };
57
58 GList *attr_delete;
59 GHashTable *attr_set;
60 bool modified = false;
61 int cib_options = cib_sync_call;
62
63 #define INDENT " "
64
65 static gboolean
66 attr_value_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
67 pcmk__str_update(&options.attr_value, optarg);
68
69 if (!options.attr_name || !options.attr_value) {
70 return TRUE;
71 }
72
73 g_hash_table_insert(attr_set, strdup(options.attr_name), strdup(options.attr_value));
74 pcmk__str_update(&options.attr_name, NULL);
75 pcmk__str_update(&options.attr_value, NULL);
76
77 modified = true;
78
79 return TRUE;
80 }
81
82 static gboolean
83 command_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
84 if (pcmk__str_any_of(option_name, "--info", "-l", NULL)) {
85 options.ticket_cmd = 'l';
86 } else if (pcmk__str_any_of(option_name, "--details", "-L", NULL)) {
87 options.ticket_cmd = 'L';
88 } else if (pcmk__str_any_of(option_name, "--raw", "-w", NULL)) {
89 options.ticket_cmd = 'w';
90 } else if (pcmk__str_any_of(option_name, "--query-xml", "-q", NULL)) {
91 options.ticket_cmd = 'q';
92 } else if (pcmk__str_any_of(option_name, "--constraints", "-c", NULL)) {
93 options.ticket_cmd = 'c';
94 } else if (pcmk__str_any_of(option_name, "--cleanup", "-C", NULL)) {
95 options.ticket_cmd = 'C';
96 }
97
98 return TRUE;
99 }
100
101 static gboolean
102 delete_attr_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
103 attr_delete = g_list_append(attr_delete, strdup(optarg));
104 modified = true;
105 return TRUE;
106 }
107
108 static gboolean
109 get_attr_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
110 pcmk__str_update(&options.get_attr_name, optarg);
111 options.ticket_cmd = 'G';
112 return TRUE;
113 }
114
115 static gboolean
116 grant_standby_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
117 if (pcmk__str_any_of(option_name, "--grant", "-g", NULL)) {
118 g_hash_table_insert(attr_set, strdup("granted"), strdup("true"));
119 modified = true;
120 } else if (pcmk__str_any_of(option_name, "--revoke", "-r", NULL)) {
121 g_hash_table_insert(attr_set, strdup("granted"), strdup("false"));
122 modified = true;
123 } else if (pcmk__str_any_of(option_name, "--standby", "-s", NULL)) {
124 g_hash_table_insert(attr_set, strdup("standby"), strdup("true"));
125 modified = true;
126 } else if (pcmk__str_any_of(option_name, "--activate", "-a", NULL)) {
127 g_hash_table_insert(attr_set, strdup("standby"), strdup("false"));
128 modified = true;
129 }
130
131 return TRUE;
132 }
133
134 static gboolean
135 set_attr_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
136 pcmk__str_update(&options.attr_name, optarg);
137
138 if (!options.attr_name || !options.attr_value) {
139 return TRUE;
140 }
141
142 g_hash_table_insert(attr_set, strdup(options.attr_name), strdup(options.attr_value));
143 pcmk__str_update(&options.attr_name, NULL);
144 pcmk__str_update(&options.attr_value, NULL);
145
146 modified = true;
147
148 return TRUE;
149 }
150
151 static GOptionEntry query_entries[] = {
152 { "info", 'l', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
153 "Display the information of ticket(s)",
154 NULL },
155
156 { "details", 'L', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
157 "Display the details of ticket(s)",
158 NULL },
159
160 { "raw", 'w', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
161 "Display the IDs of ticket(s)",
162 NULL },
163
164 { "query-xml", 'q', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
165 "Query the XML of ticket(s)",
166 NULL },
167
168 { "constraints", 'c', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
169 "Display the rsc_ticket constraints that apply to ticket(s)",
170 NULL },
171
172 { NULL }
173 };
174
175 static GOptionEntry command_entries[] = {
176 { "grant", 'g', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, grant_standby_cb,
177 "Grant a ticket to this cluster site",
178 NULL },
179
180 { "revoke", 'r', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, grant_standby_cb,
181 "Revoke a ticket from this cluster site",
182 NULL },
183
184 { "standby", 's', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, grant_standby_cb,
185 "Tell this cluster site this ticket is standby",
186 NULL },
187
188 { "activate", 'a', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, grant_standby_cb,
189 "Tell this cluster site this ticket is active",
190 NULL },
191
192 { NULL }
193 };
194
195 static GOptionEntry advanced_entries[] = {
196 { "get-attr", 'G', 0, G_OPTION_ARG_CALLBACK, get_attr_cb,
197 "Display the named attribute for a ticket",
198 "ATTRIBUTE" },
199
200 { "set-attr", 'S', 0, G_OPTION_ARG_CALLBACK, set_attr_cb,
201 "Set the named attribute for a ticket",
202 "ATTRIBUTE" },
203
204 { "delete-attr", 'D', 0, G_OPTION_ARG_CALLBACK, delete_attr_cb,
205 "Delete the named attribute for a ticket",
206 "ATTRIBUTE" },
207
208 { "cleanup", 'C', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, command_cb,
209 "Delete all state of a ticket at this cluster site",
210 NULL },
211
212 { NULL}
213 };
214
215 static GOptionEntry addl_entries[] = {
216 { "attr-value", 'v', 0, G_OPTION_ARG_CALLBACK, attr_value_cb,
217 "Attribute value to use with -S",
218 "VALUE" },
219
220 { "default", 'd', 0, G_OPTION_ARG_STRING, &options.attr_default,
221 "(Advanced) Default attribute value to display if none is found\n"
222 INDENT "(for use with -G)",
223 "VALUE" },
224
225 { "force", 'f', 0, G_OPTION_ARG_NONE, &options.force,
226 "(Advanced) Force the action to be performed",
227 NULL },
228
229 { "ticket", 't', 0, G_OPTION_ARG_STRING, &options.ticket_id,
230 "Ticket ID",
231 "ID" },
232
233 { "xml-file", 'x', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &options.xml_file,
234 NULL,
235 NULL },
236
237 { NULL }
238 };
239
240 static GOptionEntry deprecated_entries[] = {
241 { "set-name", 'n', 0, G_OPTION_ARG_STRING, &options.set_name,
242 "(Advanced) ID of the instance_attributes object to change",
243 "ID" },
244
245 { "nvpair", 'i', 0, G_OPTION_ARG_STRING, &options.attr_id,
246 "(Advanced) ID of the nvpair object to change/delete",
247 "ID" },
248
249 { "quiet", 'Q', 0, G_OPTION_ARG_NONE, &options.quiet,
250 "Print only the value on stdout",
251 NULL },
252
253 { NULL }
254 };
255
256 static pe_ticket_t *
257 find_ticket(gchar *ticket_id, pe_working_set_t * data_set)
258 {
259 return g_hash_table_lookup(data_set->tickets, ticket_id);
260 }
261
262 static void
263 print_date(time_t time)
264 {
265 int lpc = 0;
266 char date_str[26];
267
268 asctime_r(localtime(&time), date_str);
269 for (; lpc < 26; lpc++) {
270 if (date_str[lpc] == '\n') {
271 date_str[lpc] = 0;
272 }
273 }
274 fprintf(stdout, "'%s'", date_str);
275 }
276
277 static void
278 print_ticket(pe_ticket_t * ticket, bool raw, bool details)
279 {
280 if (raw) {
281 fprintf(stdout, "%s\n", ticket->id);
282 return;
283 }
284
285 fprintf(stdout, "%s\t%s %s",
286 ticket->id, ticket->granted ? "granted" : "revoked",
287 ticket->standby ? "[standby]" : " ");
288
289 if (details && g_hash_table_size(ticket->state) > 0) {
290 GHashTableIter iter;
291 const char *name = NULL;
292 const char *value = NULL;
293 int lpc = 0;
294
295 fprintf(stdout, " (");
296
297 g_hash_table_iter_init(&iter, ticket->state);
298 while (g_hash_table_iter_next(&iter, (void **)&name, (void **)&value)) {
299 if (lpc > 0) {
300 fprintf(stdout, ", ");
301 }
302 fprintf(stdout, "%s=", name);
303 if (pcmk__str_any_of(name, "last-granted", "expires", NULL)) {
304 long long time_ll;
305
306 pcmk__scan_ll(value, &time_ll, 0);
307 print_date((time_t) time_ll);
308 } else {
309 fprintf(stdout, "%s", value);
310 }
311 lpc++;
312 }
313
314 fprintf(stdout, ")\n");
315
316 } else {
317 if (ticket->last_granted > -1) {
318 fprintf(stdout, " last-granted=");
319 print_date(ticket->last_granted);
320 }
321 fprintf(stdout, "\n");
322 }
323
324 return;
325 }
326
327 static void
328 print_ticket_list(pe_working_set_t * data_set, bool raw, bool details)
329 {
330 GHashTableIter iter;
331 pe_ticket_t *ticket = NULL;
332
333 g_hash_table_iter_init(&iter, data_set->tickets);
334
335 while (g_hash_table_iter_next(&iter, NULL, (void **)&ticket)) {
336 print_ticket(ticket, raw, details);
337 }
338 }
339
340 static int
341 find_ticket_state(cib_t * the_cib, gchar *ticket_id, xmlNode ** ticket_state_xml)
342 {
343 int rc = pcmk_rc_ok;
344 xmlNode *xml_search = NULL;
345
346 GString *xpath = NULL;
347
348 CRM_ASSERT(ticket_state_xml != NULL);
349 *ticket_state_xml = NULL;
350
351 xpath = g_string_sized_new(1024);
352 g_string_append(xpath,
353 "/" XML_TAG_CIB "/" XML_CIB_TAG_STATUS
354 "/" XML_CIB_TAG_TICKETS);
355
356 if (ticket_id != NULL) {
357 pcmk__g_strcat(xpath,
358 "/" XML_CIB_TAG_TICKET_STATE
359 "[@" XML_ATTR_ID "=\"", ticket_id, "\"]", NULL);
360 }
361
362 rc = the_cib->cmds->query(the_cib, (const char *) xpath->str, &xml_search,
363 cib_sync_call | cib_scope_local | cib_xpath);
364 rc = pcmk_legacy2rc(rc);
365 g_string_free(xpath, TRUE);
366
367 if (rc != pcmk_rc_ok) {
368 return rc;
369 }
370
371 crm_log_xml_debug(xml_search, "Match");
372 if (xml_has_children(xml_search)) {
373 if (ticket_id) {
374 fprintf(stdout, "Multiple ticket_states match ticket_id=%s\n", ticket_id);
375 }
376 *ticket_state_xml = xml_search;
377 } else {
378 *ticket_state_xml = xml_search;
379 }
380 return rc;
381 }
382
383 static int
384 find_ticket_constraints(cib_t * the_cib, gchar *ticket_id, xmlNode ** ticket_cons_xml)
385 {
386 int rc = pcmk_rc_ok;
387 xmlNode *xml_search = NULL;
388
389 GString *xpath = NULL;
390 const char *xpath_base = NULL;
391
392 CRM_ASSERT(ticket_cons_xml != NULL);
393 *ticket_cons_xml = NULL;
394
395 xpath_base = pcmk_cib_xpath_for(XML_CIB_TAG_CONSTRAINTS);
396 if (xpath_base == NULL) {
397 crm_err(XML_CIB_TAG_CONSTRAINTS " CIB element not known (bug?)");
398 return -ENOMSG;
399 }
400
401 xpath = g_string_sized_new(1024);
402 pcmk__g_strcat(xpath, xpath_base, "/" XML_CONS_TAG_RSC_TICKET, NULL);
403
404 if (ticket_id != NULL) {
405 pcmk__g_strcat(xpath,
406 "[@" XML_TICKET_ATTR_TICKET "=\"", ticket_id, "\"]",
407 NULL);
408 }
409
410 rc = the_cib->cmds->query(the_cib, (const char *) xpath->str, &xml_search,
411 cib_sync_call | cib_scope_local | cib_xpath);
412 rc = pcmk_legacy2rc(rc);
413 g_string_free(xpath, TRUE);
414
415 if (rc != pcmk_rc_ok) {
416 return rc;
417 }
418
419 crm_log_xml_debug(xml_search, "Match");
420 *ticket_cons_xml = xml_search;
421
422 return rc;
423 }
424
425 static int
426 dump_ticket_xml(cib_t * the_cib, gchar *ticket_id)
427 {
428 int rc = pcmk_rc_ok;
429 xmlNode *state_xml = NULL;
430
431 rc = find_ticket_state(the_cib, ticket_id, &state_xml);
432
433 if (state_xml == NULL) {
434 return rc;
435 }
436
437 fprintf(stdout, "State XML:\n");
438 if (state_xml) {
439 char *state_xml_str = NULL;
440
441 state_xml_str = dump_xml_formatted(state_xml);
442 fprintf(stdout, "\n%s", pcmk__s(state_xml_str, "<null>\n"));
443 free_xml(state_xml);
444 free(state_xml_str);
445 }
446
447 return rc;
448 }
449
450 static int
451 dump_constraints(cib_t * the_cib, gchar *ticket_id)
452 {
453 int rc = pcmk_rc_ok;
454 xmlNode *cons_xml = NULL;
455 char *cons_xml_str = NULL;
456
457 rc = find_ticket_constraints(the_cib, ticket_id, &cons_xml);
458
459 if (cons_xml == NULL) {
460 return rc;
461 }
462
463 cons_xml_str = dump_xml_formatted(cons_xml);
464 fprintf(stdout, "Constraints XML:\n\n%s",
465 pcmk__s(cons_xml_str, "<null>\n"));
466 free_xml(cons_xml);
467 free(cons_xml_str);
468
469 return rc;
470 }
471
472 static int
473 get_ticket_state_attr(gchar *ticket_id, const char *attr_name, const char **attr_value,
474 pe_working_set_t * data_set)
475 {
476 pe_ticket_t *ticket = NULL;
477
478 CRM_ASSERT(attr_value != NULL);
479 *attr_value = NULL;
480
481 ticket = g_hash_table_lookup(data_set->tickets, ticket_id);
482 if (ticket == NULL) {
483 return ENXIO;
484 }
485
486 *attr_value = g_hash_table_lookup(ticket->state, attr_name);
487 if (*attr_value == NULL) {
488 return ENXIO;
489 }
490
491 return pcmk_rc_ok;
492 }
493
494 static void
495 ticket_warning(gchar *ticket_id, const char *action)
496 {
497 GString *warning = g_string_sized_new(1024);
498 const char *word = NULL;
499
500 CRM_ASSERT(action != NULL);
501
502 if (strcmp(action, "grant") == 0) {
503 pcmk__g_strcat(warning,
504 "This command cannot help you verify whether '",
505 ticket_id,
506 "' has been already granted elsewhere.\n", NULL);
507 word = "to";
508
509 } else {
510 pcmk__g_strcat(warning,
511 "Revoking '", ticket_id, "' can trigger the specified "
512 "'loss-policy'(s) relating to '", ticket_id, "'.\n\n"
513 "You can check that with:\n"
514 "crm_ticket --ticket ", ticket_id, " --constraints\n\n"
515 "Otherwise before revoking '", ticket_id, "', "
516 "you may want to make '", ticket_id, "' "
517 "standby with:\n"
518 "crm_ticket --ticket ", ticket_id, " --standby\n\n",
519 NULL);
520 word = "from";
521 }
522
523 pcmk__g_strcat(warning,
524 "If you really want to ", action, " '", ticket_id, "' ",
525 word, " this site now, and you know what you are doing,\n"
526 "please specify --force.", NULL);
527
528 fprintf(stdout, "%s\n", (const char *) warning->str);
529
530 g_string_free(warning, TRUE);
531 }
532
533 static bool
534 allow_modification(gchar *ticket_id)
535 {
536 const char *value = NULL;
537 GList *list_iter = NULL;
538
539 if (options.force) {
540 return true;
541 }
542
543 if (g_hash_table_lookup_extended(attr_set, "granted", NULL, (gpointer *) & value)) {
544 if (crm_is_true(value)) {
545 ticket_warning(ticket_id, "grant");
546 return false;
547
548 } else {
549 ticket_warning(ticket_id, "revoke");
550 return false;
551 }
552 }
553
554 for(list_iter = attr_delete; list_iter; list_iter = list_iter->next) {
555 const char *key = (const char *)list_iter->data;
556
557 if (pcmk__str_eq(key, "granted", pcmk__str_casei)) {
558 ticket_warning(ticket_id, "revoke");
559 return false;
560 }
561 }
562
563 return true;
564 }
565
566 static int
567 modify_ticket_state(gchar * ticket_id, cib_t * cib, pe_working_set_t * data_set)
568 {
569 int rc = pcmk_rc_ok;
570 xmlNode *xml_top = NULL;
571 xmlNode *ticket_state_xml = NULL;
572 bool found = false;
573
574 GList *list_iter = NULL;
575 GHashTableIter hash_iter;
576
577 char *key = NULL;
578 char *value = NULL;
579
580 pe_ticket_t *ticket = NULL;
581
582 rc = find_ticket_state(cib, ticket_id, &ticket_state_xml);
583 if (rc == pcmk_rc_ok) {
584 crm_debug("Found a match state for ticket: id=%s", ticket_id);
585 xml_top = ticket_state_xml;
586 found = true;
587
588 } else if (rc != ENXIO) {
589 return rc;
590
591 } else if (g_hash_table_size(attr_set) == 0){
592 return pcmk_rc_ok;
593
594 } else {
595 xmlNode *xml_obj = NULL;
596
597 xml_top = create_xml_node(NULL, XML_CIB_TAG_STATUS);
598 xml_obj = create_xml_node(xml_top, XML_CIB_TAG_TICKETS);
599 ticket_state_xml = create_xml_node(xml_obj, XML_CIB_TAG_TICKET_STATE);
600 crm_xml_add(ticket_state_xml, XML_ATTR_ID, ticket_id);
601 }
602
603 for(list_iter = attr_delete; list_iter; list_iter = list_iter->next) {
604 const char *key = (const char *)list_iter->data;
605 xml_remove_prop(ticket_state_xml, key);
606 }
607
608 ticket = find_ticket(ticket_id, data_set);
609
610 g_hash_table_iter_init(&hash_iter, attr_set);
611 while (g_hash_table_iter_next(&hash_iter, (gpointer *) & key, (gpointer *) & value)) {
612 crm_xml_add(ticket_state_xml, key, value);
613
614 if (pcmk__str_eq(key, "granted", pcmk__str_casei)
615 && (ticket == NULL || ticket->granted == FALSE)
616 && crm_is_true(value)) {
617
618 char *now = pcmk__ttoa(time(NULL));
619
620 crm_xml_add(ticket_state_xml, "last-granted", now);
621 free(now);
622 }
623 }
624
625 if (found && (attr_delete != NULL)) {
626 crm_log_xml_debug(xml_top, "Replace");
627 rc = cib->cmds->replace(cib, XML_CIB_TAG_STATUS, ticket_state_xml, cib_options);
628 rc = pcmk_legacy2rc(rc);
629
630 } else {
631 crm_log_xml_debug(xml_top, "Update");
632 rc = cib->cmds->modify(cib, XML_CIB_TAG_STATUS, xml_top, cib_options);
633 rc = pcmk_legacy2rc(rc);
634 }
635
636 free_xml(xml_top);
637 return rc;
638 }
639
640 static int
641 delete_ticket_state(gchar *ticket_id, cib_t * cib)
642 {
643 xmlNode *ticket_state_xml = NULL;
644
645 int rc = pcmk_rc_ok;
646
647 rc = find_ticket_state(cib, ticket_id, &ticket_state_xml);
648
649 if (rc == ENXIO) {
650 return pcmk_rc_ok;
651
652 } else if (rc != pcmk_rc_ok) {
653 return rc;
654 }
655
656 crm_log_xml_debug(ticket_state_xml, "Delete");
657
658 rc = cib->cmds->remove(cib, XML_CIB_TAG_STATUS, ticket_state_xml, cib_options);
659 rc = pcmk_legacy2rc(rc);
660
661 if (rc == pcmk_rc_ok) {
662 fprintf(stdout, "Cleaned up %s\n", ticket_id);
663 }
664
665 free_xml(ticket_state_xml);
666 return rc;
667 }
668
669 static GOptionContext *
670 build_arg_context(pcmk__common_args_t *args) {
671 GOptionContext *context = NULL;
672
673 const char *description = "Examples:\n\n"
674 "Display the info of tickets:\n\n"
675 "\tcrm_ticket --info\n\n"
676 "Display the detailed info of tickets:\n\n"
677 "\tcrm_ticket --details\n\n"
678 "Display the XML of 'ticketA':\n\n"
679 "\tcrm_ticket --ticket ticketA --query-xml\n\n"
680 "Display the rsc_ticket constraints that apply to 'ticketA':\n\n"
681 "\tcrm_ticket --ticket ticketA --constraints\n\n"
682 "Grant 'ticketA' to this cluster site:\n\n"
683 "\tcrm_ticket --ticket ticketA --grant\n\n"
684 "Revoke 'ticketA' from this cluster site:\n\n"
685 "\tcrm_ticket --ticket ticketA --revoke\n\n"
686 "Make 'ticketA' standby (the cluster site will treat a granted\n"
687 "'ticketA' as 'standby', and the dependent resources will be\n"
688 "stopped or demoted gracefully without triggering loss-policies):\n\n"
689 "\tcrm_ticket --ticket ticketA --standby\n\n"
690 "Activate 'ticketA' from being standby:\n\n"
691 "\tcrm_ticket --ticket ticketA --activate\n\n"
692 "Get the value of the 'granted' attribute for 'ticketA':\n\n"
693 "\tcrm_ticket --ticket ticketA --get-attr granted\n\n"
694 "Set the value of the 'standby' attribute for 'ticketA':\n\n"
695 "\tcrm_ticket --ticket ticketA --set-attr standby --attr-value true\n\n"
696 "Delete the 'granted' attribute for 'ticketA':\n\n"
697 "\tcrm_ticket --ticket ticketA --delete-attr granted\n\n"
698 "Erase the operation history of 'ticketA' at this cluster site,\n"
699 "causing the cluster site to 'forget' the existing ticket state:\n\n"
700 "\tcrm_ticket --ticket ticketA --cleanup\n\n";
701
702 context = pcmk__build_arg_context(args, NULL, NULL, NULL);
703 g_option_context_set_description(context, description);
704
705 pcmk__add_arg_group(context, "queries", "Queries:",
706 "Show queries", query_entries);
707 pcmk__add_arg_group(context, "commands", "Commands:",
708 "Show command options", command_entries);
709 pcmk__add_arg_group(context, "advanced", "Advanced Options:",
710 "Show advanced options", advanced_entries);
711 pcmk__add_arg_group(context, "additional", "Additional Options:",
712 "Show additional options", addl_entries);
713 pcmk__add_arg_group(context, "deprecated", "Deprecated Options:",
714 "Show deprecated options", deprecated_entries);
715
716 return context;
717 }
718
719 int
720 main(int argc, char **argv)
721 {
722 pe_working_set_t *data_set = NULL;
723 xmlNode *cib_xml_copy = NULL;
724
725 cib_t *cib_conn = NULL;
726 crm_exit_t exit_code = CRM_EX_OK;
727 int rc = pcmk_rc_ok;
728
729 pcmk__common_args_t *args = NULL;
730 GOptionContext *context = NULL;
731 gchar **processed_args = NULL;
732
733 attr_set = pcmk__strkey_table(free, free);
734 attr_delete = NULL;
735
736 args = pcmk__new_common_args(SUMMARY);
737 context = build_arg_context(args);
738 processed_args = pcmk__cmdline_preproc(argv, "dintvxCDGS");
739
740 if (!g_option_context_parse_strv(context, &processed_args, &error)) {
741 exit_code = CRM_EX_USAGE;
742 goto done;
743 }
744
745 pcmk__cli_init_logging("crm_ticket", args->verbosity);
746
747 if (args->version) {
748 g_strfreev(processed_args);
749 pcmk__free_arg_context(context);
750
751 pcmk__cli_help('v');
752 }
753
754 data_set = pe_new_working_set();
755 if (data_set == NULL) {
756 crm_perror(LOG_CRIT, "Could not allocate working set");
757 exit_code = CRM_EX_OSERR;
758 goto done;
759 }
760 pe__set_working_set_flags(data_set, pe_flag_no_counts|pe_flag_no_compat);
761
762 cib_conn = cib_new();
763 if (cib_conn == NULL) {
764 exit_code = CRM_EX_DISCONNECT;
765 g_set_error(&error, PCMK__EXITC_ERROR, exit_code, "Could not connect to the CIB manager");
766 goto done;
767 }
768
769 rc = cib_conn->cmds->signon(cib_conn, crm_system_name, cib_command);
770 rc = pcmk_legacy2rc(rc);
771
772 if (rc != pcmk_rc_ok) {
773 exit_code = pcmk_rc2exitc(rc);
774 g_set_error(&error, PCMK__EXITC_ERROR, exit_code, "Could not connect to the CIB: %s",
775 pcmk_rc_str(rc));
776 goto done;
777 }
778
779 if (options.xml_file != NULL) {
780 cib_xml_copy = filename2xml(options.xml_file);
781
782 } else {
783 rc = cib_conn->cmds->query(cib_conn, NULL, &cib_xml_copy, cib_scope_local | cib_sync_call);
784 rc = pcmk_legacy2rc(rc);
785
786 if (rc != pcmk_rc_ok) {
787 exit_code = pcmk_rc2exitc(rc);
788 g_set_error(&error, PCMK__EXITC_ERROR, exit_code, "Could not get local CIB: %s",
789 pcmk_rc_str(rc));
790 goto done;
791 }
792 }
793
794 if (!cli_config_update(&cib_xml_copy, NULL, FALSE)) {
795 exit_code = CRM_EX_CONFIG;
796 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
797 "Could not update local CIB to latest schema version");
798 goto done;
799 }
800
801 data_set->input = cib_xml_copy;
802 data_set->now = crm_time_new(NULL);
803
804 cluster_status(data_set);
805
806
807
808 pcmk__unpack_constraints(data_set);
809
810 if (options.ticket_cmd == 'l' || options.ticket_cmd == 'L' || options.ticket_cmd == 'w') {
811 bool raw = false;
812 bool details = false;
813
814 if (options.ticket_cmd == 'L') {
815 details = true;
816 } else if (options.ticket_cmd == 'w') {
817 raw = true;
818 }
819
820 if (options.ticket_id) {
821 pe_ticket_t *ticket = find_ticket(options.ticket_id, data_set);
822
823 if (ticket == NULL) {
824 exit_code = CRM_EX_NOSUCH;
825 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
826 "No such ticket '%s'", options.ticket_id);
827 goto done;
828 }
829 print_ticket(ticket, raw, details);
830
831 } else {
832 print_ticket_list(data_set, raw, details);
833 }
834
835 } else if (options.ticket_cmd == 'q') {
836 rc = dump_ticket_xml(cib_conn, options.ticket_id);
837 exit_code = pcmk_rc2exitc(rc);
838
839 if (rc != pcmk_rc_ok) {
840 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
841 "Could not query ticket XML: %s", pcmk_rc_str(rc));
842 }
843
844 } else if (options.ticket_cmd == 'c') {
845 rc = dump_constraints(cib_conn, options.ticket_id);
846 exit_code = pcmk_rc2exitc(rc);
847
848 if (rc != pcmk_rc_ok) {
849 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
850 "Could not show ticket constraints: %s", pcmk_rc_str(rc));
851 }
852
853 } else if (options.ticket_cmd == 'G') {
854 const char *value = NULL;
855
856 if (options.ticket_id == NULL) {
857 exit_code = CRM_EX_NOSUCH;
858 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
859 "Must supply ticket ID with -t");
860 goto done;
861 }
862
863 rc = get_ticket_state_attr(options.ticket_id, options.get_attr_name, &value, data_set);
864 if (rc == pcmk_rc_ok) {
865 fprintf(stdout, "%s\n", value);
866 } else if (rc == ENXIO && options.attr_default) {
867 fprintf(stdout, "%s\n", options.attr_default);
868 rc = pcmk_rc_ok;
869 }
870 exit_code = pcmk_rc2exitc(rc);
871
872 } else if (options.ticket_cmd == 'C') {
873 if (options.ticket_id == NULL) {
874 exit_code = CRM_EX_USAGE;
875 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
876 "Must supply ticket ID with -t");
877 goto done;
878 }
879
880 if (options.force == FALSE) {
881 pe_ticket_t *ticket = NULL;
882
883 ticket = find_ticket(options.ticket_id, data_set);
884 if (ticket == NULL) {
885 exit_code = CRM_EX_NOSUCH;
886 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
887 "No such ticket '%s'", options.ticket_id);
888 goto done;
889 }
890
891 if (ticket->granted) {
892 ticket_warning(options.ticket_id, "revoke");
893 exit_code = CRM_EX_INSUFFICIENT_PRIV;
894 goto done;
895 }
896 }
897
898 rc = delete_ticket_state(options.ticket_id, cib_conn);
899 exit_code = pcmk_rc2exitc(rc);
900
901 if (rc != pcmk_rc_ok) {
902 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
903 "Could not clean up ticket: %s", pcmk_rc_str(rc));
904 }
905
906 } else if (modified) {
907 if (options.ticket_id == NULL) {
908 exit_code = CRM_EX_USAGE;
909 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
910 "Must supply ticket ID with -t");
911 goto done;
912 }
913
914 if (options.attr_value
915 && (pcmk__str_empty(options.attr_name))) {
916 exit_code = CRM_EX_USAGE;
917 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
918 "Must supply attribute name with -S for -v %s", options.attr_value);
919 goto done;
920 }
921
922 if (options.attr_name
923 && (pcmk__str_empty(options.attr_value))) {
924 exit_code = CRM_EX_USAGE;
925 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
926 "Must supply attribute value with -v for -S %s", options.attr_value);
927 goto done;
928 }
929
930 if (!allow_modification(options.ticket_id)) {
931 exit_code = CRM_EX_INSUFFICIENT_PRIV;
932 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
933 "Ticket modification not allowed");
934 goto done;
935 }
936
937 rc = modify_ticket_state(options.ticket_id, cib_conn, data_set);
938 exit_code = pcmk_rc2exitc(rc);
939
940 if (rc != pcmk_rc_ok) {
941 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
942 "Could not modify ticket: %s", pcmk_rc_str(rc));
943 }
944
945 } else if (options.ticket_cmd == 'S') {
946
947
948
949
950 if (pcmk__str_empty(options.attr_name)) {
951
952 exit_code = CRM_EX_USAGE;
953 g_set_error(&error, PCMK__EXITC_ERROR, exit_code, "Must supply a command");
954 goto done;
955 }
956
957 if (options.ticket_id == NULL) {
958 exit_code = CRM_EX_USAGE;
959 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
960 "Must supply ticket ID with -t");
961 goto done;
962 }
963
964 if (pcmk__str_empty(options.attr_value)) {
965 exit_code = CRM_EX_USAGE;
966 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
967 "Must supply value with -v for -S %s", options.attr_name);
968 goto done;
969 }
970
971 } else {
972 exit_code = CRM_EX_USAGE;
973 g_set_error(&error, PCMK__EXITC_ERROR, exit_code,
974 "Unknown command: %c", options.ticket_cmd);
975 }
976
977 done:
978 if (attr_set) {
979 g_hash_table_destroy(attr_set);
980 }
981 attr_set = NULL;
982
983 if (attr_delete) {
984 g_list_free_full(attr_delete, free);
985 }
986 attr_delete = NULL;
987
988 pe_free_working_set(data_set);
989 data_set = NULL;
990
991 cib__clean_up_connection(&cib_conn);
992
993 g_strfreev(processed_args);
994 pcmk__free_arg_context(context);
995 g_free(options.attr_default);
996 g_free(options.attr_id);
997 free(options.attr_name);
998 free(options.attr_value);
999 free(options.get_attr_name);
1000 g_free(options.set_name);
1001 g_free(options.ticket_id);
1002 g_free(options.xml_file);
1003
1004 pcmk__output_and_clear_error(&error, NULL);
1005
1006 crm_exit(exit_code);
1007 }