Exemplo n.º 1
0
    def getKit(self,
               session: Session,
               name: str,
               version: Optional[Union[str, None]] = None,
               iteration: Optional[Union[str, None]] = None) -> Kits:
        """
        Get kit from the db.

        Raises:
            KitNotFound
        """
        self.getLogger().debug('Retrieving kit [%s]' % (name))

        try:
            if version is not None and version is not None and \
                    iteration is not None:
                dbKitQuery = session.query(Kits).filter(
                    and_(Kits.name == name, Kits.version == version,
                         Kits.iteration == iteration))
            elif version is not None:
                dbKitQuery = session.query(Kits).filter(
                    and_(Kits.name == name, Kits.version == version))
            else:
                dbKitQuery = session.query(Kits).filter(Kits.name == name)

            return dbKitQuery.one()
        except NoResultFound:
            raise KitNotFound('Kit [%s] not found' % (name))
        except MultipleResultsFound:
            raise KitNotFound(
                'Ambiguous kit specification. Specify version and/or'
                ' iteration')
Exemplo n.º 2
0
    def _get_kit_and_component_version(self,
                                       session: Session,
                                       kit_name,
                                       kit_version,
                                       kit_iteration,
                                       comp_name,
                                       comp_version=None):
        """
        Gets a Kit instance and component version.

        :param kit_name:      the name of the kit
        :param kit_version:   the version of the kit
        :param kit_iteration: the iteration of the kit
        :param comp_name:     the component name
        :param comp_version:  the component version (optional)

        :return: a tuple, consisting of (Kit, component_version)

        """
        kit = None
        if kit_name is None:
            kit = self._get_kit_by_component(session,
                                             comp_name,
                                             comp_version=comp_version)
            #
            # Get component version if required
            #
            if comp_version is None:
                for component in kit.getComponentList():
                    if component.getName() == comp_name:
                        comp_version = component.getVersion()
                        break
        elif kit_version is None or kit_iteration is None:
            kits_found = 0
            for k in self._kit_db_api.getKitList(session):
                if k.getName() == kit_name and \
                        (kit_version is None or
                         k.getVersion() == kit_version) and \
                        (kit_iteration is None or
                         k.getIteration() == kit_iteration):
                    kit = k
                    kits_found += 1

            if kits_found > 1:
                if kit_version is not None:
                    raise KitNotFound('Multiple kits found: {}-{}'.format(
                        kit_name, kit_version))
                else:
                    raise KitNotFound(
                        'Multiple kits found {}'.format(kit_name))
        else:
            kit = self._kit_db_api.getKit(session, kit_name, kit_version,
                                          kit_iteration)

        if kit is None:
            raise KitNotFound('Kit [%s] not found' %
                              (Kit(kit_name, kit_version, kit_iteration)))

        return kit, comp_version
Exemplo n.º 3
0
    def _get_kit_by_component(self, comp_name, comp_version=None):
        """
        Gets a kit by compoent name/version.
        :param comp_name:    the name of the component
        :param comp_version: the version of the component

        :raises KitNotFound:
        :raises ComponentNotFound:

        """
        kit_list = self._kit_db_api.getKitList()
        kits = [
            kit for kit in kit_list for component in kit.getComponentList()
            if component.getName() == comp_name and (
                comp_version is None or component.getVersion() == comp_version)
        ]
        if not kits:
            raise KitNotFound('Kit containing component [%s] not found' %
                              (comp_name))

        if len(kits) > 1:
            raise ComponentNotFound(
                'Kit name must be specified, multiple kits contain '
                'component: {}'.format(comp_name))

        return kits[0]
Exemplo n.º 4
0
    def _getCoreComponentForOsInfo(self, session: Session, osInfo):
        # Find core component

        baseKit = None

        for baseKit in self._kit_db_api.getKitList(session):
            if not baseKit.getName() == self.BASE_KIT_NAME:
                continue

            break
        else:
            raise KitNotFound('Kit [%s] not found.' % (self.BASE_KIT_NAME))

        baseComp = None

        for baseComp in baseKit.getComponentList():
            if baseComp.getName() != 'core':
                continue

            break
        else:
            raise ComponentNotFound('Component [%s] not found in kit [%s]' %
                                    ('core', baseKit.getName()))

        comp = self._component_db_api.getBestMatchComponent(
            session, baseComp.getName(), baseComp.getVersion(), osInfo,
            baseKit.getId())

        comp.setKit(baseKit)

        return comp
Exemplo n.º 5
0
    def _getOsInfo(self, session: Session, bOsMediaRequired: bool):
        if not bOsMediaRequired:
            # As a placeholder, use the same OS as the installer

            # Find installer node entry
            node = self._node_db_api.getNode(session,
                                             ConfigManager().getInstaller(),
                                             {'softwareprofile': True})

            return node.getSoftwareProfile().getOsInfo()

        # Use available operating system kit; raise exception if
        # multiple available

        os_kits = self._kit_db_api.getKitList(session, os_kits_only=True)
        if not os_kits:
            raise KitNotFound('No operating system kit installed')

        if len(os_kits) > 1:
            raise KitNotFound(
                'Multiple OS kits defined; use --os option to specify'
                ' operating system')

        kit = self._kit_db_api.getKit(session, os_kits[0].getName(),
                                      os_kits[0].getVersion(), '0')

        components = kit.getComponentList()

        if not components:
            raise ComponentNotFound('Malformed operating system kit [%s]' %
                                    (os_kits))

        osinfo_list = components[0].getOsInfoList()
        if len(osinfo_list) > 1:
            raise ComponentNotFound(
                'Multiple operating system components for kit [%s];'
                ' use --os argument to specify operating system' %
                (os_kits[0]))

        return osinfo_list[0]
