def execute_return_exit_code(self, cmd):
        """
        workaround for invoking pepper functions without
        this function does not provide error handling,
        so user should handle them accordingly
        cmd should return a integer value where 0 stands for success
        :param [] cmd: a list of commands
        :return: generator of salt cmd result
        """

        # os.environ['SALTAPI_URL'] = self.uri
        # os.environ['SALTAPI_EAUTH'] = self.eauth
        # os.environ['SALTAPI_USER'] = self.user
        # os.environ['SALTAPI_PASS'] = self.passwd

        # manually assemble pass-in values
        if not isinstance(cmd, (list, tuple)):
            cmd = [cmd]
        elif isinstance(cmd, tuple):
            cmd = list(cmd)

        # workaround to override all existing options
        # existing option has two sources: 1. pass-in as command to be executed
        # 2. this method has been previously invoked
        sys.argv = [sys.argv[0]] + cmd

        result_list = []
        cli = PepperCli()
        for exit_code, result in cli.run():
            if exit_code == 0:
                for i, j in json.loads(result).get('return', [{}])[0].items():
                    result_list.append(j)
            else:
                raise Exception('error while executing command')
        return result_list
Ejemplo n.º 2
0
 def __init__(self):
     self.cli = PepperCli()
     if HAS_SALT:
         self.opts = salt.config.client_config(self.cli.options.master)
     else:
         self.opts = {}
     if self.cli.options.output_file is not None:
         self.opts['output_file'] = self.cli.options.output_file
Ejemplo n.º 3
0
class Pepper(object):
    def __init__(self):
        self.cli = PepperCli()
        if HAS_SALT:
            self.opts = salt.config.client_config(self.cli.options.master)
        else:
            self.opts = {}
        if self.cli.options.output_file is not None:
            self.opts['output_file'] = self.cli.options.output_file

    @property
    def output(self):
        if not hasattr(self, 'modules'):
            self.modules = salt.loader.minion_mods(self.opts)
        try:
            oput = self.modules[self.cli.args[1]].__outputter__
        except (KeyError, AttributeError, TypeError):
            oput = 'nested'
        return oput

    def __call__(self):
        try:
            for exit_code, result in self.cli.run():
                if HAS_SALT and not self.cli.options.userun and self.opts:
                    logger.info('Use Salt outputters')
                    for ret in json.loads(result)['return']:
                        if isinstance(ret, dict):
                            if self.cli.options.client == 'local':
                                for minionid, minionret in ret.items():
                                    if isinstance(minionret,
                                                  dict) and 'ret' in minionret:
                                        # version >= 2017.7
                                        salt.output.display_output(
                                            {minionid: minionret['ret']},
                                            self.cli.options.output
                                            or minionret.get('out', None)
                                            or 'nested',
                                            opts=self.opts)
                                    else:
                                        salt.output.display_output(
                                            {minionid: minionret},
                                            self.cli.options.output
                                            or self.output,
                                            opts=self.opts)
                            elif 'data' in ret:
                                salt.output.display_output(
                                    ret['data'],
                                    self.cli.options.output
                                    or ret.get('outputter', 'nested'),
                                    opts=self.opts)
                            else:
                                salt.output.display_output(
                                    {self.cli.options.client: ret},
                                    self.cli.options.output
                                    or ret.get('outputter', 'nested'),
                                    opts=self.opts)
                        else:
                            salt.output.display_output(
                                {self.cli.options.client: ret},
                                'nested',
                                opts=self.opts,
                            )
                else:
                    if self.cli.options.output_file is not None:
                        with open(self.cli.options.output_file, 'a') as ofile:
                            print(result, file=ofile)
                    else:
                        print(result)
                if exit_code is not None:
                    if exit_code == 0:
                        return PepperRetcode().validate(
                            self.cli.options,
                            json.loads(result)['return'])
                    return exit_code
        except (PepperException, PepperAuthException,
                PepperArgumentsException) as exc:
            print('Pepper error: {0}'.format(exc), file=sys.stderr)
            return 1
        except KeyboardInterrupt:
            # TODO: mimic CLI and output JID on ctrl-c
            return 0
        except Exception as e:
            print(e)
            print(
                'Uncaught Pepper error (increase verbosity for the full traceback).',
                file=sys.stderr)
            logger.debug('Uncaught traceback:', exc_info=True)
            return 1
