Alex Lourie has uploaded a new change for review. Change subject: [WIP] packaging: Added backup DB functionality to setup ......................................................................
[WIP] packaging: Added backup DB functionality to setup This patch adds user interaction asking to perform the backup of the DB before performing an upgrade. Prior to that it checks the available space and the size of the DB and prompts the user with option to perform the backup. Change-Id: I8ccf8938099207b4e73bb947d1bb5fd673605071 Bug-Url: https://bugzilla.redhat.com/910070 Signed-off-by: Alex Lourie <[email protected]> --- M packaging/common_utils.py M packaging/ovirt-engine-dwh-setup.py 2 files changed, 121 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-dwh refs/changes/72/15772/1 diff --git a/packaging/common_utils.py b/packaging/common_utils.py index 91c70c0..97ab74d 100755 --- a/packaging/common_utils.py +++ b/packaging/common_utils.py @@ -43,6 +43,38 @@ DB_PORT = "5432" DB_ADMIN = "postgres" +# Backup validation messages +DB_BACKUP_HEADER = ( + 'The size of the DB detected is {dbsize}, free space ' + 'in the backup folder {backup} is {foldersize} Mb. \n' +) + +DB_BACKUP_SHOW_STOP = ( + '\nThere is not enough free space in the backup folder {backup} to backup ' + 'the existing database. Would you like to proceed without backup?\n' + 'Answering "no" will stop the upgrade' +) +DB_BACKUP_SHOW_CONTINUE = ( + '\nThe upgrade utility can backup the existing database. The time and ' + 'space required for the database backup depend on its size. The detected ' + 'DB size is {dbsize}. This process can take a considerable time, and in ' + 'some cases may take few hours to complete. Would you like to continue ' + 'and backup the existing database?\n' + 'Answering "no" will skip the backup step and continue the upgrade ' + 'without backing up the database' +) +DB_BACKUP_CONTINUE_WITH = ( + 'Are you sure you would like to continue ' + 'and backup database {db} with size {dbsize}?\n' + 'Answering "no" will stop the upgrade.' +) +DB_BACKUP_CONTINUE_WITHOUT = ( + 'Are you sure you would like to continue ' + 'and SKIP the backup of the database {db} ' + 'with size {dbsize}?\n' + 'Answering "no" will stop the upgrade.' +) + def getVDCOption(key): """ Get option_value from vdc_options per given key @@ -555,3 +587,77 @@ if failOnError and proc.returncode != 0: raise Exception(msg) return ("".join(output.splitlines(True)), proc.returncode) + +def getAvailableSpace(path): + logging.debug("Checking available space on %s" % (path)) + stat = os.statvfs(path) + #block size * available blocks = available space in bytes, we devide by + #1024 ^ 2 in order to get the size in megabytes + availableSpace = (stat.f_bsize * stat.f_bavail) / pow(1024, 2) + logging.debug("Available space on %s is %s" % (path, availableSpace)) + return int(availableSpace) + +# Returns db size in MB +def getDbSize(dbname): + sql = "SELECT pg_database_size(\'%s\')" % (dbname) + out, rc = execRemoteSqlCommand(getDbAdminUser(), + getDbHostName(), + getDbPort(), + basedefs.DB_POSTGRES, + sql, + True, + output_messages.ERR_DB_GET_SPACE % (dbname)) + size = int(out.splitlines()[2].strip()) + size = size / 1024 / 1024 # Get size in MB + return size + +def performBackup(dbname, backupPath): + # Check abvailable space + dbSize = utils.getDbSize(dbname) + backupPathFree = utils.getAvailableSpace(backupPath) + backupDB = False + verify = None + + if (dbSize * 1.1) < backupPathFree : + # allow upgrade, ask for backup + msg = ( + '{header}' + '{continue}' + ).format( + dbsize=dbSize, + backup=backupPath, + foldersize=backupPathFree, + ) + if utils.askYesNo(msg): + verify = DB_BACKUP_CONTINUE_WITH + else: + verify = DB_BACKUP_CONTINUE_WITHOUT + + else: + # ask to continue without backup, stop if no. + msg = ( + '{header}' + '{stop}' + ).format( + dbsize=dbSize, + backup=backupPath, + foldersize=backupPathFree, + ) + + continueSetup = utils.askYesNo(msg) + if continueSetup: + verify = DB_BACKUP_CONTINUE_WITHOUT + else: + raise UserWarning( + 'User decided to stop setup. Exiting' + ) + + # verify answer + if verify is not None and not utils.askYesNo(verify): + raise UserWarning( + 'User decided to stop setup. Exiting' + ) + else: + backupDB = True + + return backupDB diff --git a/packaging/ovirt-engine-dwh-setup.py b/packaging/ovirt-engine-dwh-setup.py index 06a9f62..7e8b02e 100755 --- a/packaging/ovirt-engine-dwh-setup.py +++ b/packaging/ovirt-engine-dwh-setup.py @@ -29,10 +29,12 @@ FILE_DB_CONN = "/etc/ovirt-engine/ovirt-engine-dwh/Default.properties" FILE_ENGINE_CONF_DEFAULTS = "/usr/share/ovirt-engine/conf/engine.conf.defaults" FILE_ENGINE_CONF = "/etc/ovirt-engine/engine.conf" +DB_BACKUPS_DIR = "/var/lib/ovirt-engine/backups" DB_NAME = "ovirt_engine_history" DB_USER_NAME = "postgres" DB_PORT = "5432" DB_HOST = "localhost" + #TODO: Create output messages file with all messages #TODO: Move all errors here to make consistent usage @@ -189,6 +191,19 @@ # Create/Upgrade DB if dbExists(db_dict): + try: + if utils.performBackup(db_dict['dbname'], DB_BACKUPS_DIR): + backupDB(db_dict) + except UserWarning: + print 'User decided to stop setup. Exiting.' + # Start Services + utils.startEngine() + # Sleep for 20 secs to allow health applet to start + time.sleep(20) + utils.startEtl() + sys.exit(1) + + # Backup went ok, so upgrade upgradeDB(db_dict) else: createDB(db_dict) -- To view, visit http://gerrit.ovirt.org/15772 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8ccf8938099207b4e73bb947d1bb5fd673605071 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
