Example #1
0
    def settings(self) -> Dict[str, BaseSetting]:
        """
        Returns all possible settings for this resource adapter.

        :return dict: a dict of all settings, where the keys are the
                      name of the setting, and the value is an instance of
                      totrgua.objects.resource_adapter_settings.BaseSetting

        """
        from tortuga.resourceAdapter.resourceAdapterFactory import \
            get_resourceadapter_class
        try:
            ra_class = get_resourceadapter_class(self.name)
        except ResourceNotFound:
            return {}
        return ra_class.settings
    def validate(self, resadapter_name: str, name: str):
        try:
            #
            # Do a lookup to make sure the configuration profile does in
            # fact exist
            #
            ResourceAdapterConfigurationApi().get(cherrypy.request.db,
                                                  resadapter_name, name)

            #
            # Get the resource adapter class
            #
            ra_class: Type[ResourceAdapter] = get_resourceadapter_class(
                resadapter_name)
            ra = ra_class()
            ra.session = cherrypy.request.db

            #
            # Validate the configuration
            #
            ra.validate_config(name)

            response = {}

        except (ResourceAdapterNotFound, ResourceNotFound) as exc:
            self.handleException(exc)
            response = self.notFoundErrorResponse(str(exc))

        except ConfigurationValidationError as exc:
            response = {}
            for k, v in exc.errors.items():
                response[k] = str(v)

        except TortugaException as exc:
            self.handleException(exc)
            response = self.errorResponse(str(exc),
                                          code=self.getTortugaStatusCode(exc))

        except Exception:
            self._logger.exception('validate() failed')
            response = self.errorResponse(
                'Internal server error',
                http_status=http.client.INTERNAL_SERVER_ERROR)

        return self.formatResponse(response)
Example #3
0
    def addHosts(self, session: Session, addHostRequest: dict) -> None:
        """
        Raises:
            HardwareProfileNotFound
            ResourceAdapterNotFound
        """

        self.getLogger().debug('addHosts()')

        dbHardwareProfile = \
            HardwareProfilesDbHandler().getHardwareProfile(
                session, addHostRequest['hardwareProfile'])

        if not dbHardwareProfile.resourceadapter:
            errmsg = ('Resource adapter not defined for hardware'
                      ' profile [%s]' % (dbHardwareProfile.name))

            self.getLogger().error(errmsg)

            raise ResourceAdapterNotFound(errmsg)

        softwareProfileName = addHostRequest['softwareProfile'] \
            if 'softwareProfile' in addHostRequest else None

        dbSoftwareProfile = \
            SoftwareProfilesDbHandler().getSoftwareProfile(
                session, softwareProfileName) \
            if softwareProfileName else None

        ResourceAdapterClass = \
            resourceAdapterFactory.get_resourceadapter_class(
                dbHardwareProfile.resourceadapter.name)

        resourceAdapter = ResourceAdapterClass(
            addHostSession=addHostRequest['addHostSession'])

        resourceAdapter.session = session

        # Call the start() method of the resource adapter
        newNodes = resourceAdapter.start(addHostRequest,
                                         session,
                                         dbHardwareProfile,
                                         dbSoftwareProfile=dbSoftwareProfile)

        session.add_all(newNodes)
        session.flush()

        if 'tags' in addHostRequest and addHostRequest['tags']:
            for node in newNodes:
                self._set_tags(node, addHostRequest['tags'])

        # Commit new node(s) to database
        session.commit()

        # Only perform post-add operations if we actually added a node
        if newNodes:
            if dbSoftwareProfile and not dbSoftwareProfile.isIdle:
                self.getLogger().info(
                    'Node(s) added to software profile [%s] and'
                    ' hardware profile [%s]' %
                    (dbSoftwareProfile.name if dbSoftwareProfile else 'None',
                     dbHardwareProfile.name))

                newNodeNames = [tmpNode.name for tmpNode in newNodes]

                resourceAdapter.hookAction('add', newNodeNames)

                self.postAddHost(session, dbHardwareProfile.name,
                                 softwareProfileName,
                                 addHostRequest['addHostSession'])

                resourceAdapter.hookAction('start', newNodeNames)

        self.getLogger().debug('Add host workflow complete')
Example #4
0
    def addHosts(self, session: Session, addHostRequest: dict) -> None:
        """
        Raises:
            HardwareProfileNotFound
            ResourceAdapterNotFound
        """

        self._logger.debug('addHosts()')

        #
        # Check if a node with the same name already exists. If it does,
        # then we should not go any further, as you cannot re-register
        # a node. Instead, throw an exception so that callers can deal with
        # the problem in an appropriate fashion.
        #
        node_details = addHostRequest.get('nodeDetails', [])
        for node_detail in node_details:
            node_name = node_detail.get('name', '').strip()
            if node_name:
                try:
                    self._nodeDbApi.getNode(session, node_name)
                    raise Exception(
                        "Node already exists: {}".format(node_name))
                except NodeNotFound:
                    pass

        dbHardwareProfile = \
            HardwareProfilesDbHandler().getHardwareProfile(
                session, addHostRequest['hardwareProfile'])

        if not dbHardwareProfile.resourceadapter:
            errmsg = ('Resource adapter not defined for hardware'
                      ' profile [%s]' % (dbHardwareProfile.name))

            self._logger.error(errmsg)

            raise ResourceAdapterNotFound(errmsg)

        softwareProfileName = addHostRequest['softwareProfile'] \
            if 'softwareProfile' in addHostRequest else None

        dbSoftwareProfile = \
            SoftwareProfilesDbHandler().getSoftwareProfile(
                session, softwareProfileName) \
            if softwareProfileName else None

        ResourceAdapterClass = \
            resourceAdapterFactory.get_resourceadapter_class(
                dbHardwareProfile.resourceadapter.name)

        resourceAdapter = ResourceAdapterClass(
            addHostSession=addHostRequest['addHostSession'])

        resourceAdapter.session = session

        # Call the start() method of the resource adapter
        newNodes = resourceAdapter.start(addHostRequest,
                                         session,
                                         dbHardwareProfile,
                                         dbSoftwareProfile=dbSoftwareProfile)

        session.add_all(newNodes)
        session.flush()

        if 'tags' in addHostRequest and addHostRequest['tags']:
            for node in newNodes:
                self._set_tags(node, addHostRequest['tags'], merge=True)

        # Commit new node(s) to database
        session.commit()

        # Only perform post-add operations if we actually added a node
        if newNodes:
            self._logger.info(
                'Node(s) added to software profile [%s] and'
                ' hardware profile [%s]',
                dbSoftwareProfile.name if dbSoftwareProfile else 'None',
                dbHardwareProfile.name,
            )

            newNodeNames = [tmpNode.name for tmpNode in newNodes]

            resourceAdapter.hookAction('add', newNodeNames)

            self.postAddHost(session, dbHardwareProfile.name,
                             softwareProfileName,
                             addHostRequest['addHostSession'])

            resourceAdapter.hookAction('start', newNodeNames)

        self._logger.debug('Add host workflow complete')