This source file includes following definitions.
- ra_param_free
- metadata_free
- metadata_cache_new
- metadata_cache_free
- metadata_cache_reset
- ra_param_from_xml
- log_ra_ocf_version
- controld_cache_metadata
- controld_get_rsc_metadata
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13 #include <glib.h>
14 #include <regex.h>
15
16 #include <crm/crm.h>
17 #include <crm/lrmd.h>
18
19 #include <pacemaker-controld.h>
20
21 static void
22 ra_param_free(void *param)
23 {
24 if (param) {
25 struct ra_param_s *p = (struct ra_param_s *) param;
26
27 if (p->rap_name) {
28 free(p->rap_name);
29 }
30 free(param);
31 }
32 }
33
34 static void
35 metadata_free(void *metadata)
36 {
37 if (metadata) {
38 struct ra_metadata_s *md = (struct ra_metadata_s *) metadata;
39
40 g_list_free_full(md->ra_params, ra_param_free);
41 free(metadata);
42 }
43 }
44
45 GHashTable *
46 metadata_cache_new(void)
47 {
48 return pcmk__strkey_table(free, metadata_free);
49 }
50
51 void
52 metadata_cache_free(GHashTable *mdc)
53 {
54 if (mdc) {
55 crm_trace("Destroying metadata cache with %d members", g_hash_table_size(mdc));
56 g_hash_table_destroy(mdc);
57 }
58 }
59
60 void
61 metadata_cache_reset(GHashTable *mdc)
62 {
63 if (mdc) {
64 crm_trace("Resetting metadata cache with %d members",
65 g_hash_table_size(mdc));
66 g_hash_table_remove_all(mdc);
67 }
68 }
69
70 static struct ra_param_s *
71 ra_param_from_xml(xmlNode *param_xml)
72 {
73 const char *param_name = crm_element_value(param_xml, "name");
74 struct ra_param_s *p;
75
76 p = calloc(1, sizeof(struct ra_param_s));
77 if (p == NULL) {
78 return NULL;
79 }
80
81 p->rap_name = strdup(param_name);
82 if (p->rap_name == NULL) {
83 free(p);
84 return NULL;
85 }
86
87 if (pcmk__xe_attr_is_true(param_xml, "reloadable")) {
88 controld_set_ra_param_flags(p, ra_param_reloadable);
89 }
90
91 if (pcmk__xe_attr_is_true(param_xml, "unique")) {
92 controld_set_ra_param_flags(p, ra_param_unique);
93 }
94
95 if (pcmk__xe_attr_is_true(param_xml, "private")) {
96 controld_set_ra_param_flags(p, ra_param_private);
97 }
98 return p;
99 }
100
101 static void
102 log_ra_ocf_version(const char *ra_key, const char *ra_ocf_version)
103 {
104 if (pcmk__str_empty(ra_ocf_version)) {
105 crm_warn("%s does not advertise OCF version supported", ra_key);
106
107 } else if (compare_version(ra_ocf_version, "2") >= 0) {
108 crm_warn("%s supports OCF version %s (this Pacemaker version supports "
109 PCMK_OCF_VERSION " and might not work properly with agent)",
110 ra_key, ra_ocf_version);
111
112 } else if (compare_version(ra_ocf_version, PCMK_OCF_VERSION) > 0) {
113 crm_info("%s supports OCF version %s (this Pacemaker version supports "
114 PCMK_OCF_VERSION " and might not use all agent features)",
115 ra_key, ra_ocf_version);
116
117 } else {
118 crm_debug("%s supports OCF version %s", ra_key, ra_ocf_version);
119 }
120 }
121
122 struct ra_metadata_s *
123 controld_cache_metadata(GHashTable *mdc, const lrmd_rsc_info_t *rsc,
124 const char *metadata_str)
125 {
126 char *key = NULL;
127 const char *reason = NULL;
128 xmlNode *metadata = NULL;
129 xmlNode *match = NULL;
130 struct ra_metadata_s *md = NULL;
131 bool any_private_params = false;
132 bool ocf1_1 = false;
133
134 CRM_CHECK(mdc && rsc && metadata_str, return NULL);
135
136 key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
137 if (!key) {
138 reason = "Invalid resource agent standard or type";
139 goto err;
140 }
141
142 metadata = string2xml(metadata_str);
143 if (!metadata) {
144 reason = "Metadata is not valid XML";
145 goto err;
146 }
147
148 md = calloc(1, sizeof(struct ra_metadata_s));
149 if (md == NULL) {
150 reason = "Could not allocate memory";
151 goto err;
152 }
153
154 if (strcmp(rsc->standard, PCMK_RESOURCE_CLASS_OCF) == 0) {
155 xmlChar *content = NULL;
156 xmlNode *version_element = first_named_child(metadata, "version");
157
158 if (version_element != NULL) {
159 content = xmlNodeGetContent(version_element);
160 }
161 log_ra_ocf_version(key, (const char *) content);
162 if (content != NULL) {
163 ocf1_1 = (compare_version((const char *) content, "1.1") >= 0);
164 xmlFree(content);
165 }
166 }
167
168
169 match = first_named_child(metadata, "actions");
170 for (match = first_named_child(match, "action"); match != NULL;
171 match = crm_next_same_xml(match)) {
172
173 const char *action_name = crm_element_value(match, "name");
174
175 if (pcmk__str_eq(action_name, PCMK_ACTION_RELOAD_AGENT,
176 pcmk__str_none)) {
177 if (ocf1_1) {
178 controld_set_ra_flags(md, key, ra_supports_reload_agent);
179 } else {
180 crm_notice("reload-agent action will not be used with %s "
181 "because it does not support OCF 1.1 or later", key);
182 }
183
184 } else if (!ocf1_1 && pcmk__str_eq(action_name, PCMK_ACTION_RELOAD,
185 pcmk__str_casei)) {
186 controld_set_ra_flags(md, key, ra_supports_legacy_reload);
187 }
188 }
189
190
191 match = first_named_child(metadata, "parameters");
192 for (match = first_named_child(match, "parameter"); match != NULL;
193 match = crm_next_same_xml(match)) {
194
195 const char *param_name = crm_element_value(match, "name");
196
197 if (param_name == NULL) {
198 crm_warn("Metadata for %s:%s:%s has parameter without a name",
199 rsc->standard, rsc->provider, rsc->type);
200 } else {
201 struct ra_param_s *p = ra_param_from_xml(match);
202
203 if (p == NULL) {
204 reason = "Could not allocate memory";
205 goto err;
206 }
207 if (pcmk_is_set(p->rap_flags, ra_param_private)) {
208 any_private_params = true;
209 }
210 md->ra_params = g_list_prepend(md->ra_params, p);
211 }
212 }
213
214
215
216
217
218
219 if (!any_private_params) {
220 for (GList *iter = md->ra_params; iter != NULL; iter = iter->next) {
221 struct ra_param_s *p = iter->data;
222
223 if (pcmk__str_any_of(p->rap_name, "password", "passwd", "user",
224 NULL)) {
225 controld_set_ra_param_flags(p, ra_param_private);
226 }
227 }
228 }
229
230 g_hash_table_replace(mdc, key, md);
231 free_xml(metadata);
232 return md;
233
234 err:
235 crm_warn("Unable to update metadata for %s (%s%s%s:%s): %s",
236 rsc->id, rsc->standard, ((rsc->provider == NULL)? "" : ":"),
237 pcmk__s(rsc->provider, ""), rsc->type, reason);
238 free(key);
239 free_xml(metadata);
240 metadata_free(md);
241 return NULL;
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255 struct ra_metadata_s *
256 controld_get_rsc_metadata(lrm_state_t *lrm_state, const lrmd_rsc_info_t *rsc,
257 uint32_t source)
258 {
259 struct ra_metadata_s *metadata = NULL;
260 char *metadata_str = NULL;
261 char *key = NULL;
262 int rc = pcmk_ok;
263
264 CRM_CHECK((lrm_state != NULL) && (rsc != NULL), return NULL);
265
266 if (pcmk_is_set(source, controld_metadata_from_cache)) {
267 key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
268 if (key != NULL) {
269 metadata = g_hash_table_lookup(lrm_state->metadata_cache, key);
270 free(key);
271 }
272 if (metadata != NULL) {
273 crm_debug("Retrieved metadata for %s (%s%s%s:%s) from cache",
274 rsc->id, rsc->standard,
275 ((rsc->provider == NULL)? "" : ":"),
276 ((rsc->provider == NULL)? "" : rsc->provider),
277 rsc->type);
278 return metadata;
279 }
280 }
281
282 if (!pcmk_is_set(source, controld_metadata_from_agent)) {
283 return NULL;
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300 crm_debug("Retrieving metadata for %s (%s%s%s:%s) synchronously",
301 rsc->id, rsc->standard,
302 ((rsc->provider == NULL)? "" : ":"),
303 ((rsc->provider == NULL)? "" : rsc->provider),
304 rsc->type);
305 rc = lrm_state_get_metadata(lrm_state, rsc->standard, rsc->provider,
306 rsc->type, &metadata_str, 0);
307 if (rc != pcmk_ok) {
308 crm_warn("Failed to get metadata for %s (%s%s%s:%s): %s",
309 rsc->id, rsc->standard,
310 ((rsc->provider == NULL)? "" : ":"),
311 ((rsc->provider == NULL)? "" : rsc->provider),
312 rsc->type, pcmk_strerror(rc));
313 return NULL;
314 }
315
316 metadata = controld_cache_metadata(lrm_state->metadata_cache, rsc,
317 metadata_str);
318 free(metadata_str);
319 return metadata;
320 }