Beispiel #1
0
class MockingTestAction(Action):
    request_schema = fields.Dictionary({
        'min': fields.Integer(),
        'max': fields.Integer(),
        'kwargs': fields.SchemalessDictionary(key_type=fields.UnicodeString()),
    })

    response_schema = fields.Dictionary({
        'random': fields.Integer(),
        'response': fields.SchemalessDictionary(),
        'extra': fields.UnicodeString(),
    })

    def run(self, request):
        try:
            # noinspection PyUnresolvedReferences
            return {
                'random': random.randint(request.body['min'], request.body['max']),
                'response': function_which_shall_be_mocked(
                    request.body['max'],
                    request.body['min'],
                    **request.body['kwargs']
                ),
                'extra': function_which_shall_be_mocked.extra.value().for_me,
            }
        except AttributeError:
            raise ActionError(errors=[Error('ATTRIBUTE_ERROR', 'An attribute error was raised')])
        except BytesWarning:
            raise ActionError(errors=[Error('BYTES_WARNING', 'A bytes warning was raised')])
        except ExpectedException:
            raise ActionError(errors=[Error('EXPECTED_EXCEPTION', 'An expected exception was raised')])
Beispiel #2
0
class StubClientTransportSchema(BasicClassSchema):
    contents = {
        'path':
        fields.UnicodeString(
            description=
            'The path to the stub client transport, in the format `module.name:ClassName`',
        ),
        'kwargs':
        fields.Dictionary(
            {
                'action_map':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(
                        description='The name of the action to stub', ),
                    value_type=fields.Dictionary(
                        {
                            'body':
                            fields.SchemalessDictionary(
                                description=
                                'The body with which the action should respond',
                            ),
                            'errors':
                            fields.List(
                                fields.Any(
                                    fields.ObjectInstance(Error),
                                    fields.Dictionary(
                                        {
                                            'code':
                                            fields.UnicodeString(),
                                            'message':
                                            fields.UnicodeString(),
                                            'field':
                                            fields.UnicodeString(),
                                            'traceback':
                                            fields.UnicodeString(),
                                            'variables':
                                            fields.SchemalessDictionary(),
                                            'denied_permissions':
                                            fields.List(
                                                fields.UnicodeString()),
                                        }, ),
                                ),
                                description='The ',
                            ),
                        },
                        description=
                        'A dictionary containing either a body dict or an errors list, providing an '
                        'instruction on how the stub action should respond to requests',
                        optional_keys=('body', 'errors'),
                    ),
                ),
            }, ),
    }

    optional_keys = ()

    description = 'The settings for the local transport'
Beispiel #3
0
class StubbingOneCallTestAction(Action):
    request_schema = fields.SchemalessDictionary()
    response_schema = fields.Dictionary({'forwarded_response': fields.SchemalessDictionary()})

    def run(self, request):
        try:
            return {
                'forwarded_response': request.client.call_action('examiner', 'magnify', body=request.body).body
            }
        except request.client.CallActionError as e:
            raise ActionError(errors=e.actions[0].errors)
Beispiel #4
0
class StubbingTwoCallsTestAction(Action):
    request_schema = fields.Dictionary({'one': fields.SchemalessDictionary(), 'two':  fields.SchemalessDictionary()})
    response_schema = fields.Dictionary({'one': fields.SchemalessDictionary(), 'two':  fields.SchemalessDictionary()})

    def run(self, request):
        try:
            return {
                'one': request.client.call_action('examiner', 'roll', body=request.body['one']).body,
                'two': request.client.call_action('player', 'pitch', body=request.body['two']).body,
            }
        except request.client.CallActionError as e:
            raise ActionError(errors=e.actions[0].errors)
Beispiel #5
0
class ServerSettings(SOASettings):
    """
    Settings specific to servers
    """

    schema = {
        'transport':
        BasicClassSchema(BaseServerTransport),
        'middleware':
        fields.List(BasicClassSchema(ServerMiddleware)),
        'client_routing':
        fields.SchemalessDictionary(),
        'logging':
        fields.SchemalessDictionary(),
        'harakiri':
        fields.Dictionary({
            'timeout': fields.Integer(
                gte=0
            ),  # seconds of inactivity before harakiri is triggered, 0 to disable
            'shutdown_grace': fields.Integer(
                gte=0
            ),  # seconds to gracefully shutdown after harakiri is triggered
        }),
    }

    defaults = {
        'client_routing': {},
        'logging': {
            'version': 1,
            'formatters': {
                'console': {
                    'format': '%(asctime)s %(levelname)7s: %(message)s'
                },
            },
            'handlers': {
                'console': {
                    'level': 'INFO',
                    'class': 'logging.StreamHandler',
                    'formatter': 'console',
                },
            },
            'root': {
                'handlers': ['console'],
                'level': 'INFO',
            },
        },
        'harakiri': {
            'timeout': 300,
            'shutdown_grace': 30,
        },
    }
Beispiel #6
0
class LocalClientTransportSchema(BasicClassSchema):
    contents = {
        'path': fields.UnicodeString(
            description='The path to the local client transport, in the format `module.name:ClassName`',
        ),
        'kwargs': fields.Dictionary({
            # server class can be an import path or a class object
            'server_class': fields.Any(
                fields.UnicodeString(
                    description='The path to the `Server` class, in the format `module.name:ClassName`',
                ),
                fields.ObjectInstance(
                    six.class_types,
                    description='A reference to the `Server`-extending class/type',
                ),
                description='The path to the `Server` class to use locally (as a library), or a reference to the '
                            '`Server`-extending class/type itself',
            ),
            # No deeper validation because the Server will perform its own validation
            'server_settings': fields.SchemalessDictionary(
                key_type=fields.UnicodeString(),
                description='The settings to use when instantiating the `server_class`'
            ),
        }),
    }

    optional_keys = ()

    description = 'The settings for the local client transport'
Beispiel #7
0
class BasicClassSchema(fields.Dictionary):
    contents = {
        'path':
        fields.UnicodeString(
            description=
            'The path to the class to be imported and used, in the format `module.name:ClassName`',
        ),
        'kwargs':
        fields.SchemalessDictionary(
            key_type=fields.UnicodeString(),
            description=
            'Any keyword arguments that should be passed to the class when constructing a new instance',
        ),
    }
    optional_keys = ['kwargs']
    object_type = None

    def __init__(self, object_type=None, **kwargs):
        super(BasicClassSchema, self).__init__(**kwargs)

        if object_type:
            assert isinstance(object_type, type)
            self.object_type = object_type

    def __repr__(self):
        return '{class_name}({object_type})'.format(
            class_name=self.__class__.__name__,
            object_type='object_type={module_name}:{class_name}'.format(
                module_name=self.object_type.__module__,
                class_name=self.object_type.__name__,
            ) if self.object_type else '',
        )
