def _get_package_file(package_path=None, file_path=None):
    if package_path:
        if not zipfile.is_zipfile(package_path):
            raise exceptions.QinlingClientException(
                'Package %s is not a valid ZIP file.' % package_path
            )

        if os.path.getsize(package_path) > MAX_ZIP_SIZE:
            raise exceptions.QinlingClientException(
                'Package file size must be no more than %sM.' %
                (MAX_ZIP_SIZE / 1024 / 1024)
            )

        return package_path

    elif file_path:
        if not os.path.isfile(file_path):
            raise exceptions.QinlingClientException(
                'File %s not exist.' % file_path
            )

        base_name, extension = os.path.splitext(file_path)
        base_name = os.path.basename(base_name)
        zip_file = os.path.join(
            tempfile.gettempdir(),
            '%s.zip' % base_name
        )

        zf = zipfile.ZipFile(zip_file, mode='w')
        try:
            # Use default compression mode, may change in future.
            zf.write(
                file_path,
                '%s%s' % (base_name, extension),
                compress_type=zipfile.ZIP_STORED
            )
        finally:
            zf.close()

        if os.path.getsize(zip_file) > MAX_ZIP_SIZE:
            raise exceptions.QinlingClientException(
                'Package file size must be no more than %sM.' %
                (MAX_ZIP_SIZE / 1024 / 1024)
            )

        return zip_file
def worker_count(value):
    try:
        value = int(value)
        if value <= 0:
            raise ValueError
    except ValueError:
        raise exceptions.QinlingClientException(
            'Worker count must be a positive integer.'
        )
    return value
    def take_action(self, parsed_args):
        client = self.app.client_manager.function_engine
        success_msg = "Request to scale down function %s has been accepted."
        error_msg = "Unable to scale down the specified function."

        try:
            client.functions.scaledown(parsed_args.function, parsed_args.count)
            print(success_msg % parsed_args.function)
        except Exception as e:
            print(e)
            raise exceptions.QinlingClientException(error_msg)
Beispiel #4
0
    def take_action(self, parsed_args):
        client = self.app.client_manager.function_engine

        success_msg = "Request to detach function %s(version %s) has been " \
                      "accepted."
        error_msg = "Unable to detach the specified function version."

        try:
            client.function_versions.detach(parsed_args.function_id,
                                            parsed_args.version_number)
            print(success_msg %
                  (parsed_args.function_id, parsed_args.version_number))
        except Exception as e:
            print(e)
            raise exceptions.QinlingClientException(error_msg)
Beispiel #5
0
    def delete_resources(self, ids):
        """Delete one or more resources."""
        failure_flag = False
        success_msg = "Request to delete %s %s has been accepted."
        error_msg = "Unable to delete the specified %s(s)."

        for id in ids:
            try:
                self.delete(id)
                print(success_msg % (self.resource, id))
            except Exception as e:
                failure_flag = True
                print(e)

        if failure_flag:
            raise exceptions.QinlingClientException(error_msg % self.resource)
    def take_action(self, parsed_args):
        client = self.app.client_manager.function_engine
        code_type = None

        if (parsed_args.file or parsed_args.package):
            code_type = 'package'
        elif (parsed_args.container or parsed_args.object):
            code_type = 'swift'
        elif parsed_args.image:
            code_type = 'image'

        runtime = parsed_args.runtime
        if runtime and not uuidutils.is_uuid_like(runtime):
            # Try to find the runtime id with name
            runtime = q_utils.find_resource_id_by_name(
                client.runtimes, runtime)

        if code_type == 'package':
            if not runtime:
                raise exceptions.QinlingClientException(
                    'Runtime needs to be specified for package type function.'
                )

            zip_file = _get_package_file(parsed_args.package, parsed_args.file)
            md5sum = q_utils.md5(file=zip_file)
            code = {"source": "package", "md5sum": md5sum}

            with open(zip_file, 'rb') as package:
                function = client.functions.create(
                    name=parsed_args.name,
                    runtime=runtime,
                    code=code,
                    package=package,
                    entry=parsed_args.entry,
                    cpu=parsed_args.cpu,
                    memory_size=parsed_args.memory_size
                )

            # Delete zip file the client created
            if parsed_args.file and not parsed_args.package:
                os.remove(zip_file)

        elif code_type == 'swift':
            if not (parsed_args.container and parsed_args.object):
                raise exceptions.QinlingClientException(
                    'Container name and object name need to be specified.'
                )
            if not runtime:
                raise exceptions.QinlingClientException(
                    'Runtime needs to be specified for package type function.'
                )

            code = {
                "source": "swift",
                "swift": {
                    "container": parsed_args.container,
                    "object": parsed_args.object
                }
            }

            function = client.functions.create(
                name=parsed_args.name,
                runtime=runtime,
                code=code,
                entry=parsed_args.entry,
                cpu=parsed_args.cpu,
                memory_size=parsed_args.memory_size
            )

        elif code_type == 'image':
            code = {
                "source": "image",
                "image": parsed_args.image
            }

            function = client.functions.create(
                name=parsed_args.name,
                code=code,
                entry=parsed_args.entry,
                cpu=parsed_args.cpu,
                memory_size=parsed_args.memory_size
            )

        return self.columns, utils.get_item_properties(function, self.columns)