Пример #1
0
def init_plugins(tm_env):
    """Inits all plugins.
    """
    for app_type in appcfg.AppType:
        namespace = _FS_PLUGIN_NAMESPACE.format(app_type.value)
        for plugin in plugin_manager.load_all(namespace):
            plugin(tm_env).init()
Пример #2
0
def list_logging_conf():
    """List all defined logging configurations."""
    import pkg_resources

    configs = set()
    for plugin in plugin_manager.load_all(__name__):
        configs.update({
            cfg for cfg in pkg_resources.resource_listdir(__name__, '.')
            if cfg.endswith('.json')
        })

    return configs
Пример #3
0
def base_api(title=None, cors_origin=None):
    """Create base_api object"""
    blueprint = flask.Blueprint('v1', __name__)

    api = restplus.Api(blueprint,
                       version='1.0',
                       title=title,
                       description='Treadmill REST API Documentation')

    error_handlers.register(api)

    # load up any external error_handlers
    for module in plugin_manager.load_all('treadmill.rest.error_handlers'):
        module.init(api)

    @blueprint.route('/docs/', endpoint='docs')
    def _swagger_ui():
        """Swagger documentation route"""
        return restplus.apidoc.ui_for(api)

    rest.FLASK_APP.register_blueprint(blueprint)
    rest.FLASK_APP.register_blueprint(restplus.apidoc.apidoc)

    cors = webutils.cors(origin=cors_origin,
                         content_type='application/json',
                         credentials=True)

    @rest.FLASK_APP.before_request
    def _before_request_user_handler():
        user = flask.request.environ.get('REMOTE_USER')
        if user:
            flask.g.user = user

    @rest.FLASK_APP.after_request
    def _after_request_cors_handler(response):
        """Process all OPTIONS request, thus don't need to add to each app"""
        if flask.request.method != 'OPTIONS':
            return response

        _LOGGER.debug('This is an OPTIONS call')

        def _noop_options():
            """No noop response handler for all OPTIONS.
            """

        headers = flask.request.headers.get('Access-Control-Request-Headers')
        options_cors = webutils.cors(origin=cors_origin,
                                     credentials=True,
                                     headers=headers)
        response = options_cors(_noop_options)()
        return response

    return (api, cors)
Пример #4
0
def load_logging_conf(name):
    """load plugin log conf from various modules
    """
    # load core logging config first
    conf = _load_logging_file(__name__, name)

    # then load plugin component config
    for plugin in plugin_manager.load_all(__name__):
        plugin_conf = _load_logging_file(plugin.__name__, name)

        # TODO: deep merge conf
        conf['loggers'].update(plugin_conf.get('loggers', {}))

    return conf
Пример #5
0
def detect():
    """Detect traits usign plugins.
    """
    result = []

    plugins = plugin_manager.load_all('treadmill.server.traits')

    for plugin in plugins:
        try:
            result.extend(plugin())
        except Exception:  # pylint: disable=W0703
            _LOGGER.exception('Error processing plugin: %s', plugin)

    return result
Пример #6
0
def load_logging_conf(name):
    """load plugin log conf from various modules
    """
    # Shortcut - check if logging already exists.
    logconf_path = os.path.join(
        os.environ.get('TREADMILL_APPROOT', ''),
        'logging',
        name
    )

    if os.path.exists(logconf_path):
        with io.open(logconf_path) as f:
            return json.loads(f.read())

    # load core logging config first
    conf = _load_logging_file(__name__, name)
    # get 'treadmill' default log configure
    default_conf = conf['loggers'].get(_package_root(__name__), {})

    # then load plugin component config
    import pkg_resources

    for plugin in plugin_manager.load_all(__name__):

        contents = pkg_resources.resource_listdir(__name__, '.')
        try:
            plugin_conf = _load_logging_file(plugin.__name__, name)

            # TODO: deep merge conf
            conf['loggers'].update(plugin_conf.get('loggers', {}))
        except FileNotFoundError as _e:
            # it is possible for module to be lack of specific log file
            # e.g. some module does not have daemon logging configuration
            # we use default configure for it
            plugin_package_root_name = _package_root(plugin.__name__)
            conf['loggers'][plugin_package_root_name] = default_conf

    return conf
