Beispiel #1
0
def get_plugin_instance(namespace, name, invoke_on_load=True):
    """Return class instance for the provided plugin name and namespace.
    """
    from stevedore.driver import DriverManager

    manager = DriverManager(namespace=namespace, name=name, invoke_on_load=invoke_on_load)
    return manager.driver
def get_backend_instance(name):
    """
    :param name: Backend name.
    :type name: ``str``
    """
    try:
        manager = DriverManager(namespace=BACKENDS_NAMESPACE,
                                name=name,
                                invoke_on_load=False)
    except RuntimeError:
        message = 'Invalid authentication backend specified: %s' % (name)
        LOG.exception(message)
        raise ValueError(message)

    backend_kwargs = cfg.CONF.auth.backend_kwargs

    if backend_kwargs:
        try:
            kwargs = json.loads(backend_kwargs)
        except ValueError as e:
            raise ValueError('Failed to JSON parse backend settings: %s' %
                             (str(e)))
    else:
        kwargs = {}

    cls = manager.driver
    cls_instance = cls(**kwargs)
    return cls_instance
Beispiel #3
0
    def test_load_driver_with_external_opts(self, m_driver_manager):
        m_driver_manager.return_value = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverWithExternalOpts.get_name(),
                entry_point="%s:%s" % (FakeDriverWithExternalOpts.__module__,
                                       FakeDriverWithExternalOpts.__name__),
                plugin=FakeDriverWithExternalOpts,
                obj=None,
            ),
            namespace=FakeDriverWithExternalOpts.namespace(),
        )

        loader_manager = DriverLoader(
            conf=cfg.CONF,
            namespace='TESTING',
            name='fake_with_ext_opts'
        )
        loaded_driver = loader_manager.load()

        self.assertEqual(
            hasattr(loaded_driver, "fake__test_external_opt"),
            True
        )
        self.assertEqual(
            loaded_driver.fake__test_external_opt,
            "fake_with_ext_opts"
        )
Beispiel #4
0
def run():
	"""Example application using pangadfs"""
	logging.basicConfig(level=logging.INFO)

	ctx = {
		'ga_settings': {
			'crossover_method': 'uniform',
			'csvpth': Path(__file__).parent / 'appdata' / 'pool.csv',
			'elite_divisor': 5,
			'elite_method': 'fittest',
			'mutation_rate': .05,
			'n_generations': 20,
			'points_column': 'proj',
			'population_size': 30000,
			'position_column': 'pos',
			'salary_column': 'salary',
			'select_method': 'roulette',
			'stop_criteria': 10,
			'verbose': True
		},

		'site_settings': {
			'flex_positions': ('RB', 'WR', 'TE'),
			'lineup_size': 9,
			'posfilter': {'QB': 14, 'RB': 8, 'WR': 8, 'TE': 5, 'DST': 4, 'FLEX': 8},
			'posmap': {'DST': 1, 'QB': 1, 'TE': 1, 'RB': 2, 'WR': 3, 'FLEX': 7},
			'salary_cap': 50000
		}
	}

	# set up driver managers
	dmgrs = {}
	emgrs = {}
	for ns in GeneticAlgorithm.PLUGIN_NAMESPACES:
		pns = f'pangadfs.{ns}'
		if ns == 'validate':
			emgrs['validate'] = NamedExtensionManager(
				namespace=pns, 
				names=['validate_salary', 'validate_duplicates'], 
				invoke_on_load=True, 
				name_order=True)
		else:
			dmgrs[ns] = DriverManager(
				namespace=pns, 
				name=f'{ns}_default', 
				invoke_on_load=True)
	
	
	# set up GeneticAlgorithm object
	ga = GeneticAlgorithm(ctx=ctx, driver_managers=dmgrs, extension_managers=emgrs)
	
	# run optimizer
	results = ga.optimize()

	# show best score and lineup at conclusion
	# will break after n_generations or when stop_criteria reached
	print(results['best_lineup'])
	print(f'Lineup score: {results["best_score"]}')
