Esempio n. 1
0
    def f(*args, **kwargs):
        _kwargs = {k: v for k, v in kwargs.items() if not k.startswith('__')}

        _kwargs['noconsole'] = True
        _kwargs['rsyslog'] = True
        _kwargs['rsyslog-level'] = 'DEBUG'
        _kwargs['rsyslog-formatter'] = 'full'

        # don't make sense here
        for k in ('nowait', 'username', 'password', 'eauth'):
            _kwargs.pop(k, None)

        # turn off salt job mode as well to prevent infinite api loop
        env = {'PRVSNR_SALT_JOB': 'no', 'PRVSNR_OUTPUT': 'json'}

        cmd = ['provisioner']
        cmd.extend(api_args_to_cli(fun, *args, **_kwargs))
        cmd = [quote(p) for p in cmd]
        return __salt__['cmd.run'](' '.join(cmd), env=env)
Esempio n. 2
0
def _api_call(fun, *args, **kwargs):
    if fun not in ('auth_init', 'get_result'):
        kwargs['nowait'] = nowait

    if api_type in ('py', 'pycli'):
        import provisioner
        provisioner.set_api(api_type)
        return getattr(provisioner, fun)(*args, **kwargs)
    else:  # cli
        from provisioner._api_cli import (
            api_args_to_cli, process_cli_result
        )
        _input = kwargs.pop('password', None)
        if _input is not None:
            kwargs['password'] = '******'

        kwargs['console'] = True
        kwargs['console-level'] = 'DEBUG'
        kwargs['console-stream'] = 'stderr'

        cmd = ['provisioner']
        cmd.extend(api_args_to_cli(fun, *args, **kwargs))
        logger.debug("Command: {}".format(cmd))

        env = os.environ.copy()
        env.update({'PRVSNR_OUTPUT': 'json'})

        try:
            res = subprocess.run(
                cmd,
                env=env,
                input=_input,
                check=True,
                universal_newlines=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
        except subprocess.CalledProcessError as exc:
            return process_cli_result(exc.stdout, exc.stderr)
        else:
            return process_cli_result(res.stdout, res.stderr)
def test_api_cli_api_args_to_cli(patch_logging):
    fun = 'some-fun'
    # positional
    assert api.api_args_to_cli(fun, 'arg1', 'arg2', [1, 2, 3], {'4': {
        '5': 6
    }}) == [fun, 'arg1', 'arg2', '[1, 2, 3]', '{"4": {"5": 6}}']
    # optional basic
    assert api.api_args_to_cli(fun, arg1=123) == [fun, '--arg1=123']
    # optional '-' and '_'
    assert api.api_args_to_cli(fun, some_arg1=123) == [fun, '--some-arg1=123']
    # optional True bool
    assert api.api_args_to_cli(fun, arg1=True) == [fun, '--arg1']
    # optional True bool
    assert api.api_args_to_cli(fun, arg1=False) == [fun]
    # optional None
    assert api.api_args_to_cli(fun, arg1=None) == [fun, f'--arg1={str(NONE)}']
    # optional List
    assert api.api_args_to_cli(fun, arg1=[1, 2,
                                          '3']) == [fun, '--arg1=[1, 2, "3"]']
    # optional Dict
    assert api.api_args_to_cli(fun, arg1={'1': {
        '2': 3,
        '4': '5'
    }}) == [fun, '--arg1={"1": {"2": 3, "4": "5"}}']