class SettingsToTest(settings.Settings):
    schema: settings.SettingsSchema = {
        'one': fields.Dictionary({
            'a': fields.ClassConfigurationSchema(base_class=ClassUsingAttrs27HintsToTest, description='Nifty schema.'),
            'b': fields.PythonPath(value_schema=fields.UnicodeString(), description='Must be a path, yo.'),
            'c': fields.TypeReference(base_classes=ClassHoldingSigsToTest, description='Refer to that thing!'),
        }),
        'two': fields.SchemalessDictionary(key_type=fields.UnicodeString(), value_type=fields.Boolean()),
        'three': fields.List(fields.Integer()),
        'four': fields.Nullable(fields.Set(fields.ByteString())),
        'five': fields.Any(fields.Integer(), fields.Float()),
        'six': fields.ObjectInstance(valid_type=ClassUsingAttrs27HintsToTest, description='Y u no instance?'),
        'seven': fields.Polymorph(
            'thing',
            {
                'thing1': fields.Dictionary({'z': fields.Boolean()}, allow_extra_keys=True),
                'thing2': fields.Dictionary({'y': fields.Boolean()}, allow_extra_keys=True, optional_keys=('y', )),
            },
        ),
    }

    defaults: settings.SettingsData = {
        'one': {
            'b': 'foo.bar:Class',
        },
        'three': [1, 5, 7],
    }
class EchoAction(Action):
    request_schema = fields.SchemalessDictionary()

    response_schema = fields.Dictionary(
        {
            'request_body': fields.SchemalessDictionary(),
            'request_context': fields.SchemalessDictionary(),
            'request_switches': fields.List(fields.Integer()),
            'request_control': fields.SchemalessDictionary(),
        }, )

    def run(self, request):
        return {
            'request_body': request.body,
            'request_context': request.context,
            'request_switches': sorted(list(request.switches)),
            'request_control': request.control,
        }
Beispiel #10
0
class FakeActionTwo(Action):
    """Test action documentation"""

    request_schema = fields.SchemalessDictionary(description='Be weird.')

    response_schema = fields.Dictionary({
        'okay':
        fields.Boolean(description='Whether it is okay'),
        'reason':
        fields.Nullable(
            fields.UnicodeString(description='Why it is not okay')),
    })
Beispiel #11
0
class LocalTransportSchema(BasicClassSchema):
    contents = {
        'path':
        fields.UnicodeString(),
        'kwargs':
        fields.Dictionary({
            # server class can be an import path or a class object
            'server_class':
            fields.Any(fields.UnicodeString(),
                       fields.ObjectInstance(six.class_types)),
            # No deeper validation because the Server will perform its own validation
            'server_settings':
            fields.SchemalessDictionary(key_type=fields.UnicodeString()),
        }),
    }
Beispiel #12
0
class MetricsSchema(BasicClassSchema):
    contents = {
        'path': fields.UnicodeString(
            description='The path to the class extending `MetricsRecorder`, in the format `module.name:ClassName`',
        ),
        'kwargs': fields.Dictionary(
            {
                'config': fields.SchemalessDictionary(description='Whatever metrics configuration is required'),
            },
            optional_keys=[
                'config',
            ],
            allow_extra_keys=True,
            description='The keyword arguments that will be passed to the constructed metrics recorder',
        ),
    }

    description = 'Configuration for defining a usage and performance metrics recorder'

    object_type = MetricsRecorder
Beispiel #13
0
class MetricsSchema(BasicClassSchema):
    contents = {
        'path':
        fields.UnicodeString(
            description='The module.name:ClassName path to the metrics recorder'
        ),
        'kwargs':
        fields.Dictionary(
            {
                'config': fields.SchemalessDictionary(),
            },
            optional_keys=[
                'config',
            ],
            allow_extra_keys=True,
            description=
            'The keyword arguments that will be passed to the constructed metrics recorder',
        ),
    }

    object_type = MetricsRecorder
Beispiel #14
0
class BasicClassSchema(fields.Dictionary):
    contents = {
        'path': fields.UnicodeString(),
        'kwargs': fields.SchemalessDictionary(key_type=fields.UnicodeString()),
    }
    optional_keys = ['kwargs']
    object_type = None

    def __init__(self, object_type=None, **kwargs):
        super(BasicClassSchema, self).__init__(**kwargs)

        if object_type:
            assert isinstance(object_type, type)
            self.object_type = object_type

    def __repr__(self):
        return '{class_name}({object_type})'.format(
            class_name=self.__class__.__name__,
            object_type='object_type={module_name}:{class_name}'.format(
                module_name=self.object_type.__module__,
                class_name=self.object_type.__name__,
            ) if self.object_type else '',
        )
class ClassHoldingSigsToTest:
    def sig1(self, one, two=None):  # type: (AnyStr, Optional[bool]) -> None
        pass

    def sig1_35(self, one: AnyStr, two: Optional[bool] = None) -> None:  # noqa: E999
        pass

    def sig2(self, one, two=None, *args):
        # type: (bool, Optional[AnyStr], *int) -> List[int]
        pass

    def sig2_35(self, one: bool, *args: int, two: Optional[AnyStr] = None) -> List[int]:
        pass

    @decorated
    def sig3(
        self,
        one,
        two=None,
        **kwargs
    ):
        # type: (six.text_type, LocalToThisModuleOptionalInt, **bool) -> Dict[six.binary_type, int]
        pass

    @decorated
    @validate_method(fields.SchemalessDictionary(), fields.Anything())
    @decorated
    def sig3_super_wrapped(
        self,
        one,
        two=None,
        **kwargs
    ):
        # type: (six.text_type, LocalToThisModuleOptionalInt, **bool) -> Dict[six.binary_type, int]
        pass

    def sig3_35(
        self,
        one: six.text_type,
        *args: AnyType,
        two: Optional[int] = None,
        **kwargs: bool
    ) -> Dict[six.binary_type, int]:
        pass

    def sig4\
            (

                self,
                one,  # type: AnyStr
                two=None,  # type:  Optional[six.text_type]
                three=lambda x: True,  # type: Callable[[AnyStr], bool]
                *args,  # type: str
                **kwargs  # type: AnyType

            ):
        # type: (...) -> six.binary_type
        pass

    def sig4_35(
        self,
        one: AnyStr,
        two: Optional[six.text_type],
        three: Callable[[AnyStr], bool] = lambda x: True,
        *args: str,
        **kwargs: AnyType
    ) -> bytes:
        pass