Пример #7
0
def load_logging_conf(name):
    """load plugin log conf from various modules
    """
    # load core logging config first
    conf = _load_logging_file(__name__, name)
    # get 'treadmill' default log configure
    default_conf = conf['loggers'].get(_package_root(__name__), {})

    # then load plugin component config
    for plugin in plugin_manager.load_all(__name__):
        try:
            plugin_conf = _load_logging_file(plugin.__name__, name)

            # TODO: deep merge conf
            conf['loggers'].update(plugin_conf.get('loggers', {}))
        except FileNotFoundError as _e:
            # it is possible for module to be lack of specific log file
            # e.g. some module does not have daemon logging configuration
            # we use default configure for it
            plugin_package_root_name = _package_root(plugin.__name__)
            conf['loggers'][plugin_package_root_name] = default_conf

    return conf
Пример #8
0
def configure_plugins(tm_env, container_dir, app):
    """Configures all plugins.
    """
    namespace = _FS_PLUGIN_NAMESPACE.format(app.type)
    for plugin in plugin_manager.load_all(namespace):
        plugin(tm_env).configure(container_dir, app)
Пример #9
0
def base_api(title=None, cors_origin=None):
    """Create base_api object"""
    blueprint = flask.Blueprint('v1', __name__)

    api = restplus.Api(blueprint,
                       version='1.0',
                       title=title,
                       description='Treadmill REST API Documentation')

    error_handlers.register(api)

    # load up any external error_handlers
    for module in plugin_manager.load_all('treadmill.rest.error_handlers'):
        module.init(api)

    @blueprint.route('/docs/', endpoint='docs')
    def _swagger_ui():
        """Swagger documentation route"""
        return restplus.apidoc.ui_for(api)

    # Need to create our own Apidoc, as the restplus one uses relative path to
    # their module to serve up the content for the Swagger UI.
    tmpl_dir = pkg_resources.resource_filename('flask_restplus', 'templates')
    static_dir = pkg_resources.resource_filename('flask_restplus', 'static')

    # This is a hack that overrides all templates and static folders for our
    # Flask app, but as it stands, only flask-restplus is using
    # render_template, so this is fine for now.
    # The main problem is that restplus.Api() internally refers to it's
    # restplus.apidoc.apidoc and that is created on load time, which all kinds
    # of Flask rule and defering going on. Ideally restplus should allow
    # creating your own Apidoc and sending that in the above Api() constructor.
    rest.FLASK_APP.template_folder = tmpl_dir
    rest.FLASK_APP.static_folder = static_dir

    rest.FLASK_APP.register_blueprint(blueprint)
    rest.FLASK_APP.register_blueprint(restplus.apidoc.apidoc)

    cors = webutils.cors(origin=cors_origin,
                         content_type='application/json',
                         credentials=True)

    @rest.FLASK_APP.before_request
    def _before_request_user_handler():
        user = flask.request.environ.get('REMOTE_USER')
        if user:
            flask.g.user = user

    @rest.FLASK_APP.after_request
    def _after_request_cors_handler(response):
        """Process all OPTIONS request, thus don't need to add to each app"""
        if flask.request.method != 'OPTIONS':
            return response

        _LOGGER.debug('This is an OPTIONS call')

        def _noop_options():
            """No noop response handler for all OPTIONS"""
            pass

        headers = flask.request.headers.get('Access-Control-Request-Headers')
        options_cors = webutils.cors(origin=cors_origin,
                                     credentials=True,
                                     headers=headers)
        response = options_cors(_noop_options)()
        return response

    return (api, cors)
Пример #10
0
def cleanup(tm_env, app, container_dir):
    """Cleanup all plugins.
    """
    for hook in plugin_manager.load_all(_PLUGINS_NS):
        _LOGGER.info('Initializing plugin %r.', hook)
        hook(tm_env).cleanup(app, container_dir)
Пример #11
0
def configure(tm_env, app, container_dir):
    """Configures all plugins.
    """
    for hook in plugin_manager.load_all(_PLUGINS_NS):
        _LOGGER.info('Configuring plugin %r.', hook)
        hook(tm_env).configure(app, container_dir)
Пример #12
0
def init(tm_env):
    """Inits all plugins.
    """
    for hook in plugin_manager.load_all(_PLUGINS_NS):
        _LOGGER.info('Initializing plugin %r.', hook)
        hook(tm_env).init()
Пример #13
0
def plugins(app):
    """Load and return all filesystem plugins for the app type.
    """
    namespace = _FS_PLUGIN_NAMESPACE.format(app.type)
    return plugin_manager.load_all(namespace)