root/daemons/controld/controld_metadata.c

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

DEFINITIONS

This source file includes following definitions.
  1. ra_param_free
  2. metadata_free
  3. metadata_cache_new
  4. metadata_cache_free
  5. metadata_cache_reset
  6. ra_param_from_xml
  7. log_ra_ocf_version
  8. controld_cache_metadata
  9. controld_get_rsc_metadata

   1 /*
   2  * Copyright 2017-2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <stdio.h>
  13 #include <glib.h>
  14 #include <regex.h>
  15 
  16 #include <crm/crm.h>
  17 #include <crm/lrmd.h>
  18 
  19 #include <pacemaker-controld.h>
  20 
  21 static void
  22 ra_param_free(void *param)
     /* [previous][next][first][last][top][bottom][index][help] */
  23 {
  24     if (param) {
  25         struct ra_param_s *p = (struct ra_param_s *) param;
  26 
  27         if (p->rap_name) {
  28             free(p->rap_name);
  29         }
  30         free(param);
  31     }
  32 }
  33 
  34 static void
  35 metadata_free(void *metadata)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37     if (metadata) {
  38         struct ra_metadata_s *md = (struct ra_metadata_s *) metadata;
  39 
  40         g_list_free_full(md->ra_params, ra_param_free);
  41         free(metadata);
  42     }
  43 }
  44 
  45 GHashTable *
  46 metadata_cache_new(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48     return pcmk__strkey_table(free, metadata_free);
  49 }
  50 
  51 void
  52 metadata_cache_free(GHashTable *mdc)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54     if (mdc) {
  55         crm_trace("Destroying metadata cache with %d members", g_hash_table_size(mdc));
  56         g_hash_table_destroy(mdc);
  57     }
  58 }
  59 
  60 void
  61 metadata_cache_reset(GHashTable *mdc)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63     if (mdc) {
  64         crm_trace("Resetting metadata cache with %d members",
  65                   g_hash_table_size(mdc));
  66         g_hash_table_remove_all(mdc);
  67     }
  68 }
  69 
  70 static struct ra_param_s *
  71 ra_param_from_xml(xmlNode *param_xml)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73     const char *param_name = crm_element_value(param_xml, "name");
  74     struct ra_param_s *p;
  75 
  76     p = calloc(1, sizeof(struct ra_param_s));
  77     if (p == NULL) {
  78         return NULL;
  79     }
  80 
  81     p->rap_name = strdup(param_name);
  82     if (p->rap_name == NULL) {
  83         free(p);
  84         return NULL;
  85     }
  86 
  87     if (pcmk__xe_attr_is_true(param_xml, "reloadable")) {
  88         controld_set_ra_param_flags(p, ra_param_reloadable);
  89     }
  90 
  91     if (pcmk__xe_attr_is_true(param_xml, "unique")) {
  92         controld_set_ra_param_flags(p, ra_param_unique);
  93     }
  94 
  95     if (pcmk__xe_attr_is_true(param_xml, "private")) {
  96         controld_set_ra_param_flags(p, ra_param_private);
  97     }
  98     return p;
  99 }
 100 
 101 static void
 102 log_ra_ocf_version(const char *ra_key, const char *ra_ocf_version)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104     if (pcmk__str_empty(ra_ocf_version)) {
 105         crm_warn("%s does not advertise OCF version supported", ra_key);
 106 
 107     } else if (compare_version(ra_ocf_version, "2") >= 0) {
 108         crm_warn("%s supports OCF version %s (this Pacemaker version supports "
 109                  PCMK_OCF_VERSION " and might not work properly with agent)",
 110                  ra_key, ra_ocf_version);
 111 
 112     } else if (compare_version(ra_ocf_version, PCMK_OCF_VERSION) > 0) {
 113         crm_info("%s supports OCF version %s (this Pacemaker version supports "
 114                  PCMK_OCF_VERSION " and might not use all agent features)",
 115                  ra_key, ra_ocf_version);
 116 
 117     } else {
 118         crm_debug("%s supports OCF version %s", ra_key, ra_ocf_version);
 119     }
 120 }
 121 
 122 struct ra_metadata_s *
 123 controld_cache_metadata(GHashTable *mdc, const lrmd_rsc_info_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 124                         const char *metadata_str)
 125 {
 126     char *key = NULL;
 127     const char *reason = NULL;
 128     xmlNode *metadata = NULL;
 129     xmlNode *match = NULL;
 130     struct ra_metadata_s *md = NULL;
 131     bool any_private_params = false;
 132     bool ocf1_1 = false;
 133 
 134     CRM_CHECK(mdc && rsc && metadata_str, return NULL);
 135 
 136     key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
 137     if (!key) {
 138         reason = "Invalid resource agent standard or type";
 139         goto err;
 140     }
 141 
 142     metadata = string2xml(metadata_str);
 143     if (!metadata) {
 144         reason = "Metadata is not valid XML";
 145         goto err;
 146     }
 147 
 148     md = calloc(1, sizeof(struct ra_metadata_s));
 149     if (md == NULL) {
 150         reason = "Could not allocate memory";
 151         goto err;
 152     }
 153 
 154     if (strcmp(rsc->standard, PCMK_RESOURCE_CLASS_OCF) == 0) {
 155         xmlChar *content = NULL;
 156         xmlNode *version_element = first_named_child(metadata, "version");
 157 
 158         if (version_element != NULL) {
 159             content = xmlNodeGetContent(version_element);
 160         }
 161         log_ra_ocf_version(key, (const char *) content);
 162         if (content != NULL) {
 163             ocf1_1 = (compare_version((const char *) content, "1.1") >= 0);
 164             xmlFree(content);
 165         }
 166     }
 167 
 168     // Check supported actions
 169     match = first_named_child(metadata, "actions");
 170     for (match = first_named_child(match, "action"); match != NULL;
 171          match = crm_next_same_xml(match)) {
 172 
 173         const char *action_name = crm_element_value(match, "name");
 174 
 175         if (pcmk__str_eq(action_name, CRMD_ACTION_RELOAD_AGENT,
 176                          pcmk__str_none)) {
 177             if (ocf1_1) {
 178                 controld_set_ra_flags(md, key, ra_supports_reload_agent);
 179             } else {
 180                 crm_notice("reload-agent action will not be used with %s "
 181                            "because it does not support OCF 1.1 or later", key);
 182             }
 183 
 184         } else if (!ocf1_1 && pcmk__str_eq(action_name, CRMD_ACTION_RELOAD,
 185                                            pcmk__str_casei)) {
 186             controld_set_ra_flags(md, key, ra_supports_legacy_reload);
 187         }
 188     }
 189 
 190     // Build a parameter list
 191     match = first_named_child(metadata, "parameters");
 192     for (match = first_named_child(match, "parameter"); match != NULL;
 193          match = crm_next_same_xml(match)) {
 194 
 195         const char *param_name = crm_element_value(match, "name");
 196 
 197         if (param_name == NULL) {
 198             crm_warn("Metadata for %s:%s:%s has parameter without a name",
 199                      rsc->standard, rsc->provider, rsc->type);
 200         } else {
 201             struct ra_param_s *p = ra_param_from_xml(match);
 202 
 203             if (p == NULL) {
 204                 reason = "Could not allocate memory";
 205                 goto err;
 206             }
 207             if (pcmk_is_set(p->rap_flags, ra_param_private)) {
 208                 any_private_params = true;
 209             }
 210             md->ra_params = g_list_prepend(md->ra_params, p);
 211         }
 212     }
 213 
 214     /* Newer resource agents support the "private" parameter attribute to
 215      * indicate sensitive parameters. For backward compatibility with older
 216      * agents, implicitly treat a few common names as private when the agent
 217      * doesn't specify any explicitly.
 218      */
 219     if (!any_private_params) {
 220         for (GList *iter = md->ra_params; iter != NULL; iter = iter->next) {
 221             struct ra_param_s *p = iter->data;
 222 
 223             if (pcmk__str_any_of(p->rap_name, "password", "passwd", "user",
 224                                  NULL)) {
 225                 controld_set_ra_param_flags(p, ra_param_private);
 226             }
 227         }
 228     }
 229 
 230     g_hash_table_replace(mdc, key, md);
 231     free_xml(metadata);
 232     return md;
 233 
 234 err:
 235     crm_warn("Unable to update metadata for %s (%s%s%s:%s): %s",
 236              rsc->id, rsc->standard, ((rsc->provider == NULL)? "" : ":"),
 237              pcmk__s(rsc->provider, ""), rsc->type, reason);
 238     free(key);
 239     free_xml(metadata);
 240     metadata_free(md);
 241     return NULL;
 242 }
 243 
 244 /*!
 245  * \internal
 246  * \brief Get meta-data for a resource
 247  *
 248  * \param[in,out] lrm_state  Use meta-data cache from this executor connection
 249  * \param[in]     rsc        Resource to get meta-data for
 250  * \param[in]     source     Allowed meta-data sources (bitmask of
 251  *                           enum controld_metadata_source_e values)
 252  *
 253  * \return Meta-data cache entry for given resource, or NULL if not available
 254  */
 255 struct ra_metadata_s *
 256 controld_get_rsc_metadata(lrm_state_t *lrm_state, const lrmd_rsc_info_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 257                           uint32_t source)
 258 {
 259     struct ra_metadata_s *metadata = NULL;
 260     char *metadata_str = NULL;
 261     char *key = NULL;
 262     int rc = pcmk_ok;
 263 
 264     CRM_CHECK((lrm_state != NULL) && (rsc != NULL), return NULL);
 265 
 266     if (pcmk_is_set(source, controld_metadata_from_cache)) {
 267         key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
 268         if (key != NULL) {
 269             metadata = g_hash_table_lookup(lrm_state->metadata_cache, key);
 270             free(key);
 271         }
 272         if (metadata != NULL) {
 273             crm_debug("Retrieved metadata for %s (%s%s%s:%s) from cache",
 274                       rsc->id, rsc->standard,
 275                       ((rsc->provider == NULL)? "" : ":"),
 276                       ((rsc->provider == NULL)? "" : rsc->provider),
 277                       rsc->type);
 278             return metadata;
 279         }
 280     }
 281 
 282     if (!pcmk_is_set(source, controld_metadata_from_agent)) {
 283         return NULL;
 284     }
 285 
 286     /* For most actions, metadata was cached asynchronously before action
 287      * execution (via metadata_complete()).
 288      *
 289      * However if that failed, and for other actions, retrieve the metadata now
 290      * via a local, synchronous, direct execution of the agent.
 291      *
 292      * This has multiple issues, which is why this is just a fallback: the
 293      * executor should execute agents, not the controller; metadata for
 294      * Pacemaker Remote nodes should be collected on those nodes, not locally;
 295      * the metadata call shouldn't eat into the timeout of the real action being
 296      * performed; and the synchronous call blocks the controller (which also
 297      * means that if the metadata action tries to contact the controller,
 298      * everything will hang until the timeout).
 299      */
 300     crm_debug("Retrieving metadata for %s (%s%s%s:%s) synchronously",
 301               rsc->id, rsc->standard,
 302               ((rsc->provider == NULL)? "" : ":"),
 303               ((rsc->provider == NULL)? "" : rsc->provider),
 304               rsc->type);
 305     rc = lrm_state_get_metadata(lrm_state, rsc->standard, rsc->provider,
 306                                 rsc->type, &metadata_str, 0);
 307     if (rc != pcmk_ok) {
 308         crm_warn("Failed to get metadata for %s (%s%s%s:%s): %s",
 309                  rsc->id, rsc->standard,
 310                  ((rsc->provider == NULL)? "" : ":"),
 311                  ((rsc->provider == NULL)? "" : rsc->provider),
 312                  rsc->type, pcmk_strerror(rc));
 313         return NULL;
 314     }
 315 
 316     metadata = controld_cache_metadata(lrm_state->metadata_cache, rsc,
 317                                        metadata_str);
 318     free(metadata_str);
 319     return metadata;
 320 }

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