LCOV - code coverage report
Current view: top level - pacemaker - pcmk_injections.c (source / functions) Hit Total Coverage
Test: Pacemaker code coverage Lines: 0 322 0.0 %
Date: 2024-05-07 11:09:47 Functions: 0 13 0.0 %

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

Generated by: LCOV version 1.14