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

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