Beispiel #5
0
def get_driver(name, invoke_on_load=False):
    """
    Retrieve a driver
    """
    from stevedore.driver import DriverManager
    manager = DriverManager(namespace=NAMESPACE,
                            name=name,
                            invoke_on_load=invoke_on_load)
    return manager.driver
Beispiel #6
0
    def load(self, name, **kwargs):
        try:
            LOG.debug("Loading in namespace %s => %s ", self.namespace, name)
            driver_manager = DriverManager(namespace=self.namespace, name=name)
            loaded = driver_manager.driver
        except Exception as exc:
            LOG.exception(exc)
            raise exception.LoadingError(name=name)

        return loaded(**kwargs)
Beispiel #7
0
def get_backend_instance(name):
    """
    Return a class instance for the provided runner name.
    """
    from stevedore.driver import DriverManager

    manager = DriverManager(namespace=BACKENDS_NAMESPACE,
                            name=name,
                            invoke_on_load=False)
    return manager.driver
def get_provider(name, invoke_args=(), invoke_kwds={}):
    """
    Get a provider based on the self.provider_name propery
    """
    mgr = DriverManager(NAMESPACE,
                        name,
                        invoke_on_load=True,
                        invoke_args=invoke_args,
                        invoke_kwds=invoke_kwds)
    return mgr.driver
Beispiel #9
0
def format(filename, env=None):
    """
    Format a file

    :param filename: Path to file
    :type filename: str or :py:class:`pathlib.Path`
    :param dict[str,str] env: List of variables
    """
    ext = str(filename).split('.')[-1]
    driver = DriverManager('eastern.formatter', ext)
    driver.propagate_map_exceptions = True
    env = env or {}

    if isinstance(filename, Path):
        file_obj = filename
    else:
        file_obj = Path(filename)

    body = file_obj.read_text()
    return driver(lambda ext: ext.plugin(body, file_obj.parent, env).format())
Beispiel #10
0
    def setUp(self):
        super(TestDriverLoader, self).setUp()
        # To load the drivers without using the config file
        self.useFixture(ConfFixture())

        def _fake_parse(self, *args, **kw):
            return cfg.ConfigOpts._parse_cli_opts(cfg.CONF, [])

        cfg.CONF._parse_cli_opts = _fake_parse

        # First dependency to be returned
        self.no_group_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverNoGroup.get_name(),
                entry_point="%s:%s" %
                (FakeDriverNoGroup.__module__, FakeDriverNoGroup.__name__),
                plugin=FakeDriverNoGroup,
                obj=None,
            ),
            namespace=FakeDriverNoGroup.namespace(),
        )

        # 2nd dependency to be returned
        self.with_ext_opts_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverWithExternalOpts.get_name(),
                entry_point="%s:%s" % (FakeDriverWithExternalOpts.__module__,
                                       FakeDriverWithExternalOpts.__name__),
                plugin=FakeDriverWithExternalOpts,
                obj=None,
            ),
            namespace=FakeDriverWithExternalOpts.namespace(),
        )

        self.patches.extend([
            # patch.object(cfg, "ConfigOpts", ),
        ])

        # Applies all of our patches before each test
        for _patch in self.patches:
            _patch.start()
Beispiel #11
0
    def setUp(self):
        super(TestDriverLoader, self).setUp()
        # To load the drivers without using the config file
        self.useFixture(ConfFixture())

        def _fake_parse(self, *args, **kw):
            return cfg.ConfigOpts._parse_cli_opts(cfg.CONF, [])

        cfg.CONF._parse_cli_opts = _fake_parse

        # First dependency to be returned
        self.no_group_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverNoGroup.get_name(),
                entry_point="%s:%s" % (FakeDriverNoGroup.__module__,
                                       FakeDriverNoGroup.__name__),
                plugin=FakeDriverNoGroup,
                obj=None,
            ),
            namespace=FakeDriverNoGroup.namespace(),
        )

        # 2nd dependency to be returned
        self.with_ext_opts_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverWithExternalOpts.get_name(),
                entry_point="%s:%s" % (FakeDriverWithExternalOpts.__module__,
                                       FakeDriverWithExternalOpts.__name__),
                plugin=FakeDriverWithExternalOpts,
                obj=None,
            ),
            namespace=FakeDriverWithExternalOpts.namespace(),
        )

        self.patches.extend([
            # patch.object(cfg, "ConfigOpts", ),
        ])

        # Applies all of our patches before each test
        for _patch in self.patches:
            _patch.start()
