Sandro Bonazzola has uploaded a new change for review.

Change subject: packaging: setup: allow db cleanup on errors
......................................................................

packaging: setup: allow db cleanup on errors

Previously if an error occurred during misc stage
while running engine-setup-2 after the db provisioning
engine-cleanup-2 was not able to cleanup the db due to
missing configuration file for accessing postgres.

Now if provisioning is performed the transaction that
creates the configuration file is committed or aborted
immediately after the db provisioning.

Change-Id: Ifb2a9d7e1c0a318d0bbbf45b2d584245d634fc02
Signed-off-by: Sandro Bonazzola <[email protected]>
---
M packaging/setup/ovirt_engine_setup/constants.py
M packaging/setup/plugins/ovirt-engine-setup/config/database.py
M packaging/setup/plugins/ovirt-engine-setup/provisioning/postgres.py
3 files changed, 124 insertions(+), 75 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/69/16869/1

diff --git a/packaging/setup/ovirt_engine_setup/constants.py 
b/packaging/setup/ovirt_engine_setup/constants.py
index 236bc2e..f6a7bda 100644
--- a/packaging/setup/ovirt_engine_setup/constants.py
+++ b/packaging/setup/ovirt_engine_setup/constants.py
@@ -525,6 +525,7 @@
     AIO_CONFIG_STORAGE = 'osetup.aio.config.storage'
 
     UPGRADE_FROM_LEGACY_CONFIG = 'osetup.legacy.upgrade'
+    POSTGRES_PROVISIONED = 'osetup.postgres.provisioned'
 
 
 @util.export
@@ -697,6 +698,8 @@
     def FIX_DB_VIOLATIONS(self):
         return 'OVESETUP_DB/fixDbViolations'
 
+    DATABASE_SETUP_TRANSACTION = 'OVESETUP_DB/setupTransaction'
+
 
 @util.export
 @util.codegen
diff --git a/packaging/setup/plugins/ovirt-engine-setup/config/database.py 
b/packaging/setup/plugins/ovirt-engine-setup/config/database.py
index a81d0d0..eb58627 100644
--- a/packaging/setup/plugins/ovirt-engine-setup/config/database.py
+++ b/packaging/setup/plugins/ovirt-engine-setup/config/database.py
@@ -28,6 +28,7 @@
 from otopi import util
 from otopi import filetransaction
 from otopi import plugin
+from otopi import transaction
 
 
 from ovirt_engine_setup import constants as osetupcons
@@ -35,18 +36,36 @@
 
 @util.export
 class Plugin(plugin.PluginBase):
-    """Databsae plugin."""
+    """Database plugin."""
 
     def __init__(self, context):
         super(Plugin, self).__init__(context=context)
         self._enabled = False
+        self._db_transaction = transaction.Transaction()
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_INIT,
+    )
+    def _init(self):
+        self.environment[
+            osetupcons.DBEnv.DATABASE_SETUP_TRANSACTION
+        ] = self._db_transaction
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_TRANSACTION_BEGIN,
+    )
+    def _prepare(self):
+        self._db_transaction.prepare()
 
     @plugin.event(
         stage=plugin.Stages.STAGE_MISC,
         name=osetupcons.Stages.CONFIG_DB_CREDENTIALS,
+        before=[
+            osetupcons.Stages.POSTGRES_PROVISIONED,
+        ]
     )
     def _misc(self):