class ServerSettings(SOASettings):
    """
    Base settings class for all servers, whose `middleware` values are restricted to subclasses of `ServerMiddleware`
    and whose `transport` values are restricted to subclasses of `BaseServerTransport`. Middleware and transport
    configuration settings schemas will automatically switch based on the configuration settings schema for the `path`
    for each.
    """

    schema = dict(
        {
            'transport':
            fields.ClassConfigurationSchema(base_class=BaseServerTransport),
            'middleware':
            fields.List(
                fields.ClassConfigurationSchema(base_class=ServerMiddleware),
                description=
                'The list of all `ServerMiddleware` objects that should be applied to requests processed '
                'by this server',
            ),
            'client_routing':
            fields.SchemalessDictionary(
                key_type=fields.UnicodeString(),
                value_type=fields.SchemalessDictionary(),
                description=
                'Client settings for sending requests to other services; keys should be service names, and '
                'values should be the corresponding configuration dicts, which will be validated using the '
                'ClientSettings schema.',
            ),
            'logging':
            PYTHON_LOGGING_CONFIG_SCHEMA,
            'harakiri':
            fields.Dictionary(
                {
                    'timeout':
                    fields.Integer(
                        gte=0,
                        description=
                        'Seconds of inactivity before harakiri is triggered; 0 to disable, defaults to 300',
                    ),
                    'shutdown_grace':
                    fields.Integer(
                        gt=0,
                        description=
                        'Seconds to forcefully shutdown after harakiri is triggered if shutdown does not '
                        'occur',
                    ),
                },
                description=
                'Instructions for automatically terminating a server process when request processing takes '
                'longer than expected.',
            ),
            'request_log_success_level':
            PythonLogLevel(
                description=
                'The logging level at which full request and response contents will be logged for '
                'successful requests', ),
            'request_log_error_level':
            PythonLogLevel(
                description=
                'The logging level at which full request and response contents will be logged for requests '
                'whose responses contain errors (setting this to a more severe level than '
                '`request_log_success_level` will allow you to easily filter for unsuccessful requests)',
            ),
            'heartbeat_file':
            fields.Nullable(
                fields.UnicodeString(
                    description=
                    'If specified, the server will create a heartbeat file at the specified path on startup, '
                    'update the timestamp in that file after the processing of every request or every time '
                    'idle operations are processed, and delete the file when the server shuts down. The file '
                    'name can optionally contain the specifier {{pid}}, which will be replaced with the '
                    'server process PID. Finally, the file name can optionally contain the specifier {{fid}}, '
                    'which will be replaced with the unique-and-deterministic forked process ID whenever the '
                    'server is started with the --fork option (the minimum value is always 1 and the maximum '
                    'value is always equal to the value of the --fork option).',
                )),
            'extra_fields_to_redact':
            fields.Set(
                fields.UnicodeString(),
                description=
                'Use this field to supplement the set of fields that are automatically redacted/censored '
                'in request and response fields with additional fields that your service needs redacted.',
            ),
        }, **extra_schema)  # type: SettingsSchema

    defaults = dict(
        {
            'client_routing': {},
            'logging': {
                'version': 1,
                'formatters': {
                    'console': {
                        'format':
                        '%(asctime)s %(levelname)7s %(correlation_id)s %(request_id)s: %(message)s'
                    },
                    'syslog': {
                        'format':
                        ('%(service_name)s_service: %(name)s %(levelname)s %(module)s %(process)d '
                         'correlation_id %(correlation_id)s request_id %(request_id)s %(message)s'
                         ),
                    },
                },
                'filters': {
                    'pysoa_logging_context_filter': {
                        '()': 'pysoa.common.logging.PySOALogContextFilter',
                    },
                },
                'handlers': {
                    'console': {
                        'level': 'INFO',
                        'class': 'logging.StreamHandler',
                        'formatter': 'console',
                        'filters': ['pysoa_logging_context_filter'],
                    },
                    'syslog': {
                        'level': 'INFO',
                        'class': 'pysoa.common.logging.SyslogHandler',
                        'facility': SyslogHandler.LOG_LOCAL7,
                        'address': ('localhost', 514),
                        'formatter': 'syslog',
                        'filters': ['pysoa_logging_context_filter'],
                    },
                },
                'loggers': {},
                'root': {
                    'handlers': ['console'],
                    'level': 'INFO',
                },
                'disable_existing_loggers': False,
            },
            'harakiri': {
                'timeout': 300,
                'shutdown_grace': 30,
            },
            'request_log_success_level': 'INFO',
            'request_log_error_level': 'INFO',
            'heartbeat_file': None,
            'extra_fields_to_redact': set(),
            'transport': {
                'path':
                'pysoa.common.transport.redis_gateway.server:RedisServerTransport',
            }
        }, **extra_defaults)  # type: SettingsData
Beispiel #17
0
class RedisTransportSchema(BasicClassSchema):
    contents = {
        'path': fields.UnicodeString(
            description='The path to the Redis client or server transport, in the format `module.name:ClassName`',
        ),
        'kwargs': fields.Dictionary(
            {
                'backend_layer_kwargs': fields.Dictionary(
                    {
                        'connection_kwargs': fields.SchemalessDictionary(
                            description='The arguments used when creating all Redis connections (see Redis-Py docs)',
                        ),
                        'hosts': fields.List(
                            fields.Any(
                                fields.Tuple(fields.UnicodeString(), fields.Integer()),
                                fields.UnicodeString(),
                            ),
                            description='The list of Redis hosts, where each is a tuple of `("address", port)` or the '
                                        'simple string address.',
                        ),
                        'redis_db': fields.Integer(
                            description='The Redis database, a shortcut for putting this in `connection_kwargs`.',
                        ),
                        'redis_port': fields.Integer(
                            description='The port number, a shortcut for putting this on all hosts',
                        ),
                        'sentinel_failover_retries': fields.Integer(
                            description='How many times to retry (with a delay) getting a connection from the Sentinel '
                                        'when a master cannot be found (cluster is in the middle of a failover); '
                                        'should only be used for Sentinel backend type'
                        ),
                        'sentinel_services': fields.List(
                            fields.UnicodeString(),
                            description='A list of Sentinel services (will be discovered by default); should only be '
                                        'used for Sentinel backend type',
                        ),
                    },
                    optional_keys=[
                        'connection_kwargs',
                        'hosts',
                        'redis_db',
                        'redis_port',
                        'sentinel_failover_retries',
                        'sentinel_services',
                    ],
                    allow_extra_keys=False,
                    description='The arguments passed to the Redis connection manager',
                ),
                'backend_type': fields.Constant(
                    *REDIS_BACKEND_TYPES,
                    description='Which backend (standard or sentinel) should be used for this Redis transport'
                ),
                'log_messages_larger_than_bytes': fields.Integer(
                    description='By default, messages larger than 100KB that do not trigger errors (see '
                                '`maximum_message_size_in_bytes`) will be logged with level WARNING to a logger named '
                                '`pysoa.transport.oversized_message`. To disable this behavior, set this setting to '
                                '0. Or, you can set it to some other number to change the threshold that triggers '
                                'logging.',
                ),
                'maximum_message_size_in_bytes': fields.Integer(
                    description='The maximum message size, in bytes, that is permitted to be transmitted over this '
                                'transport (defaults to 100KB on the client and 250KB on the server)',
                ),
                'message_expiry_in_seconds': fields.Integer(
                    description='How long after a message is sent that it is considered expired, dropped from queue',
                ),
                'queue_capacity': fields.Integer(
                    description='The capacity of the message queue to which this transport will send messages',
                ),
                'queue_full_retries': fields.Integer(
                    description='How many times to retry sending a message to a full queue before giving up',
                ),
                'receive_timeout_in_seconds': fields.Integer(
                    description='How long to block waiting on a message to be received',
                ),
                'serializer_config': BasicClassSchema(
                    object_type=BaseSerializer,
                    description='The configuration for the serializer this transport should use',
                ),
            },
            optional_keys=[
                'backend_layer_kwargs',
                'log_messages_larger_than_bytes',
                'maximum_message_size_in_bytes',
                'message_expiry_in_seconds',
                'queue_capacity',
                'queue_full_retries',
                'receive_timeout_in_seconds',
                'serializer_config',
            ],
            allow_extra_keys=False,
        ),
    }

    optional_keys = ()

    description = 'The settings for the Redis transport'
