def parseImageNameList(self, param):
        """
        Parse a string to get a list of image names.  If the string is a JSON
        list of strings or a JSON string (with quotes), it is processed as
        JSON.  Otherwise, the input value is treated as a single image name.

        :param param: a parameter with an image name, a JSON image name, or a
            JSON list of image names.
        :returns: a list of image names.
        """
        nameList = param
        if isinstance(param, six.binary_type):
            param = param.decode('utf8')
        if isinstance(param, six.string_types):
            try:
                nameList = json.loads(param)
            except ValueError:
                pass
        if isinstance(nameList, six.string_types):
            nameList = [nameList]
        if not isinstance(nameList, list):
            raise RestException(
                'A valid string or a list of strings is required.')
        for img in nameList:
            if not isinstance(img, six.string_types):
                raise RestException('%r is not a valid string.' % img)
            if ':' not in img and '@' not in img:
                raise RestException('Image %s does not have a tag or digest' %
                                    img)
        return nameList
Example #2
0
    def setImages(self, params):
        """Validates the new images to be added (if they exist or not) and then
        attempts to collect xml data to be cached. a job is then called to
        update the girder collection containing the cached docker image data
        """
        self.requireParams(('name', ), params)
        name = params['name']

        name = json.loads(name)
        docker_image_model = ModelImporter.model('docker_image_model',
                                                 'slicer_cli_web')

        if isinstance(name, list):
            for img in name:

                if not isinstance(img, six.string_types):
                    raise RestException('%s was not a valid string.' % img)
                if ':' not in img or '@' not in img:
                    raise RestException('Image %s does not have a tag or '
                                        'digest' % img)

        elif isinstance(name, six.string_types):
            name = [name]
        else:
            raise RestException('a valid string or a list of '
                                'strings was not passed in')
        docker_image_model.putDockerImage(name, self.jobType, True)
Example #3
0
    def deleteImage(self, params):

        self.requireParams(('name', ), params)
        name = params['name']
        if 'delete_from_local_repo' in params:
            deleteImage = params['delete_from_local_repo']
            if deleteImage is 'True':
                deleteImage = True
        else:
            deleteImage = False

        name = json.loads(name)
        nameList = []
        if isinstance(name, list):
            for img in name:

                if not isinstance(img, six.string_types):
                    raise RestException('%s was not a valid string.' % img)
            else:
                nameList = name
        elif isinstance(name, six.string_types):

            nameList = [name]

        else:

            raise RestException('name was not a valid JSON list or string.')

        self._deleteImage(nameList, deleteImage)
 def setImages(self, params):
     """
     Validates the new images to be added (if they exist or not) and then
     attempts to collect xml data to be cached. a job is then called to
     update the girder collection containing the cached docker image data
     """
     self.requireParams(('name', ), params)
     nameList = self.parseImageNameList(params['name'])
     folder = params.get('folder', PluginSettings.get_task_folder())
     if not folder:
         raise RestException('no upload folder given or defined by default')
     return self._createPutImageJob(nameList, folder)
Example #5
0
    def _deleteImage(self, names, deleteImage):
        """
        Removes the docker images and there respective clis endpoints

        :param names: The name of the docker image (user/rep:tag)
        :type name: list
        :param deleteImage: Boolean indicating whether to delete the docker
            image from the local machine.(if True this is equivalent to
            docker rmi -f <image> )
        :type name: boolean
        """
        removed = DockerImageItem.removeImages(names, self.getCurrentUser())
        if removed != names:
            rest = [name for name in names if name not in removed]
            raise RestException('Some docker images could not be removed. %s' % (rest))
        self.deleteImageEndpoints(removed)

        try:
            if deleteImage:
                self._deleteDockerImages(removed)
        except DockerImageNotFoundError as err:
            raise RestException('Invalid docker image name. ' + str(err))
Example #6
0
    def _deleteImage(self, names, deleteImage):
        """
        Removes the docker images and there respective clis endpoints

        :param name: The name of the docker image (user/rep:tag)
        :type name: string
        :param deleteImage: Boolean indicating whether to delete the docker
            image from the local machine.(if True this is equivalent to
            docker rmi -f <image> )
        """

        dockermodel = ModelImporter.model('docker_image_model',
                                          'slicer_cli_web_ssr')
        try:
            dockermodel.removeImages(names)

            self.deleteImageEndpoints(names)
            if deleteImage:
                dockermodel.delete_docker_image_from_repo(names, self.jobType)
        except DockerImageNotFoundError as err:
            raise RestException('Invalid docker image name. ' + str(err))