summaryrefslogtreecommitdiffstats
path: root/tools/crm_resource_ban.c
blob: 689a17867b7abc947f8c41546d1ac76ce33b8594 (plain)
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * Copyright 2004-2024 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU General Public License version 2
 * or later (GPLv2+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <crm_resource.h>

static char *
parse_cli_lifetime(pcmk__output_t *out, const char *move_lifetime)
{
    char *later_s = NULL;
    crm_time_t *now = NULL;
    crm_time_t *later = NULL;
    crm_time_t *duration = NULL;

    if (move_lifetime == NULL) {
        return NULL;
    }

    duration = crm_time_parse_duration(move_lifetime);
    if (duration == NULL) {
        out->err(out, "Invalid duration specified: %s\n"
                      "Please refer to https://en.wikipedia.org/wiki/ISO_8601#Durations "
                      "for examples of valid durations", move_lifetime);
        return NULL;
    }

    now = crm_time_new(NULL);
    later = crm_time_add(now, duration);
    if (later == NULL) {
        out->err(out, "Unable to add %s to current time\n"
                      "Please report to " PACKAGE_BUGREPORT " as possible bug",
                      move_lifetime);
        crm_time_free(now);
        crm_time_free(duration);
        return NULL;
    }

    crm_time_log(LOG_INFO, "now     ", now,
                 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
    crm_time_log(LOG_INFO, "later   ", later,
                 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
    crm_time_log(LOG_INFO, "duration", duration, crm_time_log_date | crm_time_log_timeofday);
    later_s = crm_time_as_string(later, crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
    out->info(out, "Migration will take effect until: %s", later_s);

    crm_time_free(duration);
    crm_time_free(later);
    crm_time_free(now);
    return later_s;
}

// \return Standard Pacemaker return code
int
cli_resource_ban(pcmk__output_t *out, const char *rsc_id, const char *host,
                 const char *move_lifetime, cib_t * cib_conn, int cib_options,
                 gboolean promoted_role_only, const char *promoted_role)
{
    char *later_s = NULL;
    int rc = pcmk_rc_ok;
    xmlNode *fragment = NULL;
    xmlNode *location = NULL;

    later_s = parse_cli_lifetime(out, move_lifetime);
    if(move_lifetime && later_s == NULL) {
        return EINVAL;
    }

    fragment = pcmk__xe_create(NULL, PCMK_XE_CONSTRAINTS);

    location = pcmk__xe_create(fragment, PCMK_XE_RSC_LOCATION);
    crm_xml_set_id(location, "cli-ban-%s-on-%s", rsc_id, host);

    out->info(out,
              "WARNING: Creating " PCMK_XE_RSC_LOCATION " constraint '%s' with "
              "a score of " PCMK_VALUE_MINUS_INFINITY " for resource %s on %s."
              "\n\tThis will prevent %s from %s on %s until the constraint is "
              "removed using the clear option or by editing the CIB with an "
              "appropriate tool.\n"
              "\tThis will be the case even if %s is the last node in the "
              "cluster",
              pcmk__xe_id(location), rsc_id, host, rsc_id,
              (promoted_role_only? "being promoted" : "running"), host, host);

    crm_xml_add(location, PCMK_XA_RSC, rsc_id);
    if(promoted_role_only) {
        crm_xml_add(location, PCMK_XA_ROLE, promoted_role);
    } else {
        crm_xml_add(location, PCMK_XA_ROLE, PCMK_ROLE_STARTED);
    }

    if (later_s == NULL) {
        /* Short form */
        crm_xml_add(location, PCMK_XE_NODE, host);
        crm_xml_add(location, PCMK_XA_SCORE, PCMK_VALUE_MINUS_INFINITY);

    } else {
        xmlNode *rule = pcmk__xe_create(location, PCMK_XE_RULE);
        xmlNode *expr = pcmk__xe_create(rule, PCMK_XE_EXPRESSION);

        crm_xml_set_id(rule, "cli-ban-%s-on-%s-rule", rsc_id, host);
        crm_xml_add(rule, PCMK_XA_SCORE, PCMK_VALUE_MINUS_INFINITY);
        crm_xml_add(rule, PCMK_XA_BOOLEAN_OP, PCMK_VALUE_AND);

        crm_xml_set_id(expr, "cli-ban-%s-on-%s-expr", rsc_id, host);
        crm_xml_add(expr, PCMK_XA_ATTRIBUTE, CRM_ATTR_UNAME);
        crm_xml_add(expr, PCMK_XA_OPERATION, PCMK_VALUE_EQ);
        crm_xml_add(expr, PCMK_XA_VALUE, host);
        crm_xml_add(expr, PCMK_XA_TYPE, PCMK_VALUE_STRING);

        expr = pcmk__xe_create(rule, PCMK_XE_DATE_EXPRESSION);
        crm_xml_set_id(expr, "cli-ban-%s-on-%s-lifetime", rsc_id, host);
        crm_xml_add(expr, PCMK_XA_OPERATION, PCMK_VALUE_LT);
        crm_xml_add(expr, PCMK_XA_END, later_s);
    }

    crm_log_xml_notice(fragment, "Modify");
    rc = cib_conn->cmds->modify(cib_conn, PCMK_XE_CONSTRAINTS, fragment,
                                cib_options);
    rc = pcmk_legacy2rc(rc);

    free_xml(fragment);
    free(later_s);

    if ((rc != pcmk_rc_ok)
        && promoted_role_only
        && (strcmp(promoted_role, PCMK_ROLE_PROMOTED) == 0)) {

        int banrc = cli_resource_ban(out, rsc_id, host, move_lifetime,
                              cib_conn, cib_options, promoted_role_only,
                              PCMK__ROLE_PROMOTED_LEGACY);
        if (banrc == pcmk_rc_ok) {
            rc = banrc;
        }
    }

    return rc;
}

// \return Standard Pacemaker return code
int
cli_resource_prefer(pcmk__output_t *out,const char *rsc_id, const char *host,
                    const char *move_lifetime, cib_t *cib_conn, int cib_options,
                    gboolean promoted_role_only, const char *promoted_role)
{
    char *later_s = parse_cli_lifetime(out, move_lifetime);
    int rc = pcmk_rc_ok;
    xmlNode *location = NULL;
    xmlNode *fragment = NULL;

    if(move_lifetime && later_s == NULL) {
        return EINVAL;
    }

    if(cib_conn == NULL) {
        free(later_s);
        return ENOTCONN;
    }

    fragment = pcmk__xe_create(NULL, PCMK_XE_CONSTRAINTS);

    location = pcmk__xe_create(fragment, PCMK_XE_RSC_LOCATION);
    crm_xml_set_id(location, "cli-prefer-%s", rsc_id);

    crm_xml_add(location, PCMK_XA_RSC, rsc_id);
    if(promoted_role_only) {
        crm_xml_add(location, PCMK_XA_ROLE, promoted_role);
    } else {
        crm_xml_add(location, PCMK_XA_ROLE, PCMK_ROLE_STARTED);
    }

    if (later_s == NULL) {
        /* Short form */
        crm_xml_add(location, PCMK_XE_NODE, host);
        crm_xml_add(location, PCMK_XA_SCORE, PCMK_VALUE_INFINITY);

    } else {
        xmlNode *rule = pcmk__xe_create(location, PCMK_XE_RULE);
        xmlNode *expr = pcmk__xe_create(rule, PCMK_XE_EXPRESSION);

        crm_xml_set_id(rule, "cli-prefer-rule-%s", rsc_id);
        crm_xml_add(rule, PCMK_XA_SCORE, PCMK_VALUE_INFINITY);
        crm_xml_add(rule, PCMK_XA_BOOLEAN_OP, PCMK_VALUE_AND);

        crm_xml_set_id(expr, "cli-prefer-expr-%s", rsc_id);
        crm_xml_add(expr, PCMK_XA_ATTRIBUTE, CRM_ATTR_UNAME);
        crm_xml_add(expr, PCMK_XA_OPERATION, PCMK_VALUE_EQ);
        crm_xml_add(expr, PCMK_XA_VALUE, host);
        crm_xml_add(expr, PCMK_XA_TYPE, PCMK_VALUE_STRING);

        expr = pcmk__xe_create(rule, PCMK_XE_DATE_EXPRESSION);
        crm_xml_set_id(expr, "cli-prefer-lifetime-end-%s", rsc_id);
        crm_xml_add(expr, PCMK_XA_OPERATION, PCMK_VALUE_LT);
        crm_xml_add(expr, PCMK_XA_END, later_s);
    }

    crm_log_xml_info(fragment, "Modify");
    rc = cib_conn->cmds->modify(cib_conn, PCMK_XE_CONSTRAINTS, fragment,
                                cib_options);
    rc = pcmk_legacy2rc(rc);

    free_xml(fragment);
    free(later_s);

    if ((rc != pcmk_rc_ok)
        && promoted_role_only
        && (strcmp(promoted_role, PCMK_ROLE_PROMOTED) == 0)) {

        int preferrc = cli_resource_prefer(out, rsc_id, host, move_lifetime,
                                 cib_conn, cib_options, promoted_role_only,
                                 PCMK__ROLE_PROMOTED_LEGACY);
        if (preferrc == pcmk_rc_ok) {
            rc = preferrc;
        }
    }

    return rc;
}

/* Nodes can be specified two different ways in the CIB, so we have two different
 * functions to try clearing out any constraints on them:
 *
 * (1) The node could be given by attribute=/value= in an expression XML node.
 * That's what resource_clear_node_in_expr handles.  That XML looks like this:
 *
 * <rsc_location id="cli-prefer-dummy" rsc="dummy" role="Started">
 *   <rule id="cli-prefer-rule-dummy" score="INFINITY" boolean-op="and">
 *     <expression id="cli-prefer-expr-dummy" attribute="#uname" operation="eq" value="test02" type="string"/>
 *     <date_expression id="cli-prefer-lifetime-end-dummy" operation="lt" end="2018-12-12 14:05:37 -05:00"/>
 *   </rule>
 * </rsc_location>
 *
 * (2) The node could be given by node= in a PCMK_XE_RSC_LOCATION XML node.
 * That's what resource_clear_node_in_location handles. That XML looks like
 * this:
 *
 * <rsc_location id="cli-prefer-dummy" rsc="dummy" role="Started" node="node1" score="INFINITY"/>
 *
 * \return Standard Pacemaker return code
 */
static int
resource_clear_node_in_expr(const char *rsc_id, const char *host, cib_t * cib_conn,
                            int cib_options)
{
    int rc = pcmk_rc_ok;
    char *xpath_string = NULL;

#define XPATH_FMT                                                   \
    "//" PCMK_XE_RSC_LOCATION "[@" PCMK_XA_ID "='cli-prefer-%s']"   \
    "[" PCMK_XE_RULE                                                \
        "[@" PCMK_XA_ID "='cli-prefer-rule-%s']"                    \
        "/" PCMK_XE_EXPRESSION                                      \
        "[@" PCMK_XA_ATTRIBUTE "='" CRM_ATTR_UNAME "' "             \
        "and @" PCMK_XA_VALUE "='%s']"                              \
    "]"

    xpath_string = crm_strdup_printf(XPATH_FMT, rsc_id, rsc_id, host);

    rc = cib_conn->cmds->remove(cib_conn, xpath_string, NULL, cib_xpath | cib_options);
    if (rc == -ENXIO) {
        rc = pcmk_rc_ok;
    } else {
        rc = pcmk_legacy2rc(rc);
    }

    free(xpath_string);
    return rc;
}

// \return Standard Pacemaker return code
static int
resource_clear_node_in_location(const char *rsc_id, const char *host, cib_t * cib_conn,
                                int cib_options, bool clear_ban_constraints, gboolean force)
{
    int rc = pcmk_rc_ok;
    xmlNode *fragment = NULL;
    xmlNode *location = NULL;

    fragment = pcmk__xe_create(NULL, PCMK_XE_CONSTRAINTS);

    if (clear_ban_constraints == TRUE) {
        location = pcmk__xe_create(fragment, PCMK_XE_RSC_LOCATION);
        crm_xml_set_id(location, "cli-ban-%s-on-%s", rsc_id, host);
    }

    location = pcmk__xe_create(fragment, PCMK_XE_RSC_LOCATION);
    crm_xml_set_id(location, "cli-prefer-%s", rsc_id);
    if (force == FALSE) {
        crm_xml_add(location, PCMK_XE_NODE, host);
    }

    crm_log_xml_info(fragment, "Delete");
    rc = cib_conn->cmds->remove(cib_conn, PCMK_XE_CONSTRAINTS, fragment,
                                cib_options);
    if (rc == -ENXIO) {
        rc = pcmk_rc_ok;
    } else {
        rc = pcmk_legacy2rc(rc);
    }

    free_xml(fragment);
    return rc;
}

// \return Standard Pacemaker return code
int
cli_resource_clear(const char *rsc_id, const char *host, GList *allnodes, cib_t * cib_conn,
                   int cib_options, bool clear_ban_constraints, gboolean force)
{
    int rc = pcmk_rc_ok;

    if(cib_conn == NULL) {
        return ENOTCONN;
    }

    if (host) {
        rc = resource_clear_node_in_expr(rsc_id, host, cib_conn, cib_options);

        /* rc does not tell us whether the previous operation did anything, only
         * whether it failed or not.  Thus, as long as it did not fail, we need
         * to try the second clear method.
         */
        if (rc == pcmk_rc_ok) {
            rc = resource_clear_node_in_location(rsc_id, host, cib_conn,
                                                 cib_options, clear_ban_constraints,
                                                 force);
        }

    } else {
        GList *n = allnodes;

        /* Iterate over all nodes, attempting to clear the constraint from each.
         * On the first error, abort.
         */
        for(; n; n = n->next) {
            pcmk_node_t *target = n->data;

            rc = cli_resource_clear(rsc_id, target->details->uname, NULL,
                                    cib_conn, cib_options, clear_ban_constraints,
                                    force);
            if (rc != pcmk_rc_ok) {
                break;
            }
        }
    }

    return rc;
}

static void
build_clear_xpath_string(GString *buf, const xmlNode *constraint_node,
                         const char *rsc, const char *node,
                         bool promoted_role_only)
{
    const char *cons_id = pcmk__xe_id(constraint_node);
    const char *cons_rsc = crm_element_value(constraint_node, PCMK_XA_RSC);
    GString *rsc_role_substr = NULL;
    const char *promoted_role_rule = "@" PCMK_XA_ROLE "='" PCMK_ROLE_PROMOTED
                                     "' or @" PCMK_XA_ROLE "='"
                                     PCMK__ROLE_PROMOTED_LEGACY "'";

    CRM_ASSERT(buf != NULL);
    g_string_truncate(buf, 0);

    if (!pcmk__starts_with(cons_id, "cli-ban-")
        && !pcmk__starts_with(cons_id, "cli-prefer-")) {
        return;
    }

    g_string_append(buf, "//" PCMK_XE_RSC_LOCATION);

    if ((node != NULL) || (rsc != NULL) || promoted_role_only) {
        g_string_append_c(buf, '[');

        if (node != NULL) {
            pcmk__g_strcat(buf, "@" PCMK_XE_NODE "='", node, "'", NULL);

            if (promoted_role_only || (rsc != NULL)) {
                g_string_append(buf, " and ");
            }
        }

        if ((rsc != NULL) && promoted_role_only) {
            rsc_role_substr = g_string_sized_new(64);
            pcmk__g_strcat(rsc_role_substr,
                           "@" PCMK_XA_RSC "='", rsc, "' "
                           "and (" , promoted_role_rule, ")", NULL);

        } else if (rsc != NULL) {
            rsc_role_substr = g_string_sized_new(64);
            pcmk__g_strcat(rsc_role_substr,
                           "@" PCMK_XA_RSC "='", rsc, "'", NULL);

        } else if (promoted_role_only) {
            rsc_role_substr = g_string_sized_new(64);
            g_string_append(rsc_role_substr, promoted_role_rule);
        }

        if (rsc_role_substr != NULL) {
            g_string_append(buf, rsc_role_substr->str);
        }
        g_string_append_c(buf, ']');
    }

    if (node != NULL) {
        g_string_append(buf, "|//" PCMK_XE_RSC_LOCATION);

        if (rsc_role_substr != NULL) {
            pcmk__g_strcat(buf, "[", rsc_role_substr, "]", NULL);
        }
        pcmk__g_strcat(buf,
                       "/" PCMK_XE_RULE "[" PCMK_XE_EXPRESSION
                       "[@" PCMK_XA_ATTRIBUTE "='" CRM_ATTR_UNAME "' "
                       "and @" PCMK_XA_VALUE "='", node, "']]", NULL);
    }

    g_string_append(buf, "//" PCMK_XE_DATE_EXPRESSION "[@" PCMK_XA_ID "='");
    if (pcmk__starts_with(cons_id, "cli-ban-")) {
        pcmk__g_strcat(buf, cons_id, "-lifetime']", NULL);

    } else {    // starts with "cli-prefer-"
        pcmk__g_strcat(buf,
                       "cli-prefer-lifetime-end-", cons_rsc, "']", NULL);
    }

    if (rsc_role_substr != NULL) {
        g_string_free(rsc_role_substr, TRUE);
    }
}

// \return Standard Pacemaker return code
int
cli_resource_clear_all_expired(xmlNode *root, cib_t *cib_conn, int cib_options,
                               const char *rsc, const char *node, gboolean promoted_role_only)
{
    GString *buf = NULL;
    xmlXPathObject *xpathObj = NULL;
    xmlNode *cib_constraints = NULL;
    crm_time_t *now = crm_time_new(NULL);
    int i;
    int rc = pcmk_rc_ok;

    cib_constraints = pcmk_find_cib_element(root, PCMK_XE_CONSTRAINTS);
    xpathObj = xpath_search(cib_constraints, "//" PCMK_XE_RSC_LOCATION);

    for (i = 0; i < numXpathResults(xpathObj); i++) {
        xmlNode *constraint_node = getXpathResult(xpathObj, i);
        xmlNode *date_expr_node = NULL;
        crm_time_t *end = NULL;
        int rc = pcmk_rc_ok;

        if (buf == NULL) {
            buf = g_string_sized_new(1024);
        }

        build_clear_xpath_string(buf, constraint_node, rsc, node,
                                 promoted_role_only);
        if (buf->len == 0) {
            continue;
        }

        date_expr_node = get_xpath_object((const char *) buf->str,
                                          constraint_node, LOG_DEBUG);
        if (date_expr_node == NULL) {
            continue;
        }

        /* And then finally, see if the date expression is expired.  If so,
         * clear the constraint.
         *
         * @COMPAT Check for error once we are rejecting rules with invalid end
         */
        rc = pcmk__xe_get_datetime(date_expr_node, PCMK_XA_END, &end);
        if (rc != pcmk_rc_ok) {
            crm_trace("Invalid " PCMK_XA_END ": %s", pcmk_rc_str(rc));
        }

        if (crm_time_compare(now, end) == 1) {
            xmlNode *fragment = NULL;
            xmlNode *location = NULL;

            fragment = pcmk__xe_create(NULL, PCMK_XE_CONSTRAINTS);
            location = pcmk__xe_create(fragment, PCMK_XE_RSC_LOCATION);
            crm_xml_set_id(location, "%s", pcmk__xe_id(constraint_node));
            crm_log_xml_info(fragment, "Delete");

            rc = cib_conn->cmds->remove(cib_conn, PCMK_XE_CONSTRAINTS, fragment,
                                        cib_options);
            rc = pcmk_legacy2rc(rc);

            if (rc != pcmk_rc_ok) {
                goto done;
            }

            free_xml(fragment);
        }

        crm_time_free(end);
    }

done:
    if (buf != NULL) {
        g_string_free(buf, TRUE);
    }
    freeXpathObject(xpathObj);
    crm_time_free(now);
    return rc;
}