Beispiel #12
0
    def test_load_driver_no_opt(self, m_driver_manager):
        m_driver_manager.return_value = DriverManager.make_test_instance(
            extension=Extension(name=FakeLoadable.get_name(),
                                entry_point="%s:%s" % (
                                FakeLoadable.__module__,
                                FakeLoadable.__name__),
                                plugin=FakeLoadable,
                                obj=None),
            namespace=FakeLoadable.namespace())

        loader_manager = DefaultLoader(namespace='TESTING')
        loaded_driver = loader_manager.load(name='fake')

        self.assertEqual(loaded_driver.get_name(), FakeLoadable.get_name())
Beispiel #13
0
    def test_load_driver_no_opt(self, m_driver_manager):
        m_driver_manager.return_value = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeLoadable.get_name(),
                entry_point="%s:%s" %
                (FakeLoadable.__module__, FakeLoadable.__name__),
                plugin=FakeLoadable,
                obj=None),
            namespace=FakeLoadable.namespace())

        loader_manager = DefaultLoader(namespace='TESTING')
        loaded_driver = loader_manager.load(name='fake')

        self.assertEqual(loaded_driver.get_name(), FakeLoadable.get_name())
Beispiel #14
0
 def _load_plugins(self):
     """Loads default plugins for any namespace that doesn't have a plugin"""
     for ns in self.PLUGIN_NAMESPACES:
         if ns not in self.driver_managers and ns not in self.extension_managers:
             if ns == 'validate':
                 self.extension_managers[ns] = NamedExtensionManager(
                     namespace='pangadfs.validate',
                     names=self.VALIDATE_PLUGINS,
                     invoke_on_load=True,
                     name_order=True)
             else:
                 mgr = DriverManager(namespace=f'pangadfs.{ns}',
                                     name=f'{ns}_default',
                                     invoke_on_load=True)
                 self.driver_managers[ns] = mgr
Beispiel #15
0
    def parse_line(self, line):
        if '#' not in line:
            return line

        line = self.plugin.chain('line_pre_hook', line, formatter=self)
        before, after = line.split('#', 1)

        # line must only have precending spaces
        if not re.match(r'^\s*$', before):
            return line

        splitted = after.strip().split(' ', 1)
        command = splitted[0]
        args = []

        if len(splitted) > 1:
            args = splitted[1]

        try:
            func = DriverManager(DRIVER_NS, command)
            func.propagate_map_exceptions = True
        except NoMatches:
            self.logger.debug('Command not found %s', command, exc_info=True)
            return line

        output = func(lambda ext: ext.plugin(args, line=line, formatter=self))

        if output is None:
            output = []
        elif isinstance(output, str):
            output = output.split(os.linesep)

        output = os.linesep.join([before + item for item in output])

        output = self.plugin.chain('line_post_hook', output, formatter=self)
        return output
Beispiel #16
0
def get_backend_instance(name):
    """
    Retrieve a class instance for the provided auth backend.

    :param name: Backend name.
    :type name: ``str``
    """
    from stevedore.driver import DriverManager

    LOG.debug('Retrieving backend instance for backend "%s"' % (name))

    try:
        manager = DriverManager(namespace=BACKENDS_NAMESPACE,
                                name=name,
                                invoke_on_load=False)
    except RuntimeError:
        message = 'Invalid authentication backend specified: %s' % (name)
        LOG.exception(message)
        raise ValueError(message)

    backend_kwargs = cfg.CONF.auth.backend_kwargs

    if backend_kwargs:
        try:
            kwargs = json.loads(backend_kwargs)
        except ValueError as e:
            raise ValueError(
                'Failed to JSON parse backend settings for backend "%s": %s' %
                (name, str(e)))
    else:
        kwargs = {}

    cls = manager.driver

    try:
        cls_instance = cls(**kwargs)
    except Exception as e:
        tb_msg = traceback.format_exc()
        class_name = cls.__name__
        msg = (
            'Failed to instantiate auth backend "%s" (class %s) with backend settings '
            '"%s": %s' % (name, class_name, str(kwargs), str(e)))
        msg += '\n\n' + tb_msg
        exc_cls = type(e)
        raise exc_cls(msg)

    return cls_instance
