class HardwareProfileDbApi(TortugaDbApi): """ HardwareProfile DB API class. """ def __init__(self): TortugaDbApi.__init__(self) self._hardwareProfilesDbHandler = HardwareProfilesDbHandler() self._nodesDbHandler = NodesDbHandler() self._globalParametersDbHandler = GlobalParametersDbHandler() self._adminsDbHandler = AdminsDbHandler() self._nicsDbHandler = NicsDbHandler() self._resourceAdaptersDbHandler = ResourceAdaptersDbHandler() self._networkDevicesDbHandler = NetworkDevicesDbHandler() self._networksDbHandler = NetworksDbHandler() def getHardwareProfile(self, name: str, optionDict: Optional[Union[dict, None]] = None): """ Get hardwareProfile from the db. Returns: hardwareProfile Throws: HardwareProfileNotFound DbError """ session = DbManager().openSession() try: dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, name) self.loadRelations(dbHardwareProfile, optionDict) self.loadRelations(dbHardwareProfile, dict(tags=True)) return HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__) except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def getHardwareProfileById(self, hardwareProfileId: int, optionDict: Optional[Union[dict, None]] = None): """ Get hardwareProfile from the db. Returns: hardwareProfile Throws: HardwareProfileNotFound DbError """ session = DbManager().openSession() try: dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfileById(session, hardwareProfileId) self.loadRelations(dbHardwareProfile, optionDict) return HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__) except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def getHardwareProfileList(self, optionDict: Optional[Union[dict, None]] = None, tags: Optional[Union[dict, None]] = None): """ Get list of all available hardwareProfiles from the db. Returns: [hardwareProfile] Throws: DbError """ session = DbManager().openSession() try: dbHardwareProfileList = self._hardwareProfilesDbHandler.\ getHardwareProfileList(session, tags=tags) hardwareProfileList = TortugaObjectList() for dbHardwareProfile in dbHardwareProfileList: # For now expand networks self.loadRelation(dbHardwareProfile, 'hardwareprofilenetworks') self.loadRelations(dbHardwareProfile, optionDict) self.loadRelations(dbHardwareProfile, dict(tags=True)) hardwareProfileList.append( HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__)) return hardwareProfileList except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def setIdleSoftwareProfile(self, hardwareProfileName, softwareProfileName=None): """ Sets the idle software profile Returns: -none- Raises: SoftwareProfileNotFound SoftwareProfileNotIdle """ session = DbManager().openSession() try: dbSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile(session, softwareProfileName) \ if softwareProfileName else None dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) self._hardwareProfilesDbHandler.setIdleSoftwareProfile( dbHardwareProfile, dbSoftwareProfile) session.commit() except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def addHardwareProfile(self, hardwareProfile, session=None): """ Insert hardwareProfile into the db. Returns: (none) Throws: HardwareProfileAlreadyExists DbError """ # Keep local 'session' instance. If 'session' is None, # ensure transaction is committed before returning to the # caller, otherwise the caller is responsible. On exceptions, # the rollback is performed regardless. _session = session if _session is None: _session = DbManager().openSession() try: try: self._hardwareProfilesDbHandler.getHardwareProfile( _session, hardwareProfile.getName()) raise HardwareProfileAlreadyExists( 'Hardware profile [%s] already exists' % (hardwareProfile)) except HardwareProfileNotFound as ex: pass dbHardwareProfile = self.__populateHardwareProfile( _session, hardwareProfile) _session.add(dbHardwareProfile) if session is None: _session.commit() self.getLogger().info('Added hardware profile [%s]' % (dbHardwareProfile.name)) except TortugaException as ex: _session.rollback() raise except Exception as ex: _session.rollback() self.getLogger().exception('%s' % ex) raise finally: if session is None: DbManager().closeSession() def deleteHardwareProfile(self, name): """ Delete hardwareProfile from the db. Returns: None Throws: HardwareProfileNotFound DbError TortugaException """ session = DbManager().openSession() try: hwProfile = self._hardwareProfilesDbHandler.getHardwareProfile( session, name) if hwProfile.nodes: raise TortugaException( 'Unable to remove hardware profile with associated' ' nodes') # First delete the mappings hwProfile.mappedsoftwareprofiles = [] self.getLogger().debug( 'Marking hardware profile [%s] for deletion' % (name)) session.delete(hwProfile) session.commit() except TortugaException as ex: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def copyHardwareProfile(self, srcHardwareProfileName, dstHardwareProfileName): session = DbManager().openSession() try: srcHardwareProfile = self.getHardwareProfile( srcHardwareProfileName, { 'admins': True, 'hardwareprofilenetworks': True, 'nics': True, 'resourceadapter': True, }) dstHardwareProfile = \ self.getHardwareProfile(srcHardwareProfileName) dstHardwareProfile.setName(dstHardwareProfileName) newDescription = 'Copy of %s' % ( dstHardwareProfile.getDescription()) dstHardwareProfile.setDescription(newDescription) dstHardwareProfile.setNetworks(srcHardwareProfile.getNetworks()) dstHardwareProfile.setProvisioningNics( srcHardwareProfile.getProvisioningNics()) dstHardwareProfile.setResourceAdapter( srcHardwareProfile.getResourceAdapter()) self.addHardwareProfile(dstHardwareProfile, session) session.commit() finally: DbManager().closeSession() def getNode(self, name): """ Get node from the db. Returns: node Throws: NodeNotFound DbError """ session = DbManager().openSession() try: return self._nodesDbHandler.getNode(session, name) except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def deleteNode(self, name): """ Delete node from the db. Returns: None Throws: NodeNotFound DbError """ session = DbManager().openSession() try: self._nodesDbHandler.deleteNode(session, name) session.commit() except TortugaException as ex: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def addAdmin(self, hardwareProfileName, adminUsername): """ Add an admin to this hardware profile Raises: AdminAlreadyExists """ session = DbManager().openSession() try: dbAdmin = self._adminsDbHandler.getAdmin(session, adminUsername) dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) if dbAdmin in dbHardwareProfile.admins: raise AdminAlreadyExists( 'The admin %s is already associated with %s.' % (adminUsername, hardwareProfileName)) dbHardwareProfile.admins.append(dbAdmin) session.commit() except TortugaException as ex: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def deleteAdmin(self, hardwareProfileName, adminUsername): """ Delete an admin from a hardware profile Raises: AdminNotFound """ session = DbManager().openSession() try: dbAdmin = self._adminsDbHandler.getAdmin(session, adminUsername) dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) if dbAdmin not in dbHardwareProfile.admins: raise AdminNotFound('Admin user [%s] not associated with %s.' % (adminUsername, hardwareProfileName)) dbHardwareProfile.admins.remove(dbAdmin) session.commit() except TortugaException as ex: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def updateHardwareProfile(self, hardwareProfileObject): """ Update Hardware Profile Object """ session = DbManager().openSession() try: dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfileById(session, hardwareProfileObject.getId()) self.__populateHardwareProfile(session, hardwareProfileObject, dbHardwareProfile) session.commit() except TortugaException as ex: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def __getInstallerNode(self, session): return self._nodesDbHandler.getNode(session, ConfigManager().getInstaller()) def __get_provisioning_nics(self, session): return self.__getInstallerNode(session).nics def __get_all_networks(self, session): return self._networksDbHandler.getNetworkList(session) def __get_network_devices(self, session): \ # pylint: disable=no-self-use return session.query(NetworkDevices).all() def __populateHardwareProfile(self, session, hardwareProfile, dbHardwareProfile=None): """ Helper function for creating / updating HardwareProfiles. If 'dbHardwareProfile' is specified, this is an update (vs. add) operation Raises: NicNotFound """ # Preload provisioning nics and networks prov_nics = self.__get_provisioning_nics(session) all_networks = self.__get_all_networks(session) networkdevices = self.__get_network_devices(session) # Validate hw profile if hardwareProfile.getName() is None: raise ConfigurationError('Hardware profile requires name.') if hardwareProfile.getNameFormat() is None: raise ConfigurationError( 'Hardware profile requires name format field.') # Handle the special case of a hardware profile not having an # associated idle software profile (ie. Installer hardware # profile) idleSoftwareProfileId = hardwareProfile.getIdleSoftwareProfileId() \ if hardwareProfile.getIdleSoftwareProfileId else None if dbHardwareProfile is None: dbHardwareProfile = HardwareProfiles() dbHardwareProfile.name = hardwareProfile.getName() dbHardwareProfile.description = hardwareProfile.getDescription() dbHardwareProfile.nameFormat = hardwareProfile.getNameFormat() if hardwareProfile.getInstallType() is None: if hardwareProfile.getLocation() == 'remote': dbHardwareProfile.installType = 'bootstrap' else: raise ConfigurationError( 'Hardware profile must have valid install type.') else: dbHardwareProfile.installType = hardwareProfile.\ getInstallType() if hardwareProfile.getLocation() != 'remote': dbHardwareProfile.kernel = hardwareProfile.getKernel() dbHardwareProfile.kernelParams = hardwareProfile.\ getKernelParams() dbHardwareProfile.initrd = hardwareProfile.getInitrd() dbHardwareProfile.localBootParams = hardwareProfile.\ getLocalBootParams() dbHardwareProfile.softwareOverrideAllowed = hardwareProfile.\ getSoftwareOverrideAllowed() dbHardwareProfile.idleSoftwareProfileId = idleSoftwareProfileId dbHardwareProfile.location = hardwareProfile.getLocation() dbHardwareProfile.hypervisorSoftwareProfileId = hardwareProfile.\ getHypervisorSoftwareProfileId() dbHardwareProfile.maxUnits = hardwareProfile.getMaxUnits() dbHardwareProfile.bcastEnabled = hardwareProfile.getBcastEnabled() dbHardwareProfile.mcastEnabled = hardwareProfile.getMcastEnabled() dbHardwareProfile.cost = hardwareProfile.getCost() # Add resource adapter resourceAdapter = hardwareProfile.getResourceAdapter() if resourceAdapter: dbHardwareProfile.resourceAdapter = self.\ _resourceAdaptersDbHandler.getResourceAdapter( session, resourceAdapter.getName()) dbHardwareProfile.resourceAdapterId = dbHardwareProfile.\ resourceAdapter.id # Add networks networks = [] for network in hardwareProfile.getNetworks(): for prov_network in all_networks: if prov_network.address == network.getAddress(): dbNetwork = prov_network break else: raise NetworkNotFound('Network [%s] does not exist' % (network.getAddress())) # Lookup network device for network_device in networkdevices: if network.getNetworkDevice().getName() == network_device.name: dbNetworkDevice = network_device break else: dbNetworkDevice = NetworkDevices() dbNetworkDevice.name = network.getNetworkDevice().getName() # Now check if we have this one already... for dbHardwareProfileNetwork in dbHardwareProfile.\ hardwareprofilenetworks: if dbHardwareProfileNetwork.\ networkDeviceId == dbNetworkDevice.id and \ dbHardwareProfileNetwork.networkId == dbNetwork.id: break else: dbHardwareProfileNetwork = HardwareProfileNetworks() if dbNetwork.id is not None: dbHardwareProfileNetwork.networkId = dbNetwork.id else: dbHardwareProfileNetwork.network = dbNetwork dbHardwareProfileNetwork.hardwareProfileId = \ dbHardwareProfile.id if dbNetworkDevice.id is not None: dbHardwareProfileNetwork.networkDeviceId = \ dbNetworkDevice.id else: dbHardwareProfileNetwork.networkdevice = dbNetworkDevice dbHardwareProfile.hardwareprofilenetworks.append( dbHardwareProfileNetwork) networks.append(dbHardwareProfileNetwork) # Now remove all old networks for dbNetwork in dbHardwareProfile.hardwareprofilenetworks: for network in networks: if network.networkDeviceId == dbNetwork.networkDeviceId \ and network.networkId == dbNetwork.networkId: # Its a keeper break else: # No match...delete time session.delete(dbNetwork) # Add provisioning Nics if hardwareProfile.getProvisioningNics(): # Only one provisioning nic is possible nic = hardwareProfile.getProvisioningNics()[0] for prov_nic in prov_nics: if nic.getIp() == prov_nic.ip: dbNic = prov_nic break else: raise NicNotFound('Provisioning NIC with IP [%s] not found' % (nic.getIp())) if dbNic not in dbHardwareProfile.nics: dbHardwareProfile.nics.append(dbNic) return dbHardwareProfile def getHypervisorNodes(self, hardwareProfileName): """ Get list of nodes that belong to hypervisorSoftwareProfileId assigned to the given hardware profile name. """ session = DbManager().openSession() try: dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) if not dbHardwareProfile.hypervisor: return TortugaObjectList() return self.getTortugaObjectList( Node, dbHardwareProfile.hypervisor.nodes) except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def setProvisioningNic(self, hardwareProfileName, nicId): session = DbManager().openSession() try: dbNic = self._nicsDbHandler.getNicById(session, nicId) dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) dbHardwareProfile.nics.append(dbNic) session.commit() except sqlalchemy.exc.IntegrityError as ex: # Entry for this hwProfile/nicId already exists, ignore self.getLogger().debug( 'setProvisioningNic(): entry already exists for' ' hwProfile=%s, nicId=%d' % (hardwareProfileName, nicId)) except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession() def getProvisioningNicForNetwork(self, network, netmask): """ Raises: NicNotFound """ session = DbManager().openSession() try: installer_node = self.__getInstallerNode(session) nics = [ dbNic for dbNic in installer_node.hardwareprofile.nics if dbNic.network.address == network and dbNic.network.netmask == netmask ] if not nics: raise NicNotFound( 'Unable to find provisioning NIC for network [%s]' ' netmask [%s]' % (network, netmask)) return tortuga.objects.nic.Nic.getFromDbDict(nics[0].__dict__) except TortugaException as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession()
class HardwareProfileDbApi(TagsDbApiMixin, TortugaDbApi): """ HardwareProfile DB API class. """ tag_model = HardwareProfileTag def __init__(self): super().__init__() self._hardwareProfilesDbHandler = HardwareProfilesDbHandler() self._nodesDbHandler = NodesDbHandler() self._globalParametersDbHandler = GlobalParametersDbHandler() self._adminsDbHandler = AdminsDbHandler() self._nicsDbHandler = NicsDbHandler() self._resourceAdaptersDbHandler = ResourceAdaptersDbHandler() self._networkDevicesDbHandler = NetworkDevicesDbHandler() self._networksDbHandler = NetworksDbHandler() def getHardwareProfile(self, session: Session, name: str, optionDict: Optional[OptionDict] = None) \ -> HardwareProfile: """ Get hardwareProfile from the db. Returns: hardwareProfile Throws: HardwareProfileNotFound DbError """ try: dbHardwareProfile = \ self._hardwareProfilesDbHandler.getHardwareProfile( session, name) self.loadRelations(dbHardwareProfile, get_default_relations(optionDict)) return HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__) except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise def getHardwareProfileById( self, session: Session, hardwareProfileId: int, optionDict: Optional[OptionDict] = None) -> HardwareProfile: """ Get hardwareProfile from the db. Returns: hardwareProfile Throws: HardwareProfileNotFound DbError """ try: dbHardwareProfile = \ self._hardwareProfilesDbHandler.getHardwareProfileById( session, hardwareProfileId) self.loadRelations(dbHardwareProfile, get_default_relations(optionDict)) return HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__) except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise def getHardwareProfileList( self, session: Session, optionDict: Optional[OptionDict] = None, tags: Optional[Tags] = None) -> TortugaObjectList: """ Get list of all available hardwareProfiles from the db. Returns: [hardwareProfile] Throws: DbError """ try: dbHardwareProfileList = \ self._hardwareProfilesDbHandler.getHardwareProfileList( session, tags=tags) hardwareProfileList = TortugaObjectList() for dbHardwareProfile in dbHardwareProfileList: options = dict.copy(optionDict or {}) options['hardwareprofilenetworks'] = True self.loadRelations(dbHardwareProfile, get_default_relations(options)) hardwareProfileList.append( HardwareProfile.getFromDbDict(dbHardwareProfile.__dict__)) return hardwareProfileList except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise def setIdleSoftwareProfile( self, session: Session, hardwareProfileName: str, softwareProfileName: Optional[str] = None) -> None: """ Sets the idle software profile Returns: -none- Raises: SoftwareProfileNotFound SoftwareProfileNotIdle """ try: dbSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile(session, softwareProfileName) \ if softwareProfileName else None dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) self._hardwareProfilesDbHandler.setIdleSoftwareProfile( dbHardwareProfile, dbSoftwareProfile) session.commit() except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise def addHardwareProfile(self, session: Session, hardwareProfile: HardwareProfile) -> None: """ Insert hardwareProfile into the db. Returns: (none) Throws: HardwareProfileAlreadyExists DbError """ try: try: self._hardwareProfilesDbHandler.getHardwareProfile( session, hardwareProfile.getName()) raise HardwareProfileAlreadyExists( 'Hardware profile [%s] already exists' % (hardwareProfile)) except HardwareProfileNotFound as ex: pass dbHardwareProfile = self.__populateHardwareProfile( session, hardwareProfile) session.add(dbHardwareProfile) session.flush() self._set_tags(dbHardwareProfile, hardwareProfile.getTags()) session.commit() self.getLogger().info('Added hardware profile [%s]' % (dbHardwareProfile.name)) except TortugaException: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise def deleteHardwareProfile(self, session: Session, name: str) -> None: """ Delete hardwareProfile from the db. Returns: None Throws: HardwareProfileNotFound DbError TortugaException """ try: hwProfile = self._hardwareProfilesDbHandler.getHardwareProfile( session, name) if hwProfile.nodes: raise TortugaException( 'Unable to remove hardware profile with associated' ' nodes') # First delete the mappings hwProfile.mappedsoftwareprofiles = [] self.getLogger().debug( 'Marking hardware profile [%s] for deletion' % (name)) session.delete(hwProfile) session.commit() except TortugaException: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise def copyHardwareProfile(self, session: Session, srcHardwareProfileName: str, dstHardwareProfileName: str): srcHardwareProfile = self.getHardwareProfile( session, srcHardwareProfileName, { 'admins': True, 'hardwareprofilenetworks': True, 'nics': True, 'resourceadapter': True, }) dstHardwareProfile = \ self.getHardwareProfile(session, srcHardwareProfileName) dstHardwareProfile.setName(dstHardwareProfileName) newDescription = 'Copy of %s' % (dstHardwareProfile.getDescription()) dstHardwareProfile.setDescription(newDescription) dstHardwareProfile.setNetworks(srcHardwareProfile.getNetworks()) dstHardwareProfile.setProvisioningNics( srcHardwareProfile.getProvisioningNics()) dstHardwareProfile.setResourceAdapter( srcHardwareProfile.getResourceAdapter()) dstHardwareProfile.setTags(srcHardwareProfile.getTags()) self.addHardwareProfile(session, dstHardwareProfile) def addAdmin(self, session: Session, hardwareProfileName, adminUsername: str) -> None: """ Add an admin to this hardware profile Raises: AdminAlreadyExists """ try: dbAdmin = self._adminsDbHandler.getAdmin(session, adminUsername) dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) if dbAdmin in dbHardwareProfile.admins: raise AdminAlreadyExists( 'The admin %s is already associated with %s.' % (adminUsername, hardwareProfileName)) dbHardwareProfile.admins.append(dbAdmin) session.commit() except TortugaException: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise def deleteAdmin(self, session: Session, hardwareProfileName: str, adminUsername: str) -> None: """ Delete an admin from a hardware profile Raises: AdminNotFound """ try: dbAdmin = self._adminsDbHandler.getAdmin(session, adminUsername) dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) if dbAdmin not in dbHardwareProfile.admins: raise AdminNotFound('Admin user [%s] not associated with %s.' % (adminUsername, hardwareProfileName)) dbHardwareProfile.admins.remove(dbAdmin) session.commit() except TortugaException: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise def updateHardwareProfile(self, session: Session, hardwareProfileObject: HardwareProfile) -> None: """ Update Hardware Profile Object """ try: dbHardwareProfile = \ self._hardwareProfilesDbHandler.getHardwareProfileById( session, hardwareProfileObject.getId()) self.__populateHardwareProfile(session, hardwareProfileObject, dbHardwareProfile) self._set_tags(dbHardwareProfile, hardwareProfileObject.getTags()) session.commit() except TortugaException: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise def __getInstallerNode(self, session: Session) -> Node: return self._nodesDbHandler.getNode(session, ConfigManager().getInstaller()) def __get_provisioning_nics(self, session: Session) -> List[Nic]: return self.__getInstallerNode(session).nics def __get_all_networks(self, session: Session) -> List[Network]: return self._networksDbHandler.getNetworkList(session) def __populateHardwareProfile( self, session: Session, hardwareProfile: HardwareProfile, dbHardwareProfile: Optional[HardwareProfileModel] = None) \ -> HardwareProfileModel: """ Helper function for creating / updating hardware profiles. If 'dbHardwareProfile' is specified, this is an update (vs. add) operation Raises: NicNotFound ResourceAdapterNotFound InvalidArgument ConfigurationError """ # Preload provisioning nics and networks prov_nics = self.__get_provisioning_nics(session) all_networks = self.__get_all_networks(session) # Validate hw profile if hardwareProfile.getName() is None: raise ConfigurationError('Hardware profile requires name.') if hardwareProfile.getNameFormat() is None: raise ConfigurationError( 'Hardware profile requires name format field.') # Handle the special case of a hardware profile not having an # associated idle software profile (ie. Installer hardware # profile) idleSoftwareProfileId = hardwareProfile.getIdleSoftwareProfileId() \ if hardwareProfile.getIdleSoftwareProfileId else None if dbHardwareProfile is None: dbHardwareProfile = HardwareProfileModel() dbHardwareProfile.name = hardwareProfile.getName() dbHardwareProfile.description = hardwareProfile.getDescription() dbHardwareProfile.nameFormat = hardwareProfile.getNameFormat() if hardwareProfile.getInstallType() is None: if hardwareProfile.getLocation() == 'remote': dbHardwareProfile.installType = 'bootstrap' else: dbHardwareProfile.installType = 'package' else: dbHardwareProfile.installType = hardwareProfile.\ getInstallType() if hardwareProfile.getLocation() != 'remote': dbHardwareProfile.kernel = hardwareProfile.getKernel() dbHardwareProfile.kernelParams = \ hardwareProfile.getKernelParams() dbHardwareProfile.initrd = hardwareProfile.getInitrd() dbHardwareProfile.localBootParams = \ hardwareProfile.getLocalBootParams() dbHardwareProfile.softwareOverrideAllowed = hardwareProfile.\ getSoftwareOverrideAllowed() dbHardwareProfile.idleSoftwareProfileId = idleSoftwareProfileId dbHardwareProfile.location = hardwareProfile.getLocation() dbHardwareProfile.cost = hardwareProfile.getCost() # Add resource adapter resource_adapter_name = \ hardwareProfile.getResourceAdapter().getName() \ if hardwareProfile.getResourceAdapter() else 'default' dbHardwareProfile.resourceadapter = \ self._resourceAdaptersDbHandler.getResourceAdapter( session, resource_adapter_name) if hardwareProfile.getDefaultResourceAdapterConfig(): adapter_cfg = None self.getLogger().debug( 'Setting default resource adapter config: {}'.format( hardwareProfile.getDefaultResourceAdapterConfig())) for adapter_cfg in \ dbHardwareProfile.resourceadapter.resource_adapter_config: if adapter_cfg.name == \ hardwareProfile.getDefaultResourceAdapterConfig(): break else: raise InvalidArgument( 'Resource adapter configuration profile [{}] is' ' invalid'.format( hardwareProfile.getDefaultResourceAdapterConfig())) dbHardwareProfile.default_resource_adapter_config = adapter_cfg else: dbHardwareProfile.default_resource_adapter_config = None # Add networks networks = [] for network in hardwareProfile.getNetworks(): for prov_network in all_networks: if prov_network.address == network.getAddress(): dbNetwork = prov_network break else: raise NetworkNotFound('Network [%s] does not exist' % (network.getAddress())) dbNetworkDevice = \ self._networkDevicesDbHandler.createNetworkDeviceIfNotExists( session, network.getNetworkDevice().getName()) # Now check if we have this one already... for dbHardwareProfileNetwork in \ dbHardwareProfile.hardwareprofilenetworks: if dbHardwareProfileNetwork.networkDeviceId == \ dbNetworkDevice.id and \ dbHardwareProfileNetwork.networkId == dbNetwork.id: break else: dbHardwareProfileNetwork = HardwareProfileNetwork() dbHardwareProfileNetwork.hardwareprofile = dbHardwareProfile if dbNetwork.id is not None: dbHardwareProfileNetwork.networkId = dbNetwork.id else: dbHardwareProfileNetwork.network = dbNetwork dbHardwareProfileNetwork.hardwareProfileId = \ dbHardwareProfile.id if dbNetworkDevice.id is not None: dbHardwareProfileNetwork.networkDeviceId = \ dbNetworkDevice.id else: dbHardwareProfileNetwork.networkdevice = dbNetworkDevice dbHardwareProfile.hardwareprofilenetworks.append( dbHardwareProfileNetwork) networks.append(dbHardwareProfileNetwork) # Now remove all old networks for dbNetwork in dbHardwareProfile.hardwareprofilenetworks: for network in networks: if network.networkDeviceId == dbNetwork.networkDeviceId \ and network.networkId == dbNetwork.networkId: # Its a keeper break else: # No match...delete time session.delete(dbNetwork) # Add provisioning Nics if hardwareProfile.getProvisioningNics(): # Only one provisioning nic is possible nic = hardwareProfile.getProvisioningNics()[0] for prov_nic in prov_nics: if nic.getIp() == prov_nic.ip: dbNic = prov_nic break else: raise NicNotFound('Provisioning NIC with IP [%s] not found' % nic.getIp()) if dbNic not in dbHardwareProfile.nics: dbHardwareProfile.nics.append(dbNic) return dbHardwareProfile def setProvisioningNic(self, session: Session, hardwareProfileName: str, nicId: int) -> None: try: dbNic = self._nicsDbHandler.getNicById(session, nicId) dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) dbHardwareProfile.nics.append(dbNic) session.commit() except sqlalchemy.exc.IntegrityError as ex: # Entry for this hwProfile/nicId already exists, ignore self.getLogger().debug( 'setProvisioningNic(): entry already exists for' ' hwProfile=%s, nicId=%d' % (hardwareProfileName, nicId)) except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise def getProvisioningNicForNetwork(self, session: Session, network: str, netmask: str) -> Nic: """ Raises: NicNotFound """ try: installer_node = self.__getInstallerNode(session) nics = [ dbNic for dbNic in installer_node.hardwareprofile.nics if dbNic.network.address == network and dbNic.network.netmask == netmask ] if not nics: raise NicNotFound( 'Unable to find provisioning NIC for network [%s]' ' netmask [%s]' % (network, netmask)) return tortuga.objects.nic.Nic.getFromDbDict(nics[0].__dict__) except TortugaException as exc: self.getLogger().exception('%s' % exc) raise