Ejemplo n.º 4
0
class Pepper(object):
    def __init__(self):
        self.cli = PepperCli()
        if HAS_SALT:
            self.opts = salt.config.client_config(self.cli.options.master)
        else:
            self.opts = {}
        if self.cli.options.output_file is not None:
            self.opts['output_file'] = self.cli.options.output_file

    @property
    def output(self):
        if not hasattr(self, 'modules'):
            self.modules = salt.loader.minion_mods(self.opts)
        try:
            oput = self.modules[self.cli.args[1]].__outputter__
        except (KeyError, AttributeError, TypeError):
            oput = 'nested'
        return oput

    def __call__(self):
        try:
            for exit_code, result in self.cli.run():
                if HAS_SALT and self.opts:
                    logger.debug('Use Salt outputters')
                    result = json.loads(result)

                    # unwrap ret in some cases
                    if 'return' in result:
                        result = result['return']

                    for ret in result:
                        if isinstance(ret, dict):
                            if self.cli.options.client.startswith('local'):
                                for minionid, minionret in ret.items():
                                    # rest_tornado doesnt return full_return directly
                                    # it will always be from get_event, so the output differs slightly
                                    if isinstance(
                                            minionret,
                                            dict) and 'return' in minionret:
                                        # version >= 2017.7
                                        salt.output.display_output(
                                            {minionid: minionret['return']},
                                            self.cli.options.output
                                            or minionret.get('out', None)
                                            or 'nested',
                                            opts=self.opts)
                                    # cherrypy returns with ret via full_return
                                    elif isinstance(
                                            minionret,
                                            dict) and 'ret' in minionret:
                                        # version >= 2017.7
                                        salt.output.display_output(
                                            {minionid: minionret['ret']},
                                            self.cli.options.output
                                            or minionret.get('out', None)
                                            or 'nested',
                                            opts=self.opts)
                                    else:
                                        salt.output.display_output(
                                            {minionid: minionret},
                                            self.cli.options.output
                                            or self.output,
                                            opts=self.opts)
                            elif 'data' in ret:
                                # unfold runners
                                outputter = ret.get('outputter', 'nested')
                                if isinstance(
                                        ret['data'],
                                        dict) and 'return' in ret['data']:
                                    ret = ret['data']['return']
                                salt.output.display_output(
                                    ret,
                                    self.cli.options.output or outputter,
                                    opts=self.opts)
                            else:
                                salt.output.display_output(
                                    {self.cli.options.client: ret},
                                    self.cli.options.output
                                    or ret.get('outputter', 'nested'),
                                    opts=self.opts)
                        else:
                            salt.output.display_output(
                                {self.cli.options.client: ret},
                                self.cli.options.output or 'nested',
                                opts=self.opts,
                            )
                else:
                    if self.cli.options.output_file is not None:
                        with open(self.cli.options.output_file, 'a') as ofile:
                            print(result, file=ofile)
                    else:
                        print(result)
                if exit_code is not None:
                    if exit_code == 0:
                        return PepperRetcode().validate(
                            self.cli.options, result)
                    return exit_code
        except (PepperException, PepperAuthException,
                PepperArgumentsException) as exc:
            print('Pepper error: {0}'.format(exc), file=sys.stderr)
            return 1
        except KeyboardInterrupt:
            # TODO: mimic CLI and output JID on ctrl-c
            return 0
        except Exception as e:
            print(e)
            print(
                'Uncaught Pepper error (increase verbosity for the full traceback).',
                file=sys.stderr)
            logger.debug('Uncaught traceback:', exc_info=True)
            return 1