def _customization(self): dbovirtutils = database.OvirtUtils(plugin=self) dbovirtutils.tryDatabaseConnect() dbstatement = database.Statement(environment=self.environment) rows = dbstatement.execute( statement=""" select id, storage, storage_name from storage_domain_static where storage_type=%(storage_type)s and storage_domain_type=%(storage_domain_type)s """, args=dict( storage_type=domains.StorageType.NFS, storage_domain_type=domains.StorageDomainType.ISO, ), ownConnection=True, ) for domain in rows: conn_row = dbstatement.execute( statement=""" select connection from storage_server_connections where id=%(storage)s """, args=dict(storage=domain['storage']), ownConnection=True, )[0] #returns only one line, can't exists duplicate and must exist one host, path = conn_row['connection'].split(':') if host == self.environment[osetupcons.ConfigEnv.FQDN]: self.environment[osetupcons.ConfigEnv.ISO_DOMAIN_EXISTS] = True self.environment[ osetupcons.SystemEnv.NFS_CONFIG_ENABLED] = True self.environment[ osetupcons.ConfigEnv.ISO_DOMAIN_NFS_MOUNT_POINT] = path self.environment[osetupcons.ConfigEnv. ISO_DOMAIN_NAME] = domain['storage_name'] self.environment[ osetupcons.ConfigEnv.ISO_DOMAIN_SD_UUID] = domain['id'] self.environment[ osetupcons.ConfigEnv. ISO_DOMAIN_STORAGE_DIR] = os.path.join( self.environment[ osetupcons.ConfigEnv.ISO_DOMAIN_NFS_MOUNT_POINT], self.environment[ osetupcons.ConfigEnv.ISO_DOMAIN_SD_UUID], 'images', osetupcons.Const.ISO_DOMAIN_IMAGE_UID, ) break
def _setup(self): config = configfile.ConfigFile([ osetupcons.FileLocations.OVIRT_ENGINE_SERVICE_CONFIG ]) if config.get('ENGINE_DB_PASSWORD') is not None: try: dbenv = {} for e, k in ( (osetupcons.DBEnv.HOST, 'ENGINE_DB_HOST'), (osetupcons.DBEnv.PORT, 'ENGINE_DB_PORT'), (osetupcons.DBEnv.USER, 'ENGINE_DB_USER'), (osetupcons.DBEnv.PASSWORD, 'ENGINE_DB_PASSWORD'), (osetupcons.DBEnv.DATABASE, 'ENGINE_DB_DATABASE'), ): dbenv[e] = config.get(k) for e, k in ( (osetupcons.DBEnv.SECURED, 'ENGINE_DB_SECURED'), ( osetupcons.DBEnv.SECURED_HOST_VALIDATION, 'ENGINE_DB_SECURED_VALIDATION' ) ): dbenv[e] = config.getboolean(k) self.environment[otopicons.CoreEnv.LOG_FILTER].append( dbenv[osetupcons.DBEnv.PASSWORD] ) dbovirtutils = database.OvirtUtils(plugin=self) dbovirtutils.tryDatabaseConnect(dbenv) self.environment.update(dbenv) self.environment[ osetupcons.DBEnv.NEW_DATABASE ] = dbovirtutils.isNewDatabase() except RuntimeError as e: self.logger.debug( 'Existing credential use failed', exc_info=True, ) msg = _( 'Cannot connect to database using existing ' 'credentials: {user}@{host}:{port}' ).format( host=dbenv[osetupcons.DBEnv.HOST], port=dbenv[osetupcons.DBEnv.PORT], database=dbenv[osetupcons.DBEnv.DATABASE], user=dbenv[osetupcons.DBEnv.USER], ) if self.environment[ osetupcons.CoreEnv.ACTION ] == osetupcons.Const.ACTION_REMOVE: self.logger.warning(msg) else: raise RuntimeError(msg)
def _miscUpgrade(self): self._checkSupportedVersionsPresent() dbovirtutils = database.OvirtUtils(plugin=self) backupFile = dbovirtutils.backup() self.environment[otopicons.CoreEnv.MAIN_TRANSACTION].append( self.SchemaTransaction( parent=self, backup=backupFile, )) # # TODO # rename database # why do we need rename? # consider doing that via python # self.logger.info(_('Updating database schema')) args = [ osetupcons.FileLocations.OVIRT_ENGINE_DB_UPGRADE, '-s', self.environment[osetupcons.DBEnv.HOST], '-p', str(self.environment[osetupcons.DBEnv.PORT]), '-u', self.environment[osetupcons.DBEnv.USER], '-d', self.environment[osetupcons.DBEnv.DATABASE], '-l', self.environment[otopicons.CoreEnv.LOG_FILE_NAME], ] if self.environment[osetupcons.CoreEnv.DEVELOPER_MODE]: if not os.path.exists( osetupcons.FileLocations.OVIRT_ENGINE_DB_MD5_DIR): os.makedirs(osetupcons.FileLocations.OVIRT_ENGINE_DB_MD5_DIR) args.extend([ '-m', osetupcons.FileLocations.OVIRT_ENGINE_DB_MD5_DIR, ]) else: args.extend([ '-g', # do not generate md5 ]) self.execute( args=args, envAppend={ 'ENGINE_CERTIFICATE': (osetupcons.FileLocations.OVIRT_ENGINE_PKI_ENGINE_CA_CERT), 'ENGINE_PGPASS': self.environment[osetupcons.DBEnv.PGPASS_FILE] }, )
def _misc(self): try: dbovirtutils = database.OvirtUtils(plugin=self) dbovirtutils.tryDatabaseConnect() self._bkpfile = dbovirtutils.backup() self.logger.info( _('Clearing database {database}').format( database=self.environment[osetupcons.DBEnv.DATABASE], )) dbovirtutils.clearOvirtEngineDatabase() except RuntimeError as e: self.logger.debug('exception', exc_info=True) self.logger.warning( _('Cannot clear database: {error}').format(error=e, ))
def _setDatabaseResources(self, environment): dbstatement = database.Statement(environment=environment, ) hasDatabase = dbstatement.execute( statement=""" select count(*) as count from pg_database where datname = %(database)s """, args=dict(database=self.environment[osetupcons.DBEnv.DATABASE], ), ownConnection=True, transaction=False, )[0]['count'] != 0 hasUser = dbstatement.execute( statement=""" select count(*) as count from pg_user where usename = %(user)s """, args=dict(user=self.environment[osetupcons.DBEnv.USER], ), ownConnection=True, transaction=False, )[0]['count'] != 0 generate = hasDatabase or hasUser existing = False if hasDatabase and hasUser: dbovirtutils = database.OvirtUtils( plugin=self, environment=environment, ) if dbovirtutils.isNewDatabase( database=self.environment[osetupcons.DBEnv.DATABASE], ): self.logger.debug('Found empty database') generate = False existing = True else: generate = True if generate: self.logger.debug('Existing resources found, generating names') suffix = '_%s' % datetime.datetime.now().strftime('%Y%m%d%H%M%S') self.environment[osetupcons.DBEnv.DATABASE] += suffix self.environment[osetupcons.DBEnv.USER] += suffix self._renamedDBResources = True return existing
def _waitDatabase(self, environment=None): dbovirtutils = database.OvirtUtils(plugin=self) for i in range(60): try: self.logger.debug('Attempting to connect database') dbovirtutils.tryDatabaseConnect(environment=environment) break except RuntimeError: self.logger.debug( 'Database connection failed', exc_info=True, ) time.sleep(1) except Exception: self.logger.debug( 'Database connection failed, unknown exception', exc_info=True, ) raise
def _customization(self): dbovirtutils = database.OvirtUtils(plugin=self) dbovirtutils.tryDatabaseConnect() dbstatement = database.Statement(environment=self.environment) rows = dbstatement.execute( statement=""" select id, storage, storage_name from storage_domain_static where storage_type=%(storage_type)s and storage_domain_type=%(storage_domain_type)s """, args=dict( storage_type=domains.StorageType.LOCALFS, storage_domain_type=domains.StorageDomainType.MASTER, ), ownConnection=True, ) for domain in rows: conn_row = dbstatement.execute( statement=""" select connection from storage_server_connections where id=%(storage)s """, args=dict(storage=domain['storage']), ownConnection=True, )[0] #returns only one line, can't exists duplicate and must exist one path = conn_row['connection'] self.environment[osetupcons.AIOEnv.CONFIGURE] = False self.environment[osetupcons.AIOEnv.ENABLE] = False self.environment[osetupcons.AIOEnv.STORAGE_DOMAIN_DIR] = path self.environment[ osetupcons.AIOEnv.STORAGE_DOMAIN_NAME] = domain['storage_name'] self._aio_already_configured = True break
def _validation(self): dbovirtutils = database.OvirtUtils(plugin=self) dbovirtutils.tryDatabaseConnect() dbstatement = database.Statement(environment=self.environment) my_domains = [] rows = dbstatement.execute( statement=""" select storage_name, connection from storage_domain_static s, storage_server_connections c where s.storage = c.id and s.storage_type=%(storage_type)s and s.storage_domain_type=%(storage_domain_type)s """, args=dict( storage_type=domains.StorageType.NFS, storage_domain_type=domains.StorageDomainType.ISO, ), ownConnection=True, ) for row in rows: host, path = row['connection'].split(':', 1) if host == self.environment[osetupcons.ConfigEnv.FQDN]: my_domains.append(row['storage_name']) if my_domains: self.logger.warning(_('Engine host hosting Storage Domains')) self.dialog.note(text=_( 'The following Storage Domains use the engine host\n' 'as an NFS server:\n' '\n' '{domains}\n' '\n' 'Cannot rename the engine host. Please backup relevant\n' 'data if needed, remove all of these domains, and then\n' 'run this utility again.\n').format( domains='\n'.join(sorted(my_domains))), ) raise RuntimeError(_('Cannot rename host hosting Storage Domains'))
def abort(self): self._parent.logger.info(_('Rolling back database schema')) try: dbovirtutils = database.OvirtUtils(plugin=self._parent) self._parent.logger.info( _('Clearing database {database}').format( database=self._parent.environment[ osetupcons.DBEnv.DATABASE], )) dbovirtutils.clearOvirtEngineDatabase() if self._backup is not None and os.path.exists(self._backup): self._parent.logger.info( _('Restoring database {database}').format( database=self._parent.environment[ osetupcons.DBEnv.DATABASE], )) dbovirtutils.restore(backupFile=self._backup) except Exception as e: self._parent.logger.debug( 'Exception during database restore', exc_info=True, ) self._parent.logger.error( _('Database rollback failed: {error}').format(error=e, ))
def _commands(self): dbovirtutils = database.OvirtUtils(plugin=self) dbovirtutils.detectCommands()
def _setup(self): dbovirtutils = database.OvirtUtils(plugin=self) dbovirtutils.tryDatabaseConnect() if dbovirtutils.isNewDatabase(): raise RuntimeError(_('Unexpected empty database during upgrade'))
def _customization(self): dbovirtutils = database.OvirtUtils(plugin=self) interactive = None in ( self.environment[osetupcons.DBEnv.HOST], self.environment[osetupcons.DBEnv.PORT], self.environment[osetupcons.DBEnv.DATABASE], self.environment[osetupcons.DBEnv.USER], self.environment[osetupcons.DBEnv.PASSWORD], ) if interactive: self.dialog.note(text=_( "\n" "ATTENTION\n" "\n" "Manual action required.\n" "Please create database for ovirt-engine use. " "Use the following commands as an example:\n" "\n" "create user engine password 'engine';\n" "create database engine owner engine template template0\n" "encoding 'UTF8' lc_collate 'en_US.UTF-8'\n" "lc_ctype 'en_US.UTF-8';\n" "\n" "Make sure that database can be accessed remotely.\n" "\n"), ) else: self.dialog.note(text=_('Using existing credentials'), ) connectionValid = False while not connectionValid: host = self.environment[osetupcons.DBEnv.HOST] port = self.environment[osetupcons.DBEnv.PORT] secured = self.environment[osetupcons.DBEnv.SECURED] securedHostValidation = self.environment[ osetupcons.DBEnv.SECURED_HOST_VALIDATION] db = self.environment[osetupcons.DBEnv.DATABASE] user = self.environment[osetupcons.DBEnv.USER] password = self.environment[osetupcons.DBEnv.PASSWORD] if host is None: while True: host = self.dialog.queryString( name='OVESETUP_DB_HOST', note=_('Database host [@DEFAULT@]: '), prompt=True, default=osetupcons.Defaults.DEFAULT_DB_HOST, ) try: socket.getaddrinfo(host, None) break # do while missing in python except socket.error as e: self.logger.error( _('Host is invalid: {error}').format( error=e.strerror)) if port is None: while True: try: port = osetuputil.parsePort( self.dialog.queryString( name='OVESETUP_DB_PORT', note=_('Database port [@DEFAULT@]: '), prompt=True, default=osetupcons.Defaults.DEFAULT_DB_PORT, )) break # do while missing in python except ValueError: pass if secured is None: secured = dialog.queryBoolean( dialog=self.dialog, name='OVESETUP_DB_SECURED', note=_('Database secured connection (@VALUES@) ' '[@DEFAULT@]: '), prompt=True, default=osetupcons.Defaults.DEFAULT_DB_SECURED, ) if not secured: securedHostValidation = False if securedHostValidation is None: securedHostValidation = dialog.queryBoolean( dialog=self.dialog, name='OVESETUP_DB_SECURED_HOST_VALIDATION', note=_( 'Validate host name in secured connection (@VALUES@) ' '[@DEFAULT@]: '), prompt=True, default=True, ) == 'yes' if db is None: db = self.dialog.queryString( name='OVESETUP_DB_DATABASE', note=_('Database name [@DEFAULT@]: '), prompt=True, default=osetupcons.Defaults.DEFAULT_DB_DATABASE, ) if user is None: user = self.dialog.queryString( name='OVESETUP_DB_USER', note=_('Database user [@DEFAULT@]: '), prompt=True, default=osetupcons.Defaults.DEFAULT_DB_USER, ) if password is None: password = self.dialog.queryString( name='OVESETUP_DB_PASSWORD', note=_('Database password: '******'Cannot connect to database: {error}').format( error=e, )) else: # this is usally reached in provisioning # or if full ansewr file self.environment.update(dbenv) connectionValid = True try: self.environment[ osetupcons.DBEnv.NEW_DATABASE] = dbovirtutils.isNewDatabase() except: self.logger.debug('database connection failed', exc_info=True)