root/lib/common/attrs.c

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

DEFINITIONS

This source file includes following definitions.
  1. pcmk__node_attr_target
  2. pcmk_promotion_score_name

   1 /*
   2  * Copyright 2011-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 Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #ifndef _GNU_SOURCE
  11 #  define _GNU_SOURCE
  12 #endif
  13 
  14 #include <crm_internal.h>
  15 
  16 #include <stdio.h>
  17 
  18 #include <crm/msg_xml.h>
  19 #include <crm/common/attrd_internal.h>
  20 
  21 #define LRM_TARGET_ENV "OCF_RESKEY_" CRM_META "_" XML_LRM_ATTR_TARGET
  22 
  23 /*!
  24  * \internal
  25  * \brief Get the node name that should be used to set node attributes
  26  *
  27  * If given NULL, "auto", or "localhost" as an argument, check the environment
  28  * to detect the node name that should be used to set node attributes. (The
  29  * caller might not know the correct name, for example if the target is part of
  30  * a bundle with container-attribute-target set to "host".)
  31  *
  32  * \param[in] name  NULL, "auto" or "localhost" to check environment variables,
  33  *                  or anything else to return NULL
  34  *
  35  * \return Node name that should be used for node attributes based on the
  36  *         environment if known, otherwise NULL
  37  */
  38 const char *
  39 pcmk__node_attr_target(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41     if (name == NULL || pcmk__strcase_any_of(name, "auto", "localhost", NULL)) {
  42         char *target_var = crm_meta_name(XML_RSC_ATTR_TARGET);
  43         char *phys_var = crm_meta_name(PCMK__ENV_PHYSICAL_HOST);
  44         const char *target = getenv(target_var);
  45         const char *host_physical = getenv(phys_var);
  46 
  47         // It is important to use the name by which the scheduler knows us
  48         if (host_physical && pcmk__str_eq(target, "host", pcmk__str_casei)) {
  49             name = host_physical;
  50 
  51         } else {
  52             const char *host_pcmk = getenv(LRM_TARGET_ENV);
  53 
  54             if (host_pcmk) {
  55                 name = host_pcmk;
  56             }
  57         }
  58         free(target_var);
  59         free(phys_var);
  60 
  61         // TODO? Call get_local_node_name() if name == NULL
  62         // (currently would require linkage against libcrmcluster)
  63         return name;
  64     } else {
  65         return NULL;
  66     }
  67 }
  68 
  69 /*!
  70  * \brief Return the name of the node attribute used as a promotion score
  71  *
  72  * \param[in] rsc_id  Resource ID that promotion score is for (or NULL to
  73  *                    check the OCF_RESOURCE_INSTANCE environment variable)
  74  *
  75  * \return Newly allocated string with the node attribute name (or NULL on
  76  *         error, including no ID or environment variable specified)
  77  * \note It is the caller's responsibility to free() the result.
  78  */
  79 char *
  80 pcmk_promotion_score_name(const char *rsc_id)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82     if (pcmk__str_empty(rsc_id)) {
  83         rsc_id = getenv("OCF_RESOURCE_INSTANCE");
  84         if (pcmk__str_empty(rsc_id)) {
  85             return NULL;
  86         }
  87     }
  88     return crm_strdup_printf("master-%s", rsc_id);
  89 }

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