This source file includes following definitions.
- parse_cli_lifetime
- cli_resource_ban
- cli_resource_prefer
- cli_resource_clear
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <crm_resource.h>
21 char *move_lifetime = NULL;
22
23 static char *
24 parse_cli_lifetime(const char *input)
25 {
26 char *later_s = NULL;
27 crm_time_t *now = NULL;
28 crm_time_t *later = NULL;
29 crm_time_t *duration = NULL;
30
31 if (input == NULL) {
32 return NULL;
33 }
34
35 duration = crm_time_parse_duration(move_lifetime);
36 if (duration == NULL) {
37 CMD_ERR("Invalid duration specified: %s", move_lifetime);
38 CMD_ERR("Please refer to"
39 " http://en.wikipedia.org/wiki/ISO_8601#Durations"
40 " for examples of valid durations");
41 return NULL;
42 }
43
44 now = crm_time_new(NULL);
45 later = crm_time_add(now, duration);
46 crm_time_log(LOG_INFO, "now ", now,
47 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
48 crm_time_log(LOG_INFO, "later ", later,
49 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
50 crm_time_log(LOG_INFO, "duration", duration, crm_time_log_date | crm_time_log_timeofday);
51 later_s = crm_time_as_string(later, crm_time_log_date | crm_time_log_timeofday);
52 printf("Migration will take effect until: %s\n", later_s);
53
54 crm_time_free(duration);
55 crm_time_free(later);
56 crm_time_free(now);
57 return later_s;
58 }
59
60 int
61 cli_resource_ban(const char *rsc_id, const char *host, GListPtr allnodes, cib_t * cib_conn)
62 {
63 char *later_s = NULL;
64 int rc = pcmk_ok;
65 xmlNode *fragment = NULL;
66 xmlNode *location = NULL;
67
68 if(host == NULL) {
69 GListPtr n = allnodes;
70 for(; n && rc == pcmk_ok; n = n->next) {
71 node_t *target = n->data;
72
73 rc = cli_resource_ban(rsc_id, target->details->uname, NULL, cib_conn);
74 }
75 return rc;
76 }
77
78 later_s = parse_cli_lifetime(move_lifetime);
79 if(move_lifetime && later_s == NULL) {
80 return -EINVAL;
81 }
82
83 fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
84
85 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
86 crm_xml_set_id(location, "cli-ban-%s-on-%s", rsc_id, host);
87
88 if (BE_QUIET == FALSE) {
89 CMD_ERR("WARNING: Creating rsc_location constraint '%s'"
90 " with a score of -INFINITY for resource %s"
91 " on %s.", ID(location), rsc_id, host);
92 CMD_ERR("\tThis will prevent %s from %s"
93 " on %s until the constraint is removed using"
94 " the 'crm_resource --clear' command or manually"
95 " with cibadmin", rsc_id, scope_master?"being promoted":"running", host);
96 CMD_ERR("\tThis will be the case even if %s is"
97 " the last node in the cluster", host);
98 CMD_ERR("\tThis message can be disabled with --quiet");
99 }
100
101 crm_xml_add(location, XML_LOC_ATTR_SOURCE, rsc_id);
102 if(scope_master) {
103 crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_MASTER_S);
104 } else {
105 crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_STARTED_S);
106 }
107
108 if (later_s == NULL) {
109
110 crm_xml_add(location, XML_CIB_TAG_NODE, host);
111 crm_xml_add(location, XML_RULE_ATTR_SCORE, MINUS_INFINITY_S);
112
113 } else {
114 xmlNode *rule = create_xml_node(location, XML_TAG_RULE);
115 xmlNode *expr = create_xml_node(rule, XML_TAG_EXPRESSION);
116
117 crm_xml_set_id(rule, "cli-ban-%s-on-%s-rule", rsc_id, host);
118 crm_xml_add(rule, XML_RULE_ATTR_SCORE, MINUS_INFINITY_S);
119 crm_xml_add(rule, XML_RULE_ATTR_BOOLEAN_OP, "and");
120
121 crm_xml_set_id(expr, "cli-ban-%s-on-%s-expr", rsc_id, host);
122 crm_xml_add(expr, XML_EXPR_ATTR_ATTRIBUTE, CRM_ATTR_UNAME);
123 crm_xml_add(expr, XML_EXPR_ATTR_OPERATION, "eq");
124 crm_xml_add(expr, XML_EXPR_ATTR_VALUE, host);
125 crm_xml_add(expr, XML_EXPR_ATTR_TYPE, "string");
126
127 expr = create_xml_node(rule, "date_expression");
128 crm_xml_set_id(expr, "cli-ban-%s-on-%s-lifetime", rsc_id, host);
129 crm_xml_add(expr, "operation", "lt");
130 crm_xml_add(expr, "end", later_s);
131 }
132
133 crm_log_xml_notice(fragment, "Modify");
134 rc = cib_conn->cmds->update(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);
135
136 free_xml(fragment);
137 free(later_s);
138 return rc;
139 }
140
141
142 int
143 cli_resource_prefer(const char *rsc_id, const char *host, cib_t * cib_conn)
144 {
145 char *later_s = parse_cli_lifetime(move_lifetime);
146 int rc = pcmk_ok;
147 xmlNode *location = NULL;
148 xmlNode *fragment = NULL;
149
150 if(move_lifetime && later_s == NULL) {
151 return -EINVAL;
152 }
153
154 if(cib_conn == NULL) {
155 free(later_s);
156 return -ENOTCONN;
157 }
158
159 fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
160
161 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
162 crm_xml_set_id(location, "cli-prefer-%s", rsc_id);
163
164 crm_xml_add(location, XML_LOC_ATTR_SOURCE, rsc_id);
165 if(scope_master) {
166 crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_MASTER_S);
167 } else {
168 crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_STARTED_S);
169 }
170
171 if (later_s == NULL) {
172
173 crm_xml_add(location, XML_CIB_TAG_NODE, host);
174 crm_xml_add(location, XML_RULE_ATTR_SCORE, INFINITY_S);
175
176 } else {
177 xmlNode *rule = create_xml_node(location, XML_TAG_RULE);
178 xmlNode *expr = create_xml_node(rule, XML_TAG_EXPRESSION);
179
180 crm_xml_set_id(rule, "cli-prefer-rule-%s", rsc_id);
181 crm_xml_add(rule, XML_RULE_ATTR_SCORE, INFINITY_S);
182 crm_xml_add(rule, XML_RULE_ATTR_BOOLEAN_OP, "and");
183
184 crm_xml_set_id(expr, "cli-prefer-expr-%s", rsc_id);
185 crm_xml_add(expr, XML_EXPR_ATTR_ATTRIBUTE, CRM_ATTR_UNAME);
186 crm_xml_add(expr, XML_EXPR_ATTR_OPERATION, "eq");
187 crm_xml_add(expr, XML_EXPR_ATTR_VALUE, host);
188 crm_xml_add(expr, XML_EXPR_ATTR_TYPE, "string");
189
190 expr = create_xml_node(rule, "date_expression");
191 crm_xml_set_id(expr, "cli-prefer-lifetime-end-%s", rsc_id);
192 crm_xml_add(expr, "operation", "lt");
193 crm_xml_add(expr, "end", later_s);
194 }
195
196 crm_log_xml_info(fragment, "Modify");
197 rc = cib_conn->cmds->update(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);
198
199 free_xml(fragment);
200 free(later_s);
201 return rc;
202 }
203
204 int
205 cli_resource_clear(const char *rsc_id, const char *host, GListPtr allnodes, cib_t * cib_conn)
206 {
207 int rc = pcmk_ok;
208 xmlNode *fragment = NULL;
209 xmlNode *location = NULL;
210
211 if(cib_conn == NULL) {
212 return -ENOTCONN;
213 }
214
215 fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);
216
217 if(host) {
218 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
219 crm_xml_set_id(location, "cli-ban-%s-on-%s", rsc_id, host);
220
221 } else {
222 GListPtr n = allnodes;
223 for(; n; n = n->next) {
224 node_t *target = n->data;
225
226 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
227 crm_xml_set_id(location, "cli-ban-%s-on-%s",
228 rsc_id, target->details->uname);
229 }
230 }
231
232 location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
233 crm_xml_set_id(location, "cli-prefer-%s", rsc_id);
234 if(host && do_force == FALSE) {
235 crm_xml_add(location, XML_CIB_TAG_NODE, host);
236 }
237
238 crm_log_xml_info(fragment, "Delete");
239 rc = cib_conn->cmds->delete(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);
240 if (rc == -ENXIO) {
241 rc = pcmk_ok;
242
243 } else if (rc != pcmk_ok) {
244 goto bail;
245 }
246
247 bail:
248 free_xml(fragment);
249 return rc;
250 }