Beispiel #18
0
class ServerSettings(SOASettings):
    """
    Settings specific to servers
    """

    schema = {
        'transport':
        BasicClassSchema(BaseServerTransport),
        'middleware':
        fields.List(
            BasicClassSchema(ServerMiddleware),
            description=
            'The list of all `ServerMiddleware` objects that should be applied to requests processed by '
            'this server',
        ),
        'client_routing':
        fields.SchemalessDictionary(
            key_type=fields.UnicodeString(),
            value_type=fields.SchemalessDictionary(),
            description=
            'Client settings for sending requests to other services; keys should be service names, and '
            'values should be the corresponding configuration dicts, which will be validated using the '
            'PolymorphicClientSettings schema',
        ),
        'logging':
        fields.Dictionary(
            {
                'version':
                fields.Integer(gte=1, lte=1),
                'formatters':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            'format': fields.UnicodeString(),
                            'datefmt': fields.UnicodeString(),
                        },
                        optional_keys=('datefmt', ),
                    ),
                ),
                'filters':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            '()':
                            fields.Anything(
                                description='The optional filter class'),
                            'name':
                            fields.UnicodeString(
                                description='The optional filter name'),
                        },
                        optional_keys=('()', 'name'),
                    ),
                ),
                'handlers':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            'class': fields.UnicodeString(),
                            'level': fields.UnicodeString(),
                            'formatter': fields.UnicodeString(),
                            'filters': fields.List(fields.UnicodeString()),
                        },
                        optional_keys=('level', 'formatter', 'filters'),
                        allow_extra_keys=True,
                    ),
                ),
                'loggers':
                fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=_logger_schema,
                ),
                'root':
                _logger_schema,
                'incremental':
                fields.Boolean(),
                'disable_existing_loggers':
                fields.Boolean(),
            },
            optional_keys=(
                'version',
                'formatters',
                'filters',
                'handlers',
                'root',
                'loggers',
                'incremental',
            ),
            description=
            'Settings for service logging, which should follow the standard Python logging configuration',
        ),
        'harakiri':
        fields.Dictionary(
            {
                'timeout':
                fields.Integer(
                    gte=0,
                    description=
                    'Seconds of inactivity before harakiri is triggered; 0 to disable, defaults to 300',
                ),
                'shutdown_grace':
                fields.Integer(
                    gt=0,
                    description=
                    'Seconds to forcefully shutdown after harakiri is triggered if shutdown does not occur',
                ),
            },
            description=
            'Instructions for automatically terminating a server process when request processing takes '
            'longer than expected.',
        ),
        'request_log_success_level':
        log_level_schema(
            description=
            'The logging level at which full request and response contents will be logged for successful '
            'requests', ),
        'request_log_error_level':
        log_level_schema(
            description=
            'The logging level at which full request and response contents will be logged for requests '
            'whose responses contain errors (setting this to a more severe level than '
            '`request_log_success_level` will allow you to easily filter for unsuccessful requests)',
        ),
        'heartbeat_file':
        fields.Nullable(
            fields.UnicodeString(
                description=
                'If specified, the server will create a heartbeat file at the specified path on startup, '
                'update the timestamp in that file after the processing of every request or every time '
                'idle operations are processed, and delete the file when the server shuts down. The file name '
                'can optionally contain the specifier {{pid}}, which will be replaced with the server process '
                'PID.', )),
        'extra_fields_to_redact':
        fields.Set(
            fields.UnicodeString(),
            description=
            'Use this field to supplement the set of fields that are automatically redacted/censored in '
            'request and response fields with additional fields that your service needs redacted.',
        ),
    }

    defaults = {
        'client_routing': {},
        'logging': {
            'version': 1,
            'formatters': {
                'console': {
                    'format':
                    '%(asctime)s %(levelname)7s %(correlation_id)s %(request_id)s: %(message)s'
                },
                'syslog': {
                    'format':
                    ('%(service_name)s_service: %(name)s %(levelname)s %(module)s %(process)d '
                     'correlation_id %(correlation_id)s request_id %(request_id)s %(message)s'
                     ),
                },
            },
            'filters': {
                'pysoa_logging_context_filter': {
                    '()': 'pysoa.common.logging.PySOALogContextFilter',
                },
            },
            'handlers': {
                'console': {
                    'level': 'INFO',
                    'class': 'logging.StreamHandler',
                    'formatter': 'console',
                    'filters': ['pysoa_logging_context_filter'],
                },
                'syslog': {
                    'level': 'INFO',
                    'class': 'logging.handlers.SysLogHandler',
                    'facility': SysLogHandler.LOG_LOCAL7,
                    'address': ('localhost', 514),
                    'formatter': 'syslog',
                    'filters': ['pysoa_logging_context_filter'],
                },
            },
            'loggers': {},
            'root': {
                'handlers': ['console'],
                'level': 'INFO',
            },
            'disable_existing_loggers': False,
        },
        'harakiri': {
            'timeout': 300,
            'shutdown_grace': 30,
        },
        'request_log_success_level': 'INFO',
        'request_log_error_level': 'INFO',
        'heartbeat_file': None,
        'extra_fields_to_redact': set(),
    }
Beispiel #19
0
class BaseStatusAction(Action):
    """
    Standard base action for status checks.

    Returns heath check and version information.

    If you want to use default StatusAction use StatusActionFactory(version)
    passing in the version of your service.

    If you want to make a custom StatusAction, subclass this class,
    make it get self._version from your service and add additional health check methods.
    Health check methods must start with the word check_.

    Health check methods must take no arguments, and return a list of tuples in the format:
    (is_error, code, description).

    is_error: True if this is an error, False if it is a warning.
    code: Invariant string for this error, like "MYSQL_FAILURE"
    description: Human-readable description of the problem, like "Could not connect to host on port 1234"

    Health check methods can also write to the self.diagnostics dictionary to add additional
    data which will be sent back with the response if they like. They are responsible for their
    own key management in this situation.
    """

    def __init__(self, *args, **kwargs):
        if self.__class__ is BaseStatusAction:
            raise RuntimeError('You cannot use BaseStatusAction directly; it must be subclassed')
        super(BaseStatusAction, self).__init__(*args, **kwargs)

        self.diagnostics = {}

    @property
    def _version(self):
        raise NotImplementedError('version must be defined using StatusActionFactory')

    @property
    def _build(self):
        return None

    request_schema = fields.Dictionary(
        {
            'verbose': fields.Boolean(
                description='If specified and False, this instructs the status action to return only the baseline '
                            'status information (Python, service, PySOA, and other library versions) and omit any of '
                            'the health check operations (no `healthcheck` attribute will be included in the '
                            'response). This provides a useful way to obtain the service version very quickly without '
                            'executing the often time-consuming code necessary for the full health check. It defaults '
                            'to True, which means "return everything."',
            ),
        },
        optional_keys=('verbose', ),
    )

    response_schema = fields.Dictionary(
        {
            'build': fields.UnicodeString(),
            'conformity': fields.UnicodeString(),
            'healthcheck': fields.Dictionary(
                {
                    'warnings': fields.List(fields.Tuple(fields.UnicodeString(), fields.UnicodeString())),
                    'errors': fields.List(fields.Tuple(fields.UnicodeString(), fields.UnicodeString())),
                    'diagnostics': fields.SchemalessDictionary(key_type=fields.UnicodeString()),
                },
                optional_keys=('warnings', 'errors', 'diagnostics'),
            ),
            'pysoa': fields.UnicodeString(),
            'python': fields.UnicodeString(),
            'version': fields.UnicodeString(),
        },
        optional_keys=('build', 'healthcheck', ),
    )

    def run(self, request):
        """
        Scans the class for check_ methods and runs them.
        """
        status = {
            'conformity': six.text_type(conformity.__version__),
            'pysoa': six.text_type(pysoa.__version__),
            'python': six.text_type(platform.python_version()),
            'version': self._version,
        }

        if self._build:
            status['build'] = self._build

        if request.body.get('verbose', True) is True:
            errors = []
            warnings = []
            self.diagnostics = {}

            # Find all things called "check_<something>" on this class.
            # We can't just scan __dict__ because of class inheritance.
            check_methods = [getattr(self, x) for x in dir(self) if x.startswith('check_')]
            for check_method in check_methods:
                # Call the check, and see if it returned anything
                problems = check_method()
                if problems:
                    for is_error, code, description in problems:
                        # Parcel out the values into the right return list
                        if is_error:
                            errors.append((code, description))
                        else:
                            warnings.append((code, description))

            status['healthcheck'] = {
                'errors': errors,
                'warnings': warnings,
                'diagnostics': self.diagnostics,
            }

        return status
