https://fedorahosted.org/freeipa/ticket/4499

Actually I wonder why we use backup_state/restore_state for these settings.
Rob, was there a reason for not just always setting nsslapd-port: 389 and nsslapd-security: on?

--
PetrĀ³
From 8799b97aaa1a0954c6978f6f1133c757d14f4c39 Mon Sep 17 00:00:00 2001
From: Petr Viktorin <pvikt...@redhat.com>
Date: Fri, 22 Aug 2014 14:22:06 +0200
Subject: [PATCH] upgradeinstance: Restore listeners on failure

Allow running some installation after failure,
and use this for the upgradeinstance cleanup steps.

https://fedorahosted.org/freeipa/ticket/4499
---
 ipaserver/install/service.py         | 30 ++++++++++++++++++++++++------
 ipaserver/install/upgradeinstance.py | 19 +++++++++++++------
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/ipaserver/install/service.py b/ipaserver/install/service.py
index 585c903bdd4dee66d93c426299d59de95d0ea625..018c369ffb3c8f782b3d59b82e3bb71c898b9b9f 100644
--- a/ipaserver/install/service.py
+++ b/ipaserver/install/service.py
@@ -23,6 +23,7 @@
 import pwd
 import time
 import datetime
+import traceback
 
 from ipapython import sysrestore, ipautil, dogtag, ipaldap
 from ipapython.dn import DN
@@ -329,8 +330,8 @@ def get_state(self, key):
     def print_msg(self, message):
         print_msg(message, self.output_fd)
 
-    def step(self, message, method):
-        self.steps.append((message, method))
+    def step(self, message, method, run_after_failure=False):
+        self.steps.append((message, method, run_after_failure))
 
     def start_creation(self, start_message=None, end_message=None,
         show_service_name=True, runtime=-1):
@@ -375,15 +376,32 @@ def start_creation(self, start_message=None, end_message=None,
         else:
             self.print_msg(start_message)
 
-        step = 0
-        for (message, method) in self.steps:
-            self.print_msg("  [%d/%d]: %s" % (step+1, len(self.steps), message))
+        def run_step(message, method):
+            self.print_msg(message)
             s = datetime.datetime.now()
             method()
             e = datetime.datetime.now()
             d = e - s
             root_logger.debug("  duration: %d seconds" % d.seconds)
-            step += 1
+
+        step = 0
+        steps_iter = iter(self.steps)
+        try:
+            for message, method, run_after_failure in steps_iter:
+                full_msg = "  [%d/%d]: %s" % (step+1, len(self.steps), message)
+                run_step(full_msg, method)
+                step += 1
+        except BaseException as e:
+            # show the traceback, so it's not lost if the cleanup method fails
+            root_logger.debug("%s" % traceback.format_exc())
+            self.print_msg('  [error] %s: %s' % (type(e).__name__, e))
+
+            # run through remaining methods marked run_after_failure
+            for message, method, run_after_failure in steps_iter:
+                if run_after_failure:
+                    run_step("  [cleanup]: %s" % message, method)
+
+            raise
 
         self.print_msg(end_message)
 
diff --git a/ipaserver/install/upgradeinstance.py b/ipaserver/install/upgradeinstance.py
index 062d5da22c32dd905189309d83a0295a3f6dbdd8..b553721eba615f324f9e52331cbd0910c3e14be6 100644
--- a/ipaserver/install/upgradeinstance.py
+++ b/ipaserver/install/upgradeinstance.py
@@ -82,8 +82,11 @@ def create_instance(self):
         if self.schema_files:
             self.step("updating schema", self.__update_schema)
         self.step("upgrading server", self.__upgrade)
-        self.step("stopping directory server", self.__stop_instance)
-        self.step("restoring configuration", self.__restore_config)
+
+        self.step("stopping directory server", self.__stop_instance,
+                  run_after_failure=True)
+        self.step("restoring configuration", self.__restore_config,
+                  run_after_failure=True)
         self.step("starting directory server", self.start)
 
         self.start_creation(start_message="Upgrading IPA:",
@@ -103,10 +106,14 @@ def __restore_config(self):
         port = self.restore_state('nsslapd-port')
         security = self.restore_state('nsslapd-security')
 
-        installutils.set_directive(self.filename, 'nsslapd-port',
-            port, quotes=False, separator=':')
-        installutils.set_directive(self.filename, 'nsslapd-security',
-            security, quotes=False, separator=':')
+        if port is not None:
+            installutils.set_directive(
+                self.filename, 'nsslapd-port', port,
+                quotes=False, separator=':')
+        if security is not None:
+            installutils.set_directive(
+                self.filename, 'nsslapd-security', security,
+                quotes=False, separator=':')
 
     def __disable_listeners(self):
         installutils.set_directive(self.filename, 'nsslapd-port',
-- 
1.9.3

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to