This source file includes following definitions.
- new_string_cb
- original_string_cb
- patch_cb
- print_patch
- apply_patch
- log_patch_cib_versions
- strip_patch_cib_version
- generate_patch
- build_arg_context
- main
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <sys/param.h>
18 #include <sys/types.h>
19
20 #include <crm/crm.h>
21 #include <crm/msg_xml.h>
22 #include <crm/common/cmdline_internal.h>
23 #include <crm/common/output_internal.h>
24 #include <crm/common/xml.h>
25 #include <crm/common/ipc.h>
26 #include <crm/cib.h>
27
28 #define SUMMARY "Compare two Pacemaker configurations (in XML format) to produce a custom diff-like output, " \
29 "or apply such an output as a patch"
30
31 struct {
32 gboolean apply;
33 gboolean as_cib;
34 gboolean no_version;
35 gboolean raw_1;
36 gboolean raw_2;
37 gboolean use_stdin;
38 char *xml_file_1;
39 char *xml_file_2;
40 } options;
41
42 gboolean new_string_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
43 gboolean original_string_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
44 gboolean patch_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
45
46 static GOptionEntry original_xml_entries[] = {
47 { "original", 'o', 0, G_OPTION_ARG_STRING, &options.xml_file_1,
48 "XML is contained in the named file",
49 "FILE" },
50 { "original-string", 'O', 0, G_OPTION_ARG_CALLBACK, original_string_cb,
51 "XML is contained in the supplied string",
52 "STRING" },
53
54 { NULL }
55 };
56
57 static GOptionEntry operation_entries[] = {
58 { "new", 'n', 0, G_OPTION_ARG_STRING, &options.xml_file_2,
59 "Compare the original XML to the contents of the named file",
60 "FILE" },
61 { "new-string", 'N', 0, G_OPTION_ARG_CALLBACK, new_string_cb,
62 "Compare the original XML with the contents of the supplied string",
63 "STRING" },
64 { "patch", 'p', 0, G_OPTION_ARG_CALLBACK, patch_cb,
65 "Patch the original XML with the contents of the named file",
66 "FILE" },
67
68 { NULL }
69 };
70
71 static GOptionEntry addl_entries[] = {
72 { "cib", 'c', 0, G_OPTION_ARG_NONE, &options.as_cib,
73 "Compare/patch the inputs as a CIB (includes versions details)",
74 NULL },
75 { "stdin", 's', 0, G_OPTION_ARG_NONE, &options.use_stdin,
76 "",
77 NULL },
78 { "no-version", 'u', 0, G_OPTION_ARG_NONE, &options.no_version,
79 "Generate the difference without versions details",
80 NULL },
81
82 { NULL }
83 };
84
85 gboolean
86 new_string_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
87 options.raw_2 = TRUE;
88
89 if (options.xml_file_2 != NULL) {
90 free(options.xml_file_2);
91 }
92
93 options.xml_file_2 = strdup(optarg);
94 return TRUE;
95 }
96
97 gboolean
98 original_string_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
99 options.raw_1 = TRUE;
100
101 if (options.xml_file_1 != NULL) {
102 free(options.xml_file_1);
103 }
104
105 options.xml_file_1 = strdup(optarg);
106 return TRUE;
107 }
108
109 gboolean
110 patch_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
111 options.apply = TRUE;
112
113 if (options.xml_file_2 != NULL) {
114 free(options.xml_file_2);
115 }
116
117 options.xml_file_2 = strdup(optarg);
118 return TRUE;
119 }
120
121 static void
122 print_patch(xmlNode *patch)
123 {
124 char *buffer = dump_xml_formatted(patch);
125
126 printf("%s\n", crm_str(buffer));
127 free(buffer);
128 fflush(stdout);
129 }
130
131 static int
132 apply_patch(xmlNode *input, xmlNode *patch, gboolean as_cib)
133 {
134 int rc;
135 xmlNode *output = copy_xml(input);
136
137 rc = xml_apply_patchset(output, patch, as_cib);
138 if (rc != pcmk_ok) {
139 fprintf(stderr, "Could not apply patch: %s\n", pcmk_strerror(rc));
140 free_xml(output);
141 return rc;
142 }
143
144 if (output != NULL) {
145 const char *version;
146 char *buffer;
147
148 print_patch(output);
149
150 version = crm_element_value(output, XML_ATTR_CRM_VERSION);
151 buffer = calculate_xml_versioned_digest(output, FALSE, TRUE, version);
152 crm_trace("Digest: %s\n", crm_str(buffer));
153 free(buffer);
154 free_xml(output);
155 }
156 return pcmk_ok;
157 }
158
159 static void
160 log_patch_cib_versions(xmlNode *patch)
161 {
162 int add[] = { 0, 0, 0 };
163 int del[] = { 0, 0, 0 };
164
165 const char *fmt = NULL;
166 const char *digest = NULL;
167
168 xml_patch_versions(patch, add, del);
169 fmt = crm_element_value(patch, "format");
170 digest = crm_element_value(patch, XML_ATTR_DIGEST);
171
172 if (add[2] != del[2] || add[1] != del[1] || add[0] != del[0]) {
173 crm_info("Patch: --- %d.%d.%d %s", del[0], del[1], del[2], fmt);
174 crm_info("Patch: +++ %d.%d.%d %s", add[0], add[1], add[2], digest);
175 }
176 }
177
178 static void
179 strip_patch_cib_version(xmlNode *patch, const char **vfields, size_t nvfields)
180 {
181 int format = 1;
182
183 crm_element_value_int(patch, "format", &format);
184 if (format == 2) {
185 xmlNode *version_xml = find_xml_node(patch, "version", FALSE);
186
187 if (version_xml) {
188 free_xml(version_xml);
189 }
190
191 } else {
192 int i = 0;
193
194 const char *tags[] = {
195 XML_TAG_DIFF_REMOVED,
196 XML_TAG_DIFF_ADDED,
197 };
198
199 for (i = 0; i < PCMK__NELEM(tags); i++) {
200 xmlNode *tmp = NULL;
201 int lpc;
202
203 tmp = find_xml_node(patch, tags[i], FALSE);
204 if (tmp) {
205 for (lpc = 0; lpc < nvfields; lpc++) {
206 xml_remove_prop(tmp, vfields[lpc]);
207 }
208
209 tmp = find_xml_node(tmp, XML_TAG_CIB, FALSE);
210 if (tmp) {
211 for (lpc = 0; lpc < nvfields; lpc++) {
212 xml_remove_prop(tmp, vfields[lpc]);
213 }
214 }
215 }
216 }
217 }
218 }
219
220 static int
221 generate_patch(xmlNode *object_1, xmlNode *object_2, const char *xml_file_2,
222 gboolean as_cib, gboolean no_version)
223 {
224 xmlNode *output = NULL;
225
226 const char *vfields[] = {
227 XML_ATTR_GENERATION_ADMIN,
228 XML_ATTR_GENERATION,
229 XML_ATTR_NUMUPDATES,
230 };
231
232
233
234 if (no_version) {
235 int lpc;
236
237 for (lpc = 0; lpc < PCMK__NELEM(vfields); lpc++) {
238 crm_copy_xml_element(object_1, object_2, vfields[lpc]);
239 }
240 }
241
242 xml_track_changes(object_2, NULL, object_2, FALSE);
243 if(as_cib) {
244 xml_calculate_significant_changes(object_1, object_2);
245 } else {
246 xml_calculate_changes(object_1, object_2);
247 }
248 crm_log_xml_debug(object_2, (xml_file_2? xml_file_2: "target"));
249
250 output = xml_create_patchset(0, object_1, object_2, NULL, FALSE);
251
252 xml_log_changes(LOG_INFO, __func__, object_2);
253 xml_accept_changes(object_2);
254
255 if (output == NULL) {
256 return pcmk_ok;
257 }
258
259 patchset_process_digest(output, object_1, object_2, as_cib);
260
261 if (as_cib) {
262 log_patch_cib_versions(output);
263
264 } else if (no_version) {
265 strip_patch_cib_version(output, vfields, PCMK__NELEM(vfields));
266 }
267
268 xml_log_patchset(LOG_NOTICE, __func__, output);
269 print_patch(output);
270 free_xml(output);
271 return -pcmk_err_generic;
272 }
273
274 static GOptionContext *
275 build_arg_context(pcmk__common_args_t *args) {
276 GOptionContext *context = NULL;
277
278 const char *description = "Examples:\n\n"
279 "Obtain the two different configuration files by running cibadmin on the two cluster setups to compare:\n\n"
280 "\t# cibadmin --query > cib-old.xml\n\n"
281 "\t# cibadmin --query > cib-new.xml\n\n"
282 "Calculate and save the difference between the two files:\n\n"
283 "\t# crm_diff --original cib-old.xml --new cib-new.xml > patch.xml\n\n"
284 "Apply the patch to the original file:\n\n"
285 "\t# crm_diff --original cib-old.xml --patch patch.xml > updated.xml\n\n"
286 "Apply the patch to the running cluster:\n\n"
287 "\t# cibadmin --patch -x patch.xml\n";
288
289 context = pcmk__build_arg_context(args, NULL, NULL, NULL);
290 g_option_context_set_description(context, description);
291
292 pcmk__add_arg_group(context, "xml", "Original XML:",
293 "Show original XML options", original_xml_entries);
294 pcmk__add_arg_group(context, "operation", "Operation:",
295 "Show operation options", operation_entries);
296 pcmk__add_arg_group(context, "additional", "Additional Options:",
297 "Show additional options", addl_entries);
298 return context;
299 }
300
301 int
302 main(int argc, char **argv)
303 {
304 xmlNode *object_1 = NULL;
305 xmlNode *object_2 = NULL;
306
307 crm_exit_t exit_code = CRM_EX_OK;
308 GError *error = NULL;
309
310 pcmk__common_args_t *args = pcmk__new_common_args(SUMMARY);
311 gchar **processed_args = pcmk__cmdline_preproc(argv, "nopNO");
312 GOptionContext *context = build_arg_context(args);
313
314 if (!g_option_context_parse_strv(context, &processed_args, &error)) {
315 exit_code = CRM_EX_USAGE;
316 goto done;
317 }
318
319 pcmk__cli_init_logging("crm_diff", args->verbosity);
320
321 if (args->version) {
322 g_strfreev(processed_args);
323 pcmk__free_arg_context(context);
324
325 pcmk__cli_help('v', CRM_EX_USAGE);
326 }
327
328 if (options.apply && options.no_version) {
329 fprintf(stderr, "warning: -u/--no-version ignored with -p/--patch\n");
330 } else if (options.as_cib && options.no_version) {
331 fprintf(stderr, "error: -u/--no-version incompatible with -c/--cib\n");
332 exit_code = CRM_EX_USAGE;
333 goto done;
334 }
335
336 if (options.raw_1) {
337 object_1 = string2xml(options.xml_file_1);
338
339 } else if (options.use_stdin) {
340 fprintf(stderr, "Input first XML fragment:");
341 object_1 = stdin2xml();
342
343 } else if (options.xml_file_1 != NULL) {
344 object_1 = filename2xml(options.xml_file_1);
345 }
346
347 if (options.raw_2) {
348 object_2 = string2xml(options.xml_file_2);
349
350 } else if (options.use_stdin) {
351 fprintf(stderr, "Input second XML fragment:");
352 object_2 = stdin2xml();
353
354 } else if (options.xml_file_2 != NULL) {
355 object_2 = filename2xml(options.xml_file_2);
356 }
357
358 if (object_1 == NULL) {
359 fprintf(stderr, "Could not parse the first XML fragment\n");
360 exit_code = CRM_EX_DATAERR;
361 goto done;
362 }
363 if (object_2 == NULL) {
364 fprintf(stderr, "Could not parse the second XML fragment\n");
365 exit_code = CRM_EX_DATAERR;
366 goto done;
367 }
368
369 if (options.apply) {
370 int ret = apply_patch(object_1, object_2, options.as_cib);
371 exit_code = crm_errno2exit(ret);
372 } else {
373 int ret = generate_patch(object_1, object_2, options.xml_file_2, options.as_cib, options.no_version);
374 exit_code = crm_errno2exit(ret);
375 }
376
377 done:
378 g_strfreev(processed_args);
379 pcmk__free_arg_context(context);
380 free(options.xml_file_1);
381 free(options.xml_file_2);
382 free_xml(object_1);
383 free_xml(object_2);
384
385 pcmk__output_and_clear_error(error, NULL);
386 return exit_code;
387 }