Yedidyah Bar David has uploaded a new change for review.

Change subject: packaging: setup: Prevent more than one dwh per engine
......................................................................

packaging: setup: Prevent more than one dwh per engine

Change-Id: I0c0bbeef7fbd809895f4f0c1addd36e13f4894a6
Bug-Url: https://bugzilla.redhat.com/1118350
Signed-off-by: Yedidyah Bar David <[email protected]>
---
M packaging/setup/ovirt_engine_setup/dwh/constants.py
M packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/__init__.py
A packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/single_etl.py
M packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/db/connection.py
4 files changed, 250 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-dwh refs/changes/25/31325/1

diff --git a/packaging/setup/ovirt_engine_setup/dwh/constants.py 
b/packaging/setup/ovirt_engine_setup/dwh/constants.py
index f34aef0..c8a37e5 100644
--- a/packaging/setup/ovirt_engine_setup/dwh/constants.py
+++ b/packaging/setup/ovirt_engine_setup/dwh/constants.py
@@ -122,6 +122,10 @@
         OVIRT_ENGINE_DWHD_SERVICE_CONFIGD,
         '10-setup-database.conf',
     )
+    OVIRT_ENGINE_DWHD_SERVICE_CONFIG_UUID = os.path.join(
+        OVIRT_ENGINE_DWHD_SERVICE_CONFIGD,
+        '10-setup-uuid.conf',
+    )
     OVIRT_ENGINE_DWHD_SERVICE_CONFIG_LEGACY = os.path.join(
         OVIRT_ENGINE_DWHD_SERVICE_CONFIGD,
         '20-setup-legacy.conf',
@@ -160,6 +164,8 @@
     DB_CREDENTIALS_AVAILABLE = 'osetup.dwh.db.connection.credentials'
     DB_CONNECTION_CUSTOMIZATION = 'osetup.dwh.db.connection.customization'
     DB_CONNECTION_AVAILABLE = 'osetup.dwh.db.connection.available'
+    ENGINE_DB_CONNECTION_AVAILABLE = \
+        'osetup.dwh.engine.db.connection.available'
     DB_SCHEMA = 'osetup.dwh.db.schema'
 
 
@@ -256,6 +262,12 @@
     def RESTORE_BACKUP_LATE(self):
         return 'OVESETUP_DWH_DB/restoreBackupLate'
 
+    @osetupattrs(
+        answerfile=True,
+    )
+    def DISCONNECT_EXISTING_DWH(self):
+        return 'OVESETUP_DWH_DB/disconnectExistingDwh'
+
 
 @util.export
 @util.codegen
diff --git 
a/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/__init__.py 
b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/__init__.py
index 97e1557..72d4298 100644
--- 
a/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/__init__.py
+++ 
b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/__init__.py
@@ -20,6 +20,7 @@
 
 
 from . import check_etl
+from . import single_etl
 from . import config
 from . import misc
 from . import service
@@ -28,6 +29,7 @@
 @util.export
 def createPlugins(context):
     check_etl.Plugin(context=context)
+    single_etl.Plugin(context=context)
     config.Plugin(context=context)
     misc.Plugin(context=context)
     service.Plugin(context=context)
diff --git 
a/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/single_etl.py
 
b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/single_etl.py
new file mode 100644
index 0000000..8dd5398
--- /dev/null
+++ 
b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/core/single_etl.py
@@ -0,0 +1,194 @@
+#
+# ovirt-engine-setup -- ovirt engine setup
+# Copyright (C) 2014 Red Hat, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+
+import re
+import uuid
+import gettext
+_ = lambda m: gettext.dgettext(message=m, domain='ovirt-engine-dwh')
+
+
+from otopi import util
+from otopi import plugin
+
+
+from ovirt_engine import configfile
+
+
+from ovirt_engine_setup.dwh import constants as odwhcons
+from ovirt_engine_setup.engine_common import database
+
+
[email protected]
+class Plugin(plugin.PluginBase):
+
+    _DB_KEY_RUNNING = 'DwhCurrentlyRunning'
+    _DB_KEY_HOSTNAME = 'dwhHostname'
+    _DB_KEY_UUID = 'dwhUuid'
+
+    def __init__(self, context):
+        super(Plugin, self).__init__(context=context)
+        self._dwh_uuid = None
+
+    def _getFromTimekeeping(self, name):
+        statement = database.Statement(
+            dbenvkeys=odwhcons.Const.ENGINE_DB_ENV_KEYS,
+            environment=self.environment,
+        )
+
+        result = statement.execute(
+            statement="""
+                select GetDwhHistoryTimekeepingByVarName(
+                    %(name)s
+                )
+            """,
+            args=dict(
+                name=name,
+            ),
+            ownConnection=True,
+        )
+        self.logger.debug(
+            '_getFromTimekeeping {name} result {result}'.format(
+                name=name,
+                result=result,
+            )
+        )
+        return result[0]['var_value'] if result else None
+
+    def _updateTimekeeping(self, name, value):
+        statement = self.environment[odwhcons.EngineDBEnv.STATEMENT]
+        statement.execute(
+            statement="""
+                select UpdateDwhHistoryTimekeeping(
+                    %(name)s,
+                    %(value)s,
+                    NULL
+                )
+            """,
+            args=dict(
+                name=name,
+                value=value,
+            ),
+            ownConnection=False,
+        )
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_SETUP,
+        condition=lambda self: self.environment[odwhcons.CoreEnv.ENABLE],
+    )
+    def _setup(self):
+        config = configfile.ConfigFile([
+            odwhcons.FileLocations.OVIRT_ENGINE_DWHD_SERVICE_CONFIG_DEFAULTS,
+            odwhcons.FileLocations.OVIRT_ENGINE_DWHD_SERVICE_CONFIG,
+        ])
+        self._dwh_uuid = config.get('DWH_UUID') or str(uuid.uuid4())
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_VALIDATION,
+        condition=lambda self: (
+            self.environment[odwhcons.CoreEnv.ENABLE] and
+            not self.environment[odwhcons.EngineDBEnv.NEW_DATABASE]
+        ),
+    )
+    def _validation(self):
+        db_dwh_hostname = self._getFromTimekeeping(self._DB_KEY_HOSTNAME)
+        db_dwh_uuid = self._getFromTimekeeping(self._DB_KEY_UUID)
+
+        if self._getFromTimekeeping(self._DB_KEY_RUNNING) == '1':
+            self.logger.error(
+                _(
+                    'dwhd is currently running.\n'
+                    'Its hostname is {hostname}.\n'
+                    'Please stop it before running Setup.'
+                ).format(
+                    hostname=db_dwh_hostname,
+                )
+            )
+            raise RuntimeError(_('dwhd is currently running'))
+
+        if (
+            db_dwh_uuid and
+            db_dwh_uuid != self._dwh_uuid
+        ):
+            if self.environment[
+                odwhcons.DBEnv.DISCONNECT_EXISTING_DWH
+            ] is None:
+                self.environment[
+                    odwhcons.DBEnv.DISCONNECT_EXISTING_DWH
+                ] = dialog.queryBoolean(
+                    dialog=self.dialog,
+                    name='OVESETUP_DWH_DISCONNECT_EXISTING',
+                    note=_(
+                        'An existing DWH is configured to work with this '
+                        'engine.\n'
+                        'Its hostname is {hostname}.\n'
+                        'A positive answer to the following question will '
+                        'cause the existing DWH to be permanently '
+                        'disconnected from the engine.\n'
+                        'A negative answer will stop Setup.\n'
+                        'Do you want to permanently disconnect this DWH from '
+                        'the engine?'
+                        '(@VALUES@) [@DEFAULT@]: '
+                    ).format(
+                        hostname=db_dwh_hostname,
+                    ),
+                    prompt=True,
+                    true=_('Yes'),
+                    false=_('No'),
+                    default=False,
+                )
+            if not self.environment[
+                odwhcons.DBEnv.DISCONNECT_EXISTING_DWH
+            ]:
+                raise RuntimeError(
+                    _('An existing DWH found - Setup cancelled by user')
+                )
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_MISC,
+        condition=lambda self: self.environment[odwhcons.CoreEnv.ENABLE],
+    )
+    def _misc(self):
+        uninstall_files = []
+        self.environment[
+            osetupcons.CoreEnv.REGISTER_UNINSTALL_GROUPS
+        ].addFiles(
+            group='ovirt_dwh_files',
+            fileList=uninstall_files,
+        )
+        self.environment[otopicons.CoreEnv.MAIN_TRANSACTION].append(
+            filetransaction.FileTransaction(
+                name=(
+                    odwhcons.FileLocations.
+                    OVIRT_ENGINE_DWHD_SERVICE_CONFIG_UUID
+                ),
+                content='DWH_UUID={uuid}\n'.format(uuid=self._dwh_uuid),
+                modifiedList=uninstall_files,
+            )
+        )
+
+        self._updateTimekeeping(
+            name=self._DB_KEY_HOSTNAME,
+            value=self.environment[osetupcons.ConfigEnv.FQDN]
+        )
+        self._updateTimekeeping(
+            name=self._DB_KEY_UUID,
+            value=self._dwh_uuid
+        )
+
+
+# vim: expandtab tabstop=4 shiftwidth=4
diff --git 
a/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/db/connection.py 
b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/db/connection.py
index a9ac89a..8e68c8a 100644
--- 
a/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/db/connection.py
+++ 
b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine-dwh/db/connection.py
@@ -57,10 +57,27 @@
                 connection.rollback()
                 self._parent.environment[odwhcons.DBEnv.CONNECTION] = None
 