Beispiel #17
0
    def test_load_driver_no_opt(self, m_driver_manager):
        m_driver_manager.return_value = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverNoOpt.get_name(),
                entry_point="%s:%s" %
                (FakeDriverNoOpt.__module__, FakeDriverNoOpt.__name__),
                plugin=FakeDriverNoOpt,
                obj=None,
            ),
            namespace=FakeDriverNoOpt.namespace(),
        )

        loader_manager = DriverLoader(conf=cfg.CONF,
                                      namespace='TESTING',
                                      name='fake_no_opt')
        loaded_driver = loader_manager.load()

        self.assertEqual(isinstance(loaded_driver, FakeDriverNoOpt), True)
Beispiel #18
0
    def load(self):
        driver_manager = DriverManager(
            namespace=self.namespace,
            name=self.name,
            invoke_on_load=False,
        )
        store_client_driver = driver_manager.driver

        internal_options = self._load_internal_config(store_client_driver)
        external_options = self._load_external_config(store_client_driver)

        opts = {}
        opts.update(external_options)
        opts.update(internal_options)

        client = store_client_driver(**opts)

        return client
Beispiel #19
0
    def test_load_driver_with_opts(self, m_driver_manager):
        m_driver_manager.return_value = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverWithOpts.get_name(),
                entry_point="%s:%s" %
                (FakeDriverWithOpts.__module__, FakeDriverWithOpts.__name__),
                plugin=FakeDriverWithOpts,
                obj=None,
            ),
            namespace=FakeDriverWithOpts.namespace(),
        )

        loader_manager = DriverLoader(conf=cfg.CONF,
                                      namespace='TESTING',
                                      name='fake_with_opts')
        loaded_driver = loader_manager.load()

        self.assertEqual(hasattr(loaded_driver, "test_opt"), True)
        self.assertEqual(loaded_driver.test_opt, "fake_with_opts")
Beispiel #20
0
def register_runners(experimental=False, fail_on_failure=True):
    """
    Register runners
    """
    LOG.debug('Start : register runners')
    runner_count = 0

    manager = ExtensionManager(namespace=RUNNERS_NAMESPACE, invoke_on_load=False)
    extension_names = manager.names()

    for name in extension_names:
        LOG.debug('Found runner "%s"' % (name))

        manager = DriverManager(namespace=RUNNERS_NAMESPACE, invoke_on_load=False, name=name)
        runner_metadata = manager.driver.get_metadata()
        runner_count += register_runner(runner_metadata, experimental)

    LOG.debug('End : register runners')

    return runner_count
Beispiel #21
0
    def test_load_driver_no_group(self, m_driver_manager):
        m_driver_manager.return_value = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverNoGroup.get_name(),
                entry_point="%s:%s" % (FakeDriverNoGroup.__module__,
                                       FakeDriverNoGroup.__name__),
                plugin=FakeDriverNoGroup,
                obj=None,
            ),
            namespace=FakeDriverNoGroup.namespace(),
        )

        loader_manager = DriverLoader(
            conf=cfg.CONF,
            namespace='',
            name='fake_no_group'
        )
        loaded_driver = loader_manager.load()

        self.assertEqual(hasattr(loaded_driver, "test_opt"), True)
        self.assertEqual(loaded_driver.test_opt, "fake_no_group")
Beispiel #22
0
    def test_load_driver_no_opt(self, m_driver_manager):
        m_driver_manager.return_value = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeDriverNoOpt.get_name(),
                entry_point="%s:%s" % (FakeDriverNoOpt.__module__,
                                       FakeDriverNoOpt.__name__),
                plugin=FakeDriverNoOpt,
                obj=None,
            ),
            namespace=FakeDriverNoOpt.namespace(),
        )

        loader_manager = DriverLoader(
            conf=cfg.CONF,
            namespace='TESTING',
            name='fake_no_opt'
        )
        loaded_driver = loader_manager.load()

        self.assertEqual(
            isinstance(loaded_driver, FakeDriverNoOpt),
            True
        )