Beispiel #20
0
class ServerSettings(SOASettings):
    """
    Settings specific to servers
    """

    schema = {
        'transport': BasicClassSchema(BaseServerTransport),
        'middleware': fields.List(BasicClassSchema(ServerMiddleware)),
        'client_routing': fields.SchemalessDictionary(
            key_type=fields.UnicodeString(),
            value_type=fields.SchemalessDictionary(),
            description='Client settings for sending requests to other services; keys should be service names, and '
                        'values should be the corresponding configuration dicts, which will be validated using the '
                        'PolymorphicClientSettings schema',
        ),
        'logging': fields.Dictionary(
            {
                'version': fields.Integer(gte=1, lte=1),
                'formatters': fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            'format': fields.UnicodeString(),
                            'datefmt': fields.UnicodeString(),
                        },
                        optional_keys=('datefmt', ),
                    ),
                ),
                'filters': fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary({'name': fields.UnicodeString()}, optional_keys=('name', )),
                ),
                'handlers': fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=fields.Dictionary(
                        {
                            'class': fields.UnicodeString(),
                            'level': fields.UnicodeString(),
                            'formatter': fields.UnicodeString(),
                            'filters': fields.List(fields.UnicodeString()),
                        },
                        optional_keys=('level', 'formatter', 'filters'),
                        allow_extra_keys=True,
                    ),
                ),
                'loggers': fields.SchemalessDictionary(
                    key_type=fields.UnicodeString(),
                    value_type=_logger_schema,
                ),
                'root': _logger_schema,
                'incremental': fields.Boolean(),
                'disable_existing_loggers': fields.Boolean(),
            },
            optional_keys=(
                'version',
                'formatters',
                'filters',
                'handlers',
                'root',
                'loggers',
                'incremental',
                'disable_existing_loggers',
            ),
            description='Settings for service logging, which should follow the standard Python logging configuration',
        ),
        'harakiri': fields.Dictionary(
            {
                'timeout': fields.Integer(
                    gte=0,
                    description='Seconds of inactivity before harakiri is triggered; 0 to disable, defaults to 300',
                ),
                'shutdown_grace': fields.Integer(
                    gt=0,
                    description='Seconds to forcefully shutdown after harakiri is triggered if shutdown does not occur',
                ),
            },
        ),
    }

    defaults = {
        'client_routing': {},
        'logging': {
            'version': 1,
            'formatters': {
                'console': {
                    'format': '%(asctime)s %(levelname)7s: %(message)s'
                },
            },
            'handlers': {
                'console': {
                    'level': 'INFO',
                    'class': 'logging.StreamHandler',
                    'formatter': 'console',
                },
            },
            'root': {
                'handlers': ['console'],
                'level': 'INFO',
            },
        },
        'harakiri': {
            'timeout': 300,
            'shutdown_grace': 30,
        },
    }
Beispiel #21
0
class ExpansionSettings(Settings):
    """
    Defines the schema for configuration settings used when expanding objects on responses with the Expansions tool.
    """

    schema = {
        'type_routes':
        fields.SchemalessDictionary(
            key_type=fields.UnicodeString(
                description=
                'The name of the expansion route, to be referenced from the `type_expansions` '
                'configuration', ),
            value_type=fields.Dictionary(
                {
                    'service':
                    fields.UnicodeString(
                        description=
                        'The name of the service to call to resolve this route',
                    ),
                    'action':
                    fields.UnicodeString(
                        description=
                        'The name of the action to call to resolve this route, which must accept a single '
                        'request field of type `List`, to which all the identifiers for matching candidate '
                        'expansions will be passed, and which must return a single response field of type '
                        '`Dictionary`, from which all expansion objects will be obtained',
                    ),
                    'request_field':
                    fields.UnicodeString(
                        description=
                        'The name of the `List` identifier field to place in the `ActionRequest` body when '
                        'making the request to the named service and action',
                    ),
                    'response_field':
                    fields.UnicodeString(
                        description=
                        'The name of the `Dictionary` field returned in the `ActionResponse`, from which '
                        'the expanded objects will be extracted', ),
                },
                description='The instructions for resolving this type route',
            ),
            description=
            'The definition of all recognized types that can be expanded into and information about how '
            'to resolve objects of those types through action calls',
        ),
        'type_expansions':
        fields.SchemalessDictionary(
            key_type=fields.UnicodeString(
                description=
                'The name of the type for which the herein defined expansions can be sought, which will be '
                "matched with a key from the `expansions` dict passed to one of `Client`'s `call_***` "
                'methods, and which must also match the value of a `_type` field found on response objects '
                'on which extra data will be expanded', ),
            value_type=fields.SchemalessDictionary(
                key_type=fields.UnicodeString(
                    description=
                    'The name of an expansion, which will be matched with a value from the `expansions` '
                    "dict passed to one of `Client`'s `call_***` methods corresponding to the type key in "
                    'that dict', ),
                value_type=fields.Dictionary(
                    {
                        'type':
                        fields.Nullable(
                            fields.UnicodeString(
                                description=
                                'The type of object this expansion yields, which must map back to a '
                                '`type_expansions` key in order to support nested/recursive expansions, and '
                                'may be `None` if you do not wish to support nested/recursive expansions for '
                                'this expansion', )),
                        'route':
                        fields.UnicodeString(
                            description=
                            'The route to use to resolve this expansion, which must match a key in the '
                            '`type_routes` configuration', ),
                        'source_field':
                        fields.UnicodeString(
                            description=
                            'The name of the field in the base object that contains the identifier used '
                            'for obtaining the expansion object (the identifier will be passed to the '
                            '`request_field` in the route when resolving the expansion)',
                        ),
                        'destination_field':
                        fields.UnicodeString(
                            description=
                            'The name of a not-already-existent field in the base object into which the '
                            'expansion object will be placed after it is obtained from the route',
                        ),
                        'raise_action_errors':
                        fields.Boolean(
                            description=
                            'Whether to raise action errors encountered when expanding objects these '
                            'objects (by default, action errors are suppressed, which differs from the '
                            'behavior of the `Client` to raise action errors during normal requests)',
                        ),
                    },
                    optional_keys=('raise_action_errors', ),
                    description=
                    'The definition of one specific possible expansion for this object type',
                ),
                description=
                'The definition of all possible expansions for this object type',
            ),
            description=
            'The definition of all types that may contain identifiers that can be expanded into objects '
            'using the `type_routes` configurations',
        ),
    }
