From 50d69d318f174d3f5dee30dafacbf902a837a90e Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Mon, 28 Oct 2013 17:21:59 -0400
Subject: [PATCH] Backup upgrade tracker.

The upgrade framework has been modified to backup the files used
to track the upgrade progress. If the tracker file is also modified
by the upgrade scriptlet, it will only keep the initial backup
(before any modifications were made).

Ticket #763
---
 base/common/python/pki/upgrade.py        | 8 ++++++--
 base/common/python/pki/util.py           | 6 +++++-
 base/server/python/pki/server/upgrade.py | 1 +
 specs/pki-core.spec                      | 1 +
 4 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/base/common/python/pki/upgrade.py b/base/common/python/pki/upgrade.py
index bd78ec999f12d987bc2968bf2786020720a0f465..7e481803bb24ce50a8f20fa42cd96e606f599fac 100644
--- a/base/common/python/pki/upgrade.py
+++ b/base/common/python/pki/upgrade.py
@@ -110,6 +110,7 @@ class PKIUpgradeTracker(object):
         index_key='PKI_UPGRADE_INDEX'):
 
         self.name = name
+        self.filename = filename
 
         self.version_key = version_key
         self.index_key = index_key
@@ -267,6 +268,7 @@ class PKIUpgradeScriptlet(object):
         # in this version, update the tracker version.
 
         tracker = self.upgrader.get_tracker()
+        self.backup(tracker.filename)
 
         if not self.last:
             tracker.set_index(self.index)
@@ -389,7 +391,8 @@ class PKIUpgradeScriptlet(object):
 
             if os.path.isfile(path):
                 if verbose: print 'Saving ' + path
-                pki.util.copyfile(path, dest)
+                # do not overwrite initial backup
+                pki.util.copyfile(path, dest, overwrite=False)
 
             else:
                 for sourcepath, _, filenames in os.walk(path):
@@ -405,7 +408,8 @@ class PKIUpgradeScriptlet(object):
                         targetfile = os.path.join(destpath, filename)
 
                         if verbose: print 'Saving ' + sourcefile
-                        pki.util.copyfile(sourcefile, targetfile)
+                        # do not overwrite initial backup
+                        pki.util.copyfile(sourcefile, targetfile, overwrite=False)
 
         else:
 
diff --git a/base/common/python/pki/util.py b/base/common/python/pki/util.py
index 4d25390a6b8379a35edfe991c789af266c395704..5992c7779cd89e17647a18772775df947039377a 100644
--- a/base/common/python/pki/util.py
+++ b/base/common/python/pki/util.py
@@ -53,11 +53,15 @@ def copy(source, dest):
                 targetfile = os.path.join(destpath, filename)
                 copyfile(sourcefile, targetfile)
 
-def copyfile(source, dest):
+def copyfile(source, dest, overwrite=True):
     """
     Copy a file or link while preserving its attributes.
     """
 
+    # if dest already exists and not overwriting, do nothing
+    if os.path.exists(dest) and not overwrite:
+        return
+
     if os.path.islink(source):
         target = os.readlink(source)
         os.symlink(target, dest)
diff --git a/base/server/python/pki/server/upgrade.py b/base/server/python/pki/server/upgrade.py
index 940dbe44adf2a297a2912e444cd481425de03ac5..ee0dfedf6d781da5ef604e52360f9b9b32689921 100644
--- a/base/server/python/pki/server/upgrade.py
+++ b/base/server/python/pki/server/upgrade.py
@@ -60,6 +60,7 @@ class PKIServerUpgradeScriptlet(pki.upgrade.PKIUpgradeScriptlet):
         # in this version, update the tracker version.
 
         tracker = self.upgrader.get_tracker(instance, subsystem)
+        self.backup(tracker.filename)
 
         if not self.last:
             tracker.set_index(self.index)
diff --git a/specs/pki-core.spec b/specs/pki-core.spec
index b06715f16a4e2cbe219343d82cb8e19c1f1c8c6c..fca53ff2d5b33623bb1e9cabec58e43f285dbea7 100644
--- a/specs/pki-core.spec
+++ b/specs/pki-core.spec
@@ -1138,6 +1138,7 @@ fi
 - Store authentication info in session.
 - Fixed error handling in DoUnrevoke servlet.
 - Fixed errors during Tomcat shutdown.
+- Backup upgrade tracker.
 
 * Fri Sep 6 2013 Ade Lee <alee@redhat.com> 10.0.5-1
 - Roll release to next version
-- 
1.8.1.4

