Alex Lourie has uploaded a new change for review.

Change subject: packaging: setup: fix DB detection from previous versions
......................................................................

packaging: setup: fix DB detection from previous versions

Change-Id: Ic2fe256660b233bc682dc22e79cf58d34c546c3a
Bug-Url: https://bugzilla.redhat.com/1015859
Signed-off-by: Alex Lourie <[email protected]>
---
M packaging/common_utils.py
M packaging/ovirt-engine-dwh-setup.py
2 files changed, 81 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-dwh refs/changes/54/20154/1

diff --git a/packaging/common_utils.py b/packaging/common_utils.py
index 4740b62..ffdee83 100755
--- a/packaging/common_utils.py
+++ b/packaging/common_utils.py
@@ -586,13 +586,31 @@
     output, rc = execCmd(cmdList=cmd, failOnError=True, msg="Failed to get 
package version & release")
     return output.rstrip()
 
-def dbExists(db_dict, pgpass):
+def dbExists(db_dict, TEMP_PGPASS):
+
+    exists = False
+    owner = False
     logging.debug("checking if %s db already exists" % db_dict['dbname'])
-    (output, rc) = execSqlCmd(db_dict, "select 1", envDict={'ENGINE_PGPASS': 
pgpass})
-    if (rc != 0):
-        return False
+    env = {'ENGINE_PGPASS': TEMP_PGPASS}
+    output, rc = execSqlCmd(
+        db_dict=db_dict,
+        sql_query="select 1",
+        envDict=env,
+    )
+    if rc == 0:
+        exists = True
+        if db_dict['username'] != db_dict['engine_user']:
+            owner = True
     else:
-        return True
+        output, rc = runPostgresSuQuery(
+            query='"select 1;"',
+            database=db_dict['dbname'],
+            failOnError=False,
+        )
+        if rc == 0:
+            exists = True
+
+    return exists, owner
 
 def getDbAdminUser():
     """
@@ -1028,10 +1046,10 @@
 
 
 def createUser(user, password, option='', database=''):
+    if not userValid(user):
+        return
+
     sql_query_set = [
-        (
-            '"DROP ROLE if exists {user};"'
-        ),
         (
             '"CREATE ROLE {user} with '
             '{option} login encrypted password \'{password}\';"'
@@ -1181,6 +1199,26 @@
 
     return True
 
+def updateDbOwner(db_dict, origowner, newowner):
+    out, rc = runPostgresSuCommand(
+        command=EXEC_PGDUMP,
+        database=db_dict['dbname'],
+    )
+    sql_query_set = []
+    for line in out.splitlines():
+        if 'owner to' in line.lower():
+            sql_query_set.append(
+                line.replace(
+                    'OWNER TO {orig}'.format(orig=origowner),
+                    'OWNER TO {new}'.format(new=newowner)
+                )
+            )
+
+    for sql_query in sql_query_set:
+        runPostgresSuQuery(
+            query=sql_query,
+            database=db_dict['dbname'],
+        )
 
 def updatePgHba(database, user):
     content = []
@@ -1249,6 +1287,31 @@
 def restorePgHba():
     return configHbaIdent('ident', 'md5')
 
+def runPostgresSuCommand(command, database=None, failOnError=True):
+    sql_command = [
+        command,
+    ]
+    if database is not None:
+        sql_command.extend(
+            (
+                database
+            )
+        )
+    cmd = [
+        EXEC_SU,
+        '-l',
+        'postgres',
+        '-c',
+        '{command}'.format(
+            command=' '.join(sql_command),
+        )
+    ]
+
+    return execCmd(
+        cmdList=cmd,
+        failOnError=failOnError
+    )
+
 def runPostgresSuQuery(query, database=None, failOnError=True):
     sql_command = [
         EXEC_PSQL,
diff --git a/packaging/ovirt-engine-dwh-setup.py 
b/packaging/ovirt-engine-dwh-setup.py
index 6909b1a..8bbac60 100755
--- a/packaging/ovirt-engine-dwh-setup.py
+++ b/packaging/ovirt-engine-dwh-setup.py
@@ -74,18 +74,6 @@
             )
         )
 
-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):
     """
@@ -397,9 +385,16 @@
                 readonly=db_dict['readonly'],
             )
 
-
-            if dbExists(db_dict):
+            dbExists, owned = utils.dbExists(db_dict, PGPASS_TEMP)
+            if dbExists:
                 try:
+                    if utils.localHost(db_dict['host']) and not owned:
+                        utils.createUser(
+                            user=db_dict['username'],
+                            password=db_dict['password'],
+                            option='createdb',
+                        )
+                        utils.updateLocalDbOwner(db_dict)
                     doBackup = utils.performBackup(db_dict, DB_BACKUPS_DIR, 
PGPASS_TEMP)
                     backupFile = os.path.join(
                         DB_BACKUPS_DIR,
@@ -447,7 +442,7 @@
                     if os.path.exists(PGPASS_TEMP):
                         os.remove(PGPASS_TEMP)
                     PGPASS_TEMP = utils.createTempPgpass(db_dict)
-                    if not utils.dbExists(db_dict, PGPASS_TEMP):
+                    if not utils.dbExists(db_dict, PGPASS_TEMP)[0]:
                         raise RuntimeError (
                             (
                                 'Remote installation failed. Please perform '


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

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

Reply via email to