Beispiel #1
0
 def __init__(self, info_type, result_type):
     self.task_manager = get_task_manager()
     self.info_type = info_type
     self.result_type = result_type
     self.task_id = get_task_id()
     self.info = info_type()
     self._initialize_common_info()
Beispiel #2
0
 def setUpClass(cls):
     cls.task_manager = get_task_manager()
     msg_factory = MessageFactory(cls.msgs)
     cls.description = LocalizableMessage(id='msg1',
                                          default_message='task1',
                                          args=[])
     cls.progress_msg = LocalizableMessage(id='prog1',
                                           default_message='progress',
                                           args=[])
Beispiel #3
0
    def _invoke_task_operation(self, ctx, method_id, method, method_args):
        """
        Invokes the task operations by adding them in thread pool queue.

        :type  ctx: :class:`vmware.vapi.core.ExecutionContext`
        :param ctx: Execution context for this method
        :type  method_id: :class:`str`
        :param method_id: Method id
        :type  method: :class:`str`
        :param method: Method name
        :type  method_args: :class:`args`
        :param method_args: Method input parameters

        :rtype: :class:`tuple` of :class:`str` or
                :class:`vmware.vapi.core.MethodResult` and :class:`bool`
        :return: Tuple of task id and boolean value or Method Result
        """

        from vmware.vapi.lib.thread_pool import get_threadpool
        from vmware.vapi.task.task_manager_impl import get_task_manager
        task_manager = get_task_manager()
        thread_pool = get_threadpool()
        service_id = method_id.get_interface_identifier().get_name()
        operation_id = method_id.get_name()
        task_id = task_manager.create_task(
                                str(uuid.uuid4()),
                                None,
                                service_id,
                                operation_id,
                                False)
        insert_task_id(ctx.application_context, task_id)
        event = threading.Event()
        accept_timeout = 30
        if is_task_operation(method_id.get_name()):
            thread_pool.queue_work(self._invoke_method, ctx, method,
                                   method_args, event)
            event_set = event.wait(accept_timeout)

            # In case of provider not accepting task remove task from task
            # manager.
            if not event_set:
                task_manager.remove_task(task_id)
            return task_id, event_set
        else:
            # If $task is not present in method id for a task operation
            # even then we will still use a separate thread in threadpool
            # and wait for its completion to return the result that's
            # to ensure provider implementation can be same irrespective
            # of client calling for task or non-task version of operation
            result = thread_pool.queue_work_and_wait(self._invoke_method,
                                                     ctx,
                                                     method,
                                                     method_args,
                                                     event)
            event.wait(accept_timeout)
            return result[1], None
Beispiel #4
0
 def setUpClass(cls):
     cls.task_manager = get_task_manager()
     msg_factory = MessageFactory(cls.msgs)
     cls.description = LocalizableMessage(id='msg1',
                                          default_message='task1',
                                          args=[])
     cls.progress_msg = LocalizableMessage(id='prog1',
                                           default_message='progress',
                                           args=[])
     cls.task_id = cls.task_manager.create_task(str(uuid.uuid4()),
                                                cls.description,
                                                'test.service', 'op1', True)
     app_ctx = create_default_application_context()
     app_ctx[TASK_ID] = cls.task_id
     sec_ctx = SecurityContext()
     TLS.ctx = ExecutionContext(app_ctx, sec_ctx)
     cls.task_handle = get_task_handle(Info, StringType())
Beispiel #5
0
def create_servers(spec, singleton=False):
    """
    Create RPC servers

    :type  spec: :class:`str`
    :param spec: Provider configuration
    :type  singleton: :class:`bool`
    :kwarg singleton: Specify whether to create new instances of Providers or
                      use existing ones
    :rtype: :class:`list` of
        :class:`vmware.vapi.server.server_interface.ServerInterface`
    :return: list of servers
    """
    server_providers = {
        'twisted': 'vmware.vapi.server.twisted_server.get_server',
        'stdio': 'vmware.vapi.server.stdio_server.get_server',
        'wsgi': 'vmware.vapi.server.wsgi_server.get_server',
    }

    provider_cfg, providers = _create_provider_int(spec, singleton)
    provider = providers[0]

    try:
        protocol_configs = provider_cfg.get_provider_protocols()
    except configparser.NoOptionError:
        raise Exception('No protocol configurations specified')

    # These import cannot be at the top as we need to wait for logger to get
    # initialized with right handlers
    from vmware.vapi.lib.load import dynamic_import
    from vmware.vapi.protocol.server.msg.handler_factory import (
        ProtocolHandlerFactory)
    from vmware.vapi.protocol.server.transport.async_server_adapter_factory \
        import AsyncServerAdapterFactory
    servers = {}
    msg_handlers = {}
    protocol_handlers = {}
    is_wsgi = False
    for protocol_config in protocol_configs:
        url = provider_cfg.get_provider_url(protocol_config)
        logger.info('url: %s', url)

        transport_protocol = get_url_scheme(url)
        logger.info('transport protocol: %s', transport_protocol)

        # Get request handler
        msg_handler_factory = ProtocolHandlerFactory()

        # Add all pre processors
        # TODO Add these as default values in provider config instead of
        # hardcoding here
        pre_processors = ['vmware.vapi.security.sso.JSONSSOVerifier']

        msg_protocol = provider_cfg.get_provider_message_format(
            protocol_config)
        msg_handler = msg_handler_factory.get_handler(msg_protocol, provider,
                                                      pre_processors)
        msg_handlers[(msg_protocol, provider)] = msg_handler

        # Get server constructor
        rpc_server = provider_cfg.get_provider_server(protocol_config)
        logger.info('rpc server: %s', rpc_server)

        # Get async server to msg handler adapter
        adapter_factory = AsyncServerAdapterFactory()
        if rpc_server in ['wsgi', 'stdio']:
            protocol_handler = msg_handler
        else:
            protocol_handler = adapter_factory.get_adapter(
                transport_protocol, msg_handler)

        # Extract ssl arguments
        ssl_args = provider_cfg.get_provider_ssl_args(protocol_config)

        # Register handler
        server = servers.get(rpc_server)
        if not server:
            server_name = server_providers.get(rpc_server)
            if server_name is not None:
                server_constructor = dynamic_import(server_name)
            if server_constructor is None:
                raise Exception('Could not find RPC constructor')

            server = server_constructor()
            if rpc_server == 'wsgi':
                server.provider_config = provider_cfg
            servers[rpc_server] = server
        server.register_handler(url, msg_protocol, protocol_handler, ssl_args)

        # Get tasks configuration
        if provider_cfg.are_tasks_enabled():
            thread_count = provider_cfg.get_provider_task_threads()
            from vmware.vapi.lib.thread_pool import get_threadpool
            thread_pool = get_threadpool(thread_count)

            task_manager = provider_cfg.get_provider_task_manager()
            task_manager_constructor = dynamic_import(task_manager)
            from vmware.vapi.task.task_manager_impl import get_task_manager
            task_manager_instance = get_task_manager(task_manager_constructor)

            priv_validator = provider_cfg.get_provider_privilege_validator()
            if priv_validator is not None:
                priv_validator_constructor = dynamic_import(priv_validator)
                from vmware.vapi.security.privilege import (
                    get_privilege_validator)
                privilege_validator_instance = get_privilege_validator(
                    priv_validator_constructor)

    return list(servers.values())