Beispiel #22
0
class IntrospectionAction(Action):
    """
    This action returns detailed information about the service's defined actions and the request and response schemas
    for each action, along with any documentation defined for the action or for the service itself. It can be passed
    a single action name to return information limited to that single action. Otherwise, it will return information for
    all of the service's actions.

    This action will be added to your service on your behalf if you do not define an action with name `introspect`.

    Making your services and actions capable of being introspected is simple. If your server class has a `description`
    attribute, that will be the service's documentation that introspection returns. If your server class does not have
    this attribute but does have a docstring, introspection will use the docstring. The same rule applies to action
    classes: Introspection first looks for a `description` attribute and then uses the docstring, if any. If neither of
    these are found, the applicable service or action documentation will be done.

    Introspection then looks at the `request_schema` and `response_schema` attributes for each of your actions, and
    includes the details about these schemas in the returned information for each action. Be sure you include field
    descriptions in your schema for the most effective documentation possible.
    """
    description = (
        "This action returns detailed information about the service's defined actions and the request and response "
        "schemas for each action, along with any documentation defined for the action or for the service itself. It "
        "can be passed a single action name to return information limited to that single action. Otherwise, it will "
        "return information for all of the service's actions. If an action is a switched action (meaning the action "
        "extends `SwitchedAction`, and which action code runs is controlled with SOA switches), multiple action "
        "introspection results will be returned for that action, each with a name ending in either `[switch:N]` (where "
        "`N` is the switch value) or `[DEFAULT]` for the default action."
    )

    request_schema = fields.Dictionary(
        {
            'action_name': fields.UnicodeString(
                min_length=1,
                allow_blank=False,
                description='Specify this to limit your introspection to a single action. It will be the only action '
                            'present in the `actions` response attribute. If the requested action does not exist, an '
                            'error will be returned.',
            ),
        },
        optional_keys=('action_name', ),
    )

    response_schema = fields.Dictionary(
        {
            'documentation': fields.Nullable(fields.UnicodeString(
                description='The documentation for the server, unless `action_name` is specified in the request body, '
                            'in which case this is omitted.',
            )),
            'action_names': fields.List(
                fields.UnicodeString(description='The name of an action.'),
                description='An alphabetized list of every action name included in `actions`.',
            ),
            'actions': fields.SchemalessDictionary(
                key_type=fields.UnicodeString(description='The name of the action.'),
                value_type=fields.Dictionary(
                    {
                        'documentation': fields.Nullable(
                            fields.UnicodeString(description='The documentation for the action'),
                        ),
                        'request_schema': fields.Nullable(fields.Anything(
                            description='A description of the expected request schema, including any documentation '
                                        'specified in the schema definition.',
                        )),
                        'response_schema': fields.Nullable(fields.Anything(
                            description='A description of the guaranteed response schema, including any documentation '
                                        'specified in the schema definition.',
                        )),
                    },
                    description='A introspection of a single action',
                ),
                description='A dict mapping action names to action description dictionaries. This contains details '
                            'about every action in the service unless `action_name` is specified in the request body, '
                            'in which case it contains details only for that action.',
            ),
        },
        optional_keys=('documentation', ),
    )

    def __init__(self, server):
        """
        Construct a new introspection action. Unlike its base class, which accepts a server settings object, this
        must be passed a `Server` object, from which it will obtain a settings object. The `Server` code that calls
        this action has special handling to address this requirement.

        :param server: A PySOA server instance
        :type server: Server
        """
        if not isinstance(server, Server):
            raise TypeError('First argument (server) must be a Server instance')

        super(IntrospectionAction, self).__init__(server.settings)

        self.server = server

    def run(self, request):
        """
        Introspects all of the actions on the server and returns their documentation.

        :param request: The request object
        :type request: EnrichedActionRequest

        :return: The response
        """
        if request.body.get('action_name'):
            return self._get_response_for_single_action(request.body.get('action_name'))

        return self._get_response_for_all_actions()

    def _get_response_for_single_action(self, request_action_name):
        action_name = request_action_name
        switch = None

        if SWITCHED_ACTION_RE.match(action_name):
            match = SWITCHED_ACTION_RE.match(action_name)
            action_name = match.group(str('action'))
            if match.group(str('default')):
                switch = SwitchedAction.DEFAULT_ACTION
            else:
                switch = int(match.group(str('switch')))

        if action_name not in self.server.action_class_map and action_name not in ('status', 'introspect'):
            raise ActionError(errors=[
                Error(code=ERROR_CODE_INVALID, message='Action not defined in service', field='action_name'),
            ])

        if action_name in self.server.action_class_map:
            action_class = self.server.action_class_map[action_name]
            if issubclass(action_class, SwitchedAction):
                if switch:
                    if switch == SwitchedAction.DEFAULT_ACTION:
                        action_class = action_class.switch_to_action_map[-1][1]
                    else:
                        for matching_switch, action_class in action_class.switch_to_action_map:
                            if switch == matching_switch:
                                break
                else:
                    response = {
                        'action_names': [],
                        'actions': {}
                    }
                    for sub_name, sub_class in self._iterate_switched_actions(action_name, action_class):
                        response['action_names'].append(sub_name)
                        response['actions'][sub_name] = self._introspect_action(sub_class)
                    response['action_names'] = list(sorted(response['action_names']))
                    return response
        elif action_name == 'introspect':
            action_class = self.__class__
        else:
            action_class = BaseStatusAction

        return {
            'action_names': [request_action_name],
            'actions': {request_action_name: self._introspect_action(action_class)}
        }

    def _get_response_for_all_actions(self):
        response = {
            'actions': {},
            'action_names': [],
            'documentation': getattr(self.server.__class__, 'description', self.server.__class__.__doc__) or None,
        }

        if 'introspect' not in self.server.action_class_map:
            response['action_names'].append('introspect')
            response['actions']['introspect'] = self._introspect_action(self.__class__)

        if 'status' not in self.server.action_class_map:
            response['action_names'].append('status')
            response['actions']['status'] = self._introspect_action(BaseStatusAction)

        for action_name, action_class in six.iteritems(self.server.action_class_map):
            if issubclass(action_class, SwitchedAction):
                for sub_action_name, sub_action_class in self._iterate_switched_actions(action_name, action_class):
                    response['action_names'].append(sub_action_name)
                    response['actions'][sub_action_name] = self._introspect_action(sub_action_class)
            else:
                response['action_names'].append(action_name)
                response['actions'][action_name] = self._introspect_action(action_class)

        response['action_names'] = list(sorted(response['action_names']))

        return response

    @staticmethod
    def _iterate_switched_actions(action_name, action_class):
        found_default = False
        last_index = len(action_class.switch_to_action_map) - 1
        for i, (switch, sub_action_class) in enumerate(action_class.switch_to_action_map):
            if switch == SwitchedAction.DEFAULT_ACTION:
                sub_action_name = '{}[DEFAULT]'.format(action_name)
                found_default = True
            elif not found_default and i == last_index:
                sub_action_name = '{}[DEFAULT]'.format(action_name)
            else:
                sub_action_name = '{}[switch:{}]'.format(action_name, get_switch(switch))

            yield sub_action_name, sub_action_class

    @staticmethod
    def _introspect_action(action_class):
        action = {
            'documentation': getattr(action_class, 'description', action_class.__doc__) or None,
            'request_schema': None,
            'response_schema': None,
        }

        if getattr(action_class, 'request_schema', None):
            action['request_schema'] = action_class.request_schema.introspect()

        if getattr(action_class, 'response_schema', None):
            action['response_schema'] = action_class.response_schema.introspect()

        return action
