root/lib/pacemaker/pcmk_injections.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. inject_transient_attr
  2. pcmk__inject_failcount
  3. create_node_entry
  4. create_op
  5. pcmk__inject_action_result
  6. pcmk__inject_node
  7. pcmk__inject_node_state_change
  8. find_resource_xml
  9. pcmk__inject_resource_history
  10. find_ticket_state
  11. set_ticket_state_attr
  12. inject_action
  13. pcmk__inject_scheduler_input
  14. pcmk_free_injections

   1 /*
   2  * Copyright 2009-2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <stdio.h>
  13 #include <unistd.h>
  14 #include <stdlib.h>
  15 
  16 #include <sys/stat.h>
  17 #include <sys/param.h>
  18 #include <sys/types.h>
  19 #include <dirent.h>
  20 
  21 #include <crm/crm.h>
  22 #include <crm/lrmd.h>           // lrmd_event_data_t, lrmd_free_event()
  23 #include <crm/cib.h>
  24 #include <crm/cib/internal.h>
  25 #include <crm/common/util.h>
  26 #include <crm/common/iso8601.h>
  27 #include <crm/common/xml_internal.h>
  28 #include <crm/lrmd_internal.h>
  29 #include <crm/pengine/status.h>
  30 #include <pacemaker-internal.h>
  31 
  32 #include "libpacemaker_private.h"
  33 
  34 bool pcmk__simulate_node_config = false;
  35 
  36 #define XPATH_NODE_CONFIG   "//" XML_CIB_TAG_NODE "[@uname='%s']"
  37 #define XPATH_NODE_STATE    "//" XML_CIB_TAG_STATE "[@uname='%s']"
  38 #define XPATH_RSC_HISTORY   XPATH_NODE_STATE "//" \
  39                             XML_LRM_TAG_RESOURCE "[@id='%s']"
  40 
  41 
  42 /*!
  43  * \internal
  44  * \brief Inject a fictitious transient node attribute into scheduler input
  45  *
  46  * \param[in] out       Output object for displaying error messages
  47  * \param[in] cib_node  node_state XML to inject attribute into
  48  * \param[in] name      Transient node attribute name to inject
  49  * \param[in] value     Transient node attribute value to inject
  50  */
  51 static void
  52 inject_transient_attr(pcmk__output_t *out, xmlNode *cib_node,
     /* [previous][next][first][last][top][bottom][index][help] */
  53                       const char *name, const char *value)
  54 {
  55     xmlNode *attrs = NULL;
  56     xmlNode *instance_attrs = NULL;
  57     const char *node_uuid = ID(cib_node);
  58 
  59     out->message(out, "inject-attr", name, value, cib_node);
  60 
  61     attrs = first_named_child(cib_node, XML_TAG_TRANSIENT_NODEATTRS);
  62     if (attrs == NULL) {
  63         attrs = create_xml_node(cib_node, XML_TAG_TRANSIENT_NODEATTRS);
  64         crm_xml_add(attrs, XML_ATTR_ID, node_uuid);
  65     }
  66 
  67     instance_attrs = first_named_child(attrs, XML_TAG_ATTR_SETS);
  68     if (instance_attrs == NULL) {
  69         instance_attrs = create_xml_node(attrs, XML_TAG_ATTR_SETS);
  70         crm_xml_add(instance_attrs, XML_ATTR_ID, node_uuid);
  71     }
  72 
  73     crm_create_nvpair_xml(instance_attrs, NULL, name, value);
  74 }
  75 
  76 /*!
  77  * \internal
  78  * \brief Inject a fictitious fail count into a scheduler input
  79  *
  80  * \param[in] out          Output object for displaying error messages
  81  * \param[in] cib_node     Node state XML to inject into
  82  * \param[in] resource     ID of resource for fail count to inject
  83  * \param[in] task         Action name for fail count to inject
  84  * \param[in] interval_ms  Action interval (in milliseconds) for fail count
  85  * \param[in] rc           Action result for fail count to inject (if 0, or 7
  86  *                         when interval_ms is 0, nothing will be injected)
  87  */
  88 void
  89 pcmk__inject_failcount(pcmk__output_t *out, xmlNode *cib_node,
     /* [previous][next][first][last][top][bottom][index][help] */
  90                        const char *resource, const char *task,
  91                        guint interval_ms, int rc)
  92 {
  93     if (rc == 0) {
  94         return;
  95 
  96     } else if ((rc == 7) && (interval_ms == 0)) {
  97         return;
  98 
  99     } else {
 100         char *name = NULL;
 101         char *now = pcmk__ttoa(time(NULL));
 102 
 103         name = pcmk__failcount_name(resource, task, interval_ms);
 104         inject_transient_attr(out, cib_node, name, "value++");
 105         free(name);
 106 
 107         name = pcmk__lastfailure_name(resource, task, interval_ms);
 108         inject_transient_attr(out, cib_node, name, now);
 109         free(name);
 110 
 111         free(now);
 112     }
 113 }
 114 
 115 /*!
 116  * \internal
 117  * \brief Create a CIB configuration entry for a fictitious node
 118  *
 119  * \param[in] cib_conn  CIB object to use
 120  * \param[in] node      Node name to use
 121  */
 122 static void
 123 create_node_entry(cib_t *cib_conn, const char *node)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125     int rc = pcmk_ok;
 126     char *xpath = crm_strdup_printf(XPATH_NODE_CONFIG, node);
 127 
 128     rc = cib_conn->cmds->query(cib_conn, xpath, NULL,
 129                                cib_xpath|cib_sync_call|cib_scope_local);
 130 
 131     if (rc == -ENXIO) { // Only add if not already existing
 132         xmlNode *cib_object = create_xml_node(NULL, XML_CIB_TAG_NODE);
 133 
 134         crm_xml_add(cib_object, XML_ATTR_ID, node); // Use node name as ID
 135         crm_xml_add(cib_object, XML_ATTR_UNAME, node);
 136         cib_conn->cmds->create(cib_conn, XML_CIB_TAG_NODES, cib_object,
 137                                cib_sync_call|cib_scope_local);
 138         /* Not bothering with subsequent query to see if it exists,
 139            we'll bomb out later in the call to query_node_uuid()... */
 140 
 141         free_xml(cib_object);
 142     }
 143 
 144     free(xpath);
 145 }
 146 
 147 /*!
 148  * \internal
 149  * \brief Synthesize a fake executor event for an action
 150  *
 151  * \param[in] cib_resource  XML for any existing resource action history
 152  * \param[in] task          Name of action to synthesize
 153  * \param[in] interval_ms   Interval of action to synthesize
 154  * \param[in] outcome       Result of action to synthesize
 155  *
 156  * \return Newly allocated executor event
 157  * \note It is the caller's responsibility to free the result with
 158  *       lrmd_free_event().
 159  */
 160 static lrmd_event_data_t *
 161 create_op(xmlNode *cib_resource, const char *task, guint interval_ms,
     /* [previous][next][first][last][top][bottom][index][help] */
 162           int outcome)
 163 {
 164     lrmd_event_data_t *op = NULL;
 165     xmlNode *xop = NULL;
 166 
 167     op = lrmd_new_event(ID(cib_resource), task, interval_ms);
 168     lrmd__set_result(op, outcome, PCMK_EXEC_DONE, "Simulated action result");
 169     op->params = NULL; // Not needed for simulation purposes
 170     op->t_run = (unsigned int) time(NULL);
 171     op->t_rcchange = op->t_run;
 172 
 173     // Use a call ID higher than any existing history entries
 174     op->call_id = 0;
 175     for (xop = pcmk__xe_first_child(cib_resource); xop != NULL;
 176          xop = pcmk__xe_next(xop)) {
 177 
 178         int tmp = 0;
 179 
 180         crm_element_value_int(xop, XML_LRM_ATTR_CALLID, &tmp);
 181         if (tmp > op->call_id) {
 182             op->call_id = tmp;
 183         }
 184     }
 185     op->call_id++;
 186 
 187     return op;
 188 }
 189 
 190 /*!
 191  * \internal
 192  * \brief Inject a fictitious resource history entry into a scheduler input
 193  *
 194  * \param[in] cib_resource  Resource history XML to inject entry into
 195  * \param[in] op            Action result to inject
 196  * \param[in] target_rc     Expected result for action to inject
 197  *
 198  * \return XML of injected resource history entry
 199  */
 200 xmlNode *
 201 pcmk__inject_action_result(xmlNode *cib_resource, lrmd_event_data_t *op,
     /* [previous][next][first][last][top][bottom][index][help] */
 202                            int target_rc)
 203 {
 204     return pcmk__create_history_xml(cib_resource, op, CRM_FEATURE_SET,
 205                                     target_rc, NULL, crm_system_name);
 206 }
 207 
 208 /*!
 209  * \internal
 210  * \brief Inject a fictitious node into a scheduler input
 211  *
 212  * \param[in] cib_conn  Scheduler input CIB to inject node into
 213  * \param[in] node      Name of node to inject
 214  * \param[in] uuid      UUID of node to inject
 215  *
 216  * \return XML of node_state entry for new node
 217  * \note If the global pcmk__simulate_node_config has been set to true, a
 218  *       node entry in the configuration section will be added, as well as a
 219  *       node state entry in the status section.
 220  */
 221 xmlNode *
 222 pcmk__inject_node(cib_t *cib_conn, const char *node, const char *uuid)
     /* [previous][next][first][last][top][bottom][index][help] */
 223 {
 224     int rc = pcmk_ok;
 225     xmlNode *cib_object = NULL;
 226     char *xpath = crm_strdup_printf(XPATH_NODE_STATE, node);
 227 
 228     if (pcmk__simulate_node_config) {
 229         create_node_entry(cib_conn, node);
 230     }
 231 
 232     rc = cib_conn->cmds->query(cib_conn, xpath, &cib_object,
 233                                cib_xpath|cib_sync_call|cib_scope_local);
 234 
 235     if ((cib_object != NULL) && (ID(cib_object) == NULL)) {
 236         crm_err("Detected multiple node_state entries for xpath=%s, bailing",
 237                 xpath);
 238         crm_log_xml_warn(cib_object, "Duplicates");
 239         free(xpath);
 240         crm_exit(CRM_EX_SOFTWARE);
 241         return NULL; // not reached, but makes static analysis happy
 242     }
 243 
 244     if (rc == -ENXIO) {
 245         char *found_uuid = NULL;
 246 
 247         if (uuid == NULL) {
 248             query_node_uuid(cib_conn, node, &found_uuid, NULL);
 249         } else {
 250             found_uuid = strdup(uuid);
 251         }
 252 
 253         cib_object = create_xml_node(NULL, XML_CIB_TAG_STATE);
 254         crm_xml_add(cib_object, XML_ATTR_UUID, found_uuid);
 255         crm_xml_add(cib_object, XML_ATTR_UNAME, node);
 256         cib_conn->cmds->create(cib_conn, XML_CIB_TAG_STATUS, cib_object,
 257                                cib_sync_call|cib_scope_local);
 258         free_xml(cib_object);
 259         free(found_uuid);
 260 
 261         rc = cib_conn->cmds->query(cib_conn, xpath, &cib_object,
 262                                    cib_xpath|cib_sync_call|cib_scope_local);
 263         crm_trace("Injecting node state for %s (rc=%d)", node, rc);
 264     }
 265 
 266     free(xpath);
 267     CRM_ASSERT(rc == pcmk_ok);
 268     return cib_object;
 269 }
 270 
 271 /*!
 272  * \internal
 273  * \brief Inject a fictitious node state change into a scheduler input
 274  *
 275  * \param[in] cib_conn  Scheduler input CIB to inject into
 276  * \param[in] node      Name of node to inject change for
 277  * \param[in] up        If true, change state to online, otherwise offline
 278  *
 279  * \return XML of changed (or added) node state entry
 280  */
 281 xmlNode *
 282 pcmk__inject_node_state_change(cib_t *cib_conn, const char *node, bool up)
     /* [previous][next][first][last][top][bottom][index][help] */
 283 {
 284     xmlNode *cib_node = pcmk__inject_node(cib_conn, node, NULL);
 285 
 286     if (up) {
 287         pcmk__xe_set_props(cib_node,
 288                            XML_NODE_IN_CLUSTER, XML_BOOLEAN_YES,
 289                            XML_NODE_IS_PEER, ONLINESTATUS,
 290                            XML_NODE_JOIN_STATE, CRMD_JOINSTATE_MEMBER,
 291                            XML_NODE_EXPECTED, CRMD_JOINSTATE_MEMBER,
 292                            NULL);
 293     } else {
 294         pcmk__xe_set_props(cib_node,
 295                            XML_NODE_IN_CLUSTER, XML_BOOLEAN_NO,
 296                            XML_NODE_IS_PEER, OFFLINESTATUS,
 297                            XML_NODE_JOIN_STATE, CRMD_JOINSTATE_DOWN,
 298                            XML_NODE_EXPECTED, CRMD_JOINSTATE_DOWN,
 299                            NULL);
 300     }
 301     crm_xml_add(cib_node, XML_ATTR_ORIGIN, crm_system_name);
 302     return cib_node;
 303 }
 304 
 305 /*!
 306  * \internal
 307  * \brief Check whether a node has history for a given resource
 308  *
 309  * \param[in] cib_node  Node state XML to check
 310  * \param[in] resource  Resource name to check for
 311  *
 312  * \return Resource's lrm_resource XML entry beneath \p cib_node if found,
 313  *         otherwise NULL
 314  */
 315 static xmlNode *
 316 find_resource_xml(xmlNode *cib_node, const char *resource)
     /* [previous][next][first][last][top][bottom][index][help] */
 317 {
 318     const char *node = crm_element_value(cib_node, XML_ATTR_UNAME);
 319     char *xpath = crm_strdup_printf(XPATH_RSC_HISTORY, node, resource);
 320     xmlNode *match = get_xpath_object(xpath, cib_node, LOG_TRACE);
 321 
 322     free(xpath);
 323     return match;
 324 }
 325 
 326 /*!
 327  * \internal
 328  * \brief Inject a resource history element into a scheduler input
 329  *
 330  * \param[in] out       Output object for displaying error messages
 331  * \param[in] cib_node  Node state XML to inject resource history entry into
 332  * \param[in] resource  ID (in configuration) of resource to inject
 333  * \param[in] lrm_name  ID of resource as used in history (e.g. clone instance)
 334  * \param[in] rclass    Resource agent class of resource to inject
 335  * \param[in] rtype     Resource agent type of resource to inject
 336  * \param[in] rprovider Resource agent provider of resource to inject
 337  *
 338  * \return XML of injected resource history element
 339  * \note If a history element already exists under either \p resource or
 340  *       \p lrm_name, this will return it rather than injecting a new one.
 341  */
 342 xmlNode *
 343 pcmk__inject_resource_history(pcmk__output_t *out, xmlNode *cib_node,
     /* [previous][next][first][last][top][bottom][index][help] */
 344                               const char *resource, const char *lrm_name,
 345                               const char *rclass, const char *rtype,
 346                               const char *rprovider)
 347 {
 348     xmlNode *lrm = NULL;
 349     xmlNode *container = NULL;
 350     xmlNode *cib_resource = NULL;
 351 
 352     cib_resource = find_resource_xml(cib_node, resource);
 353     if (cib_resource != NULL) {
 354         /* If an existing LRM history entry uses the resource name,
 355          * continue using it, even if lrm_name is different.
 356          */
 357         return cib_resource;
 358     }
 359 
 360     // Check for history entry under preferred name
 361     if (strcmp(resource, lrm_name) != 0) {
 362         cib_resource = find_resource_xml(cib_node, lrm_name);
 363         if (cib_resource != NULL) {
 364             return cib_resource;
 365         }
 366     }
 367 
 368     if ((rclass == NULL) || (rtype == NULL)) {
 369         // @TODO query configuration for class, provider, type
 370         out->err(out, "Resource %s not found in the status section of %s."
 371                  "  Please supply the class and type to continue", resource, ID(cib_node));
 372         return NULL;
 373 
 374     } else if (!pcmk__strcase_any_of(rclass,
 375                                      PCMK_RESOURCE_CLASS_OCF,
 376                                      PCMK_RESOURCE_CLASS_STONITH,
 377                                      PCMK_RESOURCE_CLASS_SERVICE,
 378                                      PCMK_RESOURCE_CLASS_UPSTART,
 379                                      PCMK_RESOURCE_CLASS_SYSTEMD,
 380                                      PCMK_RESOURCE_CLASS_LSB, NULL)) {
 381         out->err(out, "Invalid class for %s: %s", resource, rclass);
 382         return NULL;
 383 
 384     } else if (pcmk_is_set(pcmk_get_ra_caps(rclass), pcmk_ra_cap_provider)
 385                && (rprovider == NULL)) {
 386         // @TODO query configuration for provider
 387         out->err(out, "Please specify the provider for resource %s", resource);
 388         return NULL;
 389     }
 390 
 391     crm_info("Injecting new resource %s into node state '%s'",
 392              lrm_name, ID(cib_node));
 393 
 394     lrm = first_named_child(cib_node, XML_CIB_TAG_LRM);
 395     if (lrm == NULL) {
 396         const char *node_uuid = ID(cib_node);
 397 
 398         lrm = create_xml_node(cib_node, XML_CIB_TAG_LRM);
 399         crm_xml_add(lrm, XML_ATTR_ID, node_uuid);
 400     }
 401 
 402     container = first_named_child(lrm, XML_LRM_TAG_RESOURCES);
 403     if (container == NULL) {
 404         container = create_xml_node(lrm, XML_LRM_TAG_RESOURCES);
 405     }
 406 
 407     cib_resource = create_xml_node(container, XML_LRM_TAG_RESOURCE);
 408 
 409     // If we're creating a new entry, use the preferred name
 410     crm_xml_add(cib_resource, XML_ATTR_ID, lrm_name);
 411 
 412     crm_xml_add(cib_resource, XML_AGENT_ATTR_CLASS, rclass);
 413     crm_xml_add(cib_resource, XML_AGENT_ATTR_PROVIDER, rprovider);
 414     crm_xml_add(cib_resource, XML_ATTR_TYPE, rtype);
 415 
 416     return cib_resource;
 417 }
 418 
 419 static int
 420 find_ticket_state(pcmk__output_t *out, cib_t *the_cib, const char *ticket_id,
     /* [previous][next][first][last][top][bottom][index][help] */
 421                   xmlNode **ticket_state_xml)
 422 {
 423     int rc = pcmk_ok;
 424     xmlNode *xml_search = NULL;
 425 
 426     GString *xpath = g_string_sized_new(256);
 427 
 428     CRM_ASSERT(ticket_state_xml != NULL);
 429     *ticket_state_xml = NULL;
 430 
 431     g_string_append(xpath,
 432                     "/" XML_TAG_CIB "/" XML_CIB_TAG_STATUS
 433                     "/" XML_CIB_TAG_TICKETS);
 434 
 435     if (ticket_id) {
 436         pcmk__g_strcat(xpath,
 437                        "/" XML_CIB_TAG_TICKET_STATE
 438                        "[@" XML_ATTR_ID "=\"", ticket_id, "\"]", NULL);
 439     }
 440     rc = the_cib->cmds->query(the_cib, (const char *) xpath->str, &xml_search,
 441                               cib_sync_call|cib_scope_local|cib_xpath);
 442     g_string_free(xpath, TRUE);
 443 
 444     if (rc != pcmk_ok) {
 445         return rc;
 446     }
 447 
 448     crm_log_xml_debug(xml_search, "Match");
 449     if (xml_has_children(xml_search) && (ticket_id != NULL)) {
 450         out->err(out, "Multiple ticket_states match ticket_id=%s", ticket_id);
 451     }
 452     *ticket_state_xml = xml_search;
 453 
 454     return rc;
 455 }
 456 
 457 /*!
 458  * \internal
 459  * \brief Inject a ticket attribute into ticket state
 460  *
 461  * \param[in] out          Output object for displaying error messages
 462  * \param[in] ticket_id    Ticket whose state should be changed
 463  * \param[in] attr_name    Ticket attribute name to inject
 464  * \param[in] attr_value   Boolean value of ticket attribute to inject
 465  * \param[in] cib          CIB object to use
 466  *
 467  * \return Standard Pacemaker return code
 468  */
 469 static int
 470 set_ticket_state_attr(pcmk__output_t *out, const char *ticket_id,
     /* [previous][next][first][last][top][bottom][index][help] */
 471                       const char *attr_name, bool attr_value, cib_t *cib)
 472 {
 473     int rc = pcmk_rc_ok;
 474     xmlNode *xml_top = NULL;
 475     xmlNode *ticket_state_xml = NULL;
 476 
 477     // Check for an existing ticket state entry
 478     rc = find_ticket_state(out, cib, ticket_id, &ticket_state_xml);
 479     rc = pcmk_legacy2rc(rc);
 480 
 481     if (rc == pcmk_rc_ok) { // Ticket state found, use it
 482         crm_debug("Injecting attribute into existing ticket state %s",
 483                   ticket_id);
 484         xml_top = ticket_state_xml;
 485 
 486     } else if (rc == ENXIO) { // No ticket state, create it
 487         xmlNode *xml_obj = NULL;
 488 
 489         xml_top = create_xml_node(NULL, XML_CIB_TAG_STATUS);
 490         xml_obj = create_xml_node(xml_top, XML_CIB_TAG_TICKETS);
 491         ticket_state_xml = create_xml_node(xml_obj, XML_CIB_TAG_TICKET_STATE);
 492         crm_xml_add(ticket_state_xml, XML_ATTR_ID, ticket_id);
 493 
 494     } else { // Error
 495         return rc;
 496     }
 497 
 498     // Add the attribute to the ticket state
 499     pcmk__xe_set_bool_attr(ticket_state_xml, attr_name, attr_value);
 500     crm_log_xml_debug(xml_top, "Update");
 501 
 502     // Commit the change to the CIB
 503     rc = cib->cmds->modify(cib, XML_CIB_TAG_STATUS, xml_top,
 504                            cib_sync_call|cib_scope_local);
 505     rc = pcmk_legacy2rc(rc);
 506 
 507     free_xml(xml_top);
 508     return rc;
 509 }
 510 
 511 /*!
 512  * \internal
 513  * \brief Inject a fictitious action into the cluster
 514  *
 515  * \param[in] out       Output object for displaying error messages
 516  * \param[in] spec      Action specification to inject
 517  * \param[in] cib       CIB object for scheduler input
 518  * \param[in] data_set  Cluster working set
 519  */
 520 static void
 521 inject_action(pcmk__output_t *out, const char *spec, cib_t *cib,
     /* [previous][next][first][last][top][bottom][index][help] */
 522               pe_working_set_t *data_set)
 523 {
 524     int rc;
 525     int outcome = PCMK_OCF_OK;
 526     guint interval_ms = 0;
 527 
 528     char *key = NULL;
 529     char *node = NULL;
 530     char *task = NULL;
 531     char *resource = NULL;
 532 
 533     const char *rtype = NULL;
 534     const char *rclass = NULL;
 535     const char *rprovider = NULL;
 536 
 537     xmlNode *cib_op = NULL;
 538     xmlNode *cib_node = NULL;
 539     xmlNode *cib_resource = NULL;
 540     pe_resource_t *rsc = NULL;
 541     lrmd_event_data_t *op = NULL;
 542 
 543     out->message(out, "inject-spec", spec);
 544 
 545     key = calloc(1, strlen(spec) + 1);
 546     node = calloc(1, strlen(spec) + 1);
 547     rc = sscanf(spec, "%[^@]@%[^=]=%d", key, node, &outcome);
 548     if (rc != 3) {
 549         out->err(out, "Invalid operation spec: %s.  Only found %d fields",
 550                  spec, rc);
 551         goto done;
 552     }
 553 
 554     parse_op_key(key, &resource, &task, &interval_ms);
 555 
 556     rsc = pe_find_resource(data_set->resources, resource);
 557     if (rsc == NULL) {
 558         out->err(out, "Invalid resource name: %s", resource);
 559         goto done;
 560     }
 561 
 562     rclass = crm_element_value(rsc->xml, XML_AGENT_ATTR_CLASS);
 563     rtype = crm_element_value(rsc->xml, XML_ATTR_TYPE);
 564     rprovider = crm_element_value(rsc->xml, XML_AGENT_ATTR_PROVIDER);
 565 
 566     cib_node = pcmk__inject_node(cib, node, NULL);
 567     CRM_ASSERT(cib_node != NULL);
 568 
 569     pcmk__inject_failcount(out, cib_node, resource, task, interval_ms, outcome);
 570 
 571     cib_resource = pcmk__inject_resource_history(out, cib_node,
 572                                                  resource, resource,
 573                                                  rclass, rtype, rprovider);
 574     CRM_ASSERT(cib_resource != NULL);
 575 
 576     op = create_op(cib_resource, task, interval_ms, outcome);
 577     CRM_ASSERT(op != NULL);
 578 
 579     cib_op = pcmk__inject_action_result(cib_resource, op, 0);
 580     CRM_ASSERT(cib_op != NULL);
 581     lrmd_free_event(op);
 582 
 583     rc = cib->cmds->modify(cib, XML_CIB_TAG_STATUS, cib_node,
 584                            cib_sync_call|cib_scope_local);
 585     CRM_ASSERT(rc == pcmk_ok);
 586 
 587 done:
 588     free(task);
 589     free(node);
 590     free(key);
 591 }
 592 
 593 /*!
 594  * \internal
 595  * \brief Inject fictitious scheduler inputs
 596  *
 597  * \param[in] data_set    Cluster working set
 598  * \param[in] cib         CIB object for scheduler input to modify
 599  * \param[in] injections  Injections to apply
 600  */
 601 void
 602 pcmk__inject_scheduler_input(pe_working_set_t *data_set, cib_t *cib,
     /* [previous][next][first][last][top][bottom][index][help] */
 603                              pcmk_injections_t *injections)
 604 {
 605     int rc = pcmk_ok;
 606     GList *iter = NULL;
 607     xmlNode *cib_node = NULL;
 608     pcmk__output_t *out = data_set->priv;
 609 
 610     out->message(out, "inject-modify-config", injections->quorum,
 611                  injections->watchdog);
 612     if (injections->quorum != NULL) {
 613         xmlNode *top = create_xml_node(NULL, XML_TAG_CIB);
 614 
 615         /* crm_xml_add(top, XML_ATTR_DC_UUID, dc_uuid);      */
 616         crm_xml_add(top, XML_ATTR_HAVE_QUORUM, injections->quorum);
 617 
 618         rc = cib->cmds->modify(cib, NULL, top, cib_sync_call|cib_scope_local);
 619         CRM_ASSERT(rc == pcmk_ok);
 620     }
 621 
 622     if (injections->watchdog != NULL) {
 623         rc = cib__update_node_attr(out, cib, cib_sync_call|cib_scope_local,
 624                                    XML_CIB_TAG_CRMCONFIG, NULL, NULL, NULL, NULL,
 625                                    XML_ATTR_HAVE_WATCHDOG, injections->watchdog,
 626                                    NULL, NULL);
 627         CRM_ASSERT(rc == pcmk_rc_ok);
 628     }
 629 
 630     for (iter = injections->node_up; iter != NULL; iter = iter->next) {
 631         char *node = (char *) iter->data;
 632 
 633         out->message(out, "inject-modify-node", "Online", node);
 634 
 635         cib_node = pcmk__inject_node_state_change(cib, node, true);
 636         CRM_ASSERT(cib_node != NULL);
 637 
 638         rc = cib->cmds->modify(cib, XML_CIB_TAG_STATUS, cib_node,
 639                                cib_sync_call|cib_scope_local);
 640         CRM_ASSERT(rc == pcmk_ok);
 641         free_xml(cib_node);
 642     }
 643 
 644     for (iter = injections->node_down; iter != NULL; iter = iter->next) {
 645         char *node = (char *) iter->data;
 646         char *xpath = NULL;
 647 
 648         out->message(out, "inject-modify-node", "Offline", node);
 649 
 650         cib_node = pcmk__inject_node_state_change(cib, node, false);
 651         CRM_ASSERT(cib_node != NULL);
 652 
 653         rc = cib->cmds->modify(cib, XML_CIB_TAG_STATUS, cib_node,
 654                                cib_sync_call|cib_scope_local);
 655         CRM_ASSERT(rc == pcmk_ok);
 656         free_xml(cib_node);
 657 
 658         xpath = crm_strdup_printf("//node_state[@uname='%s']/%s",
 659                                   node, XML_CIB_TAG_LRM);
 660         cib->cmds->remove(cib, xpath, NULL,
 661                           cib_xpath|cib_sync_call|cib_scope_local);
 662         free(xpath);
 663 
 664         xpath = crm_strdup_printf("//node_state[@uname='%s']/%s",
 665                                   node, XML_TAG_TRANSIENT_NODEATTRS);
 666         cib->cmds->remove(cib, xpath, NULL,
 667                           cib_xpath|cib_sync_call|cib_scope_local);
 668         free(xpath);
 669     }
 670 
 671     for (iter = injections->node_fail; iter != NULL; iter = iter->next) {
 672         char *node = (char *) iter->data;
 673 
 674         out->message(out, "inject-modify-node", "Failing", node);
 675 
 676         cib_node = pcmk__inject_node_state_change(cib, node, true);
 677         crm_xml_add(cib_node, XML_NODE_IN_CLUSTER, XML_BOOLEAN_NO);
 678         CRM_ASSERT(cib_node != NULL);
 679 
 680         rc = cib->cmds->modify(cib, XML_CIB_TAG_STATUS, cib_node,
 681                                cib_sync_call|cib_scope_local);
 682         CRM_ASSERT(rc == pcmk_ok);
 683         free_xml(cib_node);
 684     }
 685 
 686     for (iter = injections->ticket_grant; iter != NULL; iter = iter->next) {
 687         char *ticket_id = (char *) iter->data;
 688 
 689         out->message(out, "inject-modify-ticket", "Granting", ticket_id);
 690 
 691         rc = set_ticket_state_attr(out, ticket_id, "granted", true, cib);
 692         CRM_ASSERT(rc == pcmk_rc_ok);
 693     }
 694 
 695     for (iter = injections->ticket_revoke; iter != NULL; iter = iter->next) {
 696         char *ticket_id = (char *) iter->data;
 697 
 698         out->message(out, "inject-modify-ticket", "Revoking", ticket_id);
 699 
 700         rc = set_ticket_state_attr(out, ticket_id, "granted", false, cib);
 701         CRM_ASSERT(rc == pcmk_rc_ok);
 702     }
 703 
 704     for (iter = injections->ticket_standby; iter != NULL; iter = iter->next) {
 705         char *ticket_id = (char *) iter->data;
 706 
 707         out->message(out, "inject-modify-ticket", "Standby", ticket_id);
 708 
 709         rc = set_ticket_state_attr(out, ticket_id, "standby", true, cib);
 710         CRM_ASSERT(rc == pcmk_rc_ok);
 711     }
 712 
 713     for (iter = injections->ticket_activate; iter != NULL; iter = iter->next) {
 714         char *ticket_id = (char *) iter->data;
 715 
 716         out->message(out, "inject-modify-ticket", "Activating", ticket_id);
 717 
 718         rc = set_ticket_state_attr(out, ticket_id, "standby", false, cib);
 719         CRM_ASSERT(rc == pcmk_rc_ok);
 720     }
 721 
 722     for (iter = injections->op_inject; iter != NULL; iter = iter->next) {
 723         inject_action(out, (char *) iter->data, cib, data_set);
 724     }
 725 
 726     if (!out->is_quiet(out)) {
 727         out->end_list(out);
 728     }
 729 }
 730 
 731 void
 732 pcmk_free_injections(pcmk_injections_t *injections)
     /* [previous][next][first][last][top][bottom][index][help] */
 733 {
 734     if (injections == NULL) {
 735         return;
 736     }
 737 
 738     g_list_free_full(injections->node_up, g_free);
 739     g_list_free_full(injections->node_down, g_free);
 740     g_list_free_full(injections->node_fail, g_free);
 741     g_list_free_full(injections->op_fail, g_free);
 742     g_list_free_full(injections->op_inject, g_free);
 743     g_list_free_full(injections->ticket_grant, g_free);
 744     g_list_free_full(injections->ticket_revoke, g_free);
 745     g_list_free_full(injections->ticket_standby, g_free);
 746     g_list_free_full(injections->ticket_activate, g_free);
 747     free(injections->quorum);
 748     free(injections->watchdog);
 749 
 750     free(injections);
 751 }

/* [previous][next][first][last][top][bottom][index][help] */