Beispiel #23
0
def get_backend_driver(namespace, name, invoke_on_load=False):
    """
    Retrieve a driver (module / class / function) the provided backend.

    :param name: Backend name.
    :type name: ``str``
    """
    # NOTE: We use lazy import because importing from stevedore adds significat import time
    # overhead to other modules which don't need this package (stevedore needs to inspect various
    # entrypoint files on disk for all the installed Python packages which is slow)
    from stevedore.driver import DriverManager

    LOG.debug('Retrieving driver for backend "%s"' % (name))

    try:
        manager = DriverManager(namespace=namespace, name=name,
                                invoke_on_load=invoke_on_load)
    except RuntimeError:
        message = 'Invalid "%s" backend specified: %s' % (namespace, name)
        LOG.exception(message)
        raise ValueError(message)

    return manager.driver
def base(event, context, passthrough=False):
    # Set up logging
    logger = logging.getLogger()

    # Read DEBUG value from the environment variable
    debug = os.environ.get('ST2_DEBUG', False)
    if str(debug).lower() in ['true', '1']:
        debug = True

    if debug:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    if isinstance(event, basestring):
        try:
            event = json.loads(event)
        except ValueError as e:
            LOG.error("ERROR: Can not parse `event`: '{}'\n{}".format(
                str(event), str(e)))
            raise e

    LOG.info("Received event: " + json.dumps(event, indent=2))

    # Special case for Lambda function being called over HTTP via API gateway
    # See
    # https://serverless.com/framework/docs/providers/aws/events/apigateway
    # #example-lambda-proxy-event-default
    # for details
    is_event_body_string = (isinstance(event.get('body'), basestring) is True)
    content_type = event.get('headers', {}).get('content-type', '').lower()

    if is_event_body_string:
        if content_type == 'application/json':
            try:
                event['body'] = json.loads(event['body'])
            except Exception as e:
                LOG.warn('`event` has `body` which is not JSON: %s',
                         str(e.message))
        elif content_type == 'application/x-www-form-urlencoded':
            try:
                event['body'] = dict(
                    parse_qsl(['body'], keep_blank_values=True))
            except Exception as e:
                LOG.warn('`event` has `body` which is not `%s`: %s',
                         content_type, str(e.message))
        else:
            LOG.warn('Unsupported event content type: %s' % (content_type))

    action_name = os.environ['ST2_ACTION']
    try:
        action_db = ACTIONS[action_name]
    except KeyError:
        raise ValueError('No action named "%s" has been installed.' %
                         (action_name))

    manager = DriverManager(namespace='st2common.runners.runner',
                            invoke_on_load=False,
                            name=action_db.runner_type['name'])
    runnertype_db = RunnerTypeAPI.to_model(
        RunnerTypeAPI(**manager.driver.get_metadata()[0]))

    if passthrough:
        runner = PassthroughRunner()
    else:
        runner = manager.driver.get_runner()

    runner._sandbox = False
    runner.runner_type_db = runnertype_db
    runner.action = action_db
    runner.action_name = action_db.name
    # runner.liveaction = liveaction_db
    # runner.liveaction_id = str(liveaction_db.id)
    # runner.execution = ActionExecution.get(liveaction__id=runner.liveaction_id)
    # runner.execution_id = str(runner.execution.id)
    runner.entry_point = content_utils.get_entry_point_abs_path(
        pack=action_db.pack, entry_point=action_db.entry_point)
    runner.context = {}  # getattr(liveaction_db, 'context', dict())
    # runner.callback = getattr(liveaction_db, 'callback', dict())
    runner.libs_dir_path = content_utils.get_action_libs_abs_path(
        pack=action_db.pack, entry_point=action_db.entry_point)

    # For re-run, get the ActionExecutionDB in which the re-run is based on.
    # rerun_ref_id = runner.context.get('re-run', {}).get('ref')
    # runner.rerun_ex_ref = ActionExecution.get(id=rerun_ref_id) if rerun_ref_id else None

    config_schema = CONFIG_SCHEMAS.get(action_db.pack, None)
    config_values = os.environ.get('ST2_CONFIG', None)
    if config_schema and config_values:
        runner._config = validate_config_against_schema(
            config_schema=config_schema,
            config_object=json.loads(config_values),
            config_path=None,
            pack_name=action_db.pack)

    param_values = os.environ.get('ST2_PARAMETERS', None)
    try:
        if param_values:
            live_params = param_utils.render_live_params(
                runner_parameters=runnertype_db.runner_parameters,
                action_parameters=action_db.parameters,
                params=json.loads(param_values),
                action_context={},
                additional_contexts={'input': event})
        else:
            live_params = event

        if debug and 'log_level' not in live_params:
            # Set log_level runner parameter
            live_params['log_level'] = 'DEBUG'

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters=runnertype_db.runner_parameters,
            action_parameters=action_db.parameters,
            params=live_params,
            action_context={})
    except ParamException as e:
        raise actionrunner.ActionRunnerException(str(e))

    runner.runner_parameters = runner_params

    LOG.debug('Performing pre-run for runner: %s', runner.runner_id)
    runner.pre_run()

    (status, output, context) = runner.run(action_params)

    output_values = os.environ.get('ST2_OUTPUT', None)
    if output_values:
        try:
            result = param_utils.render_live_params(
                runner_parameters=runnertype_db.runner_parameters,
                action_parameters=action_db.parameters,
                params=json.loads(output_values),
                action_context={},
                additional_contexts={
                    'input': event,
                    'output': output
                })
        except ParamException as e:
            raise actionrunner.ActionRunnerException(str(e))
    else:
        result = output

    # Log the logs generated by the action. We do that so the actual action logs
    # (action stderr) end up in CloudWatch
    output = output or {}

    if output.get('stdout', None):
        LOG.info('Action stdout: %s' % (output['stdout']))

    if output.get('stderr', None):
        LOG.info('Action stderr and logs: %s' % (output['stderr']))

    return {
        'event': event,
        'live_params': live_params,
        'output': output,
        'result': result
    }