-        self.environment[otopicons.CoreEnv.MAIN_TRANSACTION].append(
+        self._db_transaction.append(
             filetransaction.FileTransaction(
                 name=(
                     osetupcons.FileLocations.
@@ -103,5 +122,19 @@
             )
         )
 
+    @plugin.event(
+        stage=plugin.Stages.STAGE_TRANSACTION_END,
+        condition=lambda self: self.environment[
+            osetupcons.DBEnv.DATABASE_SETUP_TRANSACTION
+        ] is not None,
+    )
+    def _main_end(self):
+        try:
+            self._db_transaction.commit()
+        except:
+            self._db_transaction.abort()
+            raise
+        finally:
+            self._db_transaction = None
 
 # vim: expandtab tabstop=4 shiftwidth=4
diff --git 
a/packaging/setup/plugins/ovirt-engine-setup/provisioning/postgres.py 
b/packaging/setup/plugins/ovirt-engine-setup/provisioning/postgres.py
index 683f2d7..93166c8 100644
--- a/packaging/setup/plugins/ovirt-engine-setup/provisioning/postgres.py
+++ b/packaging/setup/plugins/ovirt-engine-setup/provisioning/postgres.py
@@ -508,28 +508,83 @@
         after=[
             osetupcons.Stages.SYSTEM_SYSCTL_CONFIG_AVAILABLE,
         ],
+        name=osetupcons.Stages.POSTGRES_PROVISIONED,
         condition=lambda self: self._enabled,
     )
     def _misc(self):
-
-        if not os.path.exists(
-            self.environment[
-                osetupcons.ProvisioningEnv.POSTGRES_PG_VERSION
-            ]
-        ):
-            self._initDB()
-
-        self.logger.info(_('Creating PostgreSQL database'))
-        localtransaction = transaction.Transaction()
         try:
-            localtransaction.prepare()
+            if not os.path.exists(
+                self.environment[
+                    osetupcons.ProvisioningEnv.POSTGRES_PG_VERSION
+                ]
+            ):
+                self._initDB()
 
-            self._setPgHbaLocalPeer(
-                transaction=localtransaction,
-                filename=self.environment[
-                    osetupcons.ProvisioningEnv.POSTGRES_PG_HBA
-                ],
-            )
+            self.logger.info(_('Creating PostgreSQL database'))
+            localtransaction = transaction.Transaction()
+            try:
+                localtransaction.prepare()
+
+                self._setPgHbaLocalPeer(
+                    transaction=localtransaction,
+                    filename=self.environment[
+                        osetupcons.ProvisioningEnv.POSTGRES_PG_HBA
+                    ],
+                )
+
+                # restart to take effect
+                for state in (False, True):
+                    self.services.state(
+                        name=self.environment[
+                            osetupcons.ProvisioningEnv.POSTGRES_SERVICE
+                        ],
+                        state=state,
+                    )
+
+                with self._alternateUser(
+                    user=self.environment[
+                        osetupcons.SystemEnv.USER_POSTGRES
+                    ],
+                ):
+                    existing = self._setDatabaseResources()
+                    self._performDatabase(
+                        op=(
+                            'alter' if existing
+                            else 'create'
+                        ),
+                        user=self.environment[
+                            osetupcons.DBEnv.USER
+                        ],
+                        password=self.environment[
+                            osetupcons.DBEnv.PASSWORD
+                        ],
+                        databaseName=self.environment[
+                            osetupcons.DBEnv.DATABASE
+                        ],
+                    )
+            finally:
+                # restore everything
+                localtransaction.abort()
+
+            self.logger.info(_('Configurating PostgreSQL'))
+            localtransaction = transaction.Transaction()
+            with localtransaction:
+                self._addPgHbaDatabaseAccess(
+                    transaction=localtransaction,
+                    filename=self.environment[
+                        osetupcons.ProvisioningEnv.POSTGRES_PG_HBA
+                    ],
+                )
+
+                self._updateMaxConnections(
+                    transaction=localtransaction,
+                    filename=self.environment[
+                        osetupcons.ProvisioningEnv.POSTGRES_CONF
+                    ],
+                    maxconn=self.environment[
+                        osetupcons.ProvisioningEnv.POSTGRES_MAX_CONN
+                    ],
+                )
 
             # restart to take effect
             for state in (False, True):
@@ -540,66 +595,24 @@
                     state=state,
                 )
 
-            with self._alternateUser(
-                user=self.environment[
-                    osetupcons.SystemEnv.USER_POSTGRES
-                ],
-            ):
-                existing = self._setDatabaseResources()
-                self._performDatabase(
-                    op=(
-                        'alter' if existing
-                        else 'create'
-                    ),
-                    user=self.environment[
-                        osetupcons.DBEnv.USER
-                    ],
-                    password=self.environment[
-                        osetupcons.DBEnv.PASSWORD
-                    ],
-                    databaseName=self.environment[
-                        osetupcons.DBEnv.DATABASE
-                    ],
-                )
-        finally:
-            # restore everything
-            localtransaction.abort()
-
-        self.logger.info(_('Configurating PostgreSQL'))
-        localtransaction = transaction.Transaction()
-        with localtransaction:
-            self._addPgHbaDatabaseAccess(
-                transaction=localtransaction,
-                filename=self.environment[
-                    osetupcons.ProvisioningEnv.POSTGRES_PG_HBA
-                ],
-            )
-
-            self._updateMaxConnections(
-                transaction=localtransaction,
-                filename=self.environment[
-                    osetupcons.ProvisioningEnv.POSTGRES_CONF
-                ],
-                maxconn=self.environment[
-                    osetupcons.ProvisioningEnv.POSTGRES_MAX_CONN
-                ],
-            )
-
-        # restart to take effect
-        for state in (False, True):
-            self.services.state(
+            self.services.startup(
                 name=self.environment[
                     osetupcons.ProvisioningEnv.POSTGRES_SERVICE
                 ],
-                state=state,
+                state=True,
             )
-
-        self.services.startup(
-            name=self.environment[
-                osetupcons.ProvisioningEnv.POSTGRES_SERVICE
-            ],
-            state=True,
-        )
+            self.environment[
+                osetupcons.DBEnv.DATABASE_SETUP_TRANSACTION
+            ].commit()
+        except:
+            self.environment[
+                osetupcons.DBEnv.DATABASE_SETUP_TRANSACTION
+            ].abort()
+            raise
+        finally:
+            self.environment[
+                osetupcons.DBEnv.DATABASE_SETUP_TRANSACTION
+            ] = None
 
     @plugin.event(
         stage=plugin.Stages.STAGE_CLOSEUP,


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

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

Reply via email to