Beispiel #23
0
class RedisTransportSchema(BasicClassSchema):
    contents = {
        'path': fields.UnicodeString(),
        'kwargs': fields.Dictionary(
            {
                'backend_layer_kwargs': fields.Dictionary(
                    {
                        'connection_kwargs': fields.SchemalessDictionary(
                            description='The arguments used when creating all Redis connections (see Redis-Py docs)',
                        ),
                        'hosts': fields.List(
                            fields.Any(
                                fields.Tuple(fields.UnicodeString(), fields.Integer()),
                                fields.UnicodeString(),
                            ),
                            description='The list of Redis hosts',
                        ),
                        'redis_db': fields.Integer(
                            description='The Redis database, a shortcut for putting this in `connection_kwargs`.',
                        ),
                        'redis_port': fields.Integer(
                            description='The port number, a shortcut for putting this on all hosts',
                        ),
                        'sentinel_failover_retries': fields.Integer(
                            description='How many times to retry (with a delay) getting a connection from the Sentinel '
                                        'when a master cannot be found (cluster is in the middle of a failover); '
                                        'should only be used for Sentinel backend type'
                        ),
                        'sentinel_refresh_interval': fields.Integer(
                            description='Deprecated; unused; to be removed before final release.',
                        ),
                        'sentinel_services': fields.List(
                            fields.UnicodeString(),
                            description='A list of Sentinel services (will be discovered by default); should only be '
                                        'used for Sentinel backend type',
                        ),
                    },
                    optional_keys=[
                        'connection_kwargs',
                        'hosts',
                        'redis_db',
                        'redis_port',
                        'sentinel_failover_retries',
                        'sentinel_refresh_interval',
                        'sentinel_services',
                    ],
                    allow_extra_keys=False,
                    description='The arguments passed to the Redis connection manager',
                ),
                'backend_type': fields.Constant(
                    *REDIS_BACKEND_TYPES,
                    description='Which backend (standard or sentinel) should be used for this Redis transport'
                ),
                'message_expiry_in_seconds': fields.Integer(
                    description='How long after a message is sent that it is considered expired, dropped from queue',
                ),
                'queue_capacity': fields.Integer(
                    description='The capacity of the message queue to which this transport will send messages',
                ),
                'queue_full_retries': fields.Integer(
                    description='How many times to retry sending a message to a full queue before giving up',
                ),
                'receive_timeout_in_seconds': fields.Integer(
                    description='How long to block waiting on a message to be received',
                ),
                'serializer_config': BasicClassSchema(
                    object_type=BaseSerializer,
                    description='The configuration for the serializer this transport should use',
                ),
            },
            optional_keys=[
                'backend_layer_kwargs',
                'message_expiry_in_seconds',
                'queue_capacity',
                'queue_full_retries',
                'receive_timeout_in_seconds',
                'serializer_config',
            ],
            allow_extra_keys=False,
        ),
    }
Beispiel #24
0
    ClientTransport,
    ReceivedMessage,
    ServerTransport,
)
from pysoa.server.server import Server

__all__ = (
    'LocalClientTransport',
    'LocalClientTransportSchema',
    'LocalServerTransport',
    'LocalServerTransportSchema',
)

_server_settings = fields.SchemalessDictionary(
    key_type=fields.UnicodeString(),
    description=
    'A dictionary of settings for the server (which will further validate them).',
)


