From 8ed5f2ea5420ba375f97649d84456e06838b7faf Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Wed, 20 Mar 2013 23:07:30 -0400
Subject: [PATCH] Added upgrade framework.

A new Python module has been added to provide a framework for upgrade
scriplets. A new tool called pkirespawn has been added to execute the
scriptlets. Upgrade tracker and command-line options will be added
separately.

The pki.conf has been moved from pki-server to pki-base.

Ticket #544, #553
---
 base/common/python/pki/upgrade.py | 128 ++++++++++++++++++++++++++++++++++++++
 base/deploy/CMakeLists.txt        |   1 +
 base/deploy/src/pkirespawn        |  54 ++++++++++++++++
 specs/pki-core.spec               |  13 ++--
 4 files changed, 192 insertions(+), 4 deletions(-)
 create mode 100644 base/common/python/pki/upgrade.py
 create mode 100755 base/deploy/src/pkirespawn

diff --git a/base/common/python/pki/upgrade.py b/base/common/python/pki/upgrade.py
new file mode 100644
index 0000000000000000000000000000000000000000..57b167b5a62d2224b57b4128580146164834d764
--- /dev/null
+++ b/base/common/python/pki/upgrade.py
@@ -0,0 +1,128 @@
+#!/usr/bin/python
+# Authors:
+#     Endi S. Dewata <edewata@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2013 Red Hat, Inc.
+# All rights reserved.
+#
+
+import os
+import re
+
+
+class PKIUpgradeScriptlet(object):
+
+    def __init__(self, script_name, message):
+
+        match = re.match('^(\d+)-(.+)$', script_name)
+        if match is None:
+            raise Error('Invalid script name: '+ script_name)
+
+        self.script_number = int(match.group(1))
+        self.script_id = match.group(2)
+
+        self.message = message
+
+        self.instance_base_dir = '/var/lib/pki'
+        self.verbose = True
+
+
+    def read_text(self, message,
+        options=None, default=None, sign=':',
+        allowEmpty=True, caseSensitive=True):
+
+        if default:
+            message = message + ' [' + default + ']'
+        message = '  ' + message + sign + ' '
+
+        done = False
+        while not done:
+            value = raw_input(message)
+            value = value.strip()
+
+            if len(value) == 0:  # empty value
+                if allowEmpty:
+                    value = default
+                    done = True
+                    break
+
+            else:  # non-empty value
+                if options is not None:
+                    for v in options:
+                        if caseSensitive:
+                            if v == value:
+                                done = True
+                                break
+                        else:
+                            if v.lower() == value.lower():
+                                done = True
+                                break
+                else:
+                    done = True
+                    break
+
+        return value
+
+
+    def instances(self):
+
+        return os.listdir(self.instance_base_dir)
+
+
+    def subsystems(self, instance):
+
+        list = []
+
+        instance_dir = os.path.join(self.instance_base_dir, instance)
+        for folder in os.listdir(instance_dir):
+
+            # check whether it is a subsystem folder
+            subsystem_conf = os.path.join(
+                instance_dir, folder, 'conf', 'CS.cfg')
+
+            if not os.path.exists(subsystem_conf):
+                continue
+
+            list.append(folder)
+
+        return list
+
+
+    def upgrade_subsystem(self, instance, subsystem):
+        pass
+
+
+    def upgrade_instance(self, instance):
+        pass
+
+
+    def upgrade_system(self):
+        pass
+
+
+    def upgrade(self):
+        message = str(self.script_number) + '. ' + self.message + ' (Y/N)'
+        self.read_text(message=message,
+            options=['Y', 'n'], default='Y', caseSensitive=False)
+
+        for instance in self.instances():
+
+            for subsystem in self.subsystems(instance):
+                self.upgrade_subsystem(instance, subsystem)
+
+            self.upgrade_instance(instance)
+
+        self.upgrade_system()
diff --git a/base/deploy/CMakeLists.txt b/base/deploy/CMakeLists.txt
index 8483482c29af44503a88850664b9886d74c4d90a..4633f81fbaeb9a7c05701ce64596c8c34cdca732 100644
--- a/base/deploy/CMakeLists.txt
+++ b/base/deploy/CMakeLists.txt
@@ -47,6 +47,7 @@ install(
 install(
     FILES
         src/pkispawn
+        src/pkirespawn
         src/pkidestroy
     DESTINATION
         ${SBIN_INSTALL_DIR}
diff --git a/base/deploy/src/pkirespawn b/base/deploy/src/pkirespawn
new file mode 100755
index 0000000000000000000000000000000000000000..c23b029b7e8360aaaf30cc8517571dcf3d0102a8
--- /dev/null
+++ b/base/deploy/src/pkirespawn
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+# Authors:
+#     Endi S. Dewata <edewata@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2013 Red Hat, Inc.
+# All rights reserved.
+#
+
+import os
+import subprocess
+
+class PKIRespawn():
+
+    def __init__(self):
+        self.upgrade_dir = '/usr/share/pki/server/upgrade'
+
+    def process(self, version):
+        print 'Upgrading from version ' + version + ':'
+
+        base_dir = os.path.join(self.upgrade_dir, version)
+
+        for scriptlet in os.listdir(base_dir):
+            command = os.path.join(base_dir, scriptlet)
+            subprocess.check_call([command])
+
+    def run(self):
+        for version in os.listdir(self.upgrade_dir):
+            self.process(version)
+
+
+if __name__ == '__main__':
+    try:
+        app = PKIRespawn()
+        app.run()
+
+        print
+        print 'Upgrade complete.'
+
+    except KeyboardInterrupt:
+        print
+        print 'Upgrade canceled.'
diff --git a/specs/pki-core.spec b/specs/pki-core.spec
index 4bd4f0100ae90c055af758a0c338e58cad1bad5e..57464afe3588c2915b33b17c88e5ea31fbb2a093 100644
--- a/specs/pki-core.spec
+++ b/specs/pki-core.spec
@@ -5,7 +5,7 @@ distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
 
 Name:             pki-core
 Version:          10.0.1
-Release:          8%{?dist}
+Release:          9%{?dist}
 Summary:          Certificate System - PKI Core Components
 URL:              http://pki.fedoraproject.org/
 License:          GPLv2
@@ -810,6 +810,8 @@ fi
 %doc base/common/LICENSE
 %dir %{_datadir}/pki
 %{_datadir}/pki/VERSION
+%dir %{_sysconfdir}/pki/
+%config(noreplace) %{_sysconfdir}/pki/pki.conf
 %dir %{_javadir}/pki
 %{_javadir}/pki/pki-cmsutil.jar
 %{_javadir}/pki/pki-nsutil.jar
@@ -858,10 +860,9 @@ fi
 %defattr(-,root,root,-)
 %doc base/common/THIRD_PARTY_LICENSES
 %doc base/deploy/LICENSE
-%dir %{_sysconfdir}/pki/
-%config(noreplace) %{_sysconfdir}/pki/pki.conf
 %{_sysconfdir}/pki/default.cfg
 %{_sbindir}/pkispawn
+%{_sbindir}/pkirespawn
 %{_sbindir}/pkidestroy
 #%{_bindir}/pki-setup-proxy
 %{python_sitelib}/pki/deployment/
@@ -902,7 +903,6 @@ fi
 %config(noreplace) %{_sysconfdir}/tmpfiles.d/pki-tomcat.conf
 
 %{_datadir}/pki/setup/
-%dir %{_datadir}/pki/server
 %{_datadir}/pki/server/
 
 %if ! 0%{?rhel} && 0%{?fedora} <= 17
@@ -1007,6 +1007,11 @@ fi
 
 
 %changelog
+* Thu Mar 21 2013 Endi S. Dewata <edewata@redhat.com> 10.0.1-10
+- Added pkirespawn.
+- Removed duplicate %{_datadir}/pki/server declaration.
+- Moved pki.conf into pki-base.
+
 * Tue Mar 19 2013 Ade Lee <alee@redhat.com> 10.0.1-8
 - Removed jython dependency
 
-- 
1.8.1.4