Beispiel #25
0
def main():
    extensions: ExtensionManager = DriverManager('pytimed', 'yaml', invoke_on_load=True)

    for ext in extensions:
        print(ext.obj)
Beispiel #26
0
def main():
    # Read DEBUG value from the environment variable
    debug = os.environ.get('ST2_DEBUG', False)
    if str(debug).lower() in ['true', '1']:
        debug = True

    if debug:
        LOG.setLevel(logging.DEBUG)
    else:
        LOG.setLevel(logging.INFO)

    # Read
    input = os.environ.get('ST2_INPUT', {})
    if isinstance(input, six.string_types):
        try:
            input = json.loads(input)
        except ValueError as e:
            LOG.error("ERROR: Can not parse `input`: '{}'\n{}".format(str(input), str(e)))
            raise e

    LOG.debug("Received input: " + json.dumps(input, indent=2))

    # Read action name from environment variable
    action_name = os.environ['ST2_ACTION']
    try:
        action_db = ACTIONS[action_name]
    except KeyError:
        raise ValueError('No action named "%s" has been installed.' % (action_name))

    # Initialize runner
    manager = DriverManager(namespace='st2common.runners.runner', invoke_on_load=False,
                            name=action_db.runner_type['name'])

    runnertype_db = RunnerTypeAPI.to_model(RunnerTypeAPI(**manager.driver.get_metadata()))

    runner = manager.driver.get_runner()

    runner._sandbox = False
    runner.runner_type_db = runnertype_db
    runner.action = action_db
    runner.action_name = action_db.name
    runner.entry_point = content_utils.get_entry_point_abs_path(pack=action_db.pack,
        entry_point=action_db.entry_point)
    runner.context = {}
    runner.libs_dir_path = content_utils.get_action_libs_abs_path(pack=action_db.pack,
        entry_point=action_db.entry_point)

    config_schema = CONFIG_SCHEMAS.get(action_db.pack, None)
    config_values = os.environ.get('ST2_CONFIG', None)
    if config_schema and config_values:
        runner._config = validate_config_against_schema(config_schema=config_schema,
                                                        config_object=json.loads(config_values),
                                                        config_path=None,
                                                        pack_name=action_db.pack)

    param_values = os.environ.get('ST2_PARAMETERS', None)
    try:
        if param_values:
            live_params = param_utils.render_live_params(
                runner_parameters=runnertype_db.runner_parameters,
                action_parameters=action_db.parameters,
                params=json.loads(param_values),
                action_context={},
                additional_contexts={
                    'input': input
                })
        else:
            live_params = input

        if debug and 'log_level' not in live_params:
            # Set log_level runner parameter
            live_params['log_level'] = 'DEBUG'

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters=runnertype_db.runner_parameters,
            action_parameters=action_db.parameters,
            params=live_params,
            action_context={})
    except ParamException as e:
        raise actionrunner.ActionRunnerException(str(e))

    runner.runner_parameters = runner_params


    LOG.debug('Performing pre-run for runner: %s', runner.runner_id)
    runner.pre_run()

    (status, output, context) = runner.run(action_params)

    try:
        output['result'] = json.loads(output['result'])
    except Exception:
        pass

    output_values = os.environ.get('ST2_OUTPUT', None)
    if output_values:
        try:
            result = param_utils.render_live_params(
                runner_parameters={},
                action_parameters={},
                params=json.loads(output_values),
                action_context={},
                additional_contexts={
                    'input': input,
                    'output': output
                })
        except ParamException as e:
            raise actionrunner.ActionRunnerException(str(e))
    else:
        result = output

    output = output or {}

    if output.get('stdout', None):
        LOG.info('Action stdout: %s' % (output['stdout']))

    if output.get('stderr', None):
        LOG.info('Action stderr and logs: %s' % (output['stderr']))

    print(json.dumps(result))
