def modifyUUIDs(): for vds_id, vds_unique_id in hostids.items(): query = "UPDATE vds_static SET vds_unique_id='%s' where vds_id='%s';" % ( vds_unique_id, vds_id) utils.execRemoteSqlCommand(SERVER_ADMIN, SERVER_NAME, SERVER_PORT, basedefs.DB_NAME, query, True, MSG_ERROR_CONNECT_DB)
def unsupportedVersionsPresent(oldversion=UNSUPPORTED_VERSION): """ Check whether there are UNSUPPORTED_VERSION objects present. If yes, throw an Exception """ queryCheckDCVersions="SELECT compatibility_version FROM storage_pool;" dcVersions, rc = utils.execRemoteSqlCommand( userName=SERVER_ADMIN, dbHost=SERVER_NAME, dbPort=SERVER_PORT, dbName=basedefs.DB_NAME, sqlQuery=queryCheckDCVersions, failOnError=True, errMsg=MSG_ERROR_CONNECT_DB, ) queryCheckClusterVersions="SELECT compatibility_version FROM vds_groups;" clusterVersions, rc = utils.execRemoteSqlCommand( userName=SERVER_ADMIN, dbHost=SERVER_NAME, dbPort=SERVER_PORT, dbName=basedefs.DB_NAME, sqlQuery=queryCheckClusterVersions, failOnError=True, errMsg=MSG_ERROR_CONNECT_DB, ) for versions in dcVersions, clusterVersions: if oldversion in versions: return True return False
def unsupportedVersionsPresent(oldversion=UNSUPPORTED_VERSION, dbName=basedefs.DB_NAME): """ Check whether there are UNSUPPORTED_VERSION objects present. If yes, throw an Exception dbName is provided for flexibility of quering different possible DBs. """ queryCheckDCVersions = "SELECT compatibility_version FROM storage_pool;" dcVersions, rc = utils.execRemoteSqlCommand( userName=SERVER_ADMIN, dbHost=SERVER_NAME, dbPort=SERVER_PORT, dbName=dbName, sqlQuery=queryCheckDCVersions, failOnError=True, errMsg=MSG_ERROR_CONNECT_DB, ) queryCheckClusterVersions = "SELECT compatibility_version FROM vds_groups;" clusterVersions, rc = utils.execRemoteSqlCommand( userName=SERVER_ADMIN, dbHost=SERVER_NAME, dbPort=SERVER_PORT, dbName=dbName, sqlQuery=queryCheckClusterVersions, failOnError=True, errMsg=MSG_ERROR_CONNECT_DB, ) for versions in dcVersions, clusterVersions: if oldversion in versions: return True return False
def modifyUUIDs(): for vds_id, vds_unique_id in hostids.items(): query="UPDATE vds_static SET vds_unique_id='%s' where vds_id='%s';" % ( vds_unique_id, vds_id ) utils.execRemoteSqlCommand( SERVER_ADMIN, SERVER_NAME, SERVER_PORT, basedefs.DB_NAME, query, True, MSG_ERROR_CONNECT_DB )
def exists(self): """ check that db exists """ logging.debug("verifying that db '%s' exists" % (basedefs.DB_NAME)) # Making sure postgresql service is up - only on local installation if utils.localHost(DB_HOST): postgresql = utils.Service("postgresql") postgresql.conditionalStart() try: # We want to make check that we can connect to basedefs.DB_NAME DB logging.debug("making sure postgresql service is up") utils.retry(utils.checkIfDbIsUp, tries=5, timeout=15, sleep=3) except: # If we're here, it means something is wrong, either there is a real db error # or the db is not installed, let's check logging.debug("checking if db is already installed..") out, rc = utils.execRemoteSqlCommand(DB_ADMIN, DB_HOST, DB_PORT, basedefs.DB_NAME, "select 1") if rc != 0: if utils.verifyStringFormat( out, ".*FATAL:\s*database\s*\"%s\"\s*does not exist" % (PREFIX)): # This means that the db is not installed, so we return false return False return True
def exists(self): """ check that db exists """ logging.debug("verifying that db '%s' exists" % (basedefs.DB_NAME)) # Checking whether pgpass file exists. If not, # we cannot cleanup DB, so return False. if not os.path.exists(basedefs.DB_PASS_FILE): logging.info(MSG_ERR_CANT_FIND_PGPASS_FILE) return False # Making sure postgresql service is up - only on local installation if utils.localHost(DB_HOST): postgresql = utils.Service("postgresql") postgresql.conditionalStart() try: # We want to make check that we can connect to basedefs.DB_NAME DB logging.debug("Checking that DB '%s' exists", basedefs.DB_NAME) utils.retry(utils.checkIfDbIsUp, tries=5, timeout=15, sleep=3) except: # If we're here, it means something is wrong, either there is a real db error # or the db is not installed, let's check logging.debug("checking if db is already installed..") out, rc = utils.execRemoteSqlCommand(DB_ADMIN, DB_HOST, DB_PORT, basedefs.DB_NAME, "select 1") if rc != 0: if utils.verifyStringFormat(out,".*FATAL:\s*database\s*\"%s\"\s*does not exist" % (PREFIX)): # This means that the db is not installed, so we return false return False else: raise Exception(MSG_ERROR_CONNECT_DB) return True
def exists(self): """ check that db exists """ logging.debug("verifying that db '%s' exists" % (basedefs.DB_NAME)) # Making sure postgresql service is up - only on local installation if utils.localHost(DB_HOST): postgresql = utils.Service("postgresql") postgresql.conditionalStart() try: # We want to make check that we can connect to basedefs.DB_NAME DB logging.debug("making sure postgresql service is up") utils.retry(utils.checkIfDbIsUp, tries=5, timeout=15, sleep=3) except: # If we're here, it means something is wrong, either there is a real db error # or the db is not installed, let's check logging.debug("checking if db is already installed..") out, rc = utils.execRemoteSqlCommand(DB_ADMIN, DB_HOST, DB_PORT, basedefs.DB_NAME, "select 1") if rc != 0: if utils.verifyStringFormat(out,".*FATAL:\s*database\s*\"%s\"\s*does not exist" % (PREFIX)): # This means that the db is not installed, so we return false return False return True
def rename(self, newname): """ Rename DB from current name to a newname""" # Check that newname is different from current if self.name == newname: return # run the rename query and raise Exception on error query = "ALTER DATABASE %s RENAME TO %s" % (self.name, newname) try: utils.execRemoteSqlCommand(SERVER_ADMIN, SERVER_NAME, SERVER_PORT, basedefs.DB_POSTGRES, query, True, MSG_ERROR_RENAME_DB) # set name to the newname self.name = newname except: # if this happened before DB update, remove DB backup file. if not self.updated and os.path.exists(self.sqlfile): os.remove(self.sqlfile) raise
def _checkUUIDExtension(dbAdminUser, dbHost, dbPort): """ Check that UUID extension is already loaded and raise Exception if not""" logging.info("Checking that uuid extension is loaded by default on the remote server") out, rc = utils.execRemoteSqlCommand(dbAdminUser, dbHost, dbPort, "ovirt_engine_test", "SELECT uuid_generate_v1();") # Extension was found if not rc and out and "1 row" in out: logging.info("Successfully passed UUID check") else: logging.error(output_messages.ERR_DB_UUID) raise Exception(output_messages.ERR_DB_UUID)
def _checkCreateDbPrivilege(dbAdminUser, dbHost, dbPort): """ _checkCreateDbPrivilege checks CREATE DB privilege on DB server""" logging.info("Creating database 'ovirt_engine_test' on remote server.") out, rc = utils.execRemoteSqlCommand(dbAdminUser, dbHost, dbPort, basedefs.DB_POSTGRES, "CREATE DATABASE ovirt_engine_test") # Error in "CREATE DATABASE", meaning we don't have enough privileges to create database. if rc: logging.error(output_messages.ERR_DB_CREATE_FAILED % dbHost) raise Exception("\n" + output_messages.ERR_DB_CREATE_FAILED % dbHost + ".\n") else: logging.info("Successfully created temp database on server %s." % dbHost)
def _checkDropDbPrivilege(dbAdminUser, dbHost, dbPort): """ _checkCreateDbPrivilege checks CREATE DB privilege on DB server""" logging.info("Deleting the test database from the remote server") out, rc = utils.execRemoteSqlCommand(dbAdminUser, dbHost, dbPort, basedefs.DB_POSTGRES, "DROP DATABASE ovirt_engine_test") # Error in "DROP DATABASE", meaning we don't have enough privileges to drop database. if rc: logging.error(output_messages.ERR_DB_DROP_PRIV % dbHost) raise Exception("\n" + output_messages.ERR_DB_DROP_PRIV % dbHost + ".\n") else: logging.info("Successfully deleted database on server %s." % dbHost)
def _checkCreateDbPrivilege(dbAdminUser, dbHost, dbPort): """ _checkCreateDbPrivilege checks CREATE DB privilege on DB server""" logging.info("Creating database 'ovirt_engine_test' on remote server.") out, rc = utils.execRemoteSqlCommand(dbAdminUser, dbHost, dbPort, basedefs.DB_POSTGRES, "CREATE DATABASE ovirt_engine_test") # Error in "CREATE DATABASE", meaning we don't have enough privileges to create database. if rc: logging.error(output_messages.ERR_DB_CREATE_PRIV, dbHost) raise Exception("\n" + output_messages.ERR_DB_CREATE_PRIV % dbHost + "\n") else: logging.info("Successfully created temp database on server %s." % dbHost)
def _checkDbConnection(dbAdminUser, dbHost, dbPort): """ _checkDbConnection checks connection to the DB""" # Connection check logging.info("Trying to connect to the remote database with provided credentials.") out, rc = utils.execRemoteSqlCommand(dbAdminUser, dbHost, dbPort, basedefs.DB_POSTGRES, "select 1") # It error is in "SELECT 1" it means that we have a problem with simple DB connection. if rc: logging.error(output_messages.ERR_DB_CONNECTION % dbHost) raise Exception("\n" + output_messages.ERR_DB_CONNECTION % dbHost + "\n") else: logging.info("Successfully connected to the DB host %s." % dbHost)
def _checkUUIDExtension(dbAdminUser, dbHost, dbPort): """ Check that UUID extension is already loaded and raise Exception if not""" logging.info( "Checking that uuid extension is loaded by default on the remote server" ) out, rc = utils.execRemoteSqlCommand(dbAdminUser, dbHost, dbPort, "ovirt_engine_test", "SELECT uuid_generate_v1();") # Extension was found if not rc and out and "1 row" in out: logging.info("Successfully passed UUID check") else: logging.error(output_messages.ERR_DB_UUID) raise Exception(output_messages.ERR_DB_UUID)
def getRunningTasks(dbName=basedefs.DB_NAME): # Get async tasks: runningTasks, rc = utils.execRemoteSqlCommand( userName=SERVER_ADMIN, dbHost=SERVER_NAME, dbPort=SERVER_PORT, dbName=dbName, sqlQuery=ASYNC_TASKS_QUERY, failOnError=True, errMsg="Can't get async tasks list", ) # We only want to return anything if there are really async tasks records if runningTasks and "RECORD" in runningTasks: return runningTasks else: return ""
def getCompensations(dbName=basedefs.DB_NAME): # Get compensations compensations, rc = utils.execRemoteSqlCommand( userName=SERVER_ADMIN, dbHost=SERVER_NAME, dbPort=SERVER_PORT, dbName=dbName, sqlQuery=COMPENSATIONS_QUERY, failOnError=True, errMsg="Can't get compensations list", ) # We only want to return anything if there are really compensations records if not compensations or \ (compensations and "0 rows" in compensations): return "" else: return compensations
def exists(self): """ check that db exists """ logging.debug("verifying that db '%s' exists" % (basedefs.DB_NAME)) # Checking whether pgpass file exists. If not, # we cannot cleanup DB, so return False. if not os.path.exists(basedefs.DB_PASS_FILE): logging.info(MSG_ERR_CANT_FIND_PGPASS_FILE) return False # Making sure postgresql service is up - only on local installation if utils.localHost(DB_HOST): postgresql = utils.Service("postgresql") postgresql.conditionalStart() try: # We want to make check that we can connect to basedefs.DB_NAME DB logging.debug("Checking that DB '%s' exists", basedefs.DB_NAME) utils.retry(utils.checkIfDbIsUp, tries=5, timeout=15, sleep=3) except: # If we're here, it means something is wrong, either there is a real db error # or the db is not installed, let's check logging.debug("checking if db is already installed..") out, rc = utils.execRemoteSqlCommand( userName=DB_ADMIN, dbHost=DB_HOST, dbPort=DB_PORT, dbName=basedefs.DB_NAME, sqlQuery="select 1", ) if rc != 0: if utils.verifyStringFormat( out, ".*FATAL:\s*database\s*\"%s\"\s*does not exist" % (PREFIX)): # This means that the db is not installed, so we return false return False else: raise Exception(MSG_ERROR_CONNECT_DB) return True
# Delete DB check _checkDropDbPrivilege(param["DB_ADMIN"], param["DB_HOST"], param["DB_PORT"]) # Everything is fine, return True return True except Exception,e: # Something failed, print the error on screen and return False print e return False finally: # if the test DB was created, drop it sqlQuery = "DROP DATABASE IF EXISTS ovirt_engine_test;" utils.execRemoteSqlCommand(param["DB_ADMIN"], param["DB_HOST"], param["DB_PORT"], basedefs.DB_POSTGRES, sqlQuery, False) # restore the original pgpass file in all cases if os.path.exists(backupFile): os.rename(backupFile, basedefs.DB_PASS_FILE) def validateFQDN(param, options=[]): logging.info("Validating %s as a FQDN"%(param)) if not validateDomain(param,options): return False try: #get set of IPs ipAddresses = utils.getConfiguredIps() if len(ipAddresses) < 1: