Beispiel #1
0
 def __init__(self, effects):
     self.lines = ['#!/bin/bash', 'set -ex']
     TypeDispatcher.__init__(
         self, {
             Run: self.perform_run,
             Sudo: perform_sudo,
             Put: perform_put,
             Comment: self.perform_comment,
             Sequence: perform_sequence
         })
     perform(self, effects)
     # Add blank line to terminate script with a newline
     self.lines.append('')
     self._script = '\n'.join(self.lines)
Beispiel #2
0
def perform_tenant_scope(authenticator,
                         log,
                         service_configs,
                         throttler,
                         dispatcher,
                         tenant_scope,
                         box,
                         _concretize=concretize_service_request):
    """
    Perform a :obj:`TenantScope` by performing its :attr:`TenantScope.effect`,
    with a dispatcher extended with a performer for :obj:`ServiceRequest`
    intents. The performer will use the tenant provided by the
    :obj:`TenantScope`.

    The first arguments before (dispatcher, tenant_scope, box) are intended
    to be partially applied, and the result is a performer that can be put into
    a dispatcher.
    """
    @sync_performer
    def scoped_performer(dispatcher, service_request):
        return _concretize(authenticator, log, service_configs, throttler,
                           tenant_scope.tenant_id, service_request)

    new_disp = ComposedDispatcher(
        [TypeDispatcher({ServiceRequest: scoped_performer}), dispatcher])
    perform(new_disp, tenant_scope.effect.on(box.succeed, box.fail))
Beispiel #3
0
 def __init__(self, effects):
     self.lines = [
         '#!/bin/bash',
         'set -ex'
     ]
     TypeDispatcher.__init__(self, {
         Run: self.perform_run,
         Sudo: perform_sudo,
         Put: perform_put,
         Comment: self.perform_comment,
         Sequence: perform_sequence
     })
     perform(self, effects)
     # Add blank line to terminate script with a newline
     self.lines.append('')
     self._script = '\n'.join(self.lines)
Beispiel #4
0
def get_msg_time_dispatcher(reactor):
    """
    Return dispatcher with performer of MsgWithTime in it
    """
    return TypeDispatcher({
        MsgWithTime: partial(perform_msg_time, reactor)
    })
Beispiel #5
0
def perform_run_remotely(base_dispatcher, intent):
    """
    Run a series of commands on a remote host.
    """

    dispatcher = ComposedDispatcher([
        TypeDispatcher({
            Run: perform_run,
            Sudo: perform_sudo,
            Put: perform_put,
            Comment: perform_comment,
        }),
        base_dispatcher,
    ])

    host_string = "%s@%s" % (intent.username, intent.address)
    with settings(
            connection_attempts=24,
            timeout=5,
            pty=False,
            host_string=host_string):

        sync_perform(dispatcher, intent.commands)

    disconnect_all()
Beispiel #6
0
    def perform_retry_without_delay(actual_retry_intent):
        should_retry = actual_retry_intent.should_retry
        if isinstance(should_retry, ShouldDelayAndRetry):

            def should_retry(exc_info):
                exc_type, exc_value, exc_traceback = exc_info
                failure = Failure(exc_value, exc_type, exc_traceback)
                return Effect(
                    Constant(
                        actual_retry_intent.should_retry.can_retry(failure)))

        new_retry_effect = Effect(
            Retry(effect=actual_retry_intent.effect,
                  should_retry=should_retry))

        _dispatchers = [
            TypeDispatcher({Retry: perform_retry}), base_dispatcher
        ]
        if fallback_dispatcher is not None:
            _dispatchers.append(fallback_dispatcher)

        seq = [(expected_retry_intent.effect.intent, performer)
               for performer in performers]

        return perform_sequence(seq, new_retry_effect,
                                ComposedDispatcher(_dispatchers))
Beispiel #7
0
def get_ssh_dispatcher(connection, context):
    """
    :param Message context: The eliot message context to log.
    :param connection: The SSH connection run commands on.
    """

    @deferred_performer
    def perform_run(dispatcher, intent):
        context.bind(
            message_type="flocker.provision.ssh:run",
            command=intent.log_command_filter(intent.command),
        ).write()
        endpoint = SSHCommandClientEndpoint.existingConnection(
            connection, intent.command)
        d = Deferred()
        connectProtocol(endpoint, CommandProtocol(
            deferred=d, context=context))
        return d

    return TypeDispatcher({
        Run: perform_run,
        Sudo: perform_sudo,
        Put: perform_put,
        Comment: perform_comment,
    })
Beispiel #8
0
    def get_dispatcher(self):
        """
        Get an :module:`effect` dispatcher for interacting with this
        :class:`FakeAWS`.
        """
        return TypeDispatcher({
            # Share implementation with real implementation
            DownloadS3KeyRecursively:
            perform_download_s3_key_recursively,
            UploadToS3Recursively:
            perform_upload_s3_key_recursively,

            # Fake implementation
            UpdateS3RoutingRule:
            self._perform_update_s3_routing_rule,
            UpdateS3ErrorPage:
            self._perform_update_s3_error_page,
            ListS3Keys:
            self._perform_list_s3_keys,
            DeleteS3Keys:
            self._perform_delete_s3_keys,
            CopyS3Keys:
            self._perform_copy_s3_keys,
            DownloadS3Key:
            self._perform_download_s3_key,
            UploadToS3:
            self._perform_upload_s3_key,
            CreateCloudFrontInvalidation:
            self._perform_create_cloudfront_invalidation,
        })
Beispiel #9
0
 def dispatcher(self):
     return ComposedDispatcher([
         TypeDispatcher({
             PackerConfigure: self.perform_packer_configure,
             PackerBuild: self.perform_packer_build,
             WriteToS3: self.perform_write_to_s3,
         }), base_dispatcher
     ])
Beispiel #10
0
def make_dispatcher(reactor):
    return ComposedDispatcher([
        TypeDispatcher({
            RunRemotely: partial(perform_run_remotely, reactor),
        }),
        make_twisted_dispatcher(reactor),
        base_dispatcher,
    ])
Beispiel #11
0
 def dispatcher(self):
     return ComposedDispatcher([
         TypeDispatcher({
             PackerConfigure: self.perform_packer_configure,
             PackerBuild: self.perform_packer_build,
             StandardOut: self.perform_standard_out,
         }), base_dispatcher
     ])
Beispiel #12
0
def test_dispatcher(disp=None):
    disps = [
        base_dispatcher,
        TypeDispatcher({ParallelEffects: perform_parallel_async}),
    ]
    if disp is not None:
        disps.append(disp)
    return ComposedDispatcher(disps)
Beispiel #13
0
def make_asyncio_dispatcher(loop=None):
    """
    Create a dispatcher that knows how to perform certain built-in Intents
    with asyncio-specific implementations.
    """
    return TypeDispatcher({
        ParallelEffects: perform_parallel_async,
        Delay: perform_delay,
    })
Beispiel #14
0
def make_dispatcher(reactor):
    patch_twisted_7672()
    return ComposedDispatcher([
        TypeDispatcher({
            RunRemotely: perform_run_remotely,
        }),
        make_twisted_dispatcher(reactor),
        base_dispatcher,
    ])
Beispiel #15
0
def get_eviction_dispatcher(supervisor):
    """
    Get a dispatcher with :class:`EvictServerFromScalingGroup`'s performer.

    :param supervisor: a :class:`otter.supervisor.ISupervisor` provider
    """
    return TypeDispatcher({
        EvictServerFromScalingGroup:
        partial(perform_evict_server, supervisor)
    })
Beispiel #16
0
 def test_delete(self):
     model = ZKCrudModel()
     eff = Effect(DeleteNode(path='/foo', version=1))
     model.create('/foo', 'initial', makepath=True)
     model.set('/foo', 'bar')
     performer = partial(perform_delete_node, model)
     dispatcher = TypeDispatcher({DeleteNode: performer})
     result = sync_perform(dispatcher, eff)
     self.assertEqual(model.nodes, {})
     self.assertEqual(result, 'delete return value')
Beispiel #17
0
def make_twisted_dispatcher(reactor):
    """
    Create a dispatcher that knows how to perform certain built-in Intents
    with Twisted-specific implementations.
    """
    return TypeDispatcher({
        ParallelEffects:
        perform_parallel_async,
        Delay:
        deferred_performer(partial(perform_delay, reactor)),
    })
Beispiel #18
0
def get_log_dispatcher(log, fields):
    """
    Get dispatcher containing performers for logging intents that
    use given logger and are bound with given fields
    """
    return TypeDispatcher({
        BoundFields: partial(perform_logging, log, fields, bound_log),
        Log: partial(perform_logging, log, fields, log_msg),
        LogErr: partial(perform_logging, log, fields, log_err),
        GetFields: sync_performer(lambda d, i: fields),
    })
def dispatcher():
    prefetch_dispatcher = TypeDispatcher({
        TryPrefetch:
        try_prefetch_performer,
        CalculateSha256Sum:
        sync_performer(lambda _, intent: calculate_sha256_sum(intent)),
        GetListRemote:
        get_list_remote_performer,
        AbortWithErrorMessage:
        abort_with_error_message_performer,
    })
    return ComposedDispatcher([base_dispatcher, prefetch_dispatcher])
Beispiel #20
0
    def check_and_call():
        class DoFunc(object):
            pass

        @deferred_performer
        def func_performer(d, i):
            return maybeDeferred(func, *args, **kwargs)

        comp_dispatcher = ComposedDispatcher(
            [TypeDispatcher({DoFunc: func_performer}), dispatcher])
        return perform(comp_dispatcher,
                       call_if_acquired(lock, Effect(DoFunc())))
Beispiel #21
0
def get_cloud_client_dispatcher(reactor, authenticator, log, service_configs):
    """
    Get a dispatcher suitable for running :obj:`ServiceRequest` and
    :obj:`TenantScope` intents.
    """
    # this throttler could be parameterized but for now it's basically a hack
    # that we want to keep private to this module
    throttler = partial(_default_throttler, WeakLocks(), reactor)
    return TypeDispatcher({
        TenantScope: partial(perform_tenant_scope, authenticator, log,
                             service_configs, throttler),
        _Throttle: _perform_throttle,
    })
Beispiel #22
0
    def get_dispatcher(self):
        """
        Get an :module:`effect` dispatcher for interacting with this
        :class:`FakeYum`.
        """
        return TypeDispatcher({
            # Share implementation with real implementation
            DownloadPackagesFromRepository:
            perform_download_packages_from_repository,

            # Fake implementation
            CreateRepo: self._perform_create_repository,
        })
Beispiel #23
0
    def get_dispatcher(self, service_request_mappings):
        """
        Set up an empty dictionary of intents to fake responses, and set up
        the dispatcher.
        """
        eq_dispatcher = EQDispatcher
        if callable(service_request_mappings[0][-1]):
            eq_dispatcher = EQFDispatcher

        return ComposedDispatcher([
            TypeDispatcher({ParallelEffects: perform_parallel_async}),
            eq_dispatcher(service_request_mappings)
        ])
Beispiel #24
0
    def test_perform_throttle(self):
        """
        The bracket given to :obj:`_Throttle` is used to call the nested
        performer.
        """
        def bracket(f, *args, **kwargs):
            return f(*args, **kwargs).addCallback(lambda r: ('bracketed', r))

        throttle = _Throttle(bracket=bracket, effect=Effect(Constant('foo')))
        dispatcher = ComposedDispatcher(
            [TypeDispatcher({_Throttle: _perform_throttle}), base_dispatcher])
        result = sync_perform(dispatcher, Effect(throttle))
        self.assertEqual(result, ('bracketed', 'foo'))
Beispiel #25
0
def get_zk_dispatcher(kz_client):
    """Get a dispatcher that can support all of the ZooKeeper intents."""
    return TypeDispatcher({
        CreateOrSet:
        partial(perform_create_or_set, kz_client),
        DeleteNode:
        partial(perform_delete_node, kz_client),
        GetChildrenWithStats:
        partial(perform_get_children_with_stats, kz_client),
        GetChildren:
        partial(perform_get_children, kz_client),
        GetStat:
        partial(perform_get_stat, kz_client)
    })
def get_dispatcher(reactor):
    """
    Create a dispatcher that can find performers for :obj:`ReadLine`,
    :obj:`HTTPRequest`, and :obj:`ParallelEffects`.
    :func:`make_twisted_dispatcher` is able to provide the ``ParallelEffects``
    performer, so we compose it with our own custom :obj:`TypeDispatcher`.
    """
    return ComposedDispatcher([
        TypeDispatcher({
            ReadLine: perform_readline_stdin,
            HTTPRequest: perform_request_with_treq,
        }),
        make_twisted_dispatcher(reactor),
    ])
Beispiel #27
0
    def test_print(self):
        outputs = []
        @sync_performer
        def perform_print(dispatcher, print_):
            outputs.append(print_.line)

        test_interpreter = ComposedDispatcher([
            TypeDispatcher({
                Print: perform_print,
                }),
            base_dispatcher])

        dispatcher = test_interpreter
        sync_perform(dispatcher, program())
        self.assertEqual(["What... is your quest?"], outputs)
def get_dispatcher():
    """
    Create a dispatcher that can find performers for :obj:`ReadLine`,
    :obj:`HTTPRequest`, and :obj:`ParallelEffects`.  There's a built-in
    performer for ParallelEffects that uses a multiprocessing ThreadPool,
    :func:`effect.perform_parallel_with_pool`.
    """
    my_pool = ThreadPool()
    pool_performer = partial(perform_parallel_with_pool, my_pool)
    return ComposedDispatcher([
        TypeDispatcher({
            ReadLine: perform_readline_stdin,
            HTTPRequest: perform_request_requests,
            ParallelEffects: pool_performer,
        })
    ])
Beispiel #29
0
    def test_perform_update_servers_cache(self):
        """
        Performing :obj:`UpdateServersCache` updates using
        CassScalingGroupServersCache
        """
        dt = datetime(1970, 1, 1)
        eff = Effect(UpdateServersCache('tid', 'gid', dt, [{'id': 'a'}]))

        @sync_performer
        def perform_update_tuple(disp, intent):
            self.assertEqual(intent, ('cacheistidgid', dt, [{'id': 'a'}]))

        disp = ComposedDispatcher([
            TypeDispatcher({tuple: perform_update_tuple}),
            self.get_dispatcher(self.get_store())
        ])
        self.assertIsNone(sync_perform(disp, eff))
Beispiel #30
0
    def test_event_added(self):
        """
        Event is added to cloud feed
        """
        class AddEvent(object):
            pass

        add_event_performer = deferred_performer(
            lambda d, i: succeed('performed'))

        cf = self.make_cf(add_event=lambda *a: Effect(AddEvent()),
                          get_disp=lambda *a: TypeDispatcher(
                              {AddEvent: add_event_performer}))
        d = cf({'event': 'dict', 'cloud_feed': True, 'message': ('m', )})

        self.assertEqual(self.successResultOf(d), 'performed')
        self.assertFalse(self.log.err.called)
Beispiel #31
0
 def get_dispatcher(self):
     """
     Get an :module:`effect` dispatcher for interacting with this
     :class:`FaleAWS`.
     """
     return TypeDispatcher({
         UpdateS3RoutingRule:
         self._perform_update_s3_routing_rule,
         ListS3Keys:
         self._perform_list_s3_keys,
         DeleteS3Keys:
         self._perform_delete_s3_keys,
         CopyS3Keys:
         self._perform_copy_s3_keys,
         CreateCloudFrontInvalidation:
         self._perform_create_cloudfront_invalidation,
     })