Alex Lourie has uploaded a new change for review.

Change subject: packaging: setup: add answerfile option
......................................................................

packaging: setup: add answerfile option

This new implementation allows unattended reports
installation.

Change-Id: Ia8a1bd52965f0f16f68ac9948622d22f1299edef
Bug-Url: https://bugzilla.redhat.com/1025339
Signed-off-by: Alex Lourie <alou...@redhat.com>
---
M packaging/common_utils.py
M packaging/ovirt-engine-reports-setup.py
2 files changed, 126 insertions(+), 38 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-reports refs/changes/37/20837/1

diff --git a/packaging/common_utils.py b/packaging/common_utils.py
index 9e7e5ea..b470863 100755
--- a/packaging/common_utils.py
+++ b/packaging/common_utils.py
@@ -526,15 +526,16 @@
     engine_service.status()
     return engine_service.lastStateUp
 
-def stopEngine():
+def stopEngine(answer=None):
     '''
     stops the ovirt-engine service
     '''
     logging.debug("checking ovirt-engine service")
     if isEngineUp():
         logging.debug("ovirt-engine is up and running")
-        print "In order to proceed the installer must stop the ovirt-engine 
service"
-        answer = askYesNo("Would you like to stop the ovirt-engine service")
+        if answer is None:
+            print "In order to proceed the installer must stop the 
ovirt-engine service"
+            answer = askYesNo("Would you like to stop the ovirt-engine 
service")
         if answer:
             stopEngineService()
         else:
diff --git a/packaging/ovirt-engine-reports-setup.py 
b/packaging/ovirt-engine-reports-setup.py
index f72ffc8..0f8b161 100755
--- a/packaging/ovirt-engine-reports-setup.py
+++ b/packaging/ovirt-engine-reports-setup.py
@@ -18,13 +18,22 @@
 import tempfile
 import re
 import glob
-import argparse
+from optparse import OptionParser
+import ConfigParser
 
 import common_utils as utils
 
 from decorators import transactionDisplay
 import pwd
 
+params = {
+    'STOP_ENGINE': None,
+    'ADMIN_PASS': "1234abc",
+    'REMOTE_DB_HOST': None,
+    'REMOTE_DB_PORT': None,
+    'REMOTE_DB_USER': None,
+    'REMOTE_DB_PASSWORD': None,
+}
 
 DIR_DEPLOY = "/usr/share/ovirt-engine"
 JRS_APP_NAME = "ovirt-engine-reports"
@@ -92,6 +101,45 @@
             )
         )
 
+
+def _parseAnswerFile(answerfile=None):
+    if (
+        answerfile is not None and
+        os.path.exists(answerfile)
+    ):
+        global params
+        fconf = ConfigParser.ConfigParser()
+        fconf.read(answerfile)
+        for param in params.keys():
+            params[param] = fconf.get('general', param)
+            if params[param] == 'None':
+                params[param] = None
+            elif params[param] in ('True', 'true'):
+                params[param] = True
+            elif params[param] in ('False', 'false'):
+                params[param] = False
+
+    return params
+
+
+def _getOptions():
+    parser = OptionParser()
+
+    parser.add_option(
+        "-a",
+        "--answer-file",
+        dest="answerfile",
+        help="Use the following answer file for dwh installation",
+    )
+    parser.add_option(
+        "-g",
+        "--gen-answer-file",
+        dest="genanswerfile",
+        help="Generate answer file",
+    )
+
+    (options, args) = parser.parse_args()
+    return (options, args)
 
 @transactionDisplay("Deploying Server")
 def deployJs(db_dict, TEMP_PGPASS):
@@ -372,7 +420,11 @@
 
     return dbhost, dbport, dbuser, userInput
 
-def getAdminPass():
+def getAdminPass(adminPass):
+
+    if adminPass is not None:
+        return adminPass.strip('"')
+
     userInput = getPassFromUser(
         'Please choose a password for the reports admin user(s) '
         '(ovirt-admin): '
@@ -882,7 +934,7 @@
     os.chdir(current_dir)
     shutil.rmtree(savedRepoDir)
 
-def main():
+def main(options):
     '''
     main
     '''
@@ -891,10 +943,6 @@
     rc = 0
     preserveReportsJobs = False
     pghba_updated = False
-
-    parser = argparse.ArgumentParser(description='Installs or upgrades your 
oVirt Engine Reports')
-    # Catch when calling ovirt-engine-dwh-setup --help
-    args = parser.parse_args()
 
     try:
         logging.debug("starting main()")
@@ -907,7 +955,7 @@
             return 0
 
         # Check if ovirt-engine is up, if so prompt the user to stop it.
-        if utils.stopEngine():
+        if utils.stopEngine(options['STOP_ENGINE']):
             warUpdated = isWarUpdated()
 
             if not warUpdated and isWarInstalled():
@@ -971,38 +1019,59 @@
                     )
                 )
                 print 'Remote database found.'
