Page MenuHomeClusterLabs Projects

No OneTemporary

diff --git a/tools/cibadmin.c b/tools/cibadmin.c
index 6c692624b3..164707f509 100644
--- a/tools/cibadmin.c
+++ b/tools/cibadmin.c
@@ -1,593 +1,594 @@
/*
* 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 <crm/crm.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <crm/msg_xml.h>
#include <crm/common/xml.h>
#include <crm/common/ipc.h>
#include <crm/cib/internal.h>
int exit_code = pcmk_ok;
int message_timer_id = -1;
int message_timeout_ms = 30;
GMainLoop *mainloop = NULL;
const char *host = NULL;
void usage(const char *cmd, int exit_status);
int do_init(void);
int do_work(xmlNode * input, int command_options, xmlNode ** output);
gboolean admin_message_timeout(gpointer data);
void cib_connection_destroy(gpointer user_data);
void cibadmin_op_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *user_data);
int command_options = 0;
const char *cib_action = NULL;
typedef struct str_list_s {
int num_items;
char *value;
struct str_list_s *next;
} str_list_t;
char *obj_type = NULL;
char *status = NULL;
char *migrate_from = NULL;
char *migrate_res = NULL;
char *subtype = NULL;
char *reset = NULL;
int request_id = 0;
int operation_status = 0;
cib_t *the_cib = NULL;
gboolean force_flag = FALSE;
/* *INDENT-OFF* */
static struct crm_option long_options[] = {
{"help", 0, 0, '?', "\tThis text"},
{"version", 0, 0, '$', "\tVersion information" },
{"verbose", 0, 0, 'V', "\tIncrease debug output\n"},
{"-spacer-", 0, 0, '-', "Commands:"},
{"upgrade", 0, 0, 'u', "\tUpgrade the configuration to the latest syntax"},
{"query", 0, 0, 'Q', "\tQuery the contents of the CIB"},
{"erase", 0, 0, 'E', "\tErase the contents of the whole CIB"},
{"bump", 0, 0, 'B', "\tIncrease the CIB's epoch value by 1"},
{"create", 0, 0, 'C', "\tCreate an object in the CIB. Will fail if the object already exists."},
{"modify", 0, 0, 'M', "\tFind the object somewhere in the CIB's XML tree and update it. Fails if the object does not exist unless -c is specified"},
{"patch", 0, 0, 'P', "\tSupply an update in the form of an xml diff (See also: crm_diff)"},
{"replace", 0, 0, 'R', "\tRecursivly replace an object in the CIB"},
{"delete", 0, 0, 'D', "\tDelete the first object matching the supplied criteria, Eg. <op id=\"rsc1_op1\" name=\"monitor\"/>"},
{"-spacer-", 0, 0, '-', "\n\t\t\tThe tagname and all attributes must match in order for the element to be deleted"},
{"delete-all", 0, 0, 'd', "\tWhen used with --xpath, remove all matching objects in the configuration instead of just the first one"},
{"md5-sum", 0, 0, '5', "\tCalculate the on-disk CIB digest"},
{"md5-sum-versioned", 0, 0, '6', "\tCalculate an on-the-wire versioned CIB digest"},
{"sync", 0, 0, 'S', "\t(Advanced) Force a refresh of the CIB to all nodes\n"},
{"make-slave", 0, 0, 'r', NULL, 1},
{"make-master", 0, 0, 'w', NULL, 1},
{"is-master", 0, 0, 'm', NULL, 1},
{"empty", 0, 0, 'a', "\tOutput an empty CIB"},
{"blank", 0, 0, 'a', NULL, 1},
{"-spacer-",1, 0, '-', "\nAdditional options:"},
{"force", 0, 0, 'f'},
{"timeout", 1, 0, 't', "Time (in seconds) to wait before declaring the operation failed"},
{"sync-call", 0, 0, 's', "Wait for call to complete before returning"},
{"local", 0, 0, 'l', "\tCommand takes effect locally. Should only be used for queries"},
{"allow-create",0, 0, 'c', "(Advanced) Allow the target of a -M operation to be created if they do not exist"},
{"no-children", 0, 0, 'n', "(Advanced) When querying an object, do not return include its children in the result\n"},
{"no-bcast", 0, 0, 'b', NULL, 1},
{"-spacer-", 0, 0, '-', "Data:"},
{"xml-text", 1, 0, 'X', "Retrieve XML from the supplied string"},
{"xml-file", 1, 0, 'x', "Retrieve XML from the named file"},
{"xml-pipe", 0, 0, 'p', "Retrieve XML from stdin\n"},
{"xpath", 1, 0, 'A', "A valid XPath to use instead of -o"},
{"scope", 1, 0, 'o', "Limit the scope of the operation to a specific section of the CIB."},
{"-spacer-", 0, 0, '-', "\t\t\tValid values are: nodes, resources, constraints, crm_config, rsc_defaults, op_defaults, status"},
{"node", 1, 0, 'N', "(Advanced) Send command to the specified host\n"},
{"-space-", 0, 0, '!', NULL, 1},
{"-spacer-", 0, 0, '-', "\nExamples:\n"},
{"-spacer-", 0, 0, '-', "Query the configuration from the local node:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --query --local", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Query the just the cluster options configuration:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --query --scope crm_config", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Query all 'target-role' settings:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --query --xpath \"//nvpair[@name='target-role']\"", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Remove all 'is-managed' settings:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --delete-all --xpath \"//nvpair[@name='is-managed']\"", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Remove the resource named 'old':", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --delete --xml-text '<primitive id=\"old\"/>'", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Remove all resources from the configuration:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --replace --scope resources --xml-text '<resources/>'", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Replace the complete configuration with the contents of $HOME/pacemaker.xml:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --replace --xml-file $HOME/pacemaker.xml", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Replace the constraints section of the configuration with the contents of $HOME/constraints.xml:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --replace --scope constraints --xml-file $HOME/constraints.xml", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Increase the configuration version to prevent old configurations from being loaded accidentally:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --modify --xml-text '<cib admin_epoch=\"admin_epoch++\"/>'", pcmk_option_example},
{"-spacer-", 0, 0, '-', "Edit the configuration with your favorite $EDITOR:", pcmk_option_paragraph},
{"-spacer-", 0, 0, '-', " cibadmin --query > $HOME/local.xml", pcmk_option_example},
{"-spacer-", 0, 0, '-', " $EDITOR $HOME/local.xml", pcmk_option_example},
{"-spacer-", 0, 0, '-', " cibadmin --replace --xml-file $HOME/local.xml", pcmk_option_example},
{"-spacer-", 0, 0, '-', "SEE ALSO:"},
{"-spacer-", 0, 0, '-', " CRM shell, crm(8), crm_shadow(8)"},
/* Legacy options */
{"host", 0, 0, 'h', NULL, 1},
{"force-quorum", 0, 0, 'f', NULL, 1},
{"obj_type", 1, 0, 'o', NULL, 1},
{F_CRM_DATA, 1, 0, 'X', NULL, 1},
{CIB_OP_ERASE, 0, 0, 'E', NULL, 1},
{CIB_OP_QUERY, 0, 0, 'Q', NULL, 1},
{CIB_OP_CREATE, 0, 0, 'C', NULL, 1},
{CIB_OP_REPLACE, 0, 0, 'R', NULL, 1},
{CIB_OP_UPDATE, 0, 0, 'U', NULL, 1},
{CIB_OP_MODIFY, 0, 0, 'M', NULL, 1},
{CIB_OP_DELETE, 0, 0, 'D', NULL, 1},
{CIB_OP_BUMP, 0, 0, 'B', NULL, 1},
{CIB_OP_SYNC, 0, 0, 'S', NULL, 1},
{CIB_OP_SLAVE, 0, 0, 'r', NULL, 1},
{CIB_OP_MASTER, 0, 0, 'w', NULL, 1},
{CIB_OP_ISMASTER,0, 0, 'm', NULL, 1},
{0, 0, 0, 0}
};
/* *INDENT-ON* */
int
main(int argc, char **argv)
{
int argerr = 0;
int flag;
const char *source = NULL;
char *admin_input_xml = NULL;
char *admin_input_file = NULL;
gboolean dangerous_cmd = FALSE;
gboolean admin_input_stdin = FALSE;
xmlNode *output = NULL;
xmlNode *input = NULL;
int option_index = 0;
crm_log_init(NULL, LOG_CRIT, FALSE, FALSE, argc, argv, FALSE);
crm_set_options(NULL, "command [options] [data]", long_options,
"Provides direct access to the cluster configuration."
"\n\nAllows the configuration, or sections of it, to be queried, modified, replaced and deleted."
"\n\nWhere necessary, XML data will be obtained using the -X, -x, or -p options\n");
if (argc < 2) {
crm_help('?', EX_USAGE);
}
while (1) {
flag = crm_get_option(argc, argv, &option_index);
if (flag == -1)
break;
switch (flag) {
case 't':
message_timeout_ms = atoi(optarg);
if (message_timeout_ms < 1) {
message_timeout_ms = 30;
}
break;
case 'A':
obj_type = strdup(optarg);
command_options |= cib_xpath;
break;
case 'u':
cib_action = CIB_OP_UPGRADE;
dangerous_cmd = TRUE;
break;
case 'E':
cib_action = CIB_OP_ERASE;
dangerous_cmd = TRUE;
break;
case 'Q':
cib_action = CIB_OP_QUERY;
break;
case 'P':
cib_action = CIB_OP_APPLY_DIFF;
break;
case 'S':
cib_action = CIB_OP_SYNC;
break;
case 'U':
case 'M':
cib_action = CIB_OP_MODIFY;
break;
case 'R':
cib_action = CIB_OP_REPLACE;
break;
case 'C':
cib_action = CIB_OP_CREATE;
break;
case 'D':
cib_action = CIB_OP_DELETE;
break;
case '5':
cib_action = "md5-sum";
break;
case '6':
cib_action = "md5-sum-versioned";
break;
case 'c':
command_options |= cib_can_create;
break;
case 'n':
command_options |= cib_no_children;
break;
case 'm':
cib_action = CIB_OP_ISMASTER;
command_options |= cib_scope_local;
break;
case 'B':
cib_action = CIB_OP_BUMP;
break;
case 'r':
dangerous_cmd = TRUE;
cib_action = CIB_OP_SLAVE;
break;
case 'w':
dangerous_cmd = TRUE;
cib_action = CIB_OP_MASTER;
command_options |= cib_scope_local;
break;
case 'V':
command_options = command_options | cib_verbose;
crm_bump_log_level(argc, argv);
break;
case '?':
case '$':
case '!':
crm_help(flag, EX_OK);
break;
case 'o':
crm_trace("Option %c => %s", flag, optarg);
obj_type = strdup(optarg);
break;
case 'X':
crm_trace("Option %c => %s", flag, optarg);
admin_input_xml = strdup(optarg);
break;
case 'x':
crm_trace("Option %c => %s", flag, optarg);
admin_input_file = strdup(optarg);
break;
case 'p':
admin_input_stdin = TRUE;
break;
case 'h':
host = strdup(optarg);
break;
case 'l':
command_options |= cib_scope_local;
break;
case 'd':
cib_action = CIB_OP_DELETE;
command_options |= cib_multiple;
dangerous_cmd = TRUE;
break;
case 'b':
dangerous_cmd = TRUE;
command_options |= cib_inhibit_bcast;
command_options |= cib_scope_local;
break;
case 's':
command_options |= cib_sync_call;
break;
case 'f':
force_flag = TRUE;
command_options |= cib_quorum_override;
break;
case 'a':
output = createEmptyCib();
crm_xml_add(output, XML_ATTR_CRM_VERSION, CRM_FEATURE_SET);
if (optind >= argc) {
crm_xml_add(output, XML_ATTR_VALIDATION, LATEST_SCHEMA_VERSION);
} else {
crm_xml_add(output, XML_ATTR_VALIDATION, argv[optind]);
}
crm_xml_add_int(output, XML_ATTR_GENERATION_ADMIN, 1);
crm_xml_add_int(output, XML_ATTR_GENERATION, 0);
crm_xml_add_int(output, XML_ATTR_NUMUPDATES, 0);
admin_input_xml = dump_xml_formatted(output);
fprintf(stdout, "%s\n", crm_str(admin_input_xml));
exit(0);
break;
default:
printf("Argument code 0%o (%c)" " is not (?yet?) supported\n", flag, flag);
++argerr;
break;
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
crm_help('?', EX_USAGE);
}
if (optind > argc || cib_action == NULL) {
++argerr;
}
if (argerr) {
crm_help('?', EX_USAGE);
}
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);
exit(1);
}
if (admin_input_file != NULL) {
input = filename2xml(admin_input_file);
source = admin_input_file;
} else if (admin_input_xml != NULL) {
source = "input string";
input = string2xml(admin_input_xml);
} else if (admin_input_stdin) {
source = "STDIN";
input = stdin2xml();
}
if (input != NULL) {
crm_log_xml_debug(input, "[admin input]");
} else if (source) {
fprintf(stderr, "Couldn't parse input from %s.\n", source);
return 1;
}
if (safe_str_eq(cib_action, "md5-sum")) {
char *digest = NULL;
if (input == NULL) {
fprintf(stderr, "Please supply XML to process with -X, -x or -p\n");
exit(1);
}
digest = calculate_on_disk_digest(input);
fprintf(stderr, "Digest: ");
fprintf(stdout, "%s\n", crm_str(digest));
free(digest);
exit(0);
} else if (safe_str_eq(cib_action, "md5-sum-versioned")) {
char *digest = NULL;
const char *version = NULL;
if (input == NULL) {
fprintf(stderr, "Please supply XML to process with -X, -x or -p\n");
exit(1);
}
version = crm_element_value(input, XML_ATTR_CRM_VERSION);
digest = calculate_xml_versioned_digest(input, FALSE, TRUE, version);
fprintf(stderr, "Versioned (%s) digest: ", version);
fprintf(stdout, "%s\n", crm_str(digest));
free(digest);
exit(0);
}
exit_code = do_init();
if (exit_code != pcmk_ok) {
crm_err("Init failed, could not perform requested operations");
fprintf(stderr, "Init failed, could not perform requested operations\n");
return -exit_code;
}
exit_code = do_work(input, command_options, &output);
if (exit_code > 0) {
/* wait for the reply by creating a mainloop and running it until
* the callbacks are invoked...
*/
request_id = exit_code;
the_cib->cmds->register_callback(the_cib, request_id, message_timeout_ms, FALSE, NULL,
"cibadmin_op_callback", cibadmin_op_callback);
mainloop = g_main_new(FALSE);
crm_trace("%s waiting for reply from the local CIB", crm_system_name);
crm_info("Starting mainloop");
g_main_run(mainloop);
} else if (exit_code < 0) {
crm_err("Call failed: %s", pcmk_strerror(exit_code));
fprintf(stderr, "Call failed: %s\n", pcmk_strerror(exit_code));
operation_status = exit_code;
if (exit_code == -pcmk_err_dtd_validation) {
if (crm_str_eq(cib_action, CIB_OP_UPGRADE, TRUE)) {
xmlNode *obj = NULL;
int version = 0, rc = 0;
rc = the_cib->cmds->query(the_cib, NULL, &obj, command_options);
if (rc == pcmk_ok) {
update_validation(&obj, &version, TRUE, FALSE);
}
} else if (output) {
validate_xml_verbose(output);
}
}
}
if (output != NULL) {
char *buffer = dump_xml_formatted(output);
fprintf(stdout, "%s\n", crm_str(buffer));
free(buffer);
free_xml(output);
}
crm_trace("%s exiting normally", crm_system_name);
free_xml(input);
free(admin_input_xml);
free(admin_input_file);
the_cib->cmds->signoff(the_cib);
cib_delete(the_cib);
crm_xml_cleanup();
+ qb_log_fini();
return -exit_code;
}
int
do_work(xmlNode * input, int call_options, xmlNode ** output)
{
/* construct the request */
the_cib->call_timeout = message_timeout_ms;
if (strcasecmp(CIB_OP_REPLACE, cib_action) == 0
&& safe_str_eq(crm_element_name(input), XML_TAG_CIB)) {
xmlNode *status = get_object_root(XML_CIB_TAG_STATUS, input);
if (status == NULL) {
create_xml_node(input, XML_CIB_TAG_STATUS);
}
}
if (strcasecmp(CIB_OP_SYNC, cib_action) == 0) {
crm_trace("Performing %s op...", cib_action);
return the_cib->cmds->sync_from(the_cib, host, obj_type, call_options);
} else if (strcasecmp(CIB_OP_SLAVE, cib_action) == 0 && (call_options ^ cib_scope_local)) {
crm_trace("Performing %s op on all nodes...", cib_action);
return the_cib->cmds->set_slave_all(the_cib, call_options);
} else if (strcasecmp(CIB_OP_MASTER, cib_action) == 0) {
crm_trace("Performing %s op on all nodes...", cib_action);
return the_cib->cmds->set_master(the_cib, call_options);
} else if (cib_action != NULL) {
crm_trace("Passing \"%s\" to variant_op...", cib_action);
return cib_internal_op(the_cib, cib_action, host, obj_type, input, output, call_options, NULL);
} else {
crm_err("You must specify an operation");
}
return -EINVAL;
}
int
do_init(void)
{
int rc = pcmk_ok;
the_cib = cib_new();
rc = the_cib->cmds->signon(the_cib, crm_system_name, cib_command);
if (rc != pcmk_ok) {
crm_err("Signon to CIB failed: %s", pcmk_strerror(rc));
fprintf(stderr, "Signon to CIB failed: %s\n", pcmk_strerror(rc));
}
return rc;
}
void
cib_connection_destroy(gpointer user_data)
{
crm_err("Connection to the CIB terminated... exiting");
g_main_quit(mainloop);
return;
}
void
cibadmin_op_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *user_data)
{
char *admin_input_xml = NULL;
exit_code = rc;
if (output != NULL) {
admin_input_xml = dump_xml_formatted(output);
}
if (safe_str_eq(cib_action, CIB_OP_ISMASTER) && rc != pcmk_ok) {
crm_info("CIB on %s is _not_ the master instance", host ? host : "localhost");
fprintf(stderr, "CIB on %s is _not_ the master instance\n", host ? host : "localhost");
} else if (safe_str_eq(cib_action, CIB_OP_ISMASTER)) {
crm_info("CIB on %s _is_ the master instance", host ? host : "localhost");
fprintf(stderr, "CIB on %s _is_ the master instance\n", host ? host : "localhost");
} else if (rc != 0) {
crm_warn("Call %s failed (%d): %s", cib_action, rc, pcmk_strerror(rc));
fprintf(stderr, "Call %s failed (%d): %s\n", cib_action, rc, pcmk_strerror(rc));
fprintf(stdout, "%s\n", crm_str(admin_input_xml));
} else if (safe_str_eq(cib_action, CIB_OP_QUERY) && output == NULL) {
crm_err("Output expected in query response");
crm_log_xml_err(msg, "no output");
} else if (output == NULL) {
crm_info("Call passed");
} else {
crm_info("Call passed");
fprintf(stdout, "%s\n", crm_str(admin_input_xml));
}
free(admin_input_xml);
if (call_id == request_id) {
g_main_quit(mainloop);
} else {
crm_info("Message was not the response we were looking for (%d vs. %d", call_id,
request_id);
}
}
diff --git a/tools/crm_attribute.c b/tools/crm_attribute.c
index 37a62e5fbd..658fe0b00f 100644
--- a/tools/crm_attribute.c
+++ b/tools/crm_attribute.c
@@ -1,311 +1,312 @@
/*
* 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 <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <time.h>
#include <sys/param.h>
#include <sys/types.h>
#include <crm/crm.h>
#include <crm/msg_xml.h>
#include <crm/common/xml.h>
#include <crm/common/ipc.h>
#include <crm/common/util.h>
#include <crm/cib.h>
#include <crm/attrd.h>
#include <sys/utsname.h>
gboolean BE_QUIET = FALSE;
char command = 'G';
char *dest_uname = NULL;
char *dest_node = NULL;
char *set_name = NULL;
char *attr_id = NULL;
char *attr_name = NULL;
const char *type = NULL;
const char *rsc_id = NULL;
const char *attr_value = NULL;
const char *attr_default = NULL;
const char *set_type = NULL;
/* *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', "\tPrint only the value on stdout\n"},
{"name", 1, 0, 'n', "Name of the attribute/option to operate on"},
{"-spacer-", 0, 0, '-', "\nCommands:"},
{"query", 0, 0, 'G', "\tQuery the current value of the attribute/option"},
{"update", 1, 0, 'v', "Update the value of the attribute/option"},
{"delete", 0, 0, 'D', "\tDelete the attribute/option"},
{"-spacer-", 0, 0, '-', "\nAdditional Options:"},
{"node", 1, 0, 'N', "Set an attribute for the named node (instead of a cluster option). See also: -l"},
{"type", 1, 0, 't', "Which part of the configuration to update/delete/query the option in."},
{"-spacer-", 0, 0, '-', "\t\t\tValid values: crm_config, rsc_defaults, op_defaults, tickets"},
{"lifetime", 1, 0, 'l', "Lifetime of the node attribute."},
{"-spacer-", 0, 0, '-', "\t\t\tValid values: reboot, forever"},
{"utilization", 0, 0, 'z', "Set an utilization attribute for the node."},
{"set-name", 1, 0, 's', "(Advanced) The attribute set in which to place the value"},
{"id", 1, 0, 'i', "\t(Advanced) The ID used to identify the attribute"},
{"default", 1, 0, 'd', "(Advanced) The default value to display if none is found in the configuration"},
{"inhibit-policy-engine", 0, 0, '!', NULL, 1},
/* legacy */
{"quiet", 0, 0, 'Q', NULL, 1},
{"node-uname", 1, 0, 'U', NULL, 1},
{"node-uuid", 1, 0, 'u', NULL, 1},
{"get-value", 0, 0, 'G', NULL, 1},
{"delete-attr", 0, 0, 'D', NULL, 1},
{"attr-value", 1, 0, 'v', NULL, 1},
{"attr-name", 1, 0, 'n', NULL, 1},
{"attr-id", 1, 0, 'i', NULL, 1},
{"-spacer-", 1, 0, '-', "\nExamples:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "Add a new attribute called 'location' with the value of 'office' for host 'myhost':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_attribute --node myhost --name location --update office", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Query the value of the 'location' node attribute for host myhost:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_attribute --node myhost --name location --query", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Change the value of the 'location' node attribute for host myhost:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_attribute --node myhost --name location --update backoffice", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Delete the 'location' node attribute for the host myhost:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_attribute --node myhost --name location --delete", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Query the value of the cluster-delay cluster option:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_attribute --type crm_config --name cluster-delay --query", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Query the value of the cluster-delay cluster option. Only print the value:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_attribute --type crm_config --name cluster-delay --query --quiet", pcmk_option_example},
{0, 0, 0, 0}
};
/* *INDENT-ON* */
int
main(int argc, char **argv)
{
cib_t *the_cib = NULL;
int rc = pcmk_ok;
int cib_opts = cib_sync_call;
int argerr = 0;
int flag;
int option_index = 0;
crm_log_cli_init("crm_attribute");
crm_set_options(NULL, "command -n attribute [options]", long_options,
"Manage node's attributes and cluster options."
"\n\nAllows node attributes and cluster options to be queried, modified and deleted.\n");
if (argc < 2) {
crm_help('?', EX_USAGE);
}
while (1) {
flag = crm_get_option(argc, argv, &option_index);
if (flag == -1)
break;
switch (flag) {
case 'V':
crm_bump_log_level(argc, argv);
break;
case '$':
case '?':
crm_help(flag, EX_OK);
break;
case 'D':
case 'G':
case 'v':
command = flag;
attr_value = optarg;
break;
case 'q':
case 'Q':
BE_QUIET = TRUE;
break;
case 'U':
case 'N':
dest_uname = strdup(optarg);
break;
case 'u':
dest_node = strdup(optarg);
break;
case 's':
set_name = strdup(optarg);
break;
case 'l':
case 't':
type = optarg;
break;
case 'z':
type = XML_CIB_TAG_NODES;
set_type = XML_TAG_UTILIZATION;
break;
case 'n':
attr_name = strdup(optarg);
break;
case 'i':
attr_id = strdup(optarg);
break;
case 'r':
rsc_id = optarg;
break;
case 'd':
attr_default = optarg;
break;
case '!':
crm_warn("Inhibiting notifications for this update");
cib_opts |= cib_inhibit_notify;
break;
default:
printf("Argument code 0%o (%c) is not (?yet?) supported\n", flag, flag);
++argerr;
break;
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
if (optind > argc) {
++argerr;
}
if (argerr) {
crm_help('?', EX_USAGE);
}
the_cib = cib_new();
rc = the_cib->cmds->signon(the_cib, crm_system_name, cib_command);
if (rc != pcmk_ok) {
fprintf(stderr, "Error signing on to the CIB service: %s\n", pcmk_strerror(rc));
return rc;
}
if (safe_str_eq(type, "reboot")) {
type = XML_CIB_TAG_STATUS;
} else if (safe_str_eq(type, "forever")) {
type = XML_CIB_TAG_NODES;
}
if (type == NULL && dest_uname == NULL) {
/* we're updating cluster options - dont populate dest_node */
type = XML_CIB_TAG_CRMCONFIG;
} else if (safe_str_neq(type, XML_CIB_TAG_TICKETS)) {
determine_host(the_cib, &dest_uname, &dest_node);
}
if ((command == 'v' || command == 'D')
&& safe_str_eq(type, XML_CIB_TAG_STATUS)
&& attrd_update_delegate(NULL, command, dest_uname, attr_name, attr_value, type, set_name, NULL, NULL)) {
crm_info("Update %s=%s sent via attrd", attr_name, command == 'D' ? "<none>" : attr_value);
} else if (command == 'D') {
rc = delete_attr_delegate(the_cib, cib_opts, type, dest_node, set_type, set_name,
attr_id, attr_name, attr_value, TRUE, NULL);
if (rc == -ENXIO) {
/* Nothing to delete...
* which means its not there...
* which is what the admin wanted
*/
rc = pcmk_ok;
} else if (rc != -EINVAL && safe_str_eq(crm_system_name, "crm_failcount")) {
char *now_s = NULL;
time_t now = time(NULL);
now_s = crm_itoa(now);
update_attr_delegate(
the_cib, cib_sync_call, XML_CIB_TAG_CRMCONFIG, NULL, NULL, NULL, NULL,
"last-lrm-refresh", now_s, TRUE, NULL);
free(now_s);
}
} else if (command == 'v') {
CRM_LOG_ASSERT(type != NULL);
CRM_LOG_ASSERT(attr_name != NULL);
CRM_LOG_ASSERT(attr_value != NULL);
rc = update_attr_delegate(
the_cib, cib_opts, type, dest_node, set_type, set_name,
attr_id, attr_name, attr_value, TRUE, NULL);
} else { /* query */
char *read_value = NULL;
rc = read_attr_delegate(the_cib, type, dest_node, set_type, set_name,
attr_id, attr_name, &read_value, TRUE, NULL);
if (rc == -ENXIO && attr_default) {
read_value = strdup(attr_default);
rc = pcmk_ok;
}
crm_info("Read %s=%s %s%s",
attr_name, crm_str(read_value), set_name ? "in " : "", set_name ? set_name : "");
if (rc == -EINVAL) {
rc = pcmk_ok;
} else if (BE_QUIET == FALSE) {
fprintf(stdout, "%s%s %s%s %s%s value=%s\n",
type ? "scope=" : "", type ? type : "",
attr_id ? "id=" : "", attr_id ? attr_id : "",
attr_name ? "name=" : "", attr_name ? attr_name : "",
read_value ? read_value : "(null)");
} else if (read_value != NULL) {
fprintf(stdout, "%s\n", read_value);
}
}
if (rc == -EINVAL) {
printf("Please choose from one of the matches above and suppy the 'id' with --attr-id\n");
} else if (rc != pcmk_ok) {
fprintf(stderr, "Error performing operation: %s\n", pcmk_strerror(rc));
}
the_cib->cmds->signoff(the_cib);
cib_delete(the_cib);
crm_xml_cleanup();
+ qb_log_fini();
return rc;
}
diff --git a/tools/crm_resource.c b/tools/crm_resource.c
index c6e6f7d8fb..edad8bbb7c 100644
--- a/tools/crm_resource.c
+++ b/tools/crm_resource.c
@@ -1,1920 +1,1924 @@
/*
* 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 <crm/crm.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <crm/msg_xml.h>
#include <crm/services.h>
#include <crm/common/xml.h>
#include <crm/common/mainloop.h>
#include <crm/cib.h>
#include <crm/attrd.h>
#include <crm/pengine/rules.h>
#include <crm/pengine/status.h>
gboolean do_force = FALSE;
gboolean BE_QUIET = FALSE;
const char *attr_set_type = XML_TAG_ATTR_SETS;
char *host_id = NULL;
const char *rsc_id = NULL;
const char *host_uname = NULL;
const char *prop_name = NULL;
const char *prop_value = NULL;
const char *rsc_type = NULL;
const char *prop_id = NULL;
const char *prop_set = NULL;
char *move_lifetime = NULL;
char rsc_cmd = 'L';
const char *rsc_long_cmd = NULL;
char *our_pid = NULL;
crm_ipc_t *crmd_channel = NULL;
char *xml_file = NULL;
int cib_options = cib_sync_call;
int crmd_replies_needed = 0;
GMainLoop *mainloop = NULL;
extern void cleanup_alloc_calculations(pe_working_set_t * data_set);
#define CMD_ERR(fmt, args...) do { \
crm_warn(fmt, ##args); \
fprintf(stderr, fmt, ##args); \
} while(0)
#define message_timeout_ms 60*1000
static gboolean
resource_ipc_timeout(gpointer data)
{
fprintf(stderr, "No messages received in %d seconds.. aborting\n",
(int)message_timeout_ms / 1000);
crm_err("No messages received in %d seconds", (int)message_timeout_ms / 1000);
+ qb_log_fini();
exit(-1);
}
static void
resource_ipc_connection_destroy(gpointer user_data)
{
crm_info("Connection to CRMd was terminated");
+ qb_log_fini();
exit(1);
}
static void
start_mainloop(void)
{
mainloop = g_main_new(FALSE);
crmd_replies_needed++; /* The welcome message */
fprintf(stderr, "Waiting for %d replies from the CRMd", crmd_replies_needed);
crm_debug("Waiting for %d replies from the CRMd", crmd_replies_needed);
g_timeout_add(message_timeout_ms, resource_ipc_timeout, NULL);
g_main_run(mainloop);
}
static int
resource_ipc_callback(const char *buffer, ssize_t length, gpointer userdata)
{
xmlNode *msg = string2xml(buffer);
fprintf(stderr, ".");
crm_log_xml_trace(msg, "[inbound]");
crmd_replies_needed--;
if (crmd_replies_needed == 0) {
fprintf(stderr, " OK\n");
crm_debug("Got all the replies we expected");
crm_xml_cleanup();
+ qb_log_fini();
exit(0);
}
free_xml(msg);
return 0;
}
struct ipc_client_callbacks crm_callbacks =
{
.dispatch = resource_ipc_callback,
.destroy = resource_ipc_connection_destroy,
};
static int
do_find_resource(const char *rsc, resource_t * the_rsc, pe_working_set_t * data_set)
{
int found = 0;
GListPtr lpc = NULL;
if (the_rsc == NULL) {
the_rsc = pe_find_resource(data_set->resources, rsc);
}
if (the_rsc == NULL) {
return -ENXIO;
}
if (the_rsc->variant > pe_clone) {
GListPtr gIter = the_rsc->children;
for (; gIter != NULL; gIter = gIter->next) {
found += do_find_resource(rsc, gIter->data, data_set);
}
return found;
}
for (lpc = the_rsc->running_on; lpc != NULL; lpc = lpc->next) {
node_t *node = (node_t *) lpc->data;
crm_trace("resource %s is running on: %s", rsc, node->details->uname);
if (BE_QUIET) {
fprintf(stdout, "%s\n", node->details->uname);
} else {
const char *state = "";
if (the_rsc->variant == pe_native && the_rsc->role == RSC_ROLE_MASTER) {
state = "Master";
}
fprintf(stdout, "resource %s is running on: %s %s\n", rsc, node->details->uname, state);
}
found++;
}
if (BE_QUIET == FALSE && found == 0) {
fprintf(stderr, "resource %s is NOT running\n", rsc);
}
return 0;
}
#define cons_string(x) x?x:"NA"
static void
print_cts_constraints(pe_working_set_t * data_set)
{
xmlNode *xml_obj = NULL;
xmlNode *lifetime = NULL;
xmlNode *cib_constraints = get_object_root(XML_CIB_TAG_CONSTRAINTS, data_set->input);
for (xml_obj = __xml_first_child(cib_constraints); xml_obj != NULL;
xml_obj = __xml_next(xml_obj)) {
const char *id = crm_element_value(xml_obj, XML_ATTR_ID);
if (id == NULL) {
continue;
}
lifetime = first_named_child(xml_obj, "lifetime");
if (test_ruleset(lifetime, NULL, data_set->now) == FALSE) {
continue;
}
if (safe_str_eq(XML_CONS_TAG_RSC_DEPEND, crm_element_name(xml_obj))) {
printf("Constraint %s %s %s %s %s %s %s\n",
crm_element_name(xml_obj),
cons_string(crm_element_value(xml_obj, XML_ATTR_ID)),
cons_string(crm_element_value(xml_obj, XML_COLOC_ATTR_SOURCE)),
cons_string(crm_element_value(xml_obj, XML_COLOC_ATTR_TARGET)),
cons_string(crm_element_value(xml_obj, XML_RULE_ATTR_SCORE)),
cons_string(crm_element_value(xml_obj, XML_COLOC_ATTR_SOURCE_ROLE)),
cons_string(crm_element_value(xml_obj, XML_COLOC_ATTR_TARGET_ROLE)));
} else if (safe_str_eq(XML_CONS_TAG_RSC_LOCATION, crm_element_name(xml_obj))) {
/* unpack_rsc_location(xml_obj, data_set); */
}
}
}
static void
print_cts_rsc(resource_t * rsc)
{
GListPtr lpc = NULL;
const char *host = NULL;
gboolean needs_quorum = TRUE;
const char *rtype = crm_element_value(rsc->xml, XML_ATTR_TYPE);
const char *rprov = crm_element_value(rsc->xml, XML_AGENT_ATTR_PROVIDER);
const char *rclass = crm_element_value(rsc->xml, XML_AGENT_ATTR_CLASS);
if (safe_str_eq(rclass, "stonith")) {
xmlNode *op = NULL;
needs_quorum = FALSE;
for (op = __xml_first_child(rsc->ops_xml); op != NULL; op = __xml_next(op)) {
if (crm_str_eq((const char *)op->name, "op", TRUE)) {
const char *name = crm_element_value(op, "name");
if (safe_str_neq(name, CRMD_ACTION_START)) {
const char *value = crm_element_value(op, "requires");
if (safe_str_eq(value, "nothing")) {
needs_quorum = FALSE;
}
break;
}
}
}
}
if (rsc->running_on != NULL && g_list_length(rsc->running_on) == 1) {
node_t *tmp = rsc->running_on->data;
host = tmp->details->uname;
}
printf("Resource: %s %s %s %s %s %s %s %s %d %lld 0x%.16llx\n",
crm_element_name(rsc->xml), rsc->id,
rsc->clone_name ? rsc->clone_name : rsc->id, rsc->parent ? rsc->parent->id : "NA",
rprov ? rprov : "NA", rclass, rtype, host ? host : "NA", needs_quorum, rsc->flags,
rsc->flags);
for (lpc = rsc->children; lpc != NULL; lpc = lpc->next) {
resource_t *child = (resource_t *) lpc->data;
print_cts_rsc(child);
}
}
static void
print_raw_rsc(resource_t * rsc)
{
GListPtr lpc = NULL;
GListPtr children = rsc->children;
if (children == NULL) {
printf("%s\n", rsc->id);
}
for (lpc = children; lpc != NULL; lpc = lpc->next) {
resource_t *child = (resource_t *) lpc->data;
print_raw_rsc(child);
}
}
static int
do_find_resource_list(pe_working_set_t * data_set, gboolean raw)
{
int found = 0;
GListPtr lpc = NULL;
for (lpc = data_set->resources; lpc != NULL; lpc = lpc->next) {
resource_t *rsc = (resource_t *) lpc->data;
if (is_set(rsc->flags, pe_rsc_orphan)
&& rsc->fns->active(rsc, TRUE) == FALSE) {
continue;
}
rsc->fns->print(rsc, NULL, pe_print_printf | pe_print_rsconly, stdout);
found++;
}
if (found == 0) {
printf("NO resources configured\n");
return -ENXIO;
}
return 0;
}
static resource_t *
find_rsc_or_clone(const char *rsc, pe_working_set_t * data_set)
{
resource_t *the_rsc = pe_find_resource(data_set->resources, rsc);
if (the_rsc == NULL) {
char *as_clone = crm_concat(rsc, "0", ':');
the_rsc = pe_find_resource(data_set->resources, as_clone);
free(as_clone);
}
return the_rsc;
}
static int
dump_resource(const char *rsc, pe_working_set_t * data_set, gboolean expanded)
{
char *rsc_xml = NULL;
resource_t *the_rsc = find_rsc_or_clone(rsc, data_set);
if (the_rsc == NULL) {
return -ENXIO;
}
the_rsc->fns->print(the_rsc, NULL, pe_print_printf, stdout);
if (expanded) {
rsc_xml = dump_xml_formatted(the_rsc->xml);
} else {
if (the_rsc->orig_xml) {
rsc_xml = dump_xml_formatted(the_rsc->orig_xml);
} else {
rsc_xml = dump_xml_formatted(the_rsc->xml);
}
}
fprintf(stdout, "%sxml:\n%s\n", expanded ? "" : "raw ", rsc_xml);
free(rsc_xml);
return 0;
}
static int
dump_resource_attr(const char *rsc, const char *attr, pe_working_set_t * data_set)
{
int rc = -ENXIO;
node_t *current = NULL;
GHashTable *params = NULL;
resource_t *the_rsc = find_rsc_or_clone(rsc, data_set);
const char *value = NULL;
if (the_rsc == NULL) {
return -ENXIO;
}
if (g_list_length(the_rsc->running_on) == 1) {
current = the_rsc->running_on->data;
} else if (g_list_length(the_rsc->running_on) > 1) {
CMD_ERR("%s is active on more than one node,"
" returning the default value for %s\n", the_rsc->id, crm_str(value));
}
params = g_hash_table_new_full(crm_str_hash, g_str_equal,
g_hash_destroy_str, g_hash_destroy_str);
if (safe_str_eq(attr_set_type, XML_TAG_ATTR_SETS)) {
get_rsc_attributes(params, the_rsc, current, data_set);
} else if (safe_str_eq(attr_set_type, XML_TAG_META_SETS)) {
get_meta_attributes(params, the_rsc, current, data_set);
} else {
unpack_instance_attributes(data_set->input, the_rsc->xml, XML_TAG_UTILIZATION, NULL,
params, NULL, FALSE, data_set->now);
}
crm_debug("Looking up %s in %s", attr, the_rsc->id);
value = g_hash_table_lookup(params, attr);
if (value != NULL) {
fprintf(stdout, "%s\n", value);
rc = 0;
}
g_hash_table_destroy(params);
return rc;
}
static int
find_resource_attr(cib_t * the_cib, const char *attr, const char *rsc, const char *set_type,
const char *set_name, const char *attr_id, const char *attr_name, char **value)
{
int offset = 0;
static int xpath_max = 1024;
int rc = pcmk_ok;
xmlNode *xml_search = NULL;
char *xpath_string = NULL;
CRM_ASSERT(value != NULL);
*value = NULL;
xpath_string = calloc(1, xpath_max);
offset +=
snprintf(xpath_string + offset, xpath_max - offset, "%s", get_object_path("resources"));
offset += snprintf(xpath_string + offset, xpath_max - offset, "//*[@id=\"%s\"]", rsc);
if (set_type) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "/%s", set_type);
if (set_name) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "[@id=\"%s\"]", set_name);
}
}
offset += snprintf(xpath_string + offset, xpath_max - offset, "//nvpair[");
if (attr_id) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "@id=\"%s\"", attr_id);
}
if (attr_name) {
if (attr_id) {
offset += snprintf(xpath_string + offset, xpath_max - offset, " and ");
}
offset += snprintf(xpath_string + offset, xpath_max - offset, "@name=\"%s\"", attr_name);
}
offset += snprintf(xpath_string + offset, xpath_max - offset, "]");
rc = the_cib->cmds->query(the_cib, xpath_string, &xml_search,
cib_sync_call | cib_scope_local | cib_xpath);
if (rc != pcmk_ok) {
goto bail;
}
crm_log_xml_debug(xml_search, "Match");
if (xml_has_children(xml_search)) {
xmlNode *child = NULL;
rc = -EINVAL;
printf("Multiple attributes match name=%s\n", attr_name);
for (child = __xml_first_child(xml_search); child != NULL; child = __xml_next(child)) {
printf(" Value: %s \t(id=%s)\n",
crm_element_value(child, XML_NVPAIR_ATTR_VALUE), ID(child));
}
} else {
const char *tmp = crm_element_value(xml_search, attr);
if (tmp) {
*value = strdup(tmp);
}
}
bail:
free(xpath_string);
free_xml(xml_search);
return rc;
}
static int
set_resource_attr(const char *rsc_id, const char *attr_set, const char *attr_id,
const char *attr_name, const char *attr_value,
cib_t * cib, pe_working_set_t * data_set)
{
int rc = pcmk_ok;
char *local_attr_id = NULL;
char *local_attr_set = NULL;
xmlNode *xml_top = NULL;
xmlNode *xml_obj = NULL;
gboolean use_attributes_tag = FALSE;
resource_t *rsc = find_rsc_or_clone(rsc_id, data_set);
if (rsc == NULL) {
return -ENXIO;
}
if (safe_str_eq(attr_set_type, XML_TAG_ATTR_SETS)) {
rc = find_resource_attr(cib, XML_ATTR_ID, rsc_id, XML_TAG_META_SETS, attr_set, attr_id,
attr_name, &local_attr_id);
if (rc == pcmk_ok) {
printf("WARNING: There is already a meta attribute called %s (id=%s)\n", attr_name,
local_attr_id);
}
}
rc = find_resource_attr(cib, XML_ATTR_ID, rsc_id, attr_set_type, attr_set, attr_id, attr_name,
&local_attr_id);
if (rc == pcmk_ok) {
crm_debug("Found a match for name=%s: id=%s", attr_name, local_attr_id);
attr_id = local_attr_id;
} else if (rc != -ENXIO) {
free(local_attr_id);
return rc;
} else {
const char *value = NULL;
xmlNode *cib_top = NULL;
const char *tag = crm_element_name(rsc->xml);
rc = cib->cmds->query(cib, "/cib", &cib_top,
cib_sync_call | cib_scope_local | cib_xpath | cib_no_children);
value = crm_element_value(cib_top, "ignore_dtd");
if (value != NULL) {
use_attributes_tag = TRUE;
} else {
value = crm_element_value(cib_top, XML_ATTR_VALIDATION);
if (value && strstr(value, "-0.6")) {
use_attributes_tag = TRUE;
}
}
free_xml(cib_top);
if (attr_set == NULL) {
local_attr_set = crm_concat(rsc_id, attr_set_type, '-');
attr_set = local_attr_set;
}
if (attr_id == NULL) {
local_attr_id = crm_concat(attr_set, attr_name, '-');
attr_id = local_attr_id;
}
if (use_attributes_tag && safe_str_eq(tag, XML_CIB_TAG_MASTER)) {
tag = "master_slave"; /* use the old name */
}
xml_top = create_xml_node(NULL, tag);
crm_xml_add(xml_top, XML_ATTR_ID, rsc_id);
xml_obj = create_xml_node(xml_top, attr_set_type);
crm_xml_add(xml_obj, XML_ATTR_ID, attr_set);
if (use_attributes_tag) {
xml_obj = create_xml_node(xml_obj, XML_TAG_ATTRS);
}
}
xml_obj = create_xml_node(xml_obj, XML_CIB_TAG_NVPAIR);
if (xml_top == NULL) {
xml_top = xml_obj;
}
crm_xml_add(xml_obj, XML_ATTR_ID, attr_id);
crm_xml_add(xml_obj, XML_NVPAIR_ATTR_NAME, attr_name);
crm_xml_add(xml_obj, XML_NVPAIR_ATTR_VALUE, attr_value);
crm_log_xml_debug(xml_top, "Update");
rc = cib->cmds->modify(cib, XML_CIB_TAG_RESOURCES, xml_top, cib_options);
free_xml(xml_top);
free(local_attr_id);
free(local_attr_set);
return rc;
}
static int
delete_resource_attr(const char *rsc_id, const char *attr_set, const char *attr_id,
const char *attr_name, cib_t * cib, pe_working_set_t * data_set)
{
xmlNode *xml_obj = NULL;
int rc = pcmk_ok;
char *local_attr_id = NULL;
resource_t *rsc = find_rsc_or_clone(rsc_id, data_set);
if (rsc == NULL) {
return -ENXIO;
}
rc = find_resource_attr(cib, XML_ATTR_ID, rsc_id, attr_set_type, attr_set, attr_id, attr_name,
&local_attr_id);
if (rc == -ENXIO) {
return pcmk_ok;
} else if (rc != pcmk_ok) {
return rc;
}
if (attr_id == NULL) {
attr_id = local_attr_id;
}
xml_obj = create_xml_node(NULL, XML_CIB_TAG_NVPAIR);
crm_xml_add(xml_obj, XML_ATTR_ID, attr_id);
crm_xml_add(xml_obj, XML_NVPAIR_ATTR_NAME, attr_name);
crm_log_xml_debug(xml_obj, "Delete");
rc = cib->cmds->delete(cib, XML_CIB_TAG_RESOURCES, xml_obj, cib_options);
if (rc == pcmk_ok) {
printf("Deleted %s option: id=%s%s%s%s%s\n", rsc_id, local_attr_id,
attr_set ? " set=" : "", attr_set ? attr_set : "",
attr_name ? " name=" : "", attr_name ? attr_name : "");
}
free_xml(xml_obj);
free(local_attr_id);
return rc;
}
static int
dump_resource_prop(const char *rsc, const char *attr, pe_working_set_t * data_set)
{
const char *value = NULL;
resource_t *the_rsc = pe_find_resource(data_set->resources, rsc);
if (the_rsc == NULL) {
return -ENXIO;
}
value = crm_element_value(the_rsc->xml, attr);
if (value != NULL) {
fprintf(stdout, "%s\n", value);
return 0;
}
return -ENXIO;
}
static int
send_lrm_rsc_op(crm_ipc_t * crmd_channel, const char *op,
const char *host_uname, const char *rsc_id,
gboolean only_failed, pe_working_set_t * data_set)
{
char *key = NULL;
int rc = -ECOMM;
xmlNode *cmd = NULL;
xmlNode *xml_rsc = NULL;
const char *value = NULL;
xmlNode *params = NULL;
xmlNode *msg_data = NULL;
resource_t *rsc = pe_find_resource(data_set->resources, rsc_id);
if (rsc == NULL) {
CMD_ERR("Resource %s not found\n", rsc_id);
return -ENXIO;
} else if (rsc->variant != pe_native) {
CMD_ERR("We can only process primitive resources, not %s\n", rsc_id);
return -EINVAL;
} else if (host_uname == NULL) {
CMD_ERR("Please supply a hostname with -H\n");
return -EINVAL;
}
key = crm_concat("0:0:crm-resource", our_pid, '-');
msg_data = create_xml_node(NULL, XML_GRAPH_TAG_RSC_OP);
crm_xml_add(msg_data, XML_ATTR_TRANSITION_KEY, key);
free(key);
xml_rsc = create_xml_node(msg_data, XML_CIB_TAG_RESOURCE);
if (rsc->clone_name) {
crm_xml_add(xml_rsc, XML_ATTR_ID, rsc->clone_name);
crm_xml_add(xml_rsc, XML_ATTR_ID_LONG, rsc->id);
} else {
crm_xml_add(xml_rsc, XML_ATTR_ID, rsc->id);
}
value = crm_element_value(rsc->xml, XML_ATTR_TYPE);
crm_xml_add(xml_rsc, XML_ATTR_TYPE, value);
if (value == NULL) {
CMD_ERR("%s has no type! Aborting...\n", rsc_id);
return -ENXIO;
}
value = crm_element_value(rsc->xml, XML_AGENT_ATTR_CLASS);
crm_xml_add(xml_rsc, XML_AGENT_ATTR_CLASS, value);
if (value == NULL) {
CMD_ERR("%s has no class! Aborting...\n", rsc_id);
return -ENXIO;
}
value = crm_element_value(rsc->xml, XML_AGENT_ATTR_PROVIDER);
crm_xml_add(xml_rsc, XML_AGENT_ATTR_PROVIDER, value);
params = create_xml_node(msg_data, XML_TAG_ATTRS);
crm_xml_add(params, XML_ATTR_CRM_VERSION, CRM_FEATURE_SET);
key = crm_meta_name(XML_LRM_ATTR_INTERVAL);
crm_xml_add(params, key, "60000"); /* 1 minute */
free(key);
cmd = create_request(op, msg_data, host_uname, CRM_SYSTEM_CRMD, crm_system_name, our_pid);
/* crm_log_xml_warn(cmd, "send_lrm_rsc_op"); */
free_xml(msg_data);
if (crm_ipc_send(crmd_channel, cmd, 0, 0, NULL) > 0) {
rc = 0;
} else {
CMD_ERR("Could not send %s op to the crmd", op);
rc = -ENOTCONN;
}
free_xml(cmd);
return rc;
}
static int
delete_lrm_rsc(crm_ipc_t * crmd_channel, const char *host_uname,
resource_t * rsc, pe_working_set_t * data_set)
{
int rc = pcmk_ok;
if (rsc == NULL) {
return -ENXIO;
} else if (rsc->children) {
GListPtr lpc = NULL;
for (lpc = rsc->children; lpc != NULL; lpc = lpc->next) {
resource_t *child = (resource_t *) lpc->data;
delete_lrm_rsc(crmd_channel, host_uname, child, data_set);
}
return pcmk_ok;
} else if (host_uname == NULL) {
GListPtr lpc = NULL;
for (lpc = data_set->nodes; lpc != NULL; lpc = lpc->next) {
node_t *node = (node_t *) lpc->data;
if (node->details->online) {
delete_lrm_rsc(crmd_channel, node->details->uname, rsc, data_set);
}
}
return pcmk_ok;
}
printf("Cleaning up %s on %s\n", rsc->id, host_uname);
rc = send_lrm_rsc_op(crmd_channel, CRM_OP_LRM_DELETE, host_uname, rsc->id, TRUE, data_set);
if (rc == pcmk_ok) {
char *attr_name = NULL;
const char *id = rsc->id;
if (rsc->clone_name) {
id = rsc->clone_name;
}
attr_name = crm_concat("fail-count", id, '-');
attrd_update_delegate(NULL, 'D', host_uname, attr_name, NULL, XML_CIB_TAG_STATUS, NULL, NULL, NULL);
free(attr_name);
}
return rc;
}
static int
fail_lrm_rsc(crm_ipc_t * crmd_channel, const char *host_uname,
const char *rsc_id, pe_working_set_t * data_set)
{
crm_warn("Failing: %s", rsc_id);
return send_lrm_rsc_op(crmd_channel, CRM_OP_LRM_FAIL, host_uname, rsc_id, FALSE, data_set);
}
static int
refresh_lrm(crm_ipc_t * crmd_channel, const char *host_uname)
{
xmlNode *cmd = NULL;
int rc = -ECOMM;
cmd = create_request(CRM_OP_LRM_REFRESH, NULL, host_uname,
CRM_SYSTEM_CRMD, crm_system_name, our_pid);
if (crm_ipc_send(crmd_channel, cmd, 0, 0, NULL) > 0) {
rc = 0;
}
free_xml(cmd);
return rc;
}
static int
move_resource(const char *rsc_id,
const char *existing_node, const char *preferred_node, cib_t * cib_conn)
{
char *later_s = NULL;
int rc = pcmk_ok;
char *id = NULL;
xmlNode *rule = NULL;
xmlNode *expr = NULL;
xmlNode *constraints = NULL;
xmlNode *fragment = NULL;
xmlNode *can_run = NULL;
xmlNode *dont_run = NULL;
fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
constraints = fragment;
id = crm_concat("cli-prefer", rsc_id, '-');
can_run = create_xml_node(NULL, XML_CONS_TAG_RSC_LOCATION);
crm_xml_add(can_run, XML_ATTR_ID, id);
free(id);
id = crm_concat("cli-standby", rsc_id, '-');
dont_run = create_xml_node(NULL, XML_CONS_TAG_RSC_LOCATION);
crm_xml_add(dont_run, XML_ATTR_ID, id);
free(id);
if (move_lifetime) {
char *life = strdup(move_lifetime);
char *life_mutable = life;
ha_time_t *now = NULL;
ha_time_t *later = NULL;
ha_time_t *duration = parse_time_duration(&life_mutable);
if (duration == NULL) {
CMD_ERR("Invalid duration specified: %s\n", move_lifetime);
CMD_ERR("Please refer to"
" http://en.wikipedia.org/wiki/ISO_8601#Duration"
" for examples of valid durations\n");
free(life);
return -EINVAL;
}
now = new_ha_date(TRUE);
later = add_time(now, duration);
log_date(LOG_INFO, "now ", now, ha_log_date | ha_log_time);
log_date(LOG_INFO, "later ", later, ha_log_date | ha_log_time);
log_date(LOG_INFO, "duration", duration, ha_log_date | ha_log_time | ha_log_local);
later_s = date_to_string(later, ha_log_date | ha_log_time);
printf("Migration will take effect until: %s\n", later_s);
free_ha_date(duration);
free_ha_date(later);
free_ha_date(now);
free(life);
}
if (existing_node == NULL) {
crm_log_xml_notice(can_run, "Deleting");
rc = cib_conn->cmds->delete(cib_conn, XML_CIB_TAG_CONSTRAINTS, dont_run, cib_options);
if (rc == -ENXIO) {
rc = pcmk_ok;
} else if (rc != pcmk_ok) {
goto bail;
}
} else {
if (BE_QUIET == FALSE) {
fprintf(stderr,
"WARNING: Creating rsc_location constraint '%s'"
" with a score of -INFINITY for resource %s"
" on %s.\n", ID(dont_run), rsc_id, existing_node);
CMD_ERR("\tThis will prevent %s from running"
" on %s until the constraint is removed using"
" the 'crm_resource -U' command or manually"
" with cibadmin\n", rsc_id, existing_node);
CMD_ERR("\tThis will be the case even if %s is"
" the last node in the cluster\n", existing_node);
CMD_ERR("\tThis message can be disabled with -Q\n");
}
crm_xml_add(dont_run, "rsc", rsc_id);
rule = create_xml_node(dont_run, XML_TAG_RULE);
expr = create_xml_node(rule, XML_TAG_EXPRESSION);
id = crm_concat("cli-standby-rule", rsc_id, '-');
crm_xml_add(rule, XML_ATTR_ID, id);
free(id);
crm_xml_add(rule, XML_RULE_ATTR_SCORE, MINUS_INFINITY_S);
crm_xml_add(rule, XML_RULE_ATTR_BOOLEAN_OP, "and");
id = crm_concat("cli-standby-expr", rsc_id, '-');
crm_xml_add(expr, XML_ATTR_ID, id);
free(id);
crm_xml_add(expr, XML_EXPR_ATTR_ATTRIBUTE, "#uname");
crm_xml_add(expr, XML_EXPR_ATTR_OPERATION, "eq");
crm_xml_add(expr, XML_EXPR_ATTR_VALUE, existing_node);
crm_xml_add(expr, XML_EXPR_ATTR_TYPE, "string");
if (later_s) {
expr = create_xml_node(rule, "date_expression");
id = crm_concat("cli-standby-lifetime-end", rsc_id, '-');
crm_xml_add(expr, XML_ATTR_ID, id);
free(id);
crm_xml_add(expr, "operation", "lt");
crm_xml_add(expr, "end", later_s);
}
add_node_copy(constraints, dont_run);
}
if (preferred_node == NULL) {
crm_log_xml_notice(can_run, "Deleting");
rc = cib_conn->cmds->delete(cib_conn, XML_CIB_TAG_CONSTRAINTS, can_run, cib_options);
if (rc == -ENXIO) {
rc = pcmk_ok;
} else if (rc != pcmk_ok) {
goto bail;
}
} else {
crm_xml_add(can_run, "rsc", rsc_id);
rule = create_xml_node(can_run, XML_TAG_RULE);
expr = create_xml_node(rule, XML_TAG_EXPRESSION);
id = crm_concat("cli-prefer-rule", rsc_id, '-');
crm_xml_add(rule, XML_ATTR_ID, id);
free(id);
crm_xml_add(rule, XML_RULE_ATTR_SCORE, INFINITY_S);
crm_xml_add(rule, XML_RULE_ATTR_BOOLEAN_OP, "and");
id = crm_concat("cli-prefer-expr", rsc_id, '-');
crm_xml_add(expr, XML_ATTR_ID, id);
free(id);
crm_xml_add(expr, XML_EXPR_ATTR_ATTRIBUTE, "#uname");
crm_xml_add(expr, XML_EXPR_ATTR_OPERATION, "eq");
crm_xml_add(expr, XML_EXPR_ATTR_VALUE, preferred_node);
crm_xml_add(expr, XML_EXPR_ATTR_TYPE, "string");
if (later_s) {
expr = create_xml_node(rule, "date_expression");
id = crm_concat("cli-prefer-lifetime-end", rsc_id, '-');
crm_xml_add(expr, XML_ATTR_ID, id);
free(id);
crm_xml_add(expr, "operation", "lt");
crm_xml_add(expr, "end", later_s);
}
add_node_copy(constraints, can_run);
}
if (preferred_node != NULL || existing_node != NULL) {
crm_log_xml_notice(fragment, "CLI Update");
rc = cib_conn->cmds->update(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);
}
bail:
free_xml(fragment);
free_xml(dont_run);
free_xml(can_run);
free(later_s);
return rc;
}
static int
list_resource_operations(const char *rsc_id, const char *host_uname, gboolean active,
pe_working_set_t * data_set)
{
resource_t *rsc = NULL;
int opts = pe_print_printf | pe_print_rsconly | pe_print_suppres_nl;
GListPtr ops = find_operations(rsc_id, host_uname, active, data_set);
GListPtr lpc = NULL;
for (lpc = ops; lpc != NULL; lpc = lpc->next) {
xmlNode *xml_op = (xmlNode *) lpc->data;
const char *op_rsc = crm_element_value(xml_op, "resource");
const char *last = crm_element_value(xml_op, "last_run");
const char *status_s = crm_element_value(xml_op, XML_LRM_ATTR_OPSTATUS);
const char *op_key = crm_element_value(xml_op, XML_LRM_ATTR_TASK_KEY);
int status = crm_parse_int(status_s, "0");
rsc = pe_find_resource(data_set->resources, op_rsc);
rsc->fns->print(rsc, "", opts, stdout);
fprintf(stdout, ": %s (node=%s, call=%s, rc=%s",
op_key ? op_key : ID(xml_op),
crm_element_value(xml_op, XML_ATTR_UNAME),
crm_element_value(xml_op, XML_LRM_ATTR_CALLID),
crm_element_value(xml_op, XML_LRM_ATTR_RC));
if (last) {
time_t run_at = crm_parse_int(last, "0");
fprintf(stdout, ", last-run=%s, exec=%sms\n",
ctime(&run_at), crm_element_value(xml_op, "exec_time"));
}
fprintf(stdout, "): %s\n", services_lrm_status_str(status));
}
return pcmk_ok;
}
#include "../pengine/pengine.h"
static void
show_location(resource_t * rsc, const char *prefix)
{
GListPtr lpc = NULL;
GListPtr list = rsc->rsc_location;
int offset = 0;
if (prefix) {
offset = strlen(prefix) - 2;
}
for (lpc = list; lpc != NULL; lpc = lpc->next) {
rsc_to_node_t *cons = (rsc_to_node_t *) lpc->data;
GListPtr lpc2 = NULL;
for (lpc2 = cons->node_list_rh; lpc2 != NULL; lpc2 = lpc2->next) {
node_t *node = (node_t *) lpc2->data;
char *score = score2char(node->weight);
fprintf(stdout, "%s: Node %-*s (score=%s, id=%s)\n",
prefix ? prefix : " ", 71 - offset, node->details->uname, score, cons->id);
free(score);
}
}
}
static void
show_colocation(resource_t * rsc, gboolean dependants, gboolean recursive, int offset)
{
char *prefix = NULL;
GListPtr lpc = NULL;
GListPtr list = rsc->rsc_cons;
prefix = calloc(1, (offset * 4) + 1);
memset(prefix, ' ', offset * 4);
if (dependants) {
list = rsc->rsc_cons_lhs;
}
if (is_set(rsc->flags, pe_rsc_allocating)) {
/* Break colocation loops */
printf("loop %s\n", rsc->id);
free(prefix);
return;
}
set_bit(rsc->flags, pe_rsc_allocating);
for (lpc = list; lpc != NULL; lpc = lpc->next) {
rsc_colocation_t *cons = (rsc_colocation_t *) lpc->data;
char *score = NULL;
resource_t *peer = cons->rsc_rh;
if (dependants) {
peer = cons->rsc_lh;
}
if (is_set(peer->flags, pe_rsc_allocating)) {
if (dependants == FALSE) {
fprintf(stdout, "%s%-*s (id=%s - loop)\n", prefix, 80 - (4 * offset), peer->id,
cons->id);
}
continue;
}
if (dependants && recursive) {
show_colocation(peer, dependants, recursive, offset + 1);
}
score = score2char(cons->score);
if (cons->role_rh > RSC_ROLE_STARTED) {
fprintf(stdout, "%s%-*s (score=%s, %s role=%s, id=%s)\n", prefix, 80 - (4 * offset),
peer->id, score, dependants ? "needs" : "with", role2text(cons->role_rh),
cons->id);
} else {
fprintf(stdout, "%s%-*s (score=%s, id=%s)\n", prefix, 80 - (4 * offset),
peer->id, score, cons->id);
}
show_location(peer, prefix);
free(score);
if (!dependants && recursive) {
show_colocation(peer, dependants, recursive, offset + 1);
}
}
free(prefix);
}
static GHashTable *
generate_resource_params(resource_t *rsc, pe_working_set_t * data_set)
{
int rc = 0;
GHashTable *params = NULL;
GHashTable *meta = NULL;
GHashTable *combined = NULL;
GHashTableIter iter;
if (!rsc) {
crm_err("Resource does not exist in config");
rc = -1;
return NULL;
}
params = g_hash_table_new_full(crm_str_hash, g_str_equal, g_hash_destroy_str, g_hash_destroy_str);
meta = g_hash_table_new_full(crm_str_hash, g_str_equal, g_hash_destroy_str, g_hash_destroy_str);
combined = g_hash_table_new_full(crm_str_hash, g_str_equal, g_hash_destroy_str, g_hash_destroy_str);
get_rsc_attributes(params, rsc, NULL/* TODO: Pass in local node */, data_set);
get_meta_attributes(meta, rsc, NULL/* TODO: Pass in local node */, data_set);
if (params) {
char *key = NULL;
char *value = NULL;
g_hash_table_iter_init(&iter, params);
while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
g_hash_table_insert(combined, strdup(key), strdup(value));
}
g_hash_table_destroy(params);
}
if (meta) {
char *key = NULL;
char *value = NULL;
g_hash_table_iter_init(&iter, meta);
while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
char *crm_name = crm_meta_name(key);
g_hash_table_insert(combined, crm_name, strdup(value));
}
g_hash_table_destroy(meta);
}
return combined;
}
/* *INDENT-OFF* */
static struct crm_option long_options[] = {
/* Top-level Options */
{"help", 0, 0, '?', "\t\tThis text"},
{"version", 0, 0, '$', "\t\tVersion information" },
{"verbose", 0, 0, 'V', "\t\tIncrease debug output"},
{"quiet", 0, 0, 'Q', "\t\tPrint only the value on stdout\n"},
{"resource", 1, 0, 'r', "\tResource ID" },
{"-spacer-",1, 0, '-', "\nQueries:"},
{"list", 0, 0, 'L', "\t\tList all cluster resources"},
{"list-raw", 0, 0, 'l', "\tList the IDs of all instantiated resources (no groups/clones/...)"},
{"list-cts", 0, 0, 'c', NULL, 1},
{"list-operations", 0, 0, 'O', "\tList active resource operations. Optionally filtered by resource (-r) and/or node (-N)"},
{"list-all-operations", 0, 0, 'o', "List all resource operations. Optionally filtered by resource (-r) and/or node (-N)\n"},
{"list-standards", 0, 0, 0, "\tList supported standards"},
{"list-ocf-providers", 0, 0, 0, "List all available OCF providers"},
{"list-agents", 1, 0, 0, "List all agents available for the named standard and/or provider."},
{"list-ocf-alternatives", 1, 0, 0, "List all available providers for the named OCF agent\n"},
{"show-metadata", 1, 0, 0, "Show the metadata for the named class:provider:agent"},
{"query-xml", 0, 0, 'q', "\tQuery the definition of a resource (template expanded)"},
{"query-xml-raw", 0, 0, 'w', "\tQuery the definition of a resource (raw xml)"},
{"locate", 0, 0, 'W', "\t\tDisplay the current location(s) of a resource"},
{"stack", 0, 0, 'A', "\t\tDisplay the prerequisites and dependents of a resource"},
{"constraints",0, 0, 'a', "\tDisplay the (co)location constraints that apply to a resource"},
{"-spacer-", 1, 0, '-', "\nCommands:"},
{"set-parameter", 1, 0, 'p', "Set the named parameter for a resource. See also -m, --meta"},
{"get-parameter", 1, 0, 'g', "Display the named parameter for a resource. See also -m, --meta"},
{"delete-parameter",1, 0, 'd', "Delete the named parameter for a resource. See also -m, --meta"},
{"get-property", 1, 0, 'G', "Display the 'class', 'type' or 'provider' of a resource", 1},
{"set-property", 1, 0, 'S', "(Advanced) Set the class, type or provider of a resource", 1},
{"move", 0, 0, 'M',
"\t\tMove a resource from its current location, optionally specifying a destination (-N) and/or a period for which it should take effect (-u)"
"\n\t\t\t\tIf -N is not specified, the cluster will force the resource to move by creating a rule for the current location and a score of -INFINITY"
"\n\t\t\t\tNOTE: This will prevent the resource from running on this node until the constraint is removed with -U"},
{"un-move", 0, 0, 'U', "\t\tRemove all constraints created by a move command"},
{"-spacer-", 1, 0, '-', "\nAdvanced Commands:"},
{"delete", 0, 0, 'D', "\t\t(Advanced) Delete a resource from the CIB"},
{"fail", 0, 0, 'F', "\t\t(Advanced) Tell the cluster this resource has failed"},
{"refresh", 0, 0, 'R', "\t\t(Advanced) Refresh the CIB from the LRM"},
{"cleanup", 0, 0, 'C', "\t\t(Advanced) Delete a resource from the LRM"},
{"reprobe", 0, 0, 'P', "\t\t(Advanced) Re-check for resources started outside of the CRM\n"},
{"force-stop", 0, 0, 0, "\t(Advanced) Bypass the cluster and stop a resource on the local node"},
{"force-start",0, 0, 0, "\t(Advanced) Bypass the cluster and start a resource on the local node"},
{"force-check",0, 0, 0, "\t(Advanced) Bypass the cluster and check the state of a resource on the local node\n"},
{"-spacer-", 1, 0, '-', "\nAdditional Options:"},
{"node", 1, 0, 'N', "\tHost uname"},
{"resource-type", 1, 0, 't', "Resource type (primitive, clone, group, ...)"},
{"parameter-value", 1, 0, 'v', "Value to use with -p, -g or -d"},
{"lifetime", 1, 0, 'u', "\tLifespan of migration constraints\n"},
{"meta", 0, 0, 'm', "\t\tModify a resource's configuration option rather than one which is passed to the resource agent script. For use with -p, -g, -d"},
{"utilization", 0, 0, 'z', "\tModify a resource's utilization attribute. For use with -p, -g, -d"},
{"set-name", 1, 0, 's', "\t(Advanced) ID of the instance_attributes object to change"},
{"nvpair", 1, 0, 'i', "\t(Advanced) ID of the nvpair object to change/delete"},
{"force", 0, 0, 'f', "\n" /* Is this actually true anymore?
"\t\tForce the resource to move by creating a rule for the current location and a score of -INFINITY"
"\n\t\tThis should be used if the resource's stickiness and constraint scores total more than INFINITY (Currently 100,000)"
"\n\t\tNOTE: This will prevent the resource from running on this node until the constraint is removed with -U or the --lifetime duration expires\n"*/ },
{"xml-file", 1, 0, 'x', NULL, 1},\
/* legacy options */
{"host-uname", 1, 0, 'H', NULL, 1},
{"migrate", 0, 0, 'M', NULL, 1},
{"un-migrate", 0, 0, 'U', NULL, 1},
{"-spacer-", 1, 0, '-', "\nExamples:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "List the configured resources:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --list", pcmk_option_example},
{"-spacer-", 1, 0, '-', "List the available OCF agents:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --list-agents ocf", pcmk_option_example},
{"-spacer-", 1, 0, '-', "List the available OCF agents from the linux-ha project:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --list-agents ocf:heartbeat", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Display the current location of 'myResource':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --locate", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Move 'myResource' to another machine:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --move", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Move 'myResource' to a specific machine:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --move --node altNode", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Allow (but not force) 'myResource' to move back to its original location:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --un-move", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Tell the cluster that 'myResource' failed:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --fail", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Stop a 'myResource' (and anything that depends on it):", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --set-parameter target-role --meta --parameter-value Stopped", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Tell the cluster not to manage 'myResource':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "The cluster will not attempt to start or stop the resource under any circumstances."},
{"-spacer-", 1, 0, '-', "Useful when performing maintenance tasks on a resource.", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --set-parameter is-managed --meta --parameter-value false", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Erase the operation history of 'myResource' on 'aNode':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "The cluster will 'forget' the existing resource state (including any errors) and attempt to recover the resource."},
{"-spacer-", 1, 0, '-', "Useful when a resource had failed permanently and has been repaired by an administrator.", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_resource --resource myResource --cleanup --node aNode", pcmk_option_example},
{0, 0, 0, 0}
};
/* *INDENT-ON* */
int
main(int argc, char **argv)
{
const char *longname = NULL;
pe_working_set_t data_set;
xmlNode *cib_xml_copy = NULL;
cib_t *cib_conn = NULL;
lrmd_t *lrmd_conn = NULL;
int rc = pcmk_ok;
int option_index = 0;
int argerr = 0;
int flag;
crm_log_cli_init("crm_resource");
crm_set_options(NULL, "(query|command) [options]", long_options,
"Perform tasks related to cluster resources.\nAllows resources to be queried (definition and location), modified, and moved around the cluster.\n");
if (argc < 2) {
crm_help('?', EX_USAGE);
}
lrmd_conn = lrmd_api_new();
while (1) {
flag = crm_get_option_long(argc, argv, &option_index, &longname);
if (flag == -1)
break;
switch (flag) {
case 0:
if(safe_str_eq("force-stop", longname)
|| safe_str_eq("force-start", longname)
|| safe_str_eq("force-check", longname)) {
rsc_cmd = flag;
rsc_long_cmd = longname;
} else if(safe_str_eq("list-ocf-providers", longname)
|| safe_str_eq("list-ocf-alternatives", longname)
|| safe_str_eq("list-standards", longname)) {
const char *text = NULL;
lrmd_list_t *list = NULL;
lrmd_list_t *iter = NULL;
if(safe_str_eq("list-ocf-providers", longname) || safe_str_eq("list-ocf-alternatives", longname)) {
rc = lrmd_conn->cmds->list_ocf_providers(lrmd_conn, optarg, &list);
text = "OCF providers";
} else if(safe_str_eq("list-standards", longname)) {
rc = lrmd_conn->cmds->list_standards(lrmd_conn, &list);
text = "standards";
}
if (rc > 0) {
rc = 0;
for (iter = list; iter != NULL; iter = iter->next) {
rc++;
printf("%s\n", iter->val);
}
lrmd_list_freeall(list);
return 0;
} else {
if(optarg) {
fprintf(stderr, "No %s found for %s\n", text, optarg);
} else {
fprintf(stderr, "No %s found\n", text);
}
return -1;
}
} else if(safe_str_eq("show-metadata", longname)) {
char standard[512];
char provider[512];
char type[512];
char *metadata = NULL;
rc = sscanf(optarg, "%[^:]:%[^:]:%s", standard, provider, type);
if(rc == 3) {
rc = lrmd_conn->cmds->get_metadata(lrmd_conn, standard, provider, type, &metadata, 0);
if(metadata) {
printf("%s\n", metadata);
return 0;
}
fprintf(stderr, "Metadata query for %s failed: %d\n", optarg, rc);
return rc;
} else if(rc == 2) {
rc = lrmd_conn->cmds->get_metadata(lrmd_conn, standard, NULL, provider, &metadata, 0);
if(metadata) {
printf("%s\n", metadata);
return 0;
}
fprintf(stderr, "Metadata query for %s failed: %d\n", optarg, rc);
return rc;
} else if(rc < 2) {
fprintf(stderr, "Please specify standard:type or standard:provider:type, not %s\n", optarg);
return -1;
}
} else if(safe_str_eq("list-agents", longname)) {
lrmd_list_t *list = NULL;
lrmd_list_t *iter = NULL;
char standard[512];
char provider[512];
rc = sscanf(optarg, "%[^:]:%s", standard, provider);
if(rc == 1) {
rc = lrmd_conn->cmds->list_agents(lrmd_conn, &list, optarg, NULL);
provider[0] = '*';
provider[1] = 0;
} else if(rc == 2) {
rc = lrmd_conn->cmds->list_agents(lrmd_conn, &list, standard, provider);
}
if (rc > 0) {
rc = 0;
for (iter = list; iter != NULL; iter = iter->next) {
printf("%s\n", iter->val);
rc++;
}
lrmd_list_freeall(list);
rc = 0;
} else {
fprintf(stderr, "No agents found for standard=%s, provider=%s\n", standard, provider);
rc = -1;
}
return rc;
} else {
crm_err("Unhandled long option: %s", longname);
}
break;
case 'V':
crm_bump_log_level(argc, argv);
break;
case '$':
case '?':
crm_help(flag, EX_OK);
break;
case 'x':
xml_file = strdup(optarg);
break;
case 'Q':
BE_QUIET = TRUE;
break;
case 'm':
attr_set_type = XML_TAG_META_SETS;
break;
case 'z':
attr_set_type = XML_TAG_UTILIZATION;
break;
case 'u':
move_lifetime = strdup(optarg);
break;
case 'f':
do_force = TRUE;
break;
case 'i':
prop_id = optarg;
break;
case 's':
prop_set = optarg;
break;
case 'r':
rsc_id = optarg;
break;
case 'v':
prop_value = optarg;
break;
case 't':
rsc_type = optarg;
break;
case 'R':
case 'P':
rsc_cmd = flag;
break;
case 'L':
case 'c':
case 'l':
case 'q':
case 'w':
case 'D':
case 'F':
case 'C':
case 'W':
case 'M':
case 'U':
case 'O':
case 'o':
case 'A':
case 'a':
rsc_cmd = flag;
break;
case 'p':
case 'g':
case 'd':
case 'S':
case 'G':
prop_name = optarg;
rsc_cmd = flag;
break;
case 'h':
case 'H':
case 'N':
crm_trace("Option %c => %s", flag, optarg);
host_uname = optarg;
break;
default:
CMD_ERR("Argument code 0%o (%c) is not (?yet?) supported\n", flag, flag);
++argerr;
break;
}
}
if (optind < argc && argv[optind] != NULL) {
CMD_ERR("non-option ARGV-elements: ");
while (optind < argc && argv[optind] != NULL) {
CMD_ERR("%s ", argv[optind++]);
++argerr;
}
CMD_ERR("\n");
}
if (optind > argc) {
++argerr;
}
if (argerr) {
crm_help('?', EX_USAGE);
}
our_pid = calloc(1, 11);
if (our_pid != NULL) {
snprintf(our_pid, 10, "%d", getpid());
our_pid[10] = '\0';
}
if (do_force) {
crm_debug("Forcing...");
cib_options |= cib_quorum_override;
}
set_working_set_defaults(&data_set);
if (rsc_cmd != 'P') {
resource_t *rsc = NULL;
cib_conn = cib_new();
rc = cib_conn->cmds->signon(cib_conn, crm_system_name, cib_command);
if (rc != pcmk_ok) {
CMD_ERR("Error signing on to the CIB service: %s\n", pcmk_strerror(rc));
return rc;
}
if (xml_file != NULL) {
cib_xml_copy = filename2xml(xml_file);
} else {
cib_xml_copy = get_cib_copy(cib_conn);
}
if (cli_config_update(&cib_xml_copy, NULL, FALSE) == FALSE) {
rc = -ENOKEY;
goto bail;
}
data_set.input = cib_xml_copy;
data_set.now = new_ha_date(TRUE);
cluster_status(&data_set);
if (rsc_id) {
rsc = find_rsc_or_clone(rsc_id, &data_set);
}
if (rsc == NULL) {
rc = -ENXIO;
}
}
if (rsc_cmd == 'R' || rsc_cmd == 'C' || rsc_cmd == 'F' || rsc_cmd == 'P') {
xmlNode *xml = NULL;
mainloop_io_t *source = mainloop_add_ipc_client(CRM_SYSTEM_CRMD, G_PRIORITY_DEFAULT, 0, NULL, &crm_callbacks);
crmd_channel = mainloop_get_ipc_client(source);
if (crmd_channel == NULL) {
CMD_ERR("Error signing on to the CRMd service\n");
rc = -ENOTCONN;
goto bail;
}
xml = create_hello_message(our_pid, crm_system_name, "0", "1");
crm_ipc_send(crmd_channel, xml, 0, 0, NULL);
free_xml(xml);
}
if (rsc_cmd == 'L') {
rc = pcmk_ok;
do_find_resource_list(&data_set, FALSE);
} else if (rsc_cmd == 'l') {
int found = 0;
GListPtr lpc = NULL;
rc = pcmk_ok;
for (lpc = data_set.resources; lpc != NULL; lpc = lpc->next) {
resource_t *rsc = (resource_t *) lpc->data;
found++;
print_raw_rsc(rsc);
}
if (found == 0) {
printf("NO resources configured\n");
rc = -ENXIO;
goto bail;
}
} else if (rsc_cmd == 0 && rsc_long_cmd) {
svc_action_t *op = NULL;
const char *rtype = NULL;
const char *rprov = NULL;
const char *rclass = NULL;
const char *action = NULL;
GHashTable *params = NULL;
resource_t *rsc = pe_find_resource(data_set.resources, rsc_id);
if (rsc == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
if(safe_str_eq(rsc_long_cmd, "force-stop")) {
action = "stop";
} else if(safe_str_eq(rsc_long_cmd, "force-start")) {
action = "start";
} else if(safe_str_eq(rsc_long_cmd, "force-check")) {
action = "monitor";
}
rclass = crm_element_value(rsc->xml, XML_AGENT_ATTR_CLASS);
rprov = crm_element_value(rsc->xml, XML_AGENT_ATTR_PROVIDER);
rtype = crm_element_value(rsc->xml, XML_ATTR_TYPE);
params = generate_resource_params(rsc, &data_set);
op = resources_action_create(
rsc->id, rclass, rprov, rtype, action, 0, -1, params);
if(services_action_sync(op)) {
int more, lpc, last;
char *local_copy = NULL;
if(op->status == PCMK_LRM_OP_DONE) {
printf("Operation %s for %s (%s:%s:%s) returned %d\n",
action, rsc->id, rclass, rprov?rprov:"", rtype, op->rc);
} else {
printf("Operation %s for %s (%s:%s:%s) failed: %d\n",
action, rsc->id, rclass, rprov?rprov:"", rtype, op->status);
}
if (op->stdout_data) {
local_copy = strdup(op->stdout_data);
more = strlen(local_copy);
last = 0;
for(lpc = 0; lpc < more; lpc++) {
if(local_copy[lpc] == '\n' || local_copy[lpc] == 0) {
local_copy[lpc] = 0;
printf(" > stdout: %s\n", local_copy+last);
last = lpc+1;
}
}
free(local_copy);
}
if (op->stderr_data) {
local_copy = strdup(op->stderr_data);
more = strlen(local_copy);
last = 0;
for(lpc = 0; lpc < more; lpc++) {
if(local_copy[lpc] == '\n' || local_copy[lpc] == 0) {
local_copy[lpc] = 0;
printf(" > stderr: %s\n", local_copy+last);
last = lpc+1;
}
}
free(local_copy);
}
}
rc = op->rc;
services_action_free(op);
return rc;
} else if (rsc_cmd == 'A' || rsc_cmd == 'a') {
GListPtr lpc = NULL;
resource_t *rsc = pe_find_resource(data_set.resources, rsc_id);
xmlNode *cib_constraints = get_object_root(XML_CIB_TAG_CONSTRAINTS, data_set.input);
if (rsc == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
unpack_constraints(cib_constraints, &data_set);
for (lpc = data_set.resources; lpc != NULL; lpc = lpc->next) {
resource_t *r = (resource_t *) lpc->data;
clear_bit(r->flags, pe_rsc_allocating);
}
show_colocation(rsc, TRUE, rsc_cmd == 'A', 1);
fprintf(stdout, "* %s\n", rsc->id);
show_location(rsc, NULL);
for (lpc = data_set.resources; lpc != NULL; lpc = lpc->next) {
resource_t *r = (resource_t *) lpc->data;
clear_bit(r->flags, pe_rsc_allocating);
}
show_colocation(rsc, FALSE, rsc_cmd == 'A', 1);
} else if (rsc_cmd == 'c') {
int found = 0;
GListPtr lpc = NULL;
rc = pcmk_ok;
for (lpc = data_set.resources; lpc != NULL; lpc = lpc->next) {
resource_t *rsc = (resource_t *) lpc->data;
print_cts_rsc(rsc);
found++;
}
print_cts_constraints(&data_set);
} else if (rsc_cmd == 'C') {
resource_t *rsc = pe_find_resource(data_set.resources, rsc_id);
rc = delete_lrm_rsc(crmd_channel, host_uname, rsc, &data_set);
if (rc == pcmk_ok) {
start_mainloop();
}
} else if (rsc_cmd == 'F') {
rc = fail_lrm_rsc(crmd_channel, host_uname, rsc_id, &data_set);
if (rc == pcmk_ok) {
start_mainloop();
}
} else if (rsc_cmd == 'O') {
rc = list_resource_operations(rsc_id, host_uname, TRUE, &data_set);
} else if (rsc_cmd == 'o') {
rc = list_resource_operations(rsc_id, host_uname, FALSE, &data_set);
} else if (rc == -ENXIO) {
CMD_ERR("Resource %s not found: %s\n", crm_str(rsc_id), pcmk_strerror(rc));
} else if (rsc_cmd == 'W') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
rc = do_find_resource(rsc_id, NULL, &data_set);
} else if (rsc_cmd == 'q') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
rc = dump_resource(rsc_id, &data_set, TRUE);
} else if (rsc_cmd == 'w') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
rc = dump_resource(rsc_id, &data_set, FALSE);
} else if (rsc_cmd == 'U') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
/* coverity[var_deref_model] False positive */
rc = move_resource(rsc_id, NULL, NULL, cib_conn);
} else if (rsc_cmd == 'M') {
node_t *dest = NULL;
node_t *current = NULL;
const char *current_uname = NULL;
resource_t *rsc = pe_find_resource(data_set.resources, rsc_id);
if (rsc != NULL && rsc->running_on != NULL) {
current = rsc->running_on->data;
if (current != NULL) {
current_uname = current->details->uname;
}
}
if (host_uname != NULL) {
dest = pe_find_node(data_set.nodes, host_uname);
}
if (rsc == NULL) {
CMD_ERR("Resource %s not moved:" " not found\n", rsc_id);
} else if (rsc->variant == pe_native && g_list_length(rsc->running_on) > 1) {
CMD_ERR("Resource %s not moved:" " active on multiple nodes\n", rsc_id);
} else if (host_uname != NULL && dest == NULL) {
CMD_ERR("Error performing operation: " "%s is not a known node\n", host_uname);
rc = -ENXIO;
} else if (host_uname != NULL && safe_str_eq(current_uname, host_uname)) {
CMD_ERR("Error performing operation: "
"%s is already active on %s\n", rsc_id, host_uname);
} else if (current_uname != NULL && (do_force || host_uname == NULL)) {
/* coverity[var_deref_model] False positive */
rc = move_resource(rsc_id, current_uname, host_uname, cib_conn);
} else if (host_uname != NULL) {
/* coverity[var_deref_model] False positive */
rc = move_resource(rsc_id, NULL, host_uname, cib_conn);
} else {
CMD_ERR("Resource %s not moved: "
"not-active and no preferred location" " specified.\n", rsc_id);
rc = -EINVAL;
}
} else if (rsc_cmd == 'G') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
rc = dump_resource_prop(rsc_id, prop_name, &data_set);
} else if (rsc_cmd == 'S') {
xmlNode *msg_data = NULL;
if (prop_value == NULL || strlen(prop_value) == 0) {
CMD_ERR("You need to supply a value with the -v option\n");
rc = -EINVAL;
goto bail;
} else if (cib_conn == NULL) {
rc = -ENOTCONN;
goto bail;
}
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
CRM_LOG_ASSERT(rsc_type != NULL);
CRM_LOG_ASSERT(prop_name != NULL);
CRM_LOG_ASSERT(prop_value != NULL);
msg_data = create_xml_node(NULL, rsc_type);
crm_xml_add(msg_data, XML_ATTR_ID, rsc_id);
crm_xml_add(msg_data, prop_name, prop_value);
rc = cib_conn->cmds->modify(cib_conn, XML_CIB_TAG_RESOURCES, msg_data, cib_options);
free_xml(msg_data);
} else if (rsc_cmd == 'g') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
rc = dump_resource_attr(rsc_id, prop_name, &data_set);
} else if (rsc_cmd == 'p') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
if (prop_value == NULL || strlen(prop_value) == 0) {
CMD_ERR("You need to supply a value with the -v option\n");
rc = -EINVAL;
goto bail;
}
/* coverity[var_deref_model] False positive */
rc = set_resource_attr(rsc_id, prop_set, prop_id, prop_name,
prop_value, cib_conn, &data_set);
} else if (rsc_cmd == 'd') {
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
/* coverity[var_deref_model] False positive */
rc = delete_resource_attr(rsc_id, prop_set, prop_id, prop_name, cib_conn, &data_set);
} else if (rsc_cmd == 'P') {
xmlNode *cmd = create_request(CRM_OP_REPROBE, NULL, host_uname,
CRM_SYSTEM_CRMD, crm_system_name, our_pid);
if (crm_ipc_send(crmd_channel, cmd, 0, 0, NULL) > 0) {
start_mainloop();
}
free_xml(cmd);
} else if (rsc_cmd == 'R') {
rc = refresh_lrm(crmd_channel, host_uname);
if (rc == pcmk_ok) {
start_mainloop();
}
} else if (rsc_cmd == 'D') {
xmlNode *msg_data = NULL;
if (rsc_id == NULL) {
CMD_ERR("Must supply a resource id with -r\n");
rc = -ENXIO;
goto bail;
}
if (rsc_type == NULL) {
CMD_ERR("You need to specify a resource type with -t");
rc = -ENXIO;
goto bail;
} else if (cib_conn == NULL) {
rc = -ENOTCONN;
goto bail;
}
msg_data = create_xml_node(NULL, rsc_type);
crm_xml_add(msg_data, XML_ATTR_ID, rsc_id);
rc = cib_conn->cmds->delete(cib_conn, XML_CIB_TAG_RESOURCES, msg_data, cib_options);
free_xml(msg_data);
} else {
CMD_ERR("Unknown command: %c\n", rsc_cmd);
}
bail:
if (cib_conn != NULL) {
cleanup_alloc_calculations(&data_set);
cib_conn->cmds->signoff(cib_conn);
cib_delete(cib_conn);
}
crm_xml_cleanup();
if (rc == -pcmk_err_no_quorum) {
CMD_ERR("Error performing operation: %s\n", pcmk_strerror(rc));
CMD_ERR("Try using -f\n");
} else if (rc != pcmk_ok) {
CMD_ERR("Error performing operation: %s\n", pcmk_strerror(rc));
}
+ qb_log_fini();
return rc;
}
diff --git a/tools/crm_ticket.c b/tools/crm_ticket.c
index ea3b80e98b..acf25d0312 100644
--- a/tools/crm_ticket.c
+++ b/tools/crm_ticket.c
@@ -1,977 +1,978 @@
/*
* Copyright (C) 2012 Gao,Yan <ygao@suse.com>
*
* 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 <crm/crm.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <crm/msg_xml.h>
#include <crm/common/xml.h>
#include <crm/common/ipc.h>
#include <crm/cib.h>
#include <crm/pengine/rules.h>
#include <crm/pengine/status.h>
#include <../pengine/pengine.h>
gboolean do_force = FALSE;
gboolean BE_QUIET = FALSE;
const char *ticket_id = NULL;
const char *attr_name = "granted";
const char *attr_value = NULL;
const char *attr_id = NULL;
const char *set_name = NULL;
const char *attr_default = NULL;
char ticket_cmd = 'S';
char *xml_file = NULL;
int cib_options = cib_sync_call;
extern void cleanup_alloc_calculations(pe_working_set_t * data_set);
#define CMD_ERR(fmt, args...) do { \
crm_warn(fmt, ##args); \
fprintf(stderr, fmt, ##args); \
} while(0)
static ticket_t *
find_ticket(const char *ticket_id , pe_working_set_t * data_set)
{
ticket_t *ticket = NULL;
ticket = g_hash_table_lookup(data_set->tickets, ticket_id);
return ticket;
}
static void
print_date(time_t time)
{
int lpc = 0;
char date_str[26];
asctime_r(localtime(&time), date_str);
for (; lpc < 26; lpc++) {
if (date_str[lpc] == '\n') {
date_str[lpc] = 0;
}
}
fprintf(stdout,"'%s'", date_str);
}
static int
print_ticket(ticket_t *ticket, gboolean raw, gboolean details)
{
if (raw) {
fprintf(stdout, "%s\n", ticket->id);
return pcmk_ok;
}
fprintf(stdout, "%s\t%s %s",
ticket->id, ticket->granted?"granted":"revoked",
ticket->standby?"[standby]":" ");
if (details && g_hash_table_size(ticket->state) > 0) {
GHashTableIter iter;
const char *name = NULL;
const char *value = NULL;
int lpc = 0;
fprintf(stdout, " (");
g_hash_table_iter_init(&iter, ticket->state);
while (g_hash_table_iter_next(&iter, (void **)&name, (void **)&value)) {
if (lpc > 0) {
fprintf(stdout, ", ");
}
fprintf(stdout, "%s=", name);
if (crm_str_eq(name, "last-granted", TRUE)
|| crm_str_eq(name, "expires", TRUE)) {
print_date(crm_parse_int(value, 0));
} else {
fprintf(stdout, "%s", value);
}
lpc++;
}
fprintf(stdout, ")\n");
} else {
if (ticket->last_granted > -1) {
fprintf(stdout, " last-granted=");
print_date(ticket->last_granted);
}
fprintf(stdout, "\n");
}
return pcmk_ok;
}
static int
print_ticket_list(pe_working_set_t * data_set, gboolean raw, gboolean details)
{
GHashTableIter iter;
ticket_t *ticket = NULL;
g_hash_table_iter_init(&iter, data_set->tickets);
while (g_hash_table_iter_next(&iter, NULL, (void **)&ticket)) {
print_ticket(ticket, raw, details);
}
return pcmk_ok;
}
static int
find_ticket_state(cib_t * the_cib, const char * ticket_id, xmlNode ** ticket_state_xml)
{
int offset = 0;
static int xpath_max = 1024;
int rc = pcmk_ok;
xmlNode *xml_search = NULL;
char *xpath_string = NULL;
CRM_ASSERT(ticket_state_xml != NULL);
*ticket_state_xml = NULL;
xpath_string = calloc(1, xpath_max);
offset +=
snprintf(xpath_string + offset, xpath_max - offset, "%s", "/cib/status/tickets");
if (ticket_id) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "/%s[@id=\"%s\"]",
XML_CIB_TAG_TICKET_STATE, ticket_id);
}
rc = the_cib->cmds->query(the_cib, xpath_string, &xml_search,
cib_sync_call | cib_scope_local | cib_xpath);
if (rc != pcmk_ok) {
goto bail;
}
crm_log_xml_debug(xml_search, "Match");
if (xml_has_children(xml_search)) {
if (ticket_id) {
fprintf(stdout, "Multiple ticket_states match ticket_id=%s\n", ticket_id);
}
*ticket_state_xml = xml_search;
} else {
*ticket_state_xml = xml_search;
}
bail:
free(xpath_string);
return rc;
}
static int
find_ticket_constraints(cib_t * the_cib, const char * ticket_id, xmlNode ** ticket_cons_xml)
{
int offset = 0;
static int xpath_max = 1024;
int rc = pcmk_ok;
xmlNode *xml_search = NULL;
char *xpath_string = NULL;
CRM_ASSERT(ticket_cons_xml != NULL);
*ticket_cons_xml = NULL;
xpath_string = calloc(1, xpath_max);
offset +=
snprintf(xpath_string + offset, xpath_max - offset, "%s/%s",
get_object_path(XML_CIB_TAG_CONSTRAINTS), XML_CONS_TAG_RSC_TICKET);
if (ticket_id) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "[@ticket=\"%s\"]",
ticket_id);
}
rc = the_cib->cmds->query(the_cib, xpath_string, &xml_search,
cib_sync_call | cib_scope_local | cib_xpath);
if (rc != pcmk_ok) {
goto bail;
}
crm_log_xml_debug(xml_search, "Match");
*ticket_cons_xml = xml_search;
bail:
free(xpath_string);
return rc;
}
static int
dump_ticket_xml(cib_t * the_cib, const char *ticket_id)
{
int rc = pcmk_ok;
xmlNode * state_xml = NULL;
rc = find_ticket_state(the_cib, ticket_id, &state_xml);
if (state_xml == NULL) {
return rc;
}
fprintf(stdout, "State XML:\n");
if (state_xml) {
char *state_xml_str = NULL;
state_xml_str = dump_xml_formatted(state_xml);
fprintf(stdout, "\n%s\n", crm_str(state_xml_str));
free_xml(state_xml);
free(state_xml_str);
}
return pcmk_ok;
}
static int
dump_constraints(cib_t * the_cib, const char * ticket_id)
{
int rc = pcmk_ok;
xmlNode * cons_xml = NULL;
char *cons_xml_str = NULL;
rc = find_ticket_constraints(the_cib, ticket_id, &cons_xml);
if (cons_xml == NULL) {
return rc;
}
cons_xml_str = dump_xml_formatted(cons_xml);
fprintf(stdout, "Constraints XML:\n\n%s\n", crm_str(cons_xml_str));
free_xml(cons_xml);
free(cons_xml_str);
return pcmk_ok;
}
static int
find_ticket_state_attr_legacy(cib_t * the_cib, const char *attr, const char *ticket_id, const char *set_type,
const char *set_name, const char *attr_id, const char *attr_name, char **value)
{
int offset = 0;
static int xpath_max = 1024;
int rc = pcmk_ok;
xmlNode *xml_search = NULL;
char *xpath_string = NULL;
CRM_ASSERT(value != NULL);
*value = NULL;
xpath_string = calloc(1, xpath_max);
offset +=
snprintf(xpath_string + offset, xpath_max - offset, "%s", "/cib/status/tickets");
if (set_type) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "/%s", set_type);
if (set_name) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "[@id=\"%s\"]", set_name);
}
}
offset += snprintf(xpath_string + offset, xpath_max - offset, "//nvpair[");
if (attr_id) {
offset += snprintf(xpath_string + offset, xpath_max - offset, "@id=\"%s\"", attr_id);
}
if (attr_name) {
const char *attr_prefix = NULL;
char *long_key = NULL;
if (crm_str_eq(attr_name, "granted", TRUE)) {
attr_prefix = "granted-ticket";
} else {
attr_prefix = attr_name;
}
long_key = crm_concat(attr_prefix, ticket_id, '-');
if (attr_id) {
offset += snprintf(xpath_string + offset, xpath_max - offset, " and ");
}
offset += snprintf(xpath_string + offset, xpath_max - offset, "@name=\"%s\"", long_key);
free(long_key);
}
offset += snprintf(xpath_string + offset, xpath_max - offset, "]");
rc = the_cib->cmds->query(the_cib, xpath_string, &xml_search,
cib_sync_call | cib_scope_local | cib_xpath);
if (rc != pcmk_ok) {
goto bail;
}
crm_log_xml_debug(xml_search, "Match");
if (xml_has_children(xml_search)) {
xmlNode *child = NULL;
rc = -EINVAL;
fprintf(stdout, "Multiple attributes match name=%s\n", attr_name);
for (child = __xml_first_child(xml_search); child != NULL; child = __xml_next(child)) {
fprintf(stdout, " Value: %s \t(id=%s)\n",
crm_element_value(child, XML_NVPAIR_ATTR_VALUE), ID(child));
}
} else {
const char *tmp = crm_element_value(xml_search, attr);
if (tmp) {
*value = strdup(tmp);
}
}
bail:
free(xpath_string);
free_xml(xml_search);
return rc;
}
static int
delete_ticket_state_attr_legacy(const char *ticket_id, const char *set_name, const char *attr_id,
const char *attr_name, cib_t * cib)
{
xmlNode *xml_obj = NULL;
int rc = pcmk_ok;
char *local_attr_id = NULL;
rc = find_ticket_state_attr_legacy(cib, XML_ATTR_ID, ticket_id, XML_TAG_ATTR_SETS, set_name, attr_id, attr_name,
&local_attr_id);
if (rc == -ENXIO) {
return pcmk_ok;
} else if (rc != pcmk_ok) {
return rc;
}
if (attr_id == NULL) {
attr_id = local_attr_id;
}
xml_obj = create_xml_node(NULL, XML_CIB_TAG_NVPAIR);
crm_xml_add(xml_obj, XML_ATTR_ID, attr_id);
/*crm_xml_add(xml_obj, XML_NVPAIR_ATTR_NAME, attr_name);*/
crm_log_xml_debug(xml_obj, "Delete");
rc = cib->cmds->delete(cib, XML_CIB_TAG_STATUS, xml_obj, cib_options);
if (rc == pcmk_ok) {
fprintf(stdout, "Deleted legacy %s state attribute: id=%s%s%s%s%s\n", ticket_id, local_attr_id,
set_name ? " set=" : "", set_name ? set_name : "",
attr_name ? " name=" : "", attr_name ? attr_name : "");
}
free_xml(xml_obj);
free(local_attr_id);
return rc;
}
static int
get_ticket_state_attr(const char *ticket_id, const char *attr_name, const char **attr_value, pe_working_set_t * data_set)
{
ticket_t *ticket = NULL;
CRM_ASSERT(attr_value != NULL);
*attr_value = NULL;
ticket = g_hash_table_lookup(data_set->tickets, ticket_id);
if (ticket == NULL) {
return -ENXIO;
}
*attr_value = g_hash_table_lookup(ticket->state, attr_name);
if (*attr_value == NULL) {
return -ENXIO;
}
return pcmk_ok;
}
static int
delete_ticket_state_attr(const char *ticket_id, const char *attr_name, cib_t * cib)
{
xmlNode *ticket_state_xml = NULL;
int rc = pcmk_ok;
rc = find_ticket_state(cib, ticket_id, &ticket_state_xml);
if (rc == -ENXIO) {
return pcmk_ok;
} else if (rc != pcmk_ok) {
return rc;
}
xml_remove_prop(ticket_state_xml, attr_name);
rc = cib->cmds->replace(cib, XML_CIB_TAG_STATUS, ticket_state_xml, cib_options);
if (rc == pcmk_ok) {
fprintf(stdout, "Deleted %s state attribute: %s%s\n", ticket_id,
attr_name ? " name=" : "", attr_name ? attr_name : "");
}
free_xml(ticket_state_xml);
return rc;
}
static int
set_ticket_state_attr(const char *ticket_id, const char *attr_name,
const char *attr_value, cib_t * cib)
{
int rc = pcmk_ok;
xmlNode *xml_top = NULL;
xmlNode *ticket_state_xml = NULL;
rc = find_ticket_state(cib, ticket_id, &ticket_state_xml);
if (rc == pcmk_ok) {
crm_debug("Found a match state for ticket: id=%s", ticket_id);
xml_top = ticket_state_xml;
} else if (rc != -ENXIO) {
return rc;
} else {
xmlNode *xml_obj = NULL;
xml_top = create_xml_node(NULL, XML_CIB_TAG_STATUS);
xml_obj = create_xml_node(xml_top, XML_CIB_TAG_TICKETS);
ticket_state_xml = create_xml_node(xml_obj, XML_CIB_TAG_TICKET_STATE);
crm_xml_add(ticket_state_xml, XML_ATTR_ID, ticket_id);
}
crm_xml_add(ticket_state_xml, attr_name, attr_value);
crm_log_xml_debug(xml_top, "Update");
rc = cib->cmds->modify(cib, XML_CIB_TAG_STATUS, xml_top, cib_options);
free_xml(xml_top);
return rc;
}
static int
delete_ticket_state(const char *ticket_id, cib_t * cib)
{
xmlNode *ticket_state_xml = NULL;
int rc = pcmk_ok;
rc = find_ticket_state(cib, ticket_id, &ticket_state_xml);
if (rc == -ENXIO) {
return pcmk_ok;
} else if (rc != pcmk_ok) {
return rc;
}
crm_log_xml_debug(ticket_state_xml, "Delete");
rc = cib->cmds->delete(cib, XML_CIB_TAG_STATUS, ticket_state_xml, cib_options);
if (rc == pcmk_ok) {
fprintf(stdout, "Cleaned up %s\n", ticket_id);
}
free_xml(ticket_state_xml);
return rc;
}
static gboolean
confirm(const char *ticket_id, const char *action)
{
gboolean rc = FALSE;
int offset = 0;
static int text_max = 1024;
char *warning = NULL;
const char * word = NULL;
warning = calloc(1, text_max);
if (safe_str_eq(action, "grant")) {
offset += snprintf(warning + offset, text_max - offset,
"The command cannot help you verify if '%s' is already granted elsewhere.\n",
ticket_id);
word = "to";
} else {
offset += snprintf(warning + offset, text_max - offset,
"Revoking '%s' can trigger the specified 'loss-policy'(s) relating to '%s'.\n\n",
ticket_id, ticket_id);
offset += snprintf(warning + offset, text_max - offset,
"You can check that with:\ncrm_ticket --ticket %s --constraints\n\n",
ticket_id);
offset += snprintf(warning + offset, text_max - offset,
"Otherwise before revoking '%s', you may want to make '%s' standby with:\ncrm_ticket --ticket %s --standby\n",
ticket_id, ticket_id, ticket_id);
word = "from";
}
fprintf(stdout, "%s\n", warning);
while (TRUE) {
char *answer = NULL;
answer = calloc(1, text_max);
fprintf(stdout, "Are you sure you want to %s '%s' %s this site now? (y/n)",
action, ticket_id, word);
rc = scanf("%s", answer);
if (strchr(answer, 'y') == answer || strchr(answer, 'Y') == answer) {
rc = TRUE;
free(answer);
goto bail;
} else if (strchr(answer, 'n') == answer || strchr(answer, 'N') == answer) {
rc = FALSE;
free(answer);
goto bail;
} else {
free(answer);
fprintf(stdout, "Please answer with y or n\n");
}
}
bail:
free(warning);
return rc;
}
/* *INDENT-OFF* */
static struct crm_option long_options[] = {
/* Top-level Options */
{"help", 0, 0, '?', "\t\tThis text"},
{"version", 0, 0, '$', "\t\tVersion information" },
{"verbose", 0, 0, 'V', "\t\tIncrease debug output"},
{"quiet", 0, 0, 'Q', "\t\tPrint only the value on stdout\n"},
{"ticket", 1, 0, 't', "\tTicket ID" },
{"-spacer-", 1, 0, '-', "\nQueries:"},
{"info", 0, 0, 'l', "\t\tDisplay the information of ticket(s)"},
{"details", 0, 0, 'L', "\t\tDisplay the details of ticket(s)"},
{"raw", 0, 0, 'w', "\t\tDisplay the IDs of ticket(s)"},
{"query-xml", 0, 0, 'q', "\tQuery the XML of ticket(s)"},
{"constraints",0, 0, 'c', "\tDisplay the rsc_ticket constraints that apply to ticket(s)"},
{"-spacer-", 1, 0, '-', "\nCommands:"},
{"grant", 0, 0, 'g', "\t\tGrant a ticket to this cluster site"},
{"revoke", 0, 0, 'r', "\t\tRevoke a ticket from this cluster site"},
{"standby", 0, 0, 's', "\t\tTell this cluster site this ticket is standby"},
{"activate", 0, 0, 'a', "\tTell this cluster site this ticket is active"},
{"-spacer-", 1, 0, '-', "\nAdvanced Commands:"},
{"get-attr", 1, 0, 'G', "\tDisplay the named attribute for a ticket"},
{"set-attr", 1, 0, 'S', "\tSet the named attribtue for a ticket"},
{"delete-attr",1, 0, 'D', "\tDelete the named attribute for a ticket"},
{"cleanup", 0, 0, 'C', "\t\tDelete all state of a ticket at this cluster site"},
{"-spacer-", 1, 0, '-', "\nAdditional Options:"},
{"attr-value", 1, 0, 'v', "\tAttribute value to use with -S"},
{"default", 1, 0, 'd', "\t(Advanced) The default attribute value to display if none is found. For use with -G"},
{"force", 0, 0, 'f', "\t\t(Advanced) Force the action to be performed"},
{"xml-file", 1, 0, 'x', NULL, 1},\
/* legacy options */
{"set-name", 1, 0, 'n', "\t(Advanced) ID of the instance_attributes object to change"},
{"nvpair", 1, 0, 'i', "\t(Advanced) ID of the nvpair object to change/delete"},
{"-spacer-", 1, 0, '-', "\nExamples:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "Display the info of tickets:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --info", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Display the detailed info of tickets:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --details", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Display the XML of 'ticketA':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --query-xml", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Display the rsc_ticket constraints that apply to 'ticketA':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --constraints", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Grant 'ticketA' to this cluster site:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --grant", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Revoke 'ticketA' from this cluster site:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --revoke", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Make 'ticketA' standby:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "The cluster site will treat a granted 'ticketA' as 'standby'."},
{"-spacer-", 1, 0, '-', "The dependent resources will be stopped or demoted gracefully without triggering loss-policies", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --standby", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Activate 'ticketA' from being standby:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --activate", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Get the value of the 'granted' attribute for 'ticketA':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --get-attr granted", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Set the value of the 'standby' attribute for 'ticketA':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --set-attr standby --attr-value true", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Delete the 'granted' attribute for 'ticketA':", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --delete-attr granted", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Erase the operation history of 'ticketA' at this cluster site:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "The cluster site will 'forget' the existing ticket state.", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_ticket --ticket ticketA --cleanup", pcmk_option_example},
{0, 0, 0, 0}
};
/* *INDENT-ON* */
int
main(int argc, char **argv)
{
pe_working_set_t data_set;
xmlNode *cib_xml_copy = NULL;
xmlNode *cib_constraints = NULL;
cib_t *cib_conn = NULL;
int rc = pcmk_ok;
int option_index = 0;
int argerr = 0;
int flag;
crm_log_init(NULL, LOG_CRIT, FALSE, FALSE, argc, argv, FALSE);
crm_set_options(NULL, "(query|command) [options]", long_options,
"Perform tasks related to cluster tickets.\nAllows ticket attributes to be queried, modified and deleted.\n");
if (argc < 2) {
crm_help('?', EX_USAGE);
}
while (1) {
flag = crm_get_option(argc, argv, &option_index);
if (flag == -1)
break;
switch (flag) {
case 'V':
crm_bump_log_level(argc, argv);
break;
case '$':
case '?':
crm_help(flag, EX_OK);
break;
case 'Q':
BE_QUIET = TRUE;
break;
case 't':
ticket_id = optarg;
break;
case 'l':
case 'L':
case 'w':
case 'q':
case 'c':
ticket_cmd = flag;
break;
case 'g':
case 'r':
case 's':
case 'a':
ticket_cmd = flag;
break;
case 'G':
case 'S':
case 'D':
attr_name = optarg;
ticket_cmd = flag;
break;
case 'C':
ticket_cmd = flag;
break;
case 'v':
attr_value = optarg;
break;
case 'd':
attr_default = optarg;
break;
case 'f':
do_force = TRUE;
break;
case 'x':
xml_file = strdup(optarg);
break;
case 'n':
set_name = optarg;
break;
case 'i':
attr_id = optarg;
break;
default:
CMD_ERR("Argument code 0%o (%c) is not (?yet?) supported\n", flag, flag);
++argerr;
break;
}
}
if (optind < argc && argv[optind] != NULL) {
CMD_ERR("non-option ARGV-elements: ");
while (optind < argc && argv[optind] != NULL) {
CMD_ERR("%s ", argv[optind++]);
++argerr;
}
CMD_ERR("\n");
}
if (optind > argc) {
++argerr;
}
if (argerr) {
crm_help('?', EX_USAGE);
}
set_working_set_defaults(&data_set);
cib_conn = cib_new();
if (cib_conn == NULL) {
rc = -ENOTCONN;
CMD_ERR("Error initiating the connection to the CIB service: %s\n",
pcmk_strerror(rc));
return rc;
}
rc = cib_conn->cmds->signon(cib_conn, crm_system_name, cib_command);
if (rc != pcmk_ok) {
CMD_ERR("Error signing on to the CIB service: %s\n", pcmk_strerror(rc));
return rc;
}
if (xml_file != NULL) {
cib_xml_copy = filename2xml(xml_file);
} else {
cib_xml_copy = get_cib_copy(cib_conn);
}
if (cli_config_update(&cib_xml_copy, NULL, FALSE) == FALSE) {
rc = -ENOKEY;
goto bail;
}
data_set.input = cib_xml_copy;
data_set.now = new_ha_date(TRUE);
cluster_status(&data_set);
/* For recording the tickets that are referenced in rsc_ticket constraints
* but have never been granted yet. */
cib_constraints = get_object_root(XML_CIB_TAG_CONSTRAINTS, data_set.input);
unpack_constraints(cib_constraints, &data_set);
if (ticket_cmd == 'l' || ticket_cmd == 'L' || ticket_cmd == 'w') {
gboolean raw = FALSE;
gboolean details = FALSE;
rc = pcmk_ok;
if (ticket_cmd == 'L') {
details = TRUE;
} else if (ticket_cmd == 'w') {
raw = TRUE;
}
if (ticket_id) {
ticket_t *ticket = find_ticket(ticket_id, &data_set);
if (ticket == NULL) {
rc = -ENXIO;
goto bail;
}
rc = print_ticket(ticket, raw, details);
} else {
rc = print_ticket_list(&data_set, raw, details);
}
} else if (ticket_cmd == 'q') {
rc = dump_ticket_xml(cib_conn, ticket_id);
} else if (ticket_cmd == 'c') {
rc = dump_constraints(cib_conn, ticket_id);
} else if (ticket_cmd == 'G') {
const char *value = NULL;
if (ticket_id == NULL) {
CMD_ERR("Must supply a ticket id with -t\n");
rc = -ENXIO;
goto bail;
}
rc = get_ticket_state_attr(ticket_id, attr_name, &value, &data_set);
if (rc == pcmk_ok) {
fprintf(stdout, "%s\n", value);
} else if (rc == -ENXIO && attr_default) {
fprintf(stdout, "%s\n", attr_default);
rc = pcmk_ok;
}
} else if (ticket_cmd == 'S'
|| ticket_cmd == 'g' || ticket_cmd == 'r'
|| ticket_cmd == 's' || ticket_cmd == 'a') {
gboolean is_granting = FALSE;
if (ticket_id == NULL) {
CMD_ERR("Must supply a ticket id with -t\n");
rc = -ENXIO;
goto bail;
}
if (ticket_cmd == 'g') {
attr_name = "granted";
attr_value = "true";
} else if (ticket_cmd == 'r') {
attr_name = "granted";
attr_value = "false";
} else if (ticket_cmd == 's') {
attr_name = "standby";
attr_value = "true";
} else if (ticket_cmd == 'a') {
attr_name = "standby";
attr_value = "false";
}
if (attr_value == NULL || strlen(attr_value) == 0) {
CMD_ERR("You need to supply a value with the -v option\n");
rc = -EINVAL;
goto bail;
}
if (safe_str_eq(attr_name, "granted") && do_force == FALSE) {
if (crm_is_true(attr_value) && confirm(ticket_id, "grant") == FALSE) {
CMD_ERR("Cancelled\n");
rc = pcmk_ok;
goto bail;
} else if (crm_is_true(attr_value) == FALSE && confirm(ticket_id, "revoke") == FALSE) {
CMD_ERR("Cancelled\n");
rc = pcmk_ok;
goto bail;
}
}
if (safe_str_eq(attr_name, "granted") && crm_is_true(attr_value)) {
ticket_t *ticket = find_ticket(ticket_id, &data_set);
if (ticket == NULL || ticket->granted == FALSE) {
is_granting = TRUE;
}
}
rc = set_ticket_state_attr(ticket_id, attr_name, attr_value, cib_conn);
delete_ticket_state_attr_legacy(ticket_id, set_name, attr_id, attr_name, cib_conn);
if(rc != pcmk_ok) {
goto bail;
}
if (is_granting == TRUE) {
set_ticket_state_attr(ticket_id, "last-granted", crm_itoa(time(NULL)), cib_conn);
delete_ticket_state_attr_legacy(ticket_id, set_name, attr_id, "last-granted", cib_conn);
}
} else if (ticket_cmd == 'D') {
if (ticket_id == NULL) {
CMD_ERR("Must supply a ticket id with -t\n");
rc = -ENXIO;
goto bail;
}
if (safe_str_eq(attr_name, "granted") && do_force == FALSE
&& confirm(ticket_id, "revoke") == FALSE) {
CMD_ERR("Cancelled\n");
rc = pcmk_ok;
goto bail;
}
delete_ticket_state_attr_legacy(ticket_id, set_name, attr_id, attr_name, cib_conn);
rc = delete_ticket_state_attr(ticket_id, attr_name, cib_conn);
} else if (ticket_cmd == 'C') {
if (ticket_id == NULL) {
CMD_ERR("Must supply a ticket id with -t\n");
rc = -ENXIO;
goto bail;
}
if (do_force == FALSE) {
ticket_t *ticket = NULL;
ticket = find_ticket(ticket_id, &data_set);
if (ticket == NULL) {
rc = -ENXIO;
goto bail;
}
if (ticket->granted && confirm(ticket_id, "revoke") == FALSE) {
CMD_ERR("Cancelled\n");
rc = pcmk_ok;
goto bail;
}
}
rc = delete_ticket_state(ticket_id, cib_conn);
} else {
CMD_ERR("Unknown command: %c\n", ticket_cmd);
}
bail:
if (cib_conn != NULL) {
cleanup_alloc_calculations(&data_set);
cib_conn->cmds->signoff(cib_conn);
cib_delete(cib_conn);
}
crm_xml_cleanup();
if (rc == -pcmk_err_no_quorum) {
CMD_ERR("Error performing operation: %s\n", pcmk_strerror(rc));
CMD_ERR("Try using -f\n");
} else if (rc != pcmk_ok) {
CMD_ERR("Error performing operation: %s\n", pcmk_strerror(rc));
}
+ qb_log_fini();
return rc;
}

File Metadata

Mime Type
text/x-diff
Expires
Fri, Sep 5, 9:21 AM (9 h, 11 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2308640
Default Alt Text
(128 KB)

Event Timeline