Alon Bar-Lev has uploaded a new change for review.

Change subject: packaging: setup: in case of rollback restore original 
versionlock
......................................................................

packaging: setup: in case of rollback restore original versionlock

new package cannot assume that version locked packages of previous
package is the same.

Change-Id: Ib2cd15fda7440d3d0afe984d5f345586e8e40af7
Signed-off-by: Alon Bar-Lev <[email protected]>
---
M packaging/setup/plugins/ovirt-engine-setup/distro-rpm/packages.py
1 file changed, 115 insertions(+), 83 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/91/16791/1

diff --git a/packaging/setup/plugins/ovirt-engine-setup/distro-rpm/packages.py 
b/packaging/setup/plugins/ovirt-engine-setup/distro-rpm/packages.py
index 65f97ef..8bb28a5 100644
--- a/packaging/setup/plugins/ovirt-engine-setup/distro-rpm/packages.py
+++ b/packaging/setup/plugins/ovirt-engine-setup/distro-rpm/packages.py
@@ -27,8 +27,10 @@
 _ = lambda m: gettext.dgettext(message=m, domain='ovirt-engine-setup')
 
 
+from otopi import constants as otopicons
 from otopi import util
 from otopi import plugin
+from otopi import transaction
 
 
 from ovirt_engine_setup import constants as osetupcons