Exemplo n.º 6
0
    def getKitById(self, session, kit_id):
        """
        Get kit from the db using its id.
        """

        self.getLogger().debug('Retrieving kit id [%s]' % (kit_id))

        dbKit = session.query(Kits).get(kit_id)

        if not dbKit:
            raise KitNotFound('Kit ID [%s] not found.' % (kit_id))

        return dbKit
Exemplo n.º 7
0
    def getKit(self,
               session: Session,
               name: str,
               version: Optional[str] = None,
               iteration: Optional[str] = None) -> Kit:
        """
        Get kit from the db.

        Raises:
            KitNotFound
        """

        try:
            if version is not None and version is not None and \
                    iteration is not None:
                dbKitQuery = session.query(Kit).filter(
                    and_(Kit.name == name, Kit.version == version,
                         Kit.iteration == iteration))
            elif version is not None:
                dbKitQuery = session.query(Kit).filter(
                    and_(Kit.name == name, Kit.version == version))
            else:
                dbKitQuery = session.query(Kit).filter(Kit.name == name)

            return dbKitQuery.one()
        except NoResultFound:
            kit_spec_str = name

            if version:
                kit_spec_str += '-{}'.format(version)

            if iteration:
                kit_spec_str += '-{}'.format(iteration)

            raise KitNotFound('Kit [{}] not found'.format(kit_spec_str))
        except MultipleResultsFound:
            raise KitNotFound(
                'Ambiguous kit specification. Specify version and/or'
                ' iteration')
Exemplo n.º 8
0
def get_kit_installer(kit_spec):
    """
    Gets a kit installer from the registry.

    :param kit_spec:     a kit spec tuple ('name', 'version', 'iteration')
    :return:             a kit installer instance
    :raises KitNotfound:

    """
    try:
        return KIT_INSTALLER_REGISTRY[kit_spec]
    except KeyError:
        raise KitNotFound()
Exemplo n.º 9
0
def get_kit_installer(kit_spec: Tuple[str, str, str]):
    """
    Gets a kit installer from the registry.

    :param kit_spec:     a kit spec tuple ('name', 'version', 'iteration')
    :return:             a kit installer instance
    :raises KitNotfound:

    """
    kit = KIT_INSTALLER_REGISTRY.get(kit_spec)

    if kit is None:
        raise KitNotFound('Kit [%s] not found' % (Kit(*kit_spec)))

    return kit
Exemplo n.º 10
0
    def getKit(self,
               name: str,
               version: Optional[str] = None,
               iteration: Optional[str] = None) -> Kit:
        """
        Get kit info.

        Raises:
            KitNotFound
        """

        url = 'kits/?name={}'.format(urllib.parse.quote_plus(name))

        if version is not None:
            url += '&version={}'.format(urllib.parse.quote_plus(version))

        if iteration is not None:
            url += '&iteration={}'.format(urllib.parse.quote_plus(iteration))

        try:
            responseDict = self.get(url)

            # response is a list, so reference first item in list
            kits = responseDict.get('kits')
            if not kits:
                kit_spec_str = name

                if version:
                    kit_spec_str += '-{}'.format(version)

                if iteration:
                    kit_spec_str += '-{}'.format(iteration)

                raise KitNotFound(
                    'Kit matching specification [{}] not found'.format(
                        kit_spec_str))

            return Kit.getFromDict(kits[0])
        except TortugaException:
            raise
        except Exception as ex:
            raise TortugaException(exception=ex)
Exemplo n.º 11
0
def getKitNameVersionIteration(kitpath):
    """
    Extract kit name/version/iteration from a kit tarball by parsing
    the enclosed 'kit.json' metadata file.

    :raises KitNotFound:

    """
    cmd = 'tar jxfO {} */kit.json'.format(kitpath)
    p = subprocess.Popen(cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         bufsize=1)

    name = None
    version = None
    iteration = None
    try:
        meta_dict = json.load(p.stdout)
        errors = KitMetadataSchema().validate(meta_dict)
        if not errors:
            name = meta_dict['name']
            version = meta_dict['version']
            iteration = meta_dict['iteration']
    except SyntaxError:
        #
        # This is probably an indication that the kit.json did not exist
        # in the specified tarball.
        #
        pass

    retval = p.wait()
    if retval != 0:
        raise KitNotFound(
            'Unable to parse metadata for kit [{}]'.format(kitpath))

    return name, version, iteration
Exemplo n.º 12
0
    def _getCoreComponentForOsInfo(self, osInfo):
        # Find core component

        # Find the version of the 'core' component
        import tortuga.kit.kitApi
        _kitApi = tortuga.kit.kitApi.KitApi()

        baseKit = None

        for baseKit in _kitApi.getKitList():
            if not baseKit.getName() == self.BASE_KIT_NAME:
                continue

            break
        else:
            raise KitNotFound('Kit [%s] not found.' % (self.BASE_KIT_NAME))

        baseComp = None

        for baseComp in baseKit.getComponentList():
            if baseComp.getName() != 'core':
                continue

            break
        else:
            raise ComponentNotFound('Component [%s] not found in kit [%s]' %
                                    ('core', baseKit.getName()))

        comp = osUtility.getOsObjectFactory().getComponentManager().\
            getBestMatchComponent(
                baseComp.getName(), baseComp.getVersion(), osInfo,
                baseKit.getId())

        comp.setKit(baseKit)

        return comp