def getAllEnabledComponentList(self): """ Get a list of all enabled components in the system Returns: [ components ] Throws: DbError """ session = DbManager().openSession() try: handler = ComponentsDbHandler() dbComponents = handler.getEnabledComponentList(session) return self.getTortugaObjectList(Component, dbComponents) except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession()
def getAllEnabledComponentList(self, session: Session) -> TortugaObjectList: """ Get a list of all enabled components in the system Returns: [ components ] Throws: DbError """ self._logger.debug('Retrieving enabled component list') try: dbComponents = \ ComponentsDbHandler().getEnabledComponentList(session) return self.getTortugaObjectList(Component, dbComponents) except TortugaException: raise except Exception as ex: self._logger.exception(str(ex)) raise
def __init__(self): TortugaDbObjectHandler.__init__(self) self._componentsDbHandler = ComponentsDbHandler()
class SoftwareProfilesDbHandler(TortugaDbObjectHandler): """ This class handles softwareProfiles table. """ def __init__(self): TortugaDbObjectHandler.__init__(self) self._componentsDbHandler = ComponentsDbHandler() def getSoftwareProfile(self, session, name): """ Return softwareProfile. """ self.getLogger().debug('Retrieving software profile [%s]' % (name)) try: return session.query(SoftwareProfiles).filter( SoftwareProfiles.name == name).one() except NoResultFound: raise SoftwareProfileNotFound('Software profile [%s] not found' % (name)) def getSoftwareProfileById(self, session, _id): """ Return software profile Raises: SoftwareProfileNotFound """ self.getLogger().debug('Retrieving software profile ID [%s]' % (_id)) dbSoftwareProfile = session.query(SoftwareProfiles).get(_id) if not dbSoftwareProfile: raise SoftwareProfileNotFound( 'Software profile ID [%s] not found.' % (_id)) return dbSoftwareProfile def getSoftwareProfileList(self, session, tags=None): """ Get list of softwareProfiles from the db. """ self.getLogger().debug('Retrieving software profile list') searchspec = [] if tags: # Build searchspec from specified tags for tag in tags: if len(tag) == 2: searchspec.append( and_(SoftwareProfiles.tags.any(name=tag[0]), SoftwareProfiles.tags.any(value=tag[1]))) else: searchspec.append(SoftwareProfiles.tags.any(name=tag[0])) return session.query(SoftwareProfiles).filter( or_(*searchspec)).order_by(SoftwareProfiles.name).all() def getIdleSoftwareProfileList(self, session): """ Get list of idle softwareProfiles from the db. """ self.getLogger().debug('Retrieving idle software profile list') return session.query(SoftwareProfiles).filter( SoftwareProfiles.isIdle == 1).all() def __getHardwareProfile(self, session, hardwareProfileName): \ # pylint: disable=no-self-use try: return session.query(HardwareProfiles).filter( HardwareProfiles.name == hardwareProfileName).one() except NoResultFound: raise HardwareProfileNotFound('Hardware profile [%s] not found' % (hardwareProfileName)) def addUsableHardwareProfileToSoftwareProfile(self, session, hardwareProfileName, softwareProfileName): """ Add usable hardwareProfile to softwareProfile. Raises: HardwareProfileNotFound """ self.getLogger().debug( 'Adding hardware profile [%s] to software profile [%s]' % (hardwareProfileName, softwareProfileName)) dbHardwareProfile = self.__getHardwareProfile(session, hardwareProfileName) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) if dbHardwareProfile in dbSoftwareProfile.hardwareprofiles: raise SoftwareUsesHardwareAlreadyExists( 'Software profile [%s] already mapped to hardware' ' profile [%s]' % (softwareProfileName, hardwareProfileName)) dbSoftwareProfile.hardwareprofiles.append(dbHardwareProfile) def _deleteUsableHardwareProfileFromSoftwareProfile( self, hardwareprofile, softwareprofile): """ Raises: SoftwareUsesHardwareNotFound """ if hardwareprofile not in softwareprofile.hardwareprofiles: raise SoftwareUsesHardwareNotFound( 'Hardware profile [%s] does not belong to software' ' profile [%s]' % (hardwareprofile.name, softwareprofile.name)) self.getLogger().debug( 'Deleting mapping of software profile [%s] to hardware' ' profile [%s]' % (softwareprofile.name, hardwareprofile.name)) softwareprofile.hardwareprofiles.remove(hardwareprofile) def deleteUsableHardwareProfileFromSoftwareProfile(self, session, hardwareProfileName, softwareProfileName): """ Delete usable hardwareProfile to softwareProfile. Raises: HardwareProfileNotFound SoftwareProfileNotFound SoftwareUsesHardwareNotFound """ dbHardwareProfile = self.__getHardwareProfile(session, hardwareProfileName) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) self._deleteUsableHardwareProfileFromSoftwareProfile( dbHardwareProfile, dbSoftwareProfile) def addComponentToSoftwareProfileEx(self, session, componentId, dbSoftwareProfile): """ Raises: ComponentNotFound """ dbComponent = self._componentsDbHandler.getComponentById( session, componentId) self._addComponent(dbComponent, dbSoftwareProfile) def _addComponent(self, dbComponent, dbSoftwareProfile): self.getLogger().debug( 'Adding component [%s] to software profile [%s]' % ('%s-%s' % (dbComponent.name, dbComponent.version), dbSoftwareProfile.name)) if dbComponent in dbSoftwareProfile.components: raise SoftwareProfileComponentAlreadyExists( 'Component [%s] already enabled on software profile [%s]' % (dbComponent.name, dbSoftwareProfile.name)) dbSoftwareProfile.components.append(dbComponent) def addComponentToSoftwareProfile(self, session, componentId, softwareProfileId): """ Add component to softwareProfile. Raises: SoftwareProfileNotFound ComponentNotFound """ dbSoftwareProfile = self.getSoftwareProfileById( session, softwareProfileId) dbComponent = self._componentsDbHandler.getComponentById( session, componentId) self._addComponent(dbComponent, dbSoftwareProfile) def deleteComponentFromSoftwareProfile(self, session, componentId, softwareProfileId): """ Delete component from softwareProfile. Raises: SoftwareProfileNotFound """ dbSoftwareProfile = self.getSoftwareProfileById( session, softwareProfileId) dbComponent = self._componentsDbHandler.getComponentById( session, componentId) compDescr = '%s-%s' % (dbComponent.name, dbComponent.version) self.getLogger().debug( 'Deleting component [%s] from software profile [%s]' % (compDescr, dbSoftwareProfile)) if dbComponent not in dbSoftwareProfile.components: raise SoftwareProfileComponentNotFound( 'Component [%s] not enabled on software profile [%s]' % (compDescr, dbSoftwareProfile.name)) dbSoftwareProfile.components.remove(dbComponent) self.getLogger().debug( 'Deleted component [%s] from software profile [%s]' % (compDescr, dbSoftwareProfile.name)) def __getPackage(self, session, packageName): \ # pylint: disable=no-self-use return session.query(Packages).filter( Packages.name == packageName).one() def addPackageToSoftwareProfile(self, session, packageName, softwareProfileName): """ Add package to software profile """ self.getLogger().debug('Adding package [%s] to software profile [%s]' % (packageName, softwareProfileName)) # Check if package record already exists try: dbPackage = self.__getPackage(session, packageName) except NoResultFound: dbPackage = None dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) if not dbPackage: dbPackage = Packages(packageName) if dbPackage in dbSoftwareProfile.packages: raise PackageAlreadyExists( 'Package [%s] already exists for software' ' profile [%s]' % (packageName, softwareProfileName)) self.getLogger().debug('Adding package [%s] to software profile [%s]' % (packageName, softwareProfileName)) dbSoftwareProfile.packages.append(dbPackage) def deletePackageFromSoftwareProfile(self, session, packageName, softwareProfileName): """ Delete package from software profile. """ self.getLogger().debug( 'Deleting package [%s] from software profile [%s]' % (packageName, softwareProfileName)) try: dbPackage = self.__getPackage(session, packageName) except NoResultFound: dbPackage = None dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) if dbPackage not in dbSoftwareProfile.packages: raise PackageNotFound( 'Package [%s] does not exist for software profile [%s]' % (packageName, softwareProfileName)) dbSoftwareProfile.packages.remove(dbPackage) if not dbPackage.softwareprofiles: session.delete(dbPackage) self.getLogger().debug( 'Deleted package [%s] from software profile ID [%s]' % (packageName, softwareProfileName)) def __getPartition(self, session, name): # pylint: disable=no-self-use return session.query(Partitions).filter(Partitions.name == name).one() def addPartitionToSoftwareProfile(self, session, partition, softwareProfileName): """ Add partition to software profile. """ self.getLogger().debug( 'Adding partition [%s] to software profile [%s]' % (partition.getName(), softwareProfileName)) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) try: dbPartition = self.__getPartition(session, partition.getName()) except NoResultFound: pass if dbPartition in dbSoftwareProfile.partitions: raise PartitionAlreadyExists( 'Partition [%s] already exists for software' ' profile [%s]' % (partition.getName(), softwareProfileName)) dbPartition = Partitions( name=partition.getName(), device=partition.getDevice(), mountPoint=partition.getMountPoint(), fsType=partition.getFsType(), size=partition.getSize(), options=partition.getOptions(), preserve=partition.getPreserve(), bootLoader=partition.getBootLoader(), directAttachment=partition.getDirectAttachment(), indirectAttachment=partition.getIndirectAttachment(), diskSize=partition.getDiskSize()) dbSoftwareProfile.partitions.append(dbPartition) def deletePartitionFromSoftwareProfile(self, session, partitionName, softwareProfileName): """ Delete partition from software profile. """ self.getLogger().debug( 'Deleting partition [%s] from software profile [%s]' % (partitionName, softwareProfileName)) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) try: dbPartition = self.__getPartition(session, partitionName) except NoResultFound: raise PartitionNotFound( 'Partition [%s] does not exist for software profile' ' [%s]' % (partitionName, softwareProfileName)) self.getLogger().debug( 'Deleted partition [%s] from software profile [%s]' % (partitionName, softwareProfileName)) dbSoftwareProfile.partitions.remove(dbPartition)
def __init__(self): TortugaDbApi.__init__(self) self._softwareProfilesDbHandler = SoftwareProfilesDbHandler() self._componentsDbHandler = ComponentsDbHandler()
class ComponentDbApi(TortugaDbApi): """ Component DB API class. """ def __init__(self): TortugaDbApi.__init__(self) self._softwareProfilesDbHandler = SoftwareProfilesDbHandler() self._componentsDbHandler = ComponentsDbHandler() def getComponent(self, session: Session, name: str, version: str, osInfo: OsInfo, optionDict: Optional[Union[dict, None]] = None) \ -> Component: """ Get component from the db. Returns: component Throws: ComponentNotFound DbError """ try: dbComponent = self._componentsDbHandler.getComponentByOsInfo( session, name, version, osInfo) self.loadRelations(dbComponent, optionDict) return Component.getFromDbDict(dbComponent.__dict__) except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise def getBestMatchComponent(self, session: Session, name, version, osInfo, kitId): """ Get component from the db. Returns: component Throws: ComponentNotFound DbError """ try: dbComponent = self._componentsDbHandler.getBestMatchComponent( session, name, version, osInfo, kitId) self.loadRelations( dbComponent, { 'os': True, 'family': True, 'kit': True, 'os_components': True, 'osfamily_components': True, }) return Component.getFromDbDict(dbComponent.__dict__) except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise def addComponentToSoftwareProfile(self, session: Session, componentId, softwareProfileId): """ Add component to softwareProfile. Returns: None Throws: SoftwareProfileNotFound ComponentNotFound SoftwareProfileComponentAlreadyExists DbError """ try: self._softwareProfilesDbHandler.addComponentToSoftwareProfile( session, componentId, softwareProfileId) session.commit() except TortugaException: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise def deleteComponentFromSoftwareProfile(self, session: Session, componentId, softwareProfileId): """ Delete component to software profile. Returns: None Throws: SoftwareProfileNotFound ComponentNotFound SoftwareProfileComponentNotFound DbError """ try: self._softwareProfilesDbHandler.\ deleteComponentFromSoftwareProfile( session, componentId, softwareProfileId) session.commit() except TortugaException: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise def getComponentList(self, session: Session, softwareProfile=None): try: if softwareProfile: return self._softwareProfilesDbHandler.getSoftwareProfile( session, softwareProfile).components # List all components self.getLogger().debug('Retrieving component list') dbComps = self._componentsDbHandler.getComponentList(session) return self.getTortugaObjectList(Component, dbComps) except Exception as ex: self.getLogger().exception('%s' % ex) raise
def __init__(self): super().__init__() self._componentsDbHandler = ComponentsDbHandler()
class SoftwareProfilesDbHandler(TortugaDbObjectHandler): """ This class handles softwareProfiles table. """ def __init__(self): super().__init__() self._componentsDbHandler = ComponentsDbHandler() def getSoftwareProfile(self, session: Session, name: str) -> SoftwareProfile: """ Return softwareProfile Raises: SoftwareProfileNotFound """ self._logger.debug('Retrieving software profile [%s]', name) try: return session.query(SoftwareProfile).filter( SoftwareProfile.name == name).one() except NoResultFound: raise SoftwareProfileNotFound('Software profile [%s] not found' % (name)) def getSoftwareProfileById(self, session: Session, _id: int) -> SoftwareProfile: """ Return software profile Raises: SoftwareProfileNotFound """ self._logger.debug('Retrieving software profile ID [%s]', _id) dbSoftwareProfile = session.query(SoftwareProfile).get(_id) if not dbSoftwareProfile: raise SoftwareProfileNotFound( 'Software profile ID [%s] not found.' % (_id)) return dbSoftwareProfile def getSoftwareProfileList( self, session, tags: Optional[Tags] = None, profile_type: Optional[str] = None) -> List[SoftwareProfile]: """ Get list of softwareProfiles from the db. """ self._logger.debug('Retrieving software profile list') q = session.query(SoftwareProfile) if profile_type: # filter by profile type q = q.filter(SoftwareProfile.type == profile_type) searchspec = [] if tags: for name, value in tags.items(): if value: # # Match both name and value # searchspec.append( and_(SoftwareProfile.tags.any(name=name), SoftwareProfile.tags.any(value=value))) else: # # Match name only # searchspec.append(SoftwareProfile.tags.any(name=name)) return q.filter(and_(*searchspec)).order_by(SoftwareProfile.name).all() def __getHardwareProfile(self, session: Session, hardwareProfileName: str): \ # pylint: disable=no-self-use try: return session.query(HardwareProfile).filter( HardwareProfile.name == hardwareProfileName).one() except NoResultFound: raise HardwareProfileNotFound('Hardware profile [%s] not found' % (hardwareProfileName)) def addUsableHardwareProfileToSoftwareProfile( self, session: Session, hardwareProfileName: str, softwareProfileName: str) -> None: """ Add usable hardwareProfile to softwareProfile. Raises: HardwareProfileNotFound SoftwareUsesHardwareAlreadyExists """ self._logger.debug( 'Adding hardware profile [%s] to software profile [%s]', hardwareProfileName, softwareProfileName) dbHardwareProfile = self.__getHardwareProfile(session, hardwareProfileName) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) if dbHardwareProfile in dbSoftwareProfile.hardwareprofiles: raise SoftwareUsesHardwareAlreadyExists( 'Software profile [%s] already mapped to hardware' ' profile [%s]' % (softwareProfileName, hardwareProfileName)) dbSoftwareProfile.hardwareprofiles.append(dbHardwareProfile) def _deleteUsableHardwareProfileFromSoftwareProfile( self, hardwareprofile: HardwareProfile, softwareprofile: SoftwareProfile) -> None: """ Raises: SoftwareUsesHardwareNotFound """ if hardwareprofile not in softwareprofile.hardwareprofiles: raise SoftwareUsesHardwareNotFound( 'Hardware profile [%s] does not belong to software' ' profile [%s]' % (hardwareprofile.name, softwareprofile.name)) self._logger.debug( 'Deleting mapping of software profile [%s] to hardware' ' profile [%s]' % (softwareprofile.name, hardwareprofile.name)) softwareprofile.hardwareprofiles.remove(hardwareprofile) def deleteUsableHardwareProfileFromSoftwareProfile( self, session: Session, hardwareProfileName: str, softwareProfileName: str) -> None: """ Delete usable hardwareProfile to softwareProfile. Raises: HardwareProfileNotFound SoftwareProfileNotFound SoftwareUsesHardwareNotFound """ dbHardwareProfile = self.__getHardwareProfile(session, hardwareProfileName) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) self._deleteUsableHardwareProfileFromSoftwareProfile( dbHardwareProfile, dbSoftwareProfile) def addComponentToSoftwareProfileEx( self, session: Session, componentId: int, dbSoftwareProfile: SoftwareProfile) -> None: """ Raises: ComponentNotFound """ dbComponent = self._componentsDbHandler.getComponentById( session, componentId) self._addComponent(dbComponent, dbSoftwareProfile) def _addComponent(self, dbComponent: Component, dbSoftwareProfile: SoftwareProfile) -> None: self._logger.debug( 'Adding component [%s] to software profile [%s]' % ('%s-%s' % (dbComponent.name, dbComponent.version), dbSoftwareProfile.name)) if dbComponent in dbSoftwareProfile.components: raise SoftwareProfileComponentAlreadyExists( 'Component [%s] already enabled on software profile [%s]' % (dbComponent.name, dbSoftwareProfile.name)) dbSoftwareProfile.components.append(dbComponent) def addComponentToSoftwareProfile(self, session: Session, componentId: int, softwareProfileId: int) -> None: """ Add component to softwareProfile. Raises: SoftwareProfileNotFound ComponentNotFound """ dbSoftwareProfile = self.getSoftwareProfileById( session, softwareProfileId) dbComponent = self._componentsDbHandler.getComponentById( session, componentId) self._addComponent(dbComponent, dbSoftwareProfile) def deleteComponentFromSoftwareProfile(self, session: Session, componentId: int, softwareProfileId: int) -> None: """ Delete component from softwareProfile. Raises: SoftwareProfileNotFound """ dbSoftwareProfile = self.getSoftwareProfileById( session, softwareProfileId) dbComponent = self._componentsDbHandler.getComponentById( session, componentId) compDescr = '%s-%s' % (dbComponent.name, dbComponent.version) self._logger.debug( 'Deleting component [%s] from software profile [%s]' % (compDescr, dbSoftwareProfile)) if dbComponent not in dbSoftwareProfile.components: raise SoftwareProfileComponentNotFound( 'Component [%s] not enabled on software profile [%s]' % (compDescr, dbSoftwareProfile.name)) dbSoftwareProfile.components.remove(dbComponent) self._logger.debug( 'Deleted component [%s] from software profile [%s]' % (compDescr, dbSoftwareProfile.name)) def __getPartition(self, session: Session, name: str) -> Partition: \ # pylint: disable=no-self-use return session.query(Partition).filter(Partition.name == name).one() def addPartitionToSoftwareProfile(self, session: Session, partition: PartitionObject, softwareProfileName: str) -> None: """ Add partition to software profile. """ self._logger.debug('Adding partition [%s] to software profile [%s]' % (partition.getName(), softwareProfileName)) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) try: dbPartition = self.__getPartition(session, partition.getName()) except NoResultFound: pass if dbPartition in dbSoftwareProfile.partitions: raise PartitionAlreadyExists( 'Partition [%s] already exists for software' ' profile [%s]' % (partition.getName(), softwareProfileName)) dbPartition = Partition( name=partition.getName(), device=partition.getDevice(), mountPoint=partition.getMountPoint(), fsType=partition.getFsType(), size=partition.getSize(), options=partition.getOptions(), preserve=partition.getPreserve(), bootLoader=partition.getBootLoader(), directAttachment=partition.getDirectAttachment(), indirectAttachment=partition.getIndirectAttachment(), diskSize=partition.getDiskSize()) dbSoftwareProfile.partitions.append(dbPartition) def deletePartitionFromSoftwareProfile(self, session: Session, partitionName: str, softwareProfileName: str) -> None: """ Delete partition from software profile. Raises: PartitionNotFound """ self._logger.debug( 'Deleting partition [%s] from software profile [%s]' % (partitionName, softwareProfileName)) dbSoftwareProfile = self.getSoftwareProfile(session, softwareProfileName) try: dbPartition = self.__getPartition(session, partitionName) except NoResultFound: raise PartitionNotFound( 'Partition [%s] does not exist for software profile' ' [%s]' % (partitionName, softwareProfileName)) self._logger.debug( 'Deleted partition [%s] from software profile [%s]' % (partitionName, softwareProfileName)) dbSoftwareProfile.partitions.remove(dbPartition) def get_software_profiles_with_component( self, session: Session, kit_name: str, component_name: str, *, kit_version: Optional[str] = None) -> List[SoftwareProfile]: \ # pylint: disable=no-self-use """ Return list of software profiles with component enabled. Raises: ResourceNotFound """ try: q = session.query(Component).join(Kit) if kit_version: q = q.filter( and_(Kit.name == kit_name, Kit.version == kit_version)) else: q = q.filter(Kit.name == kit_name) return q.filter( Component.name == component_name).one().softwareprofiles except NoResultFound: raise ResourceNotFound( f'Component [{component_name}] (from kit [{kit_name}]) not' ' found')