Yaniv Dary has uploaded a new change for review.

Change subject: packging: fixed remote install setup
......................................................................

packging: fixed remote install setup

Change-Id: Ic315856f91c60f240c6830fb90e5b259774e9745
Bug-Url: https://bugzilla.redhat.com/1045846
Signed-off-by: Yaniv Dary <[email protected]>
---
M packaging/legacy-setup/common_utils.py
M packaging/legacy-setup/ovirt-engine-dwh-setup.py
2 files changed, 99 insertions(+), 75 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-dwh refs/changes/01/22701/1

diff --git a/packaging/legacy-setup/common_utils.py 
b/packaging/legacy-setup/common_utils.py
index 1dd31a7..ada1526 100755
--- a/packaging/legacy-setup/common_utils.py
+++ b/packaging/legacy-setup/common_utils.py
@@ -62,6 +62,7 @@
 DB_HOST = "localhost"
 DB_PORT = "5432"
 DB_ADMIN = "postgres"
+DB_USER = "engine_history"
 
 # DB related messages
 DB_BACKUP_HEADER = (
@@ -627,6 +628,7 @@
 
     exists = False
     owner = False
+    hasData = False
     logging.debug("checking if %s db already exists" % db_dict['dbname'])
     env = {'ENGINE_PGPASS': TEMP_PGPASS}
     if (
@@ -648,15 +650,19 @@
     if rc == 0:
         exists = True
         if (
-            db_dict['username'] != db_dict['engine_user'] and
-            (
-                db_dict['password'] != 'dummy' and
-                db_dict['username'] != 'admin'
-            )
+            db_dict['username'] == DB_USER
         ):
             owner = True
 
-    return exists, owner
+        output, rc = execSqlCmd(
+            db_dict=db_dict,
+            sql_query="select 1 from history_configuration;",
+            envDict=env,
+        )
+        if rc == 0:
+            hasData = True
+
+    return exists, owner, hasData
 
 def getDbAdminUser():
     """
diff --git a/packaging/legacy-setup/ovirt-engine-dwh-setup.py 
b/packaging/legacy-setup/ovirt-engine-dwh-setup.py
index dd499e7..6c81cc8 100755
--- a/packaging/legacy-setup/ovirt-engine-dwh-setup.py
+++ b/packaging/legacy-setup/ovirt-engine-dwh-setup.py
@@ -38,6 +38,8 @@
     'REMOTE_DB_PASSWORD': None,
 }
 
+DIR_PKI = "/etc/pki/ovirt-engine"
+DIR_DEPLOY = "/usr/share/ovirt-engine"
 DWH_PACKAGE_NAME="ovirt-engine-dwh"
 PATH_DB_SCRIPTS="/usr/share/ovirt-engine-dwh/dbscripts"
 PATH_WATCHDOG="/usr/share/ovirt-engine-dwh/etl/ovirt_engine_dwh_watchdog.cron"
@@ -58,7 +60,7 @@
 )
 DB_BACKUPS_DIR = "/var/lib/ovirt-engine/backups"
 DB_NAME = "ovirt_engine_history"
-DB_USER = 'engine_history'
+DB_USER = "engine_history"
 DB_PORT = "5432"
 DB_HOST = "localhost"
 PGPASS_TEMP = ''
@@ -127,18 +129,6 @@
     (options, args) = parser.parse_args()
     return (options, args)
 
-
-def dbExists(db_dict):
-    logging.debug("checking if %s db already exists" % db_dict['dbname'])
-    (output, rc) = utils.execSqlCmd(
-        db_dict=db_dict,
-        sql_query="select 1",
-        envDict={'ENGINE_PGPASS': PGPASS_TEMP},
-    )
-    if (rc != 0):
-        return False
-    else:
-        return True
 
 @transactionDisplay("Creating DB")
 def createDbSchema(db_dict):
@@ -283,7 +273,7 @@
     return db_dict
 
 def getDBStatus(db_dict, TEMP_PGPASS):
-    exists = owned = False
+    exists = owned = hasData = False
     for dbdict in (
         db_dict,
         {
@@ -305,13 +295,9 @@
             'engine_pass': db_dict['engine_pass'],
         },
     ):
-        exists, owned = utils.dbExists(dbdict, TEMP_PGPASS)
-        if exists:
-            db_dict['username'] = dbdict['username']
-            db_dict['password'] = dbdict['password']
-            break
+        exists, owned, hasData = utils.dbExists(dbdict, TEMP_PGPASS)
 
-    return exists, owned
+    return exists, owned, hasData
 
 
 @transactionDisplay("Setting DB connectivity")
@@ -362,6 +348,27 @@
     file_handler.editParam("etlVersion", "%s.%s" % (currentVersion, 
currentMinorVersion))
     file_handler.close()
 
+def userExists(user):
+    sql_query = '"select 1 from pg_roles where rolname=\'{user}\';"'.format(
+        user=user
+    )
+
+    output, rc = utils.runPostgresSuQuery(sql_query)
+    return '1' in output
+
+def isOvirtEngineInstalled():
+    keystore = os.path.join(DIR_PKI, "keys", "engine.p12")
+    engine_ear = "%s/engine.ear" % DIR_DEPLOY
+
+    if os.path.exists(keystore):
+        logging.debug("%s exists, ovirt-engine is installed", keystore)
+        return True
+    elif os.path.exists(engine_ear):
+        logging.debug("ear exists, ovirt-engine is installed")
+        return True
+    else:
+        return False
+
 def main(options):
     '''
     main
@@ -383,6 +390,12 @@
     try:
         logging.debug("starting main()")
         print "Welcome to ovirt-engine-dwh setup utility\n"
+
+        # Check that oVirt-Engine is installed, otherwise exit gracefully with 
an informative message
+        if not isOvirtEngineInstalled():
+            logging.debug("ovirt-engine is not installed, cannot continue")
+            print "Please install & configure oVirt engine by executing 
\"engine-setup\" prior to setting up the data warehouse."
+            return 0
 
         db_dict = getDbDictFromOptions()
         PGPASS_TEMP = utils.createTempPgpass(db_dict)
@@ -407,6 +420,7 @@
             setVersion()
             readUserCreated = False
             createReadUser = False
+            dbExists = owned = hasData = False
             errMsg = ''
 
             # Create/Upgrade DB
@@ -471,16 +485,51 @@
                 readonly=db_dict['readonly'],
             )
 
-            dbExists, owned = getDBStatus(db_dict, PGPASS_TEMP)
+            dbExists, owned, hasData = getDBStatus(db_dict, PGPASS_TEMP)
+            if not utils.localHost(db_dict["host"]) and not dbExists:
+                print 'Remote installation is selected.\n'
+                if options['REMOTE_DB_HOST'] is None:
+                     (
+                        db_dict['username'],
+                        db_dict['password'],
+                     ) = getDbCredentials(
+                         userdefault=db_dict['username'],
+                    )
+                else:
+                    db_dict['host'] = options['REMOTE_DB_HOST']
+                    db_dict['port'] = options['REMOTE_DB_PORT']
+                    db_dict['username'] = options['REMOTE_DB_USER']
+                    db_dict['password'] = options['REMOTE_DB_PASSWORD']
+
+                if os.path.exists(PGPASS_TEMP):
+                    os.remove(PGPASS_TEMP)
+                PGPASS_TEMP = utils.createTempPgpass(db_dict)
+                dbExists, owned, hasData = getDBStatus(db_dict, PGPASS_TEMP)
+                if not dbExists:
+                    raise RuntimeError (
+                       (
+                          'Remote installation failed. Please perform '
+                           '\tcreate role {role} with login '
+                           'encrypted password {password};\n'
+                           '\tcreate {db} owner {role}\n'
+                           'on the remote DB, verify it and rerun the setup.'
+                        ).format(
+                           role=db_dict['username'],
+                           db=db_dict['dbname'],
+                           password=db_dict['password'],
+                        )
+                    )
+
             if dbExists:
                 try:
                     if utils.localHost(db_dict['host']) and not owned:
-                        utils.createUser(
-                            user=db_dict['username'],
-                            password=db_dict['password'],
-                            option='createdb',
-                            validate=False,
-                        )
+                        if not userExists(db_dict['username']):
+                            utils.createUser(
+                                user=db_dict['username'],
+                                password=db_dict['password'],
+                                option='createdb',
+                                validate=False,
+                            )
                         utils.updateDbOwner(db_dict)
 
                     if options['BACKUP_DB'] is None:
@@ -510,13 +559,17 @@
                     utils.startEngine()
                     # Sleep for 20 secs to allow health applet to start
                     time.sleep(20)
+                    setDbPass(db_dict)
                     utils.startEtl()
                     sys.exit(0)
 
-                # Backup went ok, so upgrade
-                upgradeDB(db_dict)
+                if hasData:
+                    # Backup went ok, so upgrade
+                    upgradeDB(db_dict)
+                else:
+                    createDbSchema(db_dict)
             else:
-                if utils.localHost(db_dict["host"]):
+                if not userExists(db_dict['username']):
                     utils.createUser(
                         user=db_dict['username'],
                         password=db_dict['password'],
@@ -524,43 +577,9 @@
                         validate=False,
                     )
 
-                    utils.createDB(db_dict['dbname'], db_dict['username'])
-                    utils.updatePgHba(db_dict['dbname'], db_dict['username'])
-                    utils.restartPostgres()
-
-                else:
-                    print 'Remote installation is selected.\n'
-                    if options['REMOTE_DB_HOST'] is None:
-                        (
-                            db_dict['username'],
-                            db_dict['password'],
-                        ) = getDbCredentials(
-                            userdefault=db_dict['username'],
-                        )
-                    else:
-                        db_dict['host'] = options['REMOTE_DB_HOST']
-                        db_dict['port'] = options['REMOTE_DB_PORT']
-                        db_dict['username'] = options['REMOTE_DB_USER']
-                        db_dict['password'] = options['REMOTE_DB_PASSWORD']
-
-                    if os.path.exists(PGPASS_TEMP):
-                        os.remove(PGPASS_TEMP)
-                    PGPASS_TEMP = utils.createTempPgpass(db_dict)
-                    if not utils.dbExists(db_dict, PGPASS_TEMP)[0]:
-                        raise RuntimeError (
-                            (
-                                'Remote installation failed. Please perform '
-                                '\tcreate role {role} with login '
-                                'encrypted password {password};\n'
-                                '\tcreate {db} owner {role}\n'
-                                'on the remote DB, verify it and rerun the 
setup.'
-                            ).format(
-                                role=db_dict['username'],
-                                db=db_dict['dbname'],
-                                password=db_dict['password'],
-                            )
-                        )
-
+                utils.createDB(db_dict['dbname'], db_dict['username'])
+                utils.updatePgHba(db_dict['dbname'], db_dict['username'])
+                utils.restartPostgres()
                 createDbSchema(db_dict)
 
             if createReadUser:
@@ -583,8 +602,7 @@
                     db_dict['readonly'] = readonly_user
 
             # Set DB connecitivty (user/pass)
-            if db_dict['password']:
-                setDbPass(db_dict)
+            setDbPass(db_dict)
 
             if pg_updated:
                 utils.restorePgHba()


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

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

Reply via email to