class LocalClientTransportSchema(fields.Dictionary):
    contents = {
        # Server class can be an import path or a class object
        'server_class':
        fields.Any(
            fields.TypePath(
                description=
                'The importable Python path to the `Server`-extending class.',
                base_classes=Server,
            ),
            fields.TypeReference(
Beispiel #25
0
PYTHON_LOGGING_CONFIG_SCHEMA = fields.Dictionary(
    collections.OrderedDict((
        ('version', fields.Integer(gte=1, lte=1)),
        ('formatters',
         fields.SchemalessDictionary(
             key_type=fields.UnicodeString(),
             value_type=fields.Dictionary(
                 {
                     'format':
                     fields.UnicodeString(
                         description='The format string for this formatter (see '
                         'https://docs.python.org/3/library/logging.html#logrecord-attributes).',
                     ),
                     'datefmt':
                     fields.UnicodeString(
                         description=
                         'The optional date format used when formatting dates in the log output (see '
                         'https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior).',
                     ),
                 },
                 optional_keys=('datefmt', ),
             ),
             description=
             'This defines a mapping of logging formatter names to formatter configurations. The `format` '
             'key specifies the log format and the `datefmt` key specifies the date format.',
         )),
        ('filters',
         fields.SchemalessDictionary(
             key_type=fields.UnicodeString(),
             value_type=fields.Dictionary(
                 {
Beispiel #26
0
class BaseStatusAction(Action):
    """
    Standard base action for status checks. Returns health check and version information.

    If you want to use the status action use `StatusActionFactory(version)`, passing in the version of your service
    and, optionally, the build of your service. If you do not specify an action with name `status` in your server,
    this will be done on your behalf.

    If you want to make a custom status action, subclass this class, make `self._version` return your service's version
    string, `self._build` optionally return your service's build string, and add any additional health check methods
    you desire. Health check methods must start with `check_`.

    Health check methods accept a single argument, the request object (an instance of `ActionRequest`), and return a
    list of tuples in the format `(is_error, code, description)` (or a false-y value if there are no problems):

    - `is_error`: `True` if this is an error, `False` if it is a warning.
    - `code`: Invariant string for this error, like "MYSQL_FAILURE"
    - `description`: Human-readable description of the problem, like "Could not connect to host on port 1234"

    Health check methods can also write to the `self.diagnostics` dictionary to add additional data which will be sent
    back with the response if they like. They are responsible for their own key management in this situation.

    This base status action comes with a disabled-by-default health check method named `_check_client_settings` (the
    leading underscore disables it), which calls `status` on all other services that this service is configured to call
    (using `verbose: False`, which guarantees no further recursive status checking) and includes those responses in
    this action's response. To enable this health check, simply reference it as a new, valid `check_` method name, like
    so:

    .. code:: python

        class MyStatusAction(BaseStatusAction):
            ...
            check_client_settings = BaseStatusAction._check_client_settings
    """
    def __init__(self, *args, **kwargs):
        """
        Constructs a new base status action. Concrete status actions can override this if they want, but must call
        `super`.

        :param settings: The server settings object
        :type settings: dict
        """
        super(BaseStatusAction, self).__init__(*args, **kwargs)

        self.diagnostics = {}

    @abc.abstractproperty
    def _version(self):
        raise NotImplementedError(
            'version must be defined using StatusActionFactory')

    @property
    def _build(self):
        return None

    description = (
        'Returns version info for the service, Python, PySOA, and Conformity. If the service has a build string, that '
        'is also returned. If the service has defined additional health check behavior and the `verbose` request '
        'attribute is not set to `False`, those additional health checks are performed and returned in the '
        '`healthcheck` response attribute. If the `verbose` request attribute is set to `False`, the additional '
        'health checks are not performed and `healthcheck` is not included in the response (importantly, the `check_` '
        'methods are not invoked).')

    request_schema = fields.Nullable(
        fields.Dictionary(
            {
                'verbose':
                fields.Boolean(
                    description=
                    'If specified and False, this instructs the status action to return only the baseline '
                    'status information (Python, service, PySOA, and other library versions) and omit any of '
                    'the health check operations (no `healthcheck` attribute will be included in the '
                    'response). This provides a useful way to obtain the service version very quickly without '
                    'executing the often time-consuming code necessary for the full health check. It defaults '
                    'to True, which means "return everything."', ),
            },
            optional_keys=('verbose', ),
        ))

    response_schema = fields.Dictionary(
        {
            'build':
            fields.UnicodeString(
                description='The version build string, if applicable.'),
            'conformity':
            fields.UnicodeString(
                description='The version of Conformity in use.'),
            'healthcheck':
            fields.Dictionary(
                {
                    'warnings':
                    fields.List(
                        fields.Tuple(
                            fields.UnicodeString(
                                description='The invariant warning code'),
                            fields.UnicodeString(
                                description='The readable warning description'
                            ),
                        ),
                        description=
                        'A list of any warnings encountered during the health checks.',
                    ),
                    'errors':
                    fields.List(
                        fields.Tuple(
                            fields.UnicodeString(
                                description='The invariant error code'),
                            fields.UnicodeString(
                                description='The readable error description'),
                        ),
                        description=
                        'A list of any errors encountered during the health checks.',
                    ),
                    'diagnostics':
                    fields.SchemalessDictionary(
                        key_type=fields.UnicodeString(),
                        description=
                        'A dictionary containing any additional diagnostic information output by the '
                        'health check operations.',
                    ),
                },
                optional_keys=('warnings', 'errors', 'diagnostics'),
                description=
                'Information about any additional health check operations performed.',
            ),
            'pysoa':
            fields.UnicodeString(description='The version of PySOA in use.'),
            'python':
            fields.UnicodeString(description='The version of Python in use.'),
            'version':
            fields.UnicodeString(
                description='The version of the responding service.'),
        },
        optional_keys=(
            'build',
            'healthcheck',
        ),
    )

    def run(self, request):
        """
        Adds version information for Conformity, PySOA, Python, and the service to the response, then scans the class
        for `check_` methods and runs them (unless `verbose` is `False`).

        :param request: The request object
        :type request: EnrichedActionRequest

        :return: The response
        """
        status = {
            'conformity': six.text_type(conformity.__version__),
            'pysoa': six.text_type(pysoa.__version__),
            'python': six.text_type(platform.python_version()),
            'version': self._version,
        }

        if self._build:
            status['build'] = self._build

        if not request.body or request.body.get('verbose', True) is True:
            errors = []
            warnings = []
            self.diagnostics = {}

            # Find all things called "check_<something>" on this class.
            # We can't just scan __dict__ because of class inheritance.
            check_methods = [
                getattr(self, x) for x in dir(self) if x.startswith('check_')
            ]
            for check_method in check_methods:
                # Call the check, and see if it returned anything
                try:
                    problems = check_method(request)
                except TypeError as e:
                    raise RuntimeError(
                        'Status action check_* methods must accept a single argument of type ActionRequest',
                        e,
                    )
                if problems:
                    for is_error, code, description in problems:
                        # Parcel out the values into the right return list
                        if is_error:
                            errors.append((code, description))
                        else:
                            warnings.append((code, description))

            status['healthcheck'] = {
                'errors': errors,
                'warnings': warnings,
                'diagnostics': self.diagnostics,
            }

        return status

    def _check_client_settings(self, request):
        """
        This method checks any client settings configured for this service to call other services, calls the `status`
        action of each configured service with `verbose: False` (which guarantees no further recursive status checking),
        adds that diagnostic information, and reports any problems. To include this check in your status action, define
        `check_client_settings = BaseStatusAction._check_client_settings` in your status action class definition.
        """
        if not request.client.settings:
            # There's no need to even add diagnostic details if no client settings are configured
            return

        self.diagnostics['services'] = {}

        service_names = list(six.iterkeys(request.client.settings))
        try:
            job_responses = request.client.call_jobs_parallel(
                [{
                    'service_name': service_name,
                    'actions': [{
                        'action': 'status',
                        'body': {
                            'verbose': False
                        }
                    }]
                } for service_name in service_names],
                timeout=2,
                catch_transport_errors=True,
                raise_action_errors=False,
                raise_job_errors=False,
            )
        except Exception as e:
            return [(True, 'CHECK_SERVICES_UNKNOWN_ERROR', six.text_type(e))]

        problems = []
        for i, service_name in enumerate(service_names):
            response = job_responses[i]
            if isinstance(response, Exception):
                problems.append((True, '{}_TRANSPORT_ERROR'.format(
                    service_name.upper()), six.text_type(response)), )
            elif response.errors:
                problems.append((True, '{}_CALL_ERROR'.format(
                    service_name.upper()), six.text_type(response.errors)), )
            elif response.actions[0].errors:
                problems.append(
                    (True, '{}_STATUS_ERROR'.format(service_name.upper()),
                     six.text_type(response.actions[0].errors)), )
            else:
                self.diagnostics['services'][service_name] = response.actions[
                    0].body

        return problems
Beispiel #27
0
 fields.Integer(
     description=
     'The port number on which the Dogstatsd server is listening'),
 'maximum_packet_size':
 fields.Integer(
     description=
     'The maximum packet size to send (packets will be fragmented above this limit), defaults to '
     '8000 bytes.', ),
 'network_timeout':
 fields.Any(fields.Float(gt=0.0),
            fields.Integer(gt=0),
            description='The network timeout'),
 'global_tags':
 fields.SchemalessDictionary(
     key_type=fields.UnicodeString(),
     value_type=_datadog_tags_value_type,
     description='Datadog tags to apply to all published metrics.',
 ),
 'extra_gauge_tags':
 fields.SchemalessDictionary(
     key_type=fields.UnicodeString(),
     value_type=_datadog_tags_value_type,
     description=
     'Extra datadog tags, in addition to `global_tags` if applicable, to apply to all published '
     'gauges. This is necessary when multiple processes are simultaneously publishing gauges with '
     'the same name and you need to create charts or monitors that sum the values of all of these '
     'gauges across all processes (because Datadog does not support identical distributed gauge '
     'names+tags and will eliminate duplicates).',
 ),
 'use_distributions':
 fields.Boolean(