pacemaker  3.0.0-d8340737c4
Scalable High-Availability cluster resource manager
pcmk_rule.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022-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 Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <crm/cib/internal.h>
13 #include <crm/common/cib.h>
14 #include <crm/common/iso8601.h>
15 #include <crm/common/xml.h>
16 #include <crm/pengine/internal.h>
18 #include <pacemaker-internal.h>
19 
20 #include "libpacemaker_private.h"
21 
22 #define XPATH_NODE_RULE "//" PCMK_XE_RULE "[@" PCMK_XA_ID "='%s']"
23 
34 static int
35 eval_rule(pcmk_scheduler_t *scheduler, const char *rule_id, const char **error)
36 {
37  xmlNodePtr cib_constraints = NULL;
38  xmlNodePtr match = NULL;
39  xmlXPathObjectPtr xpath_obj = NULL;
40  char *xpath = NULL;
41  int rc = pcmk_rc_ok;
42  int num_results = 0;
43 
44  *error = NULL;
45 
46  /* Rules are under the constraints node in the XML, so first find that. */
47  cib_constraints = pcmk_find_cib_element(scheduler->input,
49 
50  /* Get all rules matching the given ID that are also simple enough for us
51  * to check. For the moment, these rules must only have a single
52  * date_expression child and:
53  * - Do not have a date_spec operation, or
54  * - Have a date_spec operation that contains years=
55  *
56  * We do this in steps to provide better error messages. First, check that
57  * there's any rule with the given ID.
58  */
59  xpath = crm_strdup_printf(XPATH_NODE_RULE, rule_id);
60  xpath_obj = xpath_search(cib_constraints, xpath);
61  num_results = numXpathResults(xpath_obj);
62 
63  free(xpath);
64  freeXpathObject(xpath_obj);
65 
66  if (num_results == 0) {
67  *error = "Rule not found";
68  return ENXIO;
69  }
70 
71  if (num_results > 1) {
72  // Should not be possible; schema prevents this
73  *error = "Found more than one rule with matching ID";
74  return pcmk_rc_duplicate_id;
75  }
76 
77  /* Next, make sure it has exactly one date_expression. */
78  xpath = crm_strdup_printf(XPATH_NODE_RULE "//date_expression", rule_id);
79  xpath_obj = xpath_search(cib_constraints, xpath);
80  num_results = numXpathResults(xpath_obj);
81 
82  free(xpath);
83  freeXpathObject(xpath_obj);
84 
85  if (num_results != 1) {
86  if (num_results == 0) {
87  *error = "Rule does not have a date expression";
88  } else {
89  *error = "Rule has more than one date expression";
90  }
91  return EOPNOTSUPP;
92  }
93 
94  /* Then, check that it's something we actually support. */
98  "!='" PCMK_VALUE_DATE_SPEC "']",
99  rule_id);
100  xpath_obj = xpath_search(cib_constraints, xpath);
101  num_results = numXpathResults(xpath_obj);
102 
103  free(xpath);
104 
105  if (num_results == 0) {
106  freeXpathObject(xpath_obj);
107 
110  "[@" PCMK_XA_OPERATION
111  "='" PCMK_VALUE_DATE_SPEC "' "
112  "and " PCMK_XE_DATE_SPEC
113  "/@" PCMK_XA_YEARS "]",
114  rule_id);
115  xpath_obj = xpath_search(cib_constraints, xpath);
116  num_results = numXpathResults(xpath_obj);
117 
118  free(xpath);
119 
120  if (num_results == 0) {
121  freeXpathObject(xpath_obj);
122  *error = "Rule must either not use " PCMK_XE_DATE_SPEC ", or use "
123  PCMK_XE_DATE_SPEC " with " PCMK_XA_YEARS "=";
124  return EOPNOTSUPP;
125  }
126  }
127 
128  match = getXpathResult(xpath_obj, 0);
129 
130  /* We should have ensured this with the xpath query above, but double-
131  * checking can't hurt.
132  */
133  pcmk__assert((match != NULL)
135 
136  rc = pcmk__evaluate_date_expression(match, scheduler->priv->now, NULL);
137  if ((rc != pcmk_rc_ok) && (rc != pcmk_rc_within_range)) {
138  // Malformed or missing
139  *error = "Error parsing rule";
140  }
141 
142  freeXpathObject(xpath_obj);
143  return rc;
144 }
145 
159 int
160 pcmk__check_rules(pcmk__output_t *out, xmlNodePtr input, const crm_time_t *date,
161  const char **rule_ids)
162 {
163  pcmk_scheduler_t *scheduler = NULL;
164  int rc = pcmk_rc_ok;
165 
166  pcmk__assert(out != NULL);
167 
168  if (rule_ids == NULL) {
169  // Trivial case; every rule specified is in effect
170  return pcmk_rc_ok;
171  }
172 
173  rc = pcmk__init_scheduler(out, input, date, &scheduler);
174  if (rc != pcmk_rc_ok) {
175  return rc;
176  }
177 
178  for (const char **rule_id = rule_ids; *rule_id != NULL; rule_id++) {
179  const char *error = NULL;
180  int last_rc = eval_rule(scheduler, *rule_id, &error);
181 
182  out->message(out, "rule-check", *rule_id, last_rc, error);
183 
184  if (last_rc != pcmk_rc_ok) {
185  rc = last_rc;
186  }
187  }
188 
190  return rc;
191 }
192 
193 // Documented in pacemaker.h
194 int
195 pcmk_check_rules(xmlNodePtr *xml, xmlNodePtr input, const crm_time_t *date,
196  const char **rule_ids)
197 {
198  pcmk__output_t *out = NULL;
199  int rc = pcmk_rc_ok;
200 
201  rc = pcmk__xml_output_new(&out, xml);
202  if (rc != pcmk_rc_ok) {
203  return rc;
204  }
205 
207 
208  rc = pcmk__check_rules(out, input, date, rule_ids);
209  pcmk__xml_output_finish(out, pcmk_rc2exitc(rc), xml);
210  return rc;
211 }
#define PCMK_XA_YEARS
Definition: xml_names.h:456
int(* message)(pcmk__output_t *out, const char *message_id,...)
struct crm_time_s crm_time_t
Definition: iso8601.h:32
#define PCMK_VALUE_DATE_SPEC
Definition: options.h:142
#define PCMK_XE_CONSTRAINTS
Definition: xml_names.h:89
crm_exit_t pcmk_rc2exitc(int rc)
Map a function return code to the most similar exit code.
Definition: results.c:810
pcmk__scheduler_private_t * priv
Definition: scheduler.h:99
void pcmk__xml_output_finish(pcmk__output_t *out, crm_exit_t exit_status, xmlNodePtr *xml)
Definition: output.c:271
#define PCMK_XA_OPERATION
Definition: xml_names.h:349
int pcmk__check_rules(pcmk__output_t *out, xmlNodePtr input, const crm_time_t *date, const char **rule_ids)
Definition: pcmk_rule.c:160
#define PCMK_XE_DATE_EXPRESSION
Definition: xml_names.h:96
xmlNode * pcmk_find_cib_element(xmlNode *cib, const char *element_name)
Find an element in the CIB.
Definition: cib.c:172
#define PCMK_XE_DATE_SPEC
Definition: xml_names.h:97
int pcmk__xml_output_new(pcmk__output_t **out, xmlNodePtr *xml)
Definition: output.c:244
#define XPATH_NODE_RULE
Definition: pcmk_rule.c:22
G_GNUC_INTERNAL int pcmk__init_scheduler(pcmk__output_t *out, xmlNodePtr input, const crm_time_t *date, pcmk_scheduler_t **scheduler)
void pcmk__register_lib_messages(pcmk__output_t *out)
Definition: pcmk_output.c:2709
Wrappers for and extensions to libxml2.
ISO_8601 Date handling.
#define pcmk__assert(expr)
void pe_free_working_set(pcmk_scheduler_t *scheduler)
Free scheduler data.
Definition: status.c:56
xmlXPathObjectPtr xpath_search(const xmlNode *xml_top, const char *path)
Definition: xpath.c:139
int pcmk_check_rules(xmlNodePtr *xml, xmlNodePtr input, const crm_time_t *date, const char **rule_ids)
Check whether each rule in a list is in effect.
Definition: pcmk_rule.c:195
pcmk_scheduler_t * scheduler
xmlNode * input
enum expression_type pcmk__condition_type(const xmlNode *condition)
Definition: rules.c:37
xmlNode * input
Definition: scheduler.h:81
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
Definition: xpath.c:58
This structure contains everything that makes up a single output formatter.
int pcmk__evaluate_date_expression(const xmlNode *date_expression, const crm_time_t *now, crm_time_t *next_change)
Definition: rules.c:469
void freeXpathObject(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:39
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1