This source file includes following definitions.
- parse_cli_lifetime
- promoted_role_name
- cli_resource_ban
- cli_resource_prefer
- resource_clear_node_in_expr
- resource_clear_node_in_location
- cli_resource_clear
- build_clear_xpath_string
- cli_resource_clear_all_expired
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <crm_resource.h>
13
14 static char *
15 parse_cli_lifetime(pcmk__output_t *out, const char *move_lifetime)
16 {
17 char *later_s = NULL;
18 crm_time_t *now = NULL;
19 crm_time_t *later = NULL;
20 crm_time_t *duration = NULL;
21
22 if (move_lifetime == NULL) {
23 return NULL;
24 }
25
26 duration = crm_time_parse_duration(move_lifetime);
27 if (duration == NULL) {
28 out->err(out, "Invalid duration specified: %s\n"
29 "Please refer to https://en.wikipedia.org/wiki/ISO_8601#Durations "
30 "for examples of valid durations", move_lifetime);
31 return NULL;
32 }
33
34 now = crm_time_new(NULL);
35 later = crm_time_add(now, duration);
36 if (later == NULL) {
37 out->err(out, "Unable to add %s to current time\n"
38 "Please report to " PACKAGE_BUGREPORT " as possible bug",
39 move_lifetime);
40 crm_time_free(now);
41 crm_time_free(duration);
42 return NULL;
43 }
44
45 crm_time_log(LOG_INFO, "now ", now,
46 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
47 crm_time_log(LOG_INFO, "later ", later,
48 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
49 crm_time_log(LOG_INFO, "duration", duration, crm_time_log_date | crm_time_log_timeofday);
50 later_s = crm_time_as_string(later, crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
51 out->info(out, "Migration will take effect until: %s", later_s);
52
53 crm_time_free(duration);
54 crm_time_free(later);
55 crm_time_free(now);
56 return later_s;
57 }
58
59 static const char *
60 promoted_role_name(void)
61 {
62
63
64
65
66 #ifdef PCMK__COMPAT_2_0
67 return RSC_ROLE_PROMOTED_LEGACY_S;
68 #else
69 return RSC_ROLE_PROMOTED_S;
70 #endif
71 }
72
73
74 int
75 cli_resource_ban(pcmk__output_t *out, const char *rsc_id, const char *host,
76 const char *move_lifetime, GList *allnodes, cib_t * cib_conn,
77 int cib_options, gboolean promoted_role_only)
78 {
79 char *later_s = NULL;
80 int rc = pcmk_rc_ok;
81 xmlNode *fragment = NULL;
82 xmlNode *location = NULL;
83
84 if(host == NULL) {
85 GList *n = allnodes;
86 for(; n && rc == pcmk_rc_ok; n = n->next) {
87 pe_node_t *target = n->data;
88
89 rc = cli_resource_ban(out, rsc_id, target->details->uname, move_lifetime,
90 NULL, cib_conn, cib_options, promoted_role_only);
91 }
92 return rc;
93 }
94
95 later_s = parse_cli_lifetime(out, move_lifetime);
96 if(move_lifetime && later_s == NULL) {
97 return EINVAL;
98 }
99
100 fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
101
102 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
103 crm_xml_set_id(location, "cli-ban-%s-on-%s", rsc_id, host);
104
105 out->info(out, "WARNING: Creating rsc_location constraint '%s' with a "
106 "score of -INFINITY for resource %s on %s.\n\tThis will "
107 "prevent %s from %s on %s until the constraint is removed "
108 "using the clear option or by editing the CIB with an "
109 "appropriate tool\n\tThis will be the case even if %s "
110 "is the last node in the cluster",
111 ID(location), rsc_id, host, rsc_id,
112 (promoted_role_only? "being promoted" : "running"),
113 host, host);
114
115 crm_xml_add(location, XML_LOC_ATTR_SOURCE, rsc_id);
116 if(promoted_role_only) {
117 crm_xml_add(location, XML_RULE_ATTR_ROLE, promoted_role_name());
118 } else {
119 crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_STARTED_S);
120 }
121
122 if (later_s == NULL) {
123
124 crm_xml_add(location, XML_CIB_TAG_NODE, host);
125 crm_xml_add(location, XML_RULE_ATTR_SCORE, CRM_MINUS_INFINITY_S);
126
127 } else {
128 xmlNode *rule = create_xml_node(location, XML_TAG_RULE);
129 xmlNode *expr = create_xml_node(rule, XML_TAG_EXPRESSION);
130
131 crm_xml_set_id(rule, "cli-ban-%s-on-%s-rule", rsc_id, host);
132 crm_xml_add(rule, XML_RULE_ATTR_SCORE, CRM_MINUS_INFINITY_S);
133 crm_xml_add(rule, XML_RULE_ATTR_BOOLEAN_OP, "and");
134
135 crm_xml_set_id(expr, "cli-ban-%s-on-%s-expr", rsc_id, host);
136 crm_xml_add(expr, XML_EXPR_ATTR_ATTRIBUTE, CRM_ATTR_UNAME);
137 crm_xml_add(expr, XML_EXPR_ATTR_OPERATION, "eq");
138 crm_xml_add(expr, XML_EXPR_ATTR_VALUE, host);
139 crm_xml_add(expr, XML_EXPR_ATTR_TYPE, "string");
140
141 expr = create_xml_node(rule, "date_expression");
142 crm_xml_set_id(expr, "cli-ban-%s-on-%s-lifetime", rsc_id, host);
143 crm_xml_add(expr, "operation", "lt");
144 crm_xml_add(expr, "end", later_s);
145 }
146
147 crm_log_xml_notice(fragment, "Modify");
148 rc = cib_conn->cmds->update(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);
149 rc = pcmk_legacy2rc(rc);
150
151 free_xml(fragment);
152 free(later_s);
153 return rc;
154 }
155
156
157 int
158 cli_resource_prefer(pcmk__output_t *out,const char *rsc_id, const char *host,
159 const char *move_lifetime, cib_t * cib_conn, int cib_options,
160 gboolean promoted_role_only)
161 {
162 char *later_s = parse_cli_lifetime(out, move_lifetime);
163 int rc = pcmk_rc_ok;
164 xmlNode *location = NULL;
165 xmlNode *fragment = NULL;
166
167 if(move_lifetime && later_s == NULL) {
168 return EINVAL;
169 }
170
171 if(cib_conn == NULL) {
172 free(later_s);
173 return ENOTCONN;
174 }
175
176 fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
177
178 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
179 crm_xml_set_id(location, "cli-prefer-%s", rsc_id);
180
181 crm_xml_add(location, XML_LOC_ATTR_SOURCE, rsc_id);
182 if(promoted_role_only) {
183 crm_xml_add(location, XML_RULE_ATTR_ROLE, promoted_role_name());
184 } else {
185 crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_STARTED_S);
186 }
187
188 if (later_s == NULL) {
189
190 crm_xml_add(location, XML_CIB_TAG_NODE, host);
191 crm_xml_add(location, XML_RULE_ATTR_SCORE, CRM_INFINITY_S);
192
193 } else {
194 xmlNode *rule = create_xml_node(location, XML_TAG_RULE);
195 xmlNode *expr = create_xml_node(rule, XML_TAG_EXPRESSION);
196
197 crm_xml_set_id(rule, "cli-prefer-rule-%s", rsc_id);
198 crm_xml_add(rule, XML_RULE_ATTR_SCORE, CRM_INFINITY_S);
199 crm_xml_add(rule, XML_RULE_ATTR_BOOLEAN_OP, "and");
200
201 crm_xml_set_id(expr, "cli-prefer-expr-%s", rsc_id);
202 crm_xml_add(expr, XML_EXPR_ATTR_ATTRIBUTE, CRM_ATTR_UNAME);
203 crm_xml_add(expr, XML_EXPR_ATTR_OPERATION, "eq");
204 crm_xml_add(expr, XML_EXPR_ATTR_VALUE, host);
205 crm_xml_add(expr, XML_EXPR_ATTR_TYPE, "string");
206
207 expr = create_xml_node(rule, "date_expression");
208 crm_xml_set_id(expr, "cli-prefer-lifetime-end-%s", rsc_id);
209 crm_xml_add(expr, "operation", "lt");
210 crm_xml_add(expr, "end", later_s);
211 }
212
213 crm_log_xml_info(fragment, "Modify");
214 rc = cib_conn->cmds->update(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);
215 rc = pcmk_legacy2rc(rc);
216
217 free_xml(fragment);
218 free(later_s);
219 return rc;
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 static int
243 resource_clear_node_in_expr(const char *rsc_id, const char *host, cib_t * cib_conn,
244 int cib_options)
245 {
246 int rc = pcmk_rc_ok;
247 char *xpath_string = NULL;
248
249 xpath_string = crm_strdup_printf("//rsc_location[@id='cli-prefer-%s'][rule[@id='cli-prefer-rule-%s']/expression[@attribute='#uname' and @value='%s']]",
250 rsc_id, rsc_id, host);
251
252 rc = cib_conn->cmds->remove(cib_conn, xpath_string, NULL, cib_xpath | cib_options);
253 if (rc == -ENXIO) {
254 rc = pcmk_rc_ok;
255 } else {
256 rc = pcmk_legacy2rc(rc);
257 }
258
259 free(xpath_string);
260 return rc;
261 }
262
263
264 static int
265 resource_clear_node_in_location(const char *rsc_id, const char *host, cib_t * cib_conn,
266 int cib_options, bool clear_ban_constraints, gboolean force)
267 {
268 int rc = pcmk_rc_ok;
269 xmlNode *fragment = NULL;
270 xmlNode *location = NULL;
271
272 fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
273
274 if (clear_ban_constraints == TRUE) {
275 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
276 crm_xml_set_id(location, "cli-ban-%s-on-%s", rsc_id, host);
277 }
278
279 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
280 crm_xml_set_id(location, "cli-prefer-%s", rsc_id);
281 if (force == FALSE) {
282 crm_xml_add(location, XML_CIB_TAG_NODE, host);
283 }
284
285 crm_log_xml_info(fragment, "Delete");
286 rc = cib_conn->cmds->remove(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);
287 if (rc == -ENXIO) {
288 rc = pcmk_rc_ok;
289 } else {
290 rc = pcmk_legacy2rc(rc);
291 }
292
293 free_xml(fragment);
294 return rc;
295 }
296
297
298 int
299 cli_resource_clear(const char *rsc_id, const char *host, GList *allnodes, cib_t * cib_conn,
300 int cib_options, bool clear_ban_constraints, gboolean force)
301 {
302 int rc = pcmk_rc_ok;
303
304 if(cib_conn == NULL) {
305 return ENOTCONN;
306 }
307
308 if (host) {
309 rc = resource_clear_node_in_expr(rsc_id, host, cib_conn, cib_options);
310
311
312
313
314
315 if (rc == pcmk_rc_ok) {
316 rc = resource_clear_node_in_location(rsc_id, host, cib_conn,
317 cib_options, clear_ban_constraints,
318 force);
319 }
320
321 } else {
322 GList *n = allnodes;
323
324
325
326
327 for(; n; n = n->next) {
328 pe_node_t *target = n->data;
329
330 rc = cli_resource_clear(rsc_id, target->details->uname, NULL,
331 cib_conn, cib_options, clear_ban_constraints,
332 force);
333 if (rc != pcmk_rc_ok) {
334 break;
335 }
336 }
337 }
338
339 return rc;
340 }
341
342 static void
343 build_clear_xpath_string(GString *buf, const xmlNode *constraint_node,
344 const char *rsc, const char *node,
345 bool promoted_role_only)
346 {
347 const char *cons_id = ID(constraint_node);
348 const char *cons_rsc = crm_element_value(constraint_node,
349 XML_LOC_ATTR_SOURCE);
350 GString *rsc_role_substr = NULL;
351
352 CRM_ASSERT(buf != NULL);
353 g_string_truncate(buf, 0);
354
355 if (!pcmk__starts_with(cons_id, "cli-ban-")
356 && !pcmk__starts_with(cons_id, "cli-prefer-")) {
357 return;
358 }
359
360 g_string_append(buf, "//" XML_CONS_TAG_RSC_LOCATION);
361
362 if ((node != NULL) || (rsc != NULL) || promoted_role_only) {
363 g_string_append_c(buf, '[');
364
365 if (node != NULL) {
366 pcmk__g_strcat(buf, "@" XML_CIB_TAG_NODE "='", node, "'", NULL);
367
368 if (promoted_role_only || (rsc != NULL)) {
369 g_string_append(buf, " and ");
370 }
371 }
372
373 if ((rsc != NULL) && promoted_role_only) {
374 rsc_role_substr = g_string_sized_new(64);
375 pcmk__g_strcat(rsc_role_substr,
376 "@" XML_LOC_ATTR_SOURCE "='", rsc, "' "
377 "and @" XML_RULE_ATTR_ROLE "='",
378 promoted_role_name(), "'", NULL);
379
380 } else if (rsc != NULL) {
381 rsc_role_substr = g_string_sized_new(64);
382 pcmk__g_strcat(rsc_role_substr,
383 "@" XML_LOC_ATTR_SOURCE "='", rsc, "'", NULL);
384
385 } else if (promoted_role_only) {
386 rsc_role_substr = g_string_sized_new(64);
387 pcmk__g_strcat(rsc_role_substr,
388 "@" XML_RULE_ATTR_ROLE "='", promoted_role_name(),
389 "'", NULL);
390 }
391
392 if (rsc_role_substr != NULL) {
393 g_string_append(buf, rsc_role_substr->str);
394 }
395 g_string_append_c(buf, ']');
396 }
397
398 if (node != NULL) {
399 g_string_append(buf, "|//" XML_CONS_TAG_RSC_LOCATION);
400
401 if (rsc_role_substr != NULL) {
402 pcmk__g_strcat(buf, "[", rsc_role_substr, "]", NULL);
403 }
404 pcmk__g_strcat(buf,
405 "/" XML_TAG_RULE "[" XML_TAG_EXPRESSION
406 "[@" XML_EXPR_ATTR_ATTRIBUTE "='" CRM_ATTR_UNAME "' "
407 "and @" XML_EXPR_ATTR_VALUE "='", node, "']]", NULL);
408 }
409
410 g_string_append(buf, "//" PCMK_XE_DATE_EXPRESSION "[@" XML_ATTR_ID "='");
411 if (pcmk__starts_with(cons_id, "cli-ban-")) {
412 pcmk__g_strcat(buf, cons_id, "-lifetime']", NULL);
413
414 } else {
415 pcmk__g_strcat(buf,
416 "cli-prefer-lifetime-end-", cons_rsc, "']", NULL);
417 }
418
419 if (rsc_role_substr != NULL) {
420 g_string_free(rsc_role_substr, TRUE);
421 }
422 }
423
424
425 int
426 cli_resource_clear_all_expired(xmlNode *root, cib_t *cib_conn, int cib_options,
427 const char *rsc, const char *node, gboolean promoted_role_only)
428 {
429 GString *buf = NULL;
430 xmlXPathObject *xpathObj = NULL;
431 xmlNode *cib_constraints = NULL;
432 crm_time_t *now = crm_time_new(NULL);
433 int i;
434 int rc = pcmk_rc_ok;
435
436 cib_constraints = pcmk_find_cib_element(root, XML_CIB_TAG_CONSTRAINTS);
437 xpathObj = xpath_search(cib_constraints, "//" XML_CONS_TAG_RSC_LOCATION);
438
439 for (i = 0; i < numXpathResults(xpathObj); i++) {
440 xmlNode *constraint_node = getXpathResult(xpathObj, i);
441 xmlNode *date_expr_node = NULL;
442 crm_time_t *end = NULL;
443
444 if (buf == NULL) {
445 buf = g_string_sized_new(1024);
446 }
447
448 build_clear_xpath_string(buf, constraint_node, rsc, node,
449 promoted_role_only);
450 if (buf->len == 0) {
451 continue;
452 }
453
454 date_expr_node = get_xpath_object((const char *) buf->str,
455 constraint_node, LOG_DEBUG);
456 if (date_expr_node == NULL) {
457 continue;
458 }
459
460
461
462
463 end = crm_time_new(crm_element_value(date_expr_node, "end"));
464
465 if (crm_time_compare(now, end) == 1) {
466 xmlNode *fragment = NULL;
467 xmlNode *location = NULL;
468
469 fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
470 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
471 crm_xml_set_id(location, "%s", ID(constraint_node));
472 crm_log_xml_info(fragment, "Delete");
473
474 rc = cib_conn->cmds->remove(cib_conn, XML_CIB_TAG_CONSTRAINTS,
475 fragment, cib_options);
476 rc = pcmk_legacy2rc(rc);
477
478 if (rc != pcmk_rc_ok) {
479 goto done;
480 }
481
482 free_xml(fragment);
483 }
484
485 crm_time_free(end);
486 }
487
488 done:
489 if (buf != NULL) {
490 g_string_free(buf, TRUE);
491 }
492 freeXpathObject(xpathObj);
493 crm_time_free(now);
494 return rc;
495 }