-                if utils.askYesNo(
-                    'Setup could not connect to remote database server with '
-                    'automatically detected credentials. '
-                    'Would you like to manually provide db credentials?'
-                ):
-                    DB_EXIST = False
-                    while not DB_EXIST:
-                        (
-                            db_dict['host'],
-                            db_dict['port'],
-                            db_dict['username'],
-                            db_dict['password']
-                        ) = getDbCredentials()
-                        if os.path.exists(TEMP_PGPASS):
-                            os.remove(TEMP_PGPASS)
 
-                        TEMP_PGPASS = utils.createTempPgpass(
-                            db_dict=db_dict,
-                            mode='own',
-                        )
-                        DB_EXIST, owned = getDBStatus(
-                            db_dict,
-                            TEMP_PGPASS,
-                        )
-                        if not DB_EXIST:
+                while not DB_EXIST:
+                    if options['REMOTE_DB_HOST'] is None:
+                        if utils.askYesNo(
+                            'Setup could not connect to remote database server 
with '
+                            'automatically detected credentials. '
+                            'Would you like to manually provide db 
credentials?'
+                        ):
+                            (
+                                db_dict['host'],
+                                db_dict['port'],
+                                db_dict['username'],
+                                db_dict['password'],
+                            ) = getDbCredentials()
+                        else:
                             print (
                                 'error: cannot connect to the '
                                 'remote db with provided credentials. '
                                 'verify that the provided user is defined '
                                 'user exists on a remote db server and '
                                 'is the owner of the provided database.\n'
+                                'Then rerun the setup.\n'
                             )
+                            sys.exit(1)
+                    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(TEMP_PGPASS):
+                        os.remove(TEMP_PGPASS)
+
+                    TEMP_PGPASS = utils.createTempPgpass(
+                        db_dict=db_dict,
+                        mode='own',
+                    )
+                    DB_EXIST, owned = getDBStatus(
+                        db_dict,
+                        TEMP_PGPASS,
+                    )
+                    if not DB_EXIST:
+                        print (
+                            'error: cannot connect to the '
+                            'remote db with provided credentials. '
+                            'verify that the provided user is defined '
+                            'user exists on a remote db server and '
+                            'is the owner of the provided database.\n'
+                            'Then rerun the setup.\n'
+                        )
+                        if options['REMOTE_DB_HOST'] is not None:
+                            sys.exit(1)
+
                 else:
                     raise RuntimeError('Could not connect to the remote DB')
 
@@ -1028,7 +1097,7 @@
             if not warUpdated and DB_EXIST:
                 #backupWAR()
                 #backupDB(db_dict, TEMP_PGPASS)
-               pass
+                pass
 
             # Catch failures on configuration
             try:
@@ -1043,7 +1112,7 @@
                 ):
                     savedDir = utils.exportUsers()
                 else:
-                    adminPass = getAdminPass()
+                    adminPass = getAdminPass(options['ADMIN_PASS'])
 
                 # Execute js-ant to create DB and deploy WAR
                 # May also set DB_EXIST to False if WAR is in need of an 
upgrade
@@ -1167,5 +1236,23 @@
         "ovirt-engine-reports-setup",
         "/var/log/ovirt-engine"
     )
-    rc = main()
+    options, args = _getOptions()
+    if options.genanswerfile:
+        with open(options.genanswerfile, 'w') as af:
+            content = '[general]\n'
+            for param in params.keys():
+                content = '{content}{newline}\n'.format(
+                    content=content,
+                    newline='{key}={value}'.format(
+                        key=param,
+                        value=params[param],
+                    )
+                )
+            af.write(content)
+            print 'Answer file generated at {answerfile}\n'.format(
+                answerfile=options.genanswerfile
+            )
+            sys.exit(0)
+
+    rc = main(_parseAnswerFile(options.answerfile))
     sys.exit(rc)


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia8a1bd52965f0f16f68ac9948622d22f1299edef
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-reports
Gerrit-Branch: master
Gerrit-Owner: Alex Lourie <alou...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to