Blame SOURCES/bz1404233-01-cluster-cib-push-allows-to-obtain-and-push-a-diff.patch

585c70
From 24f48d185b03b3bcff28d78a3be657f2a1295e4b Mon Sep 17 00:00:00 2001
585c70
From: Tomas Jelinek <tojeline@redhat.com>
585c70
Date: Fri, 6 Jan 2017 15:26:11 +0100
585c70
Subject: [PATCH] 'cluster cib-push' allows to obtain and push a diff
585c70
585c70
---
585c70
 pcs/cluster.py | 43 +++++++++++++++++++++++++++++++++++++------
585c70
 pcs/pcs.8      | 10 ++++++++--
585c70
 pcs/usage.py   | 14 +++++++++++---
585c70
 3 files changed, 56 insertions(+), 11 deletions(-)
585c70
585c70
diff --git a/pcs/cluster.py b/pcs/cluster.py
585c70
index 4572643..69e2852 100644
585c70
--- a/pcs/cluster.py
585c70
+++ b/pcs/cluster.py
585c70
@@ -1192,6 +1192,8 @@ def cluster_push(argv):
585c70
     filename = None
585c70
     scope = None
585c70
     timeout = None
585c70
+    diff_against = None
585c70
+
585c70
     if "--wait" in utils.pcs_options:
585c70
         timeout = utils.validate_wait_get_timeout()
585c70
     for arg in argv:
585c70
@@ -1204,6 +1206,8 @@ def cluster_push(argv):
585c70
                     utils.err("invalid CIB scope '%s'" % arg_value)
585c70
                 else:
585c70
                     scope = arg_value
585c70
+            if arg_name == "diff-against":
585c70
+                diff_against = arg_value
585c70
             else:
585c70
                 usage.cluster(["cib-push"])
585c70
                 sys.exit(1)
585c70
@@ -1212,6 +1216,8 @@ def cluster_push(argv):
585c70
     if not filename:
585c70
         usage.cluster(["cib-push"])
585c70
         sys.exit(1)
585c70
+    if diff_against and scope:
585c70
+        utils.err("Cannot use both scope and diff-against")
585c70
 
585c70
     try:
585c70
         new_cib_dom = xml.dom.minidom.parse(filename)
585c70
@@ -1223,13 +1229,38 @@ def cluster_push(argv):
585c70
     except (EnvironmentError, xml.parsers.expat.ExpatError) as e:
585c70
         utils.err("unable to parse new cib: %s" % e)
585c70
 
585c70
-    command = ["cibadmin", "--replace", "--xml-file", filename]
585c70
-    if scope:
585c70
-        command.append("--scope=%s" % scope)
585c70
-    output, retval = utils.run(command)
585c70
-    if retval != 0:
585c70
-        utils.err("unable to push cib\n" + output)
585c70
+    if diff_against:
585c70
+        try:
585c70
+            xml.dom.minidom.parse(diff_against)
585c70
+        except (EnvironmentError, xml.parsers.expat.ExpatError) as e:
585c70
+            utils.err("unable to parse original cib: %s" % e)
585c70
+        runner = utils.cmd_runner()
585c70
+        command = [
585c70
+            "crm_diff", "--original", diff_against, "--new", filename,
585c70
+            "--no-version"
585c70
+        ]
585c70
+        patch, error, dummy_retval = runner.run(command)
585c70
+        # dummy_retval == -1 means one of two things:
585c70
+        # a) an error has occured
585c70
+        # b) --original and --new differ
585c70
+        if error.strip():
585c70
+            utils.err("unable to diff the CIBs:\n" + error)
585c70
+
585c70
+        command = ["cibadmin", "--patch", "--xml-pipe"]
585c70
+        output, error, retval = runner.run(command, patch)
585c70
+        if retval != 0:
585c70
+            utils.err("unable to push cib\n" + error + output)
585c70
+
585c70
+    else:
585c70
+        command = ["cibadmin", "--replace", "--xml-file", filename]
585c70
+        if scope:
585c70
+            command.append("--scope=%s" % scope)
585c70
+        output, retval = utils.run(command)
585c70
+        if retval != 0:
585c70
+            utils.err("unable to push cib\n" + output)
585c70
+
585c70
     print("CIB updated")
585c70
+
585c70
     if "--wait" not in utils.pcs_options:
585c70
         return
585c70
     cmd = ["crm_resource", "--wait"]