+            if not self.environment[odwhcons.EngineCoreEnv.ENABLE]:
+                engine_conn = self._parent.environment[
+                    odwhcons.EngineDBEnv.CONNECTION
+                ]
+                if engine_conn is not None:
+                    engine_conn.rollback()
+                    self._parent.environment[
+                        odwhcons.EngineDBEnv.CONNECTION
+                    ] = None
+
         def commit(self):
             connection = self._parent.environment[odwhcons.DBEnv.CONNECTION]
             if connection is not None:
                 connection.commit()
+
+            if not self.environment[odwhcons.EngineCoreEnv.ENABLE]:
+                engine_conn = self._parent.environment[
+                    odwhcons.EngineDBEnv.CONNECTION
+                ]
+                if engine_conn is not None:
+                    engine_conn.commit()
 
     def __init__(self, context):
         super(Plugin, self).__init__(context=context)
@@ -147,5 +164,30 @@
             odwhcons.DBEnv.CONNECTION
         ] = self.environment[odwhcons.DBEnv.STATEMENT].connect()
 
+    @plugin.event(
+        stage=plugin.Stages.STAGE_MISC,
+        name=odwhcons.Stages.ENGINE_DB_CONNECTION_AVAILABLE,
+        condition=lambda self: (
+            self.environment[odwhcons.CoreEnv.ENABLE] and
+            # If engine is enabled, STATEMENT and CONNECTION are set there
+            not self.environment[odwhcons.EngineCoreEnv.ENABLE]
+        ),
+        after=(
+            odwhcons.Stages.DB_SCHEMA,
+            oengcommcons.Stages.DB_CONNECTION_AVAILABLE,
+        ),
+    )
+    def _engine_connection(self):
+        self.environment[
+            odwhcons.EngineDBEnv.STATEMENT
+        ] = database.Statement(
+            environment=self.environment,
+            dbenvkeys=odwhcons.Const.ENGINE_DB_ENV_KEYS,
+        )
+        # must be here as we do not have database at validation
+        self.environment[
+            odwhcons.EngineDBEnv.CONNECTION
+        ] = self.environment[odwhcons.EngineDBEnv.STATEMENT].connect()
+
 
 # vim: expandtab tabstop=4 shiftwidth=4


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

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

Reply via email to