1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/*
* Copyright 2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU Lesser General Public License
* version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
*/
#include <crm_internal.h>
#include <glib.h> // GSList, GString
#include "crmcommon_private.h"
/*!
* \internal
* \brief Output an option's possible values
*
* \param[in,out] out Output object
* \param[in] option Option whose possible values to add
*/
static void
add_possible_values_default(pcmk__output_t *out,
const pcmk__cluster_option_t *option)
{
const char *id = _("Possible values");
GString *buf = g_string_sized_new(256);
pcmk__assert(option->type != NULL);
if (pcmk_is_set(option->flags, pcmk__opt_generated)) {
id = _("Possible values (generated by Pacemaker)");
}
if ((option->values != NULL) && (strcmp(option->type, "select") == 0)) {
const char *delim = ", ";
bool found_default = (option->default_value == NULL);
char *str = pcmk__str_copy(option->values);
for (const char *value = strtok(str, delim); value != NULL;
value = strtok(NULL, delim)) {
if (buf->len > 0) {
g_string_append(buf, delim);
}
g_string_append_c(buf, '"');
g_string_append(buf, value);
g_string_append_c(buf, '"');
if (!found_default && (strcmp(value, option->default_value) == 0)) {
found_default = true;
g_string_append(buf, _(" (default)"));
}
}
free(str);
} else if (option->default_value != NULL) {
pcmk__g_strcat(buf,
option->type, _(" (default: \""), option->default_value,
"\")", NULL);
} else {
pcmk__g_strcat(buf, option->type, _(" (no default)"), NULL);
}
out->list_item(out, id, "%s", buf->str);
g_string_free(buf, TRUE);
}
/*!
* \internal
* \brief Output a single option's metadata
*
* \param[in,out] out Output object
* \param[in] option Option to add
*/
static void
add_option_metadata_default(pcmk__output_t *out,
const pcmk__cluster_option_t *option)
{
const char *desc_short = option->description_short;
const char *desc_long = option->description_long;
pcmk__assert((desc_short != NULL) || (desc_long != NULL));
if (desc_short == NULL) {
desc_short = desc_long;
desc_long = NULL;
}
out->list_item(out, option->name, "%s", _(desc_short));
out->begin_list(out, NULL, NULL, NULL);
if (desc_long != NULL) {
out->list_item(out, NULL, "%s", _(desc_long));
}
add_possible_values_default(out, option);
out->end_list(out);
}
/*!
* \internal
* \brief Output the metadata for a list of options
*
* \param[in,out] out Output object
* \param[in] args Message-specific arguments
*
* \return Standard Pacemaker return code
*
* \note \p args should contain the following:
* -# Fake resource agent name for the option list (ignored)
* -# Short description of option list
* -# Long description of option list
* -# Filter: Group of <tt>enum pcmk__opt_flags</tt>; output an option
* only if its \c flags member has all these flags set
* -# <tt>NULL</tt>-terminated list of options whose metadata to format
* -# All: If \c true, output all options; otherwise, exclude advanced and
* deprecated options unless \c pcmk__opt_advanced and
* \c pcmk__opt_deprecated flags (respectively) are set in the filter.
*/
PCMK__OUTPUT_ARGS("option-list", "const char *", "const char *", "const char *",
"uint32_t", "const pcmk__cluster_option_t *", "bool")
static int
option_list_default(pcmk__output_t *out, va_list args)
{
const char *name G_GNUC_UNUSED = va_arg(args, const char *);
const char *desc_short = va_arg(args, const char *);
const char *desc_long = va_arg(args, const char *);
const uint32_t filter = va_arg(args, uint32_t);
const pcmk__cluster_option_t *option_list =
va_arg(args, pcmk__cluster_option_t *);
const bool all = (bool) va_arg(args, int);
const bool show_deprecated = all
|| pcmk_is_set(filter, pcmk__opt_deprecated);
const bool show_advanced = all || pcmk_is_set(filter, pcmk__opt_advanced);
bool old_fancy = false;
GSList *deprecated = NULL;
GSList *advanced = NULL;
pcmk__assert((out != NULL) && (desc_short != NULL)
&& (desc_long != NULL) && (option_list != NULL));
old_fancy = pcmk__output_text_get_fancy(out);
pcmk__output_text_set_fancy(out, true);
out->info(out, "%s", _(desc_short));
out->spacer(out);
out->info(out, "%s", _(desc_long));
out->begin_list(out, NULL, NULL, NULL);
for (const pcmk__cluster_option_t *option = option_list;
option->name != NULL; option++) {
// Store deprecated and advanced options to display later if appropriate
if (pcmk_all_flags_set(option->flags, filter)) {
if (pcmk_is_set(option->flags, pcmk__opt_deprecated)) {
if (show_deprecated) {
deprecated = g_slist_prepend(deprecated, (gpointer) option);
}
} else if (pcmk_is_set(option->flags, pcmk__opt_advanced)) {
if (show_advanced) {
advanced = g_slist_prepend(advanced, (gpointer) option);
}
} else {
out->spacer(out);
add_option_metadata_default(out, option);
}
}
}
if (advanced != NULL) {
advanced = g_slist_reverse(advanced);
out->spacer(out);
out->begin_list(out, NULL, NULL, _("ADVANCED OPTIONS"));
for (const GSList *iter = advanced; iter != NULL; iter = iter->next) {
const pcmk__cluster_option_t *option = iter->data;
out->spacer(out);
add_option_metadata_default(out, option);
}
out->end_list(out);
g_slist_free(advanced);
}
if (deprecated != NULL) {
deprecated = g_slist_reverse(deprecated);
out->spacer(out);
out->begin_list(out, NULL, NULL,
_("DEPRECATED OPTIONS (will be removed in a future "
"release)"));
for (const GSList *iter = deprecated; iter != NULL; iter = iter->next) {
const pcmk__cluster_option_t *option = iter->data;
out->spacer(out);
add_option_metadata_default(out, option);
}
out->end_list(out);
g_slist_free(deprecated);
}
out->end_list(out);
pcmk__output_text_set_fancy(out, old_fancy);
return pcmk_rc_ok;
}
/*!
* \internal
* \brief Add a description element to an OCF-like metadata XML node
*
* Include a translation based on the current locale if \c ENABLE_NLS is
* defined.
*
* \param[in,out] out Output object
* \param[in] for_long If \c true, add long description; otherwise, add
* short description
* \param[in] desc Textual description to add
*/
static void
add_desc_xml(pcmk__output_t *out, bool for_long, const char *desc)
{
const char *tag = (for_long? PCMK_XE_LONGDESC : PCMK_XE_SHORTDESC);
xmlNode *node = pcmk__output_create_xml_text_node(out, tag, desc);
crm_xml_add(node, PCMK_XA_LANG, PCMK__VALUE_EN);
#ifdef ENABLE_NLS
{
static const char *locale = NULL;
if (strcmp(desc, _(desc)) == 0) {
return;
}
if (locale == NULL) {
locale = strtok(setlocale(LC_ALL, NULL), "_");
}
node = pcmk__output_create_xml_text_node(out, tag, _(desc));
crm_xml_add(node, PCMK_XA_LANG, locale);
}
#endif
}
/*!
* \internal
* \brief Output an option's possible values
*
* Add a \c PCMK_XE_OPTION element for each of the option's possible values.
*
* \param[in,out] out Output object
* \param[in] option Option whose possible values to add
*/
static void
add_possible_values_xml(pcmk__output_t *out,
const pcmk__cluster_option_t *option)
{
if ((option->values != NULL) && (strcmp(option->type, "select") == 0)) {
const char *delim = ", ";
char *str = pcmk__str_copy(option->values);
const char *ptr = strtok(str, delim);
while (ptr != NULL) {
pcmk__output_create_xml_node(out, PCMK_XE_OPTION,
PCMK_XA_VALUE, ptr,
NULL);
ptr = strtok(NULL, delim);
}
free(str);
}
}
/*!
* \internal
* \brief Map an option type to one suitable for daemon metadata
*
* \param[in] type Option type to map
*
* \return String suitable for daemon metadata to display as an option type
*/
static const char *
map_legacy_option_type(const char *type)
{
// @COMPAT Drop this function when we drop daemon metadata commands
if (pcmk__str_any_of(type, PCMK_VALUE_DURATION, PCMK_VALUE_TIMEOUT, NULL)) {
return PCMK__VALUE_TIME;
} else if (pcmk__str_any_of(type,
PCMK_VALUE_NONNEGATIVE_INTEGER,
PCMK_VALUE_SCORE, NULL)) {
return PCMK_VALUE_INTEGER;
} else if (pcmk__str_eq(type, PCMK_VALUE_VERSION, pcmk__str_none)) {
return PCMK_VALUE_STRING;
} else {
return type;
}
}
/*!
* \internal
* \brief Add a \c PCMK_XE_PARAMETER element to an OCF-like metadata XML node
*
* \param[in,out] out Output object
* \param[in] option Option to add as a \c PCMK_XE_PARAMETER element
*/
static void
add_option_metadata_xml(pcmk__output_t *out,
const pcmk__cluster_option_t *option)
{
const char *type = option->type;
const char *desc_long = option->description_long;
const char *desc_short = option->description_short;
const bool advanced = pcmk_is_set(option->flags, pcmk__opt_advanced);
const bool deprecated = pcmk_is_set(option->flags, pcmk__opt_deprecated);
const bool generated = pcmk_is_set(option->flags, pcmk__opt_generated);
// OCF requires "1"/"0" and does not allow "true"/"false
// @COMPAT Variables no longer needed after we drop legacy mode
const char *advanced_s = advanced? "1" : "0";
const char *generated_s = generated? "1" : "0";
// @COMPAT For daemon metadata only; drop when daemon metadata is dropped
const bool legacy = pcmk__output_get_legacy_xml(out);
char *desc_long_legacy = NULL;
GString *desc_short_legacy = NULL;
// The standard requires a parameter type
pcmk__assert(type != NULL);
// The standard requires long and short parameter descriptions
pcmk__assert((desc_long != NULL) || (desc_short != NULL));
if (desc_long == NULL) {
desc_long = desc_short;
} else if (desc_short == NULL) {
desc_short = desc_long;
}
if (legacy) {
// This is ugly but it will go away at a major release bump
type = map_legacy_option_type(type);
if (option->values != NULL) {
desc_long_legacy = crm_strdup_printf("%s Allowed values: %s",
desc_long, option->values);
desc_long = desc_long_legacy;
}
if (deprecated || advanced) {
const size_t init_sz = 1023;
if (desc_long != option->description_long) {
/* desc_long was NULL and got assigned desc_short, which was
* non-empty. Let desc_long have the "real" description, and put
* the flag in desc_short.
*/
desc_short = "";
} else {
desc_short = pcmk__s(option->description_short, "");
}
if (deprecated) {
pcmk__add_separated_word(&desc_short_legacy, init_sz,
"*** Deprecated ***", NULL);
}
if (advanced) {
pcmk__add_separated_word(&desc_short_legacy, init_sz,
"*** Advanced Use Only ***", NULL);
}
pcmk__add_separated_word(&desc_short_legacy, 0, desc_short, NULL);
desc_short = desc_short_legacy->str;
}
/* These must be NULL when used as attribute values later.
* PCMK_XA_ADVANCED and PCMK_XA_GENERATED break validation for some
* legacy tools.
*/
advanced_s = NULL;
generated_s = NULL;
}
pcmk__output_xml_create_parent(out, PCMK_XE_PARAMETER,
PCMK_XA_NAME, option->name,
PCMK_XA_ADVANCED, advanced_s,
PCMK_XA_GENERATED, generated_s,
NULL);
if (deprecated && !legacy) {
// No need yet to support "replaced-with" or "desc"; add if needed
pcmk__output_create_xml_node(out, PCMK_XE_DEPRECATED, NULL);
}
add_desc_xml(out, true, desc_long);
add_desc_xml(out, false, desc_short);
pcmk__output_xml_create_parent(out, PCMK_XE_CONTENT,
PCMK_XA_TYPE, type,
PCMK_XA_DEFAULT, option->default_value,
NULL);
add_possible_values_xml(out, option);
pcmk__output_xml_pop_parent(out);
pcmk__output_xml_pop_parent(out);
free(desc_long_legacy);
if (desc_short_legacy != NULL) {
g_string_free(desc_short_legacy, TRUE);
}
}
/*!
* \internal
* \brief Output the metadata for a list of options as OCF-like XML
*
* \param[in,out] out Output object
* \param[in] args Message-specific arguments
*
* \return Standard Pacemaker return code
*
* \note \p args should contain the following:
* -# Fake resource agent name for the option list
* -# Short description of option list
* -# Long description of option list
* -# Filter: Group of <tt>enum pcmk__opt_flags</tt>; output an option
* only if its \c flags member has all these flags set
* -# <tt>NULL</tt>-terminated list of options whose metadata to format
* -# Whether to output all options (ignored, treated as \c true)
*/
PCMK__OUTPUT_ARGS("option-list", "const char *", "const char *", "const char *",
"uint32_t", "const pcmk__cluster_option_t *", "bool")
static int
option_list_xml(pcmk__output_t *out, va_list args)
{
const char *name = va_arg(args, const char *);
const char *desc_short = va_arg(args, const char *);
const char *desc_long = va_arg(args, const char *);
const uint32_t filter = va_arg(args, uint32_t);
const pcmk__cluster_option_t *option_list =
va_arg(args, pcmk__cluster_option_t *);
pcmk__assert((out != NULL) && (name != NULL) && (desc_short != NULL)
&& (desc_long != NULL) && (option_list != NULL));
pcmk__output_xml_create_parent(out, PCMK_XE_RESOURCE_AGENT,
PCMK_XA_NAME, name,
PCMK_XA_VERSION, PACEMAKER_VERSION,
NULL);
pcmk__output_create_xml_text_node(out, PCMK_XE_VERSION, PCMK_OCF_VERSION);
add_desc_xml(out, true, desc_long);
add_desc_xml(out, false, desc_short);
pcmk__output_xml_create_parent(out, PCMK_XE_PARAMETERS, NULL);
for (const pcmk__cluster_option_t *option = option_list;
option->name != NULL; option++) {
if (pcmk_all_flags_set(option->flags, filter)) {
add_option_metadata_xml(out, option);
}
}
pcmk__output_xml_pop_parent(out);
pcmk__output_xml_pop_parent(out);
return pcmk_rc_ok;
}
static pcmk__message_entry_t fmt_functions[] = {
{ "option-list", "default", option_list_default },
{ "option-list", "xml", option_list_xml },
{ NULL, NULL, NULL }
};
/*!
* \internal
* \brief Register the formatting functions for option lists
*
* \param[in,out] out Output object
*/
void
pcmk__register_option_messages(pcmk__output_t *out) {
pcmk__register_messages(out, fmt_functions);
}
|