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. set_ticket_state_attr
  11. inject_action
  12. pcmk__inject_scheduler_input
  13. pcmk_free_injections

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

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