@@ -41,75 +43,113 @@
     Package upgrade plugin.
     """
 
-    def _filterVersionLock(self):
-        modified = False
-        content = []
+    class VersionLockTransaction(transaction.TransactionElement):
+        """version lock transaction element.
+        Not that this is real transaction, but we need to
+        rollback/commit same as packager.
+        We cannot actually prepare the transaction at prepration
+        because new packages are not installed.
+        But we must restore file as we do not know what packages
+        were locked at previous version.
+        """
 
-        if os.path.exists(
-            osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK
-        ):
-            with open(
-                osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
-            ) as f:
-                for line in f.read().splitlines():
-                    if line.find(
-                        osetupcons.Const.ENGINE_PACKAGE_NAME
-                    ) == -1:
-                        content.append(line)
-                    else:
-                        modified = True
+        def _filterVersionLock(self):
+            modified = False
+            content = []
 
-        return (modified, content)
+            if os.path.exists(
+                osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK
+            ):
+                with open(
+                    osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
+                ) as f:
+                    for line in f.read().splitlines():
+                        if line.find(
+                            osetupcons.Const.ENGINE_PACKAGE_NAME
+                        ) == -1:
+                            content.append(line)
+                        else:
+                            modified = True
+            return (modified, content)
 
-    def _removeMeFromVersionLock(self):
-        modified, content = self._filterVersionLock()
-        if modified:
-            os.rename(
-                osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
-                '%s.%s' % (
+        @property
+        def environment(self):
+            return self._parent.environment
+
+        def __init__(self, parent):
+            self._parent = parent
+            self._backup = None
+
+        def __str__(self):
+            return _("Version Lock Transaction")
+
+        def prepare(self):
+            if not self._parent._enabled:
+                return
+
+            modified, content = self._filterVersionLock()
+
+            if modified:
+                self._backup = '%s.%s' % (
                     osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
                     datetime.datetime.now().strftime('%Y%m%d%H%M%S'),
-                ),
+                )
+                os.rename(
+                    osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
+                    self._backup,
+                )
+                with open(
+                    osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
+                    'w'
+                ) as f:
+                    f.write('\n'.join(content)+'\n')
+
+        def abort(self):
+            if (
+                self._backup is not None and
+                os.path.exists(self._backup)
+            ):
+                os.rename(
+                    self._backup,
+                    osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
+                )
+
+        def commit(self):
+            # This must be always execucted so we be sure we
+            # are locked
+
+            # execute rpm directly
+            # yum is not good in offline usage
+            rc, out, err = self._parent.execute(
+                args=[
+                    self._parent.command.get('rpm'),
+                    '-q',
+                ] + osetupcons.Const.RPM_LOCK_LIST,
             )
+
+            self.environment[
+                osetupcons.CoreEnv.UNINSTALL_UNREMOVABLE_FILES
+            ].append(osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK)
+
+            self.environment[
+                osetupcons.CoreEnv.REGISTER_UNINSTALL_GROUPS
+            ].createGroup(
+                group='versionlock',
+                description='YUM version locking configuration',
+                optional=False
+            ).addLines(
+                'versionlock',
+                osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
+                out,
+            )
+
+            modified, content = self._filterVersionLock()
+            content.extend(out)
             with open(
                 osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
-                'w'
+                'w',
             ) as f:
-                f.write('\n'.join(content)+'\n')
-
-    def _addMeToVersionLock(self):
-        # execute rpm directly
-        # yum is not good in offline usage
-        rc, out, err = self.execute(
-            args=[
-                self.command.get('rpm'),
-                '-q',
-            ] + osetupcons.Const.RPM_LOCK_LIST,
-        )
-
-        self.environment[
-            osetupcons.CoreEnv.UNINSTALL_UNREMOVABLE_FILES
-        ].append(osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK)
-
-        self.environment[
-            osetupcons.CoreEnv.REGISTER_UNINSTALL_GROUPS
-        ].createGroup(
-            group='versionlock',
-            description='YUM version locking configuration',
-            optional=False
-        ).addLines(
-            'versionlock',
-            osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
-            out,
-        )
-
-        modified, content = self._filterVersionLock()
-        content.extend(out)
-        with open(
-            osetupcons.FileLocations.OVIRT_ENGINE_YUM_VERSIONLOCK,
-            'w',
-        ) as f:
-            f.write('\n'.join(content) + '\n')
+                f.write('\n'.join(content) + '\n')
 
     def _getSink(self):
         class MyMiniYumSink(self._miniyum.MiniYumSinkBase):
@@ -239,6 +279,12 @@
         if self._distribution in ('redhat', 'fedora', 'centos'):
             self.command.detect('rpm')
 
+            self.environment[otopicons.CoreEnv.MAIN_TRANSACTION].append(
+                self.VersionLockTransaction(
+                    parent=self,
+                )
+            )
+
             from otopi import miniyum
             self._miniyum = miniyum
             self._enabled = True
@@ -254,6 +300,9 @@
         condition=lambda self: self._enabled,
     )
     def _customization(self):
+        # assume we have nothing to do
+        self._enabled = False
+
         upgradeAvailable = None
         haveRollback = None
 
@@ -303,7 +352,9 @@
                     haveRollback,
                 ) = self._checkForProductUpdate()
 
-            if upgradeAvailable:
+            if not upgradeAvailable:
+                self.dialog.note(text=_('No update is available'))
+            else:
                 if not haveRollback:
                     if self.environment[
                         osetupcons.RPMDistroEnv.REQUIRE_ROLLBACK
@@ -332,21 +383,9 @@
                             _('Package rollback information is unavailable')
                         )
 
-        self._enabled = self.environment[
-            osetupcons.RPMDistroEnv.ENABLE_UPGRADE
-        ]
-
-        if not upgradeAvailable:
-            self.dialog.note(text=_('No update is available'))
-
-    @plugin.event(
-        stage=plugin.Stages.STAGE_TRANSACTION_BEGIN,
-        priority=plugin.Stages.PRIORITY_HIGH,
-        condition=lambda self: self._enabled,
-    )
-    def transactionBegin(self):
-        self._shouldResultVersionLock = True
-        self._removeMeFromVersionLock()
+                self._enabled = self.environment[
+                    osetupcons.RPMDistroEnv.ENABLE_UPGRADE
+                ]
 
     @plugin.event(
         stage=plugin.Stages.STAGE_PACKAGES,
@@ -361,13 +400,6 @@
             self.packager.update(
                 packages=(osetupcons.Const.ENGINE_PACKAGE_NAME,),
             )
-
-    @plugin.event(
-        stage=plugin.Stages.STAGE_CLEANUP,
-        condition=lambda self: self._shouldResultVersionLock,
-    )
-    def cleanup(self):
-        self._addMeToVersionLock()
 
 
 # vim: expandtab tabstop=4 shiftwidth=4


-- 
To view, visit http://gerrit.ovirt.org/16791
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib2cd15fda7440d3d0afe984d5f345586e8e40af7
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alon Bar-Lev <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to