585c70
diff --git a/pcs/pcs.8 b/pcs/pcs.8
585c70
index dffaddd..bf4b859 100644
585c70
--- a/pcs/pcs.8
585c70
+++ b/pcs/pcs.8
585c70
@@ -262,8 +262,14 @@ Sync corosync configuration to all nodes found from current corosync.conf file (
585c70
 cib [filename] [scope=<scope> | \fB\-\-config\fR]
585c70
 Get the raw xml from the CIB (Cluster Information Base).  If a filename is provided, we save the CIB to that file, otherwise the CIB is printed.  Specify scope to get a specific section of the CIB.  Valid values of the scope are: configuration, nodes, resources, constraints, crm_config, rsc_defaults, op_defaults, status.  \fB\-\-config\fR is the same as scope=configuration.  Do not specify a scope if you want to edit the saved CIB using pcs (pcs -f <command>).
585c70
 .TP
585c70
-cib-push <filename> [scope=<scope> | \fB\-\-config\fR] [\fB\-\-wait\fR[=<n>]]
585c70
-Push the raw xml from <filename> to the CIB (Cluster Information Base).  You can obtain the CIB by running the 'pcs cluster cib' command, which is recommended first step when you want to perform desired modifications (pcs \fB\-f\fR <command>) for the one-off push.  Specify scope to push a specific section of the CIB.  Valid values of the scope are: configuration, nodes, resources, constraints, crm_config, rsc_defaults, op_defaults.  \fB\-\-config\fR is the same as scope=configuration.  Use of \fB\-\-config\fR is recommended.  Do not specify a scope if you need to push the whole CIB or be warned in the case of outdated CIB.  If --wait is specified wait up to 'n' seconds for changes to be applied.  WARNING: the selected scope of the CIB will be overwritten by the current content of the specified file.
585c70
+cib-push <filename> [diff\-against=<filename_original> scope=<scope> | \fB\-\-config\fR] [\fB\-\-wait\fR[=<n>]]
585c70
+Push the raw xml from <filename> to the CIB (Cluster Information Base).  You can obtain the CIB by running the 'pcs cluster cib' command, which is recommended first step when you want to perform desired modifications (pcs \fB\-f\fR <command>) for the one\-off push.  If diff\-against is specified, pcs diffs contents of filename against contents of filename_original and pushes the result to the CIB.  Specify scope to push a specific section of the CIB.  Valid values of the scope are: configuration, nodes, resources, constraints, crm_config, rsc_defaults, op_defaults.  \fB\-\-config\fR is the same as scope=configuration.  Use of \fB\-\-config\fR is recommended.  Do not specify a scope if you need to push the whole CIB or be warned in the case of outdated CIB.  If --wait is specified wait up to 'n' seconds for changes to be applied.  WARNING: the selected scope of the CIB will be overwritten by the current content of the specified file.
585c70
+
585c70
+Example:
585c70
+    pcs cluster cib > original.xml
585c70
+    cp original.xml new.xml
585c70
+    pcs -f new.xml constraint location apache prefers node2
585c70
+    pcs cluster cib-push new.xml diff-against=original.xml
585c70
 .TP
585c70
 cib\-upgrade
585c70
 Upgrade the CIB to conform to the latest version of the document schema.
585c70
diff --git a/pcs/usage.py b/pcs/usage.py
585c70
index 0ebebe0..b33da35 100644
585c70
--- a/pcs/usage.py
585c70
+++ b/pcs/usage.py
585c70
@@ -656,20 +656,28 @@ Commands:
585c70
         scope=configuration.  Do not specify a scope if you want to edit
585c70
         the saved CIB using pcs (pcs -f <command>).
585c70
 
585c70
-    cib-push <filename> [scope=<scope> | --config] [--wait[=<n>]]
585c70
+    cib-push <filename> [--wait[=<n>]]
585c70
+            [diff-against=<filename_orignal> | scope=<scope> | --config]
585c70
         Push the raw xml from <filename> to the CIB (Cluster Information Base).
585c70
         You can obtain the CIB by running the 'pcs cluster cib' command, which
585c70
         is recommended first step when you want to perform desired
585c70
         modifications (pcs -f <command>) for the one-off push.
585c70
+        If diff-against is specified, pcs diffs contents of filename against
585c70
+        contents of filename_original and pushes the result to the CIB.
585c70
         Specify scope to push a specific section of the CIB.  Valid values
585c70
         of the scope are: configuration, nodes, resources, constraints,
585c70
         crm_config, rsc_defaults, op_defaults.  --config is the same as
585c70
         scope=configuration.  Use of --config is recommended.  Do not specify
585c70
         a scope if you need to push the whole CIB or be warned in the case
585c70
-        of outdated CIB. If --wait is specified wait up to 'n' seconds for
585c70
-        changes to be applied.
585c70
+        of outdated CIB.
585c70
+        If --wait is specified wait up to 'n' seconds for changes to be applied.
585c70
         WARNING: the selected scope of the CIB will be overwritten by the
585c70
         current content of the specified file.
585c70
+        Example:
585c70
+            pcs cluster cib > original.xml
585c70
+            cp original.xml new.xml
585c70
+            pcs -f new.xml constraint location apache prefers node2
585c70
+            pcs cluster cib-push new.xml diff-against=original.xml
585c70
 
585c70
     cib-upgrade
585c70
         Upgrade the CIB to conform to the latest version of the document schema.
585c70
-- 
585c70
1.8.3.1
585c70