def isCompatibleComponent(self, osInfo, component): osconfig = osHelper.getOsInfo(osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) for osComponent in component.getOsInfoList(): if osComponent.getName() == osInfo.getName() and \ self._isMatchingMajorVersion(osComponent.getVersion(), osInfo.getVersion()) and \ osComponent.getArch() == osInfo.getArch(): return True for osFamilyComponent in component.getOsFamilyInfoList(): if osFamilyComponent.getName() == 'root': return True if osFamilyComponent.getName() == osconfig.getOsFamilyInfo().\ getName(): # Family matches if self._isMatchingMajorVersion( osFamilyComponent.getVersion(), osconfig.getOsFamilyInfo().getVersion()) or \ osFamilyComponent.getVersion is None: # Version matches if osFamilyComponent.getArch() == osconfig.getArch() or \ osFamilyComponent.getArch() is None: # Arch matches return True return False
def _create_kit_db_entry(self, session: Session, kit) -> Kit: """ Creates a database entry for a kit. :param kit: :return: a Kit instance (TortugaObject) """ try: return self._kit_db_api.getKit(session, kit['name'], kit['ver']) except KitNotFound: pass # Add the database entries for the kit kitObj = Kit(name=kit['name'], version=kit['ver'], iteration='0') kitObj.setDescription(kit['sum']) kitObj.setIsOs(True) kitObj.setIsRemovable(True) kit_descr = format_kit_descriptor(kit['name'], kit['ver'], kit['arch']) newComp = Component(name=kit_descr, version=kit['ver']) newComp.setDescription('%s mock component' % (kit_descr)) newComp.addOsInfo( osHelper.getOsInfo(kit['name'], kit['ver'], kit['arch'])) kitObj.addComponent(newComp) # Kit does not previously exist, perform 'normal' add kit operation self._kit_db_api.addKit(session, kitObj) return kitObj
def addOsIfNotFound(self, session, osInfo): """ Insert operating system into the db if it is not found. """ try: return self.getOsInfo( session, osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) except OsNotFound: pass # Translate osInfo tmpOsInfo = osHelper.getOsInfo(osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) # Check for existence of OS family dbOsFamily = self.addOsFamilyIfNotFound( session, tmpOsInfo.getOsFamilyInfo()) dbOs = OperatingSystems(name=osInfo.getName(), version=osInfo.getVersion(), arch=osInfo.getArch()) dbOs.family = dbOsFamily session.add(dbOs) return dbOs
def getBestMatchComponent(self, session, name, version, osInfo, kitId): """ Return best match component The query will search for an exact or family match. """ self.getLogger().debug( 'Retrieving best match component for %s-%s (%s)' % ( name, version, osInfo)) osConfig = osHelper.getOsInfo( osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) matchSpec = or_( Component.os.any( name=osInfo.getName(), version=osInfo.getVersion(), arch=osInfo.getArch()), Component.family.any( name=osConfig.getOsFamilyInfo().getName(), version=osConfig.getOsFamilyInfo().getVersion(), arch=osInfo.getArch()), Component.family.any( name=osConfig.getOsFamilyInfo().getName(), version=osConfig.getOsFamilyInfo().getVersion(), arch=None), Component.family.any(name='root') ) if version: filter_spec = and_(Component.kitId == kitId, Component.name == name, Component.version == version, matchSpec) else: filter_spec = and_(Component.kitId == kitId, Component.name == name, matchSpec) dbComponent = session.query(Component).filter(filter_spec).first() if not dbComponent: comp_label = '%s-%s' % (name, version) if version else name excmsg = 'Component %s (%s) is not found.' % (comp_label, osInfo) raise ComponentNotFound(excmsg) return dbComponent
def runCommand(self): self.parseArgs( _(""" Display list of components available for software profiles in the system. """)) softwareProfileName = self.__get_software_profile() if softwareProfileName: # Display all components enabled for software profile for c in SoftwareProfileWsApi( username=self.getUsername(), password=self.getPassword(), baseurl=self.getUrl(), verify=self._verify).getEnabledComponentList( softwareProfileName): displayComponent(c, c.getKit()) return if self.getArgs().os: try: name, version, arch = self.getArgs().os.split('-', 3) except ValueError: self.getParser().error( 'Malformed argument to --os. Must be in form of' ' NAME-VERSION-ARCH') osinfo = getOsInfo(name, version, arch) else: osinfo = None # Display all components for kit in KitWsApi(username=self.getUsername(), password=self.getPassword(), baseurl=self.getUrl(), verify=self._verify).getKitList(): for c in kit.getComponentList(): if osinfo and osinfo not in c.getOsInfoList() and \ osinfo.getOsFamilyInfo() not in c.getOsFamilyInfoList(): # Exclude those components that cannot be enabled on the # specified operating system. continue displayComponent(c, kit)
def __init__(self, logger, cmdline_options=None): self._cm = ConfigManager() self._logger = logger self._osObjectFactory = osUtility.getOsObjectFactory() self._settings = self.__load_settings(cmdline_options) self._settings['installer_software_profile'] = 'Installer' self._settings['installer_hardware_profile'] = 'Installer' self._settings['eulaAccepted'] = False self._settings['fqdn'] = getfqdn() self._settings['osInfo'] = getOsInfo() self._forceCleaning = False self._depotCreated = False fsManager = self._osObjectFactory.getOsFileSystemManager() self._lockFilePath = os.path.join( fsManager.getOsLockFilePath(), 'tortuga-setup') langdomain = 'tortuga-config' localedir = os.path.join(self._cm.getRoot(), 'share', 'locale') if not os.path.exists(localedir): # Try the system path localedir = '/usr/share/locale' gettext.bindtextdomain(langdomain, localedir) gettext.textdomain(langdomain) self.gettext = gettext.gettext self._ = self.gettext self._logger.info('Detected OS: [%s]', self._settings['osInfo'])
def createSoftwareProfile(self, swProfileSpec, settingsDict=None): """ Exceptions: ConfigurationError NetworkNotFound ComponentNotFound KitNotFound OSError """ # Parse 'settingsDict' if settingsDict: # ... bOsMediaRequired; default is True bOsMediaRequired = settingsDict['bOsMediaRequired'] \ if 'bOsMediaRequired' in settingsDict else True # ... unmanagedProfile; default is False unmanagedProfile = settingsDict['unmanagedProfile'] \ if 'unmanagedProfile' in settingsDict else False # Validate software profile name validation.validateProfileName(swProfileSpec.getName()) # Insert default description for software profile if not swProfileSpec.getDescription() or \ swProfileSpec.getDescription() == '**DEFAULT**': swProfileSpec.setDescription('%s Nodes' % (swProfileSpec.getName())) self.getLogger().debug('Creating software profile [%s]' % (swProfileSpec)) osInfo = swProfileSpec.getOsInfo() \ if swProfileSpec.getOsInfo() else self._getOsInfo(bOsMediaRequired) # If we're creating an unmanaged software profile (no # DHCP/PXE/kickstart/OS) just create it now and we're done if unmanagedProfile: self._sp_db_api.addSoftwareProfile(swProfileSpec) else: if bOsMediaRequired and swProfileSpec.getOsInfo(): try: kitApiFactory.getKitApi().getKit( swProfileSpec.getOsInfo().getName(), swProfileSpec.getOsInfo().getVersion(), '0') except KitNotFound: self._logger.error('OS kit for [%s] not found' % (swProfileSpec.getOsInfo())) raise else: swProfileSpec.setOsInfo(osInfo) # Get component manager for appropriate OS family osConfig = osHelper.getOsInfo(osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) osObjFactory = osUtility.getOsObjectFactory( osConfig.getOsFamilyInfo().getName()) compManager = osObjFactory.getComponentManager() # Need to be fancy with components spComponents = swProfileSpec.getComponents() swProfileSpec.setComponents(TortugaObjectList()) bFoundOsComponent = False bFoundCoreComponent = False components = [] # Iterate over components, adding them to the software profile for c in spComponents: cobj = compManager.getBestMatchComponent( c.getName(), c.getVersion(), osInfo, c.getKit().getId()) k = cobj.getKit() if k.getIsOs(): # This component is a member of the OS kit, set the flag bFoundOsComponent = True else: if c.getName() == 'core': # Found the 'core' component, set the flag bFoundCoreComponent = True components.append(cobj) # If the operating system is undefined for this software # profile, use the same OS as the installer. if bOsMediaRequired and not bFoundOsComponent: # Find OS component osCompName = '%s-%s-%s' % ( osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) self.getLogger().debug('Automatically adding OS component [%s]' ' (not specified in template)' % (osCompName)) try: osComponent = self._component_db_api.getComponent( osCompName, osInfo.getVersion(), osInfo, {'kit': True}) components.append(osComponent) except ComponentNotFound: # Cannot find OS component, don't freak out pass # Ensure 'core' component is enabled if not bFoundCoreComponent: # Attempt to automatically add the core component, only # if one exists for this OS try: comp = self._getCoreComponentForOsInfo(osInfo) self.getLogger().debug( 'Automatically adding [core] component' ' (not specified in template)') components.append(comp) except ComponentNotFound: pass # Initialize values for kernel, kernelParams, and initrd if not swProfileSpec.getKernel(): swProfileSpec.setKernel( osObjFactory.getOsSysManager().getKernel(osInfo)) if not swProfileSpec.getInitrd(): swProfileSpec.setInitrd( osObjFactory.getOsSysManager().getInitrd(osInfo)) # Add the software profile self._sp_db_api.addSoftwareProfile(swProfileSpec) # Enable components in one fell swoop for comp in components: self.getLogger().debug('Enabling component [%s]' % (comp.getName())) if comp.getKit().getIsOs(): # Don't use enableComponent() on OS kit self._component_db_api.\ addComponentToSoftwareProfile( comp.getId(), swProfileSpec.getId()) continue self.enableComponent(swProfileSpec.getName(), comp.getKit().getName(), comp.getKit().getVersion(), comp.getKit().getIteration(), comp.getName(), comp.getVersion()) self.getLogger().debug( 'Software profile [%s] created successfully' % (swProfileSpec.getName()))
def get_os_info(self) -> osInfo: """ :return: OsInfo """ return osHelper.getOsInfo(self.name, self.version, self.architecture)
def createSoftwareProfile(self, session: Session, swProfileSpec, settingsDict=None): """ Exceptions: ConfigurationError NetworkNotFound ComponentNotFound KitNotFound OSError """ if settingsDict == None: settingsDict = {} bOsMediaRequired = settingsDict.get('bOsMediaRequired', True) unmanagedProfile = settingsDict.get('unmanagedProfile', False) # Validate software profile name validation.validateProfileName(swProfileSpec.getName()) # Insert default description for software profile if swProfileSpec.getDescription() is None: swProfileSpec.setDescription('%s Nodes' % (swProfileSpec.getName())) self._logger.debug('Creating software profile [%s]' % (swProfileSpec)) osInfo = swProfileSpec.getOsInfo() \ if swProfileSpec.getOsInfo() else self._getOsInfo( session, bOsMediaRequired) # If we're creating an unmanaged software profile (no # DHCP/PXE/kickstart/OS) just create it now and we're done if unmanagedProfile: self._sp_db_api.addSoftwareProfile(session, swProfileSpec) else: if bOsMediaRequired and swProfileSpec.getOsInfo(): try: self._kit_db_api.getKit( session, swProfileSpec.getOsInfo().getName(), swProfileSpec.getOsInfo().getVersion(), '0') except KitNotFound: self._logger.error('OS kit for [%s] not found' % (swProfileSpec.getOsInfo())) raise else: swProfileSpec.setOsInfo(osInfo) # Get component manager for appropriate OS family osConfig = osHelper.getOsInfo(osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) osObjFactory = osUtility.getOsObjectFactory( osConfig.getOsFamilyInfo().getName()) # Need to be fancy with components spComponents = swProfileSpec.getComponents() swProfileSpec.setComponents(TortugaObjectList()) bFoundOsComponent = False bFoundCoreComponent = False components = [] # Iterate over components, adding them to the software profile for c in spComponents: cobj = self._component_db_api.getBestMatchComponent( session, c.getName(), c.getVersion(), osInfo, c.getKit().getId()) k = cobj.getKit() if k.getIsOs(): # This component is a member of the OS kit, set the flag bFoundOsComponent = True elif k.getName() == 'base' and c.getName() == 'core': # Found the 'core' component in 'base' kit bFoundCoreComponent = True components.append(cobj) # If the operating system is undefined for this software # profile, use the same OS as the installer. if bOsMediaRequired and not bFoundOsComponent: # Find OS component osCompName = '%s-%s-%s' % ( osInfo.getName(), osInfo.getVersion(), osInfo.getArch()) self._logger.debug('Automatically adding OS component [%s]' ' (not specified in template)' % (osCompName)) try: osComponent = self._component_db_api.getComponent( session, osCompName, osInfo.getVersion(), osInfo, {'kit': True}) components.append(osComponent) except ComponentNotFound: # Cannot find OS component, don't freak out pass # Ensure 'core' component is enabled if not bFoundCoreComponent: # Attempt to automatically add the core component, only # if one exists for this OS try: comp = self._getCoreComponentForOsInfo(session, osInfo) self._logger.debug('Automatically adding [core] component' ' (not specified in template)') components.append(comp) except ComponentNotFound: self._logger.warning( 'OS [{}] does not have a compatible \'core\'' ' component'.format(osInfo)) # Initialize values for kernel, kernelParams, and initrd if not swProfileSpec.getKernel(): swProfileSpec.setKernel( osObjFactory.getOsSysManager().getKernel(osInfo)) if not swProfileSpec.getInitrd(): swProfileSpec.setInitrd( osObjFactory.getOsSysManager().getInitrd(osInfo)) # Add the software profile self._sp_db_api.addSoftwareProfile(session, swProfileSpec) # Enable components in one fell swoop for comp in components: self._logger.debug('Enabling component [%s]' % (comp.getName())) if comp.getKit().getIsOs(): # Don't use enableComponent() on OS kit self._component_db_api.addComponentToSoftwareProfile( session, comp.getId(), swProfileSpec.getId()) continue self.enableComponent(session, swProfileSpec.getName(), comp.getKit().getName(), comp.getKit().getVersion(), comp.getKit().getIteration(), comp.getName(), comp.getVersion()) # # Fire the tags changed event for all creates that have tags # # Get the latest version from the db in case the create method # added some embellishments # swp = self.getSoftwareProfile(session, swProfileSpec.getName()) if swp.getTags(): SoftwareProfileTagsChanged.fire(softwareprofile_id=str( swp.getId()), softwareprofile_name=swp.getName(), tags=swp.getTags(), previous_tags={})
def __init__(self): OsObjectManager.__init__(self) self._osInfo = osHelper.getOsInfo()