root/tools/crm_diff.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. new_string_cb
  2. original_string_cb
  3. patch_cb
  4. print_patch
  5. apply_patch
  6. log_patch_cib_versions
  7. strip_patch_cib_version
  8. generate_patch
  9. build_arg_context
  10. main

   1 /*
   2  * Copyright 2005-2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  87     options.raw_2 = TRUE;
  88     pcmk__str_update(&options.xml_file_2, optarg);
  89     return TRUE;
  90 }
  91 
  92 gboolean
  93 original_string_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
     /* [previous][next][first][last][top][bottom][index][help] */
  94     options.raw_1 = TRUE;
  95     pcmk__str_update(&options.xml_file_1, optarg);
  96     return TRUE;
  97 }
  98 
  99 gboolean
 100 patch_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
     /* [previous][next][first][last][top][bottom][index][help] */
 101     options.apply = TRUE;
 102     pcmk__str_update(&options.xml_file_2, optarg);
 103     return TRUE;
 104 }
 105 
 106 static void
 107 print_patch(xmlNode *patch)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109     char *buffer = dump_xml_formatted(patch);
 110 
 111     printf("%s\n", crm_str(buffer));
 112     free(buffer);
 113     fflush(stdout);
 114 }
 115 
 116 static int
 117 apply_patch(xmlNode *input, xmlNode *patch, gboolean as_cib)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119     int rc;
 120     xmlNode *output = copy_xml(input);
 121 
 122     rc = xml_apply_patchset(output, patch, as_cib);
 123     if (rc != pcmk_ok) {
 124         fprintf(stderr, "Could not apply patch: %s\n", pcmk_strerror(rc));
 125         free_xml(output);
 126         return rc;
 127     }
 128 
 129     if (output != NULL) {
 130         const char *version;
 131         char *buffer;
 132 
 133         print_patch(output);
 134 
 135         version = crm_element_value(output, XML_ATTR_CRM_VERSION);
 136         buffer = calculate_xml_versioned_digest(output, FALSE, TRUE, version);
 137         crm_trace("Digest: %s\n", crm_str(buffer));
 138         free(buffer);
 139         free_xml(output);
 140     }
 141     return pcmk_ok;
 142 }
 143 
 144 static void
 145 log_patch_cib_versions(xmlNode *patch)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147     int add[] = { 0, 0, 0 };
 148     int del[] = { 0, 0, 0 };
 149 
 150     const char *fmt = NULL;
 151     const char *digest = NULL;
 152 
 153     xml_patch_versions(patch, add, del);
 154     fmt = crm_element_value(patch, "format");
 155     digest = crm_element_value(patch, XML_ATTR_DIGEST);
 156 
 157     if (add[2] != del[2] || add[1] != del[1] || add[0] != del[0]) {
 158         crm_info("Patch: --- %d.%d.%d %s", del[0], del[1], del[2], fmt);
 159         crm_info("Patch: +++ %d.%d.%d %s", add[0], add[1], add[2], digest);
 160     }
 161 }
 162 
 163 static void
 164 strip_patch_cib_version(xmlNode *patch, const char **vfields, size_t nvfields)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166     int format = 1;
 167 
 168     crm_element_value_int(patch, "format", &format);
 169     if (format == 2) {
 170         xmlNode *version_xml = find_xml_node(patch, "version", FALSE);
 171 
 172         if (version_xml) {
 173             free_xml(version_xml);
 174         }
 175 
 176     } else {
 177         int i = 0;
 178 
 179         const char *tags[] = {
 180             XML_TAG_DIFF_REMOVED,
 181             XML_TAG_DIFF_ADDED,
 182         };
 183 
 184         for (i = 0; i < PCMK__NELEM(tags); i++) {
 185             xmlNode *tmp = NULL;
 186             int lpc;
 187 
 188             tmp = find_xml_node(patch, tags[i], FALSE);
 189             if (tmp) {
 190                 for (lpc = 0; lpc < nvfields; lpc++) {
 191                     xml_remove_prop(tmp, vfields[lpc]);
 192                 }
 193 
 194                 tmp = find_xml_node(tmp, XML_TAG_CIB, FALSE);
 195                 if (tmp) {
 196                     for (lpc = 0; lpc < nvfields; lpc++) {
 197                         xml_remove_prop(tmp, vfields[lpc]);
 198                     }
 199                 }
 200             }
 201         }
 202     }
 203 }
 204 
 205 static int
 206 generate_patch(xmlNode *object_1, xmlNode *object_2, const char *xml_file_2,
     /* [previous][next][first][last][top][bottom][index][help] */
 207                gboolean as_cib, gboolean no_version)
 208 {
 209     xmlNode *output = NULL;
 210 
 211     const char *vfields[] = {
 212         XML_ATTR_GENERATION_ADMIN,
 213         XML_ATTR_GENERATION,
 214         XML_ATTR_NUMUPDATES,
 215     };
 216 
 217     /* If we're ignoring the version, make the version information
 218      * identical, so it isn't detected as a change. */
 219     if (no_version) {
 220         int lpc;
 221 
 222         for (lpc = 0; lpc < PCMK__NELEM(vfields); lpc++) {
 223             crm_copy_xml_element(object_1, object_2, vfields[lpc]);
 224         }
 225     }
 226 
 227     xml_track_changes(object_2, NULL, object_2, FALSE);
 228     if(as_cib) {
 229         xml_calculate_significant_changes(object_1, object_2);
 230     } else {
 231         xml_calculate_changes(object_1, object_2);
 232     }
 233     crm_log_xml_debug(object_2, (xml_file_2? xml_file_2: "target"));
 234 
 235     output = xml_create_patchset(0, object_1, object_2, NULL, FALSE);
 236 
 237     xml_log_changes(LOG_INFO, __func__, object_2);
 238     xml_accept_changes(object_2);
 239 
 240     if (output == NULL) {
 241         return pcmk_ok;
 242     }
 243 
 244     patchset_process_digest(output, object_1, object_2, as_cib);
 245 
 246     if (as_cib) {
 247         log_patch_cib_versions(output);
 248 
 249     } else if (no_version) {
 250         strip_patch_cib_version(output, vfields, PCMK__NELEM(vfields));
 251     }
 252 
 253     xml_log_patchset(LOG_NOTICE, __func__, output);
 254     print_patch(output);
 255     free_xml(output);
 256     return -pcmk_err_generic;
 257 }
 258 
 259 static GOptionContext *
 260 build_arg_context(pcmk__common_args_t *args) {
     /* [previous][next][first][last][top][bottom][index][help] */
 261     GOptionContext *context = NULL;
 262 
 263     const char *description = "Examples:\n\n"
 264                               "Obtain the two different configuration files by running cibadmin on the two cluster setups to compare:\n\n"
 265                               "\t# cibadmin --query > cib-old.xml\n\n"
 266                               "\t# cibadmin --query > cib-new.xml\n\n"
 267                               "Calculate and save the difference between the two files:\n\n"
 268                               "\t# crm_diff --original cib-old.xml --new cib-new.xml > patch.xml\n\n"
 269                               "Apply the patch to the original file:\n\n"
 270                               "\t# crm_diff --original cib-old.xml --patch patch.xml > updated.xml\n\n"
 271                               "Apply the patch to the running cluster:\n\n"
 272                               "\t# cibadmin --patch -x patch.xml\n";
 273 
 274     context = pcmk__build_arg_context(args, NULL, NULL, NULL);
 275     g_option_context_set_description(context, description);
 276 
 277     pcmk__add_arg_group(context, "xml", "Original XML:",
 278                         "Show original XML options", original_xml_entries);
 279     pcmk__add_arg_group(context, "operation", "Operation:",
 280                         "Show operation options", operation_entries);
 281     pcmk__add_arg_group(context, "additional", "Additional Options:",
 282                         "Show additional options", addl_entries);
 283     return context;
 284 }
 285 
 286 int
 287 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289     xmlNode *object_1 = NULL;
 290     xmlNode *object_2 = NULL;
 291 
 292     crm_exit_t exit_code = CRM_EX_OK;
 293     GError *error = NULL;
 294 
 295     pcmk__common_args_t *args = pcmk__new_common_args(SUMMARY);
 296     gchar **processed_args = pcmk__cmdline_preproc(argv, "nopNO");
 297     GOptionContext *context = build_arg_context(args);
 298 
 299     if (!g_option_context_parse_strv(context, &processed_args, &error)) {
 300         exit_code = CRM_EX_USAGE;
 301         goto done;
 302     }
 303 
 304     pcmk__cli_init_logging("crm_diff", args->verbosity);
 305 
 306     if (args->version) {
 307         g_strfreev(processed_args);
 308         pcmk__free_arg_context(context);
 309         /* FIXME:  When crm_diff is converted to use formatted output, this can go. */
 310         pcmk__cli_help('v', CRM_EX_OK);
 311     }
 312 
 313     if (options.apply && options.no_version) {
 314         fprintf(stderr, "warning: -u/--no-version ignored with -p/--patch\n");
 315     } else if (options.as_cib && options.no_version) {
 316         fprintf(stderr, "error: -u/--no-version incompatible with -c/--cib\n");
 317         exit_code = CRM_EX_USAGE;
 318         goto done;
 319     }
 320 
 321     if (options.raw_1) {
 322         object_1 = string2xml(options.xml_file_1);
 323 
 324     } else if (options.use_stdin) {
 325         fprintf(stderr, "Input first XML fragment:");
 326         object_1 = stdin2xml();
 327 
 328     } else if (options.xml_file_1 != NULL) {
 329         object_1 = filename2xml(options.xml_file_1);
 330     }
 331 
 332     if (options.raw_2) {
 333         object_2 = string2xml(options.xml_file_2);
 334 
 335     } else if (options.use_stdin) {
 336         fprintf(stderr, "Input second XML fragment:");
 337         object_2 = stdin2xml();
 338 
 339     } else if (options.xml_file_2 != NULL) {
 340         object_2 = filename2xml(options.xml_file_2);
 341     }
 342 
 343     if (object_1 == NULL) {
 344         fprintf(stderr, "Could not parse the first XML fragment\n");
 345         exit_code = CRM_EX_DATAERR;
 346         goto done;
 347     }
 348     if (object_2 == NULL) {
 349         fprintf(stderr, "Could not parse the second XML fragment\n");
 350         exit_code = CRM_EX_DATAERR;
 351         goto done;
 352     }
 353 
 354     if (options.apply) {
 355         int ret = apply_patch(object_1, object_2, options.as_cib);
 356         exit_code = crm_errno2exit(ret);
 357     } else {
 358         int ret = generate_patch(object_1, object_2, options.xml_file_2, options.as_cib, options.no_version);
 359         exit_code = crm_errno2exit(ret);
 360     }
 361 
 362 done:
 363     g_strfreev(processed_args);
 364     pcmk__free_arg_context(context);
 365     free(options.xml_file_1);
 366     free(options.xml_file_2);
 367     free_xml(object_1);
 368     free_xml(object_2);
 369 
 370     pcmk__output_and_clear_error(error, NULL);
 371     crm_exit(exit_code);
 372 }

/* [previous][next][first][last][top][bottom][index][help] */