Beispiel #27
0
    def setUp(self):
        super(TestAgent, self).setUp()
        self.conf = cfg.ConfigOpts()
        # To load the drivers without using the config file
        self.useFixture(ConfFixture(self.conf))

        def _fake_parse(self, args=[]):
            return cfg.ConfigOpts._parse_cli_opts(self, [])

        _fake_parse_method = types.MethodType(_fake_parse, self.conf)
        self.conf._parse_cli_opts = _fake_parse_method

        # First dependency to be returned
        self.dummy_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=DummyMetricPuller.get_name(),
                entry_point='fake.entry.point',
                plugin=DummyMetricPuller,
                obj=None,
            ),
            namespace='TESTING',
        )
        # 2nd dependency to be returned
        self.fake_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeMetricPuller.get_name(),
                entry_point='fake.entry.point',
                plugin=FakeMetricPuller,
                obj=None,
            ),
            namespace='TESTING',
        )
        self.defaults_drivers = {
            DummyMetricPuller.get_name(): self.dummy_driver_manager,
            FakeMetricPuller.get_name(): self.fake_driver_manager,
        }

        def _fake_loader(name, **kw):
            return self.defaults_drivers[name]

        # Patches the agent socket
        self.m_agent_socket = MagicMock(autospec=True)

        self.patches.extend([
            # Deactivates the nanomsg socket
            patch(
                "watcher_metering.agent.agent.nanomsg.Socket",
                new=self.m_agent_socket,
            ),
            # Sets the test namespace to 'TESTING'
            patch.object(
                Agent,
                "namespace",
                PropertyMock(return_value='TESTING'),
            ),
            # Patches the driver manager to retourn our test drivers
            # instead of the real ones
            patch(
                "watcher_metering.load.loader.DriverManager",
                MagicMock(side_effect=_fake_loader),
            ),
        ])

        # Applies all of our patches before each test
        for _patch in self.patches:
            _patch.start()

        self.agent = Agent(conf=self.conf,
                           driver_names=self.conf.agent.driver_names,
                           use_nanoconfig_service=False,
                           publisher_endpoint="fake",
                           nanoconfig_service_endpoint="",
                           nanoconfig_update_endpoint="",
                           nanoconfig_profile="nanoconfig://test_profile")
        # Default ticking is set to 0 to reduce test execution time
        self.agent.TICK_INTERVAL = 0
