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

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