Beispiel #28
0
 def create_builder(builder_type, build_spec):
     driver_mgr = DriverManager(namespace=BUILDER_DRIVER_NAMESPACE,
                                name=builder_type,
                                invoke_args=(build_spec, ),
                                invoke_on_load=True)
     return driver_mgr.driver
Beispiel #29
0
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from stevedore.driver import DriverManager
from stevedore.extension import ExtensionManager

from st2common import config
config.parse_args()

manager = ExtensionManager(namespace='st2common.runners.runner',
                           invoke_on_load=False)
extension_names = manager.names()

print('Available / installed action runners:')
for name in extension_names:
    manager = DriverManager(namespace='st2common.runners.runner',
                            invoke_on_load=False,
                            name=name)
    runner_instance = manager.driver.get_runner()
    runner_metadata = manager.driver.get_metadata()[0]

    print('- %s (runner_module=%s,cls=%s)' %
          (name, runner_metadata['runner_module'], runner_instance.__class__))
Beispiel #30
0
    def setUp(self):
        super(TestAgent, self).setUp()
        self.conf = cfg.ConfigOpts()
        # To load the drivers without using the config file
        self.useFixture(ConfFixture(self.conf))

        def _fake_parse(self, args=[]):
            return cfg.ConfigOpts._parse_cli_opts(self, [])

        _fake_parse_method = types.MethodType(_fake_parse, self.conf)
        self.conf._parse_cli_opts = _fake_parse_method

        # First dependency to be returned
        self.dummy_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=DummyMetricPuller.get_name(),
                entry_point='fake.entry.point',
                plugin=DummyMetricPuller,
                obj=None,
            ),
            namespace='TESTING',
        )
        # 2nd dependency to be returned
        self.fake_driver_manager = DriverManager.make_test_instance(
            extension=Extension(
                name=FakeMetricPuller.get_name(),
                entry_point='fake.entry.point',
                plugin=FakeMetricPuller,
                obj=None,
            ),
            namespace='TESTING',
        )
        self.defaults_drivers = {
            DummyMetricPuller.get_name(): self.dummy_driver_manager,
            FakeMetricPuller.get_name(): self.fake_driver_manager,
        }

        def _fake_loader(name, **kw):
            return self.defaults_drivers[name]

        # Patches the agent socket
        self.m_agent_socket = MagicMock(autospec=True)

        self.patches.extend([
            # Deactivates the nanomsg socket
            patch(
                "watcher_metering.agent.agent.nanomsg.Socket",
                new=self.m_agent_socket,
            ),
            # Sets the test namespace to 'TESTING'
            patch.object(
                Agent,
                "namespace",
                PropertyMock(return_value='TESTING'),
            ),
            # Patches the driver manager to retourn our test drivers
            # instead of the real ones
            patch(
                "watcher_metering.load.loader.DriverManager",
                MagicMock(side_effect=_fake_loader),
            ),
        ])

        # Applies all of our patches before each test
        for _patch in self.patches:
            _patch.start()

        self.agent = Agent(
            conf=self.conf,
            driver_names=self.conf.agent.driver_names,
            use_nanoconfig_service=False,
            publisher_endpoint="fake",
            nanoconfig_service_endpoint="",
            nanoconfig_update_endpoint="",
            nanoconfig_profile="nanoconfig://test_profile"
        )
        # Default ticking is set to 0 to reduce test execution time
        self.agent.TICK_INTERVAL = 0
Beispiel #31
0
    def init_parser(self):
        manager = DriverManager('logmetrics.parser',
                                self.parsed_args.parser)

        self.parser = manager.driver()
Beispiel #32
0
def load_driver(driver_name):
    """Return the loaded driver"""
    return DriverManager(namespace='drivers', name=driver_name).driver