예제 #1
0
파일: caller.py 프로젝트: vitorarins/salt
    def call(self):
        '''
        Call the module
        '''
        ret = {}
        fun = self.opts['fun']

        if fun not in self.minion.functions:
            sys.stderr.write('Function {0} is not available\n'.format(fun))
            sys.exit(1)
        try:
            args, kw = salt.minion.detect_kwargs(self.minion.functions[fun],
                                                 self.opts['arg'])
            ret['return'] = self.minion.functions[fun](*args, **kw)
        except (TypeError, CommandExecutionError) as exc:
            msg = 'Error running \'{0}\': {1}\n'
            active_level = LOG_LEVELS.get(self.opts['log_level'].lower,
                                          logging.ERROR)
            if active_level <= logging.DEBUG:
                sys.stderr.write(traceback.format_exc())
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(1)
        except CommandNotFoundError as exc:
            msg = 'Command required for \'{0}\' not found: {1}\n'
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(1)
        if hasattr(self.minion.functions[fun], '__outputter__'):
            oput = self.minion.functions[fun].__outputter__
            if isinstance(oput, string_types):
                ret['out'] = oput
        return ret
예제 #2
0
파일: caller.py 프로젝트: BackSeat/salt
    def call(self):
        '''
        Call the module
        '''
        ret = {}
        fun = self.opts['fun']

        if fun not in self.minion.functions:
            sys.stderr.write('Function {0} is not available\n'.format(fun))
            sys.exit(1)
        try:
            args, kw = salt.minion.detect_kwargs(
                self.minion.functions[fun], self.opts['arg'])
            ret['return'] = self.minion.functions[fun](*args, **kw)
        except (TypeError, CommandExecutionError) as exc:
            msg = 'Error running \'{0}\': {1}\n'
            active_level = LOG_LEVELS.get(
                self.opts['log_level'].lower, logging.ERROR)
            if active_level <= logging.DEBUG:
                sys.stderr.write(traceback.format_exc())
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(1)
        except CommandNotFoundError as exc:
            msg = 'Command required for \'{0}\' not found: {1}\n'
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(1)
        if hasattr(self.minion.functions[fun], '__outputter__'):
            oput = self.minion.functions[fun].__outputter__
            if isinstance(oput, string_types):
                ret['out'] = oput
        return ret
예제 #3
0
파일: caller.py 프로젝트: herlo/salt
 def call(self):
     '''
     Call the module
     '''
     ret = {}
     fun = self.opts['fun']
     ret['jid'] = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())
     proc_fn = os.path.join(
             salt.minion.get_proc_dir(self.opts['cachedir']),
             ret['jid'])
     if fun not in self.minion.functions:
         sys.stderr.write('Function {0} is not available\n'.format(fun))
         sys.exit(-1)
     try:
         args, kwargs = salt.minion.detect_kwargs(
             self.minion.functions[fun], self.opts['arg'])
         args, kwargs = self.parse_args(args, kwargs)
         sdata = {
                 'fun': fun,
                 'pid': os.getpid(),
                 'jid': ret['jid'],
                 'tgt': 'salt-call'}
         with salt.utils.fopen(proc_fn, 'w+') as fp_:
             fp_.write(self.serial.dumps(sdata))
         func = self.minion.functions[fun]
         ret['return'] = func(*args, **kwargs)
         ret['retcode'] = sys.modules[func.__module__].__context__.get(
                 'retcode', 0)
     except (CommandExecutionError) as exc:
         msg = 'Error running \'{0}\': {1}\n'
         active_level = LOG_LEVELS.get(
             self.opts['log_level'].lower(), logging.ERROR)
         if active_level <= logging.DEBUG:
             sys.stderr.write(traceback.format_exc())
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     except CommandNotFoundError as exc:
         msg = 'Command required for \'{0}\' not found: {1}\n'
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     try:
         os.remove(proc_fn)
     except (IOError, OSError):
         pass
     if hasattr(self.minion.functions[fun], '__outputter__'):
         oput = self.minion.functions[fun].__outputter__
         if isinstance(oput, string_types):
             ret['out'] = oput
         if oput == 'highstate':
             ret['return'] = {'local': ret['return']}
     if self.opts.get('return', ''):
         ret['id'] = self.opts['id']
         ret['fun'] = fun
         for returner in self.opts['return'].split(','):
             try:
                 self.minion.returners['{0}.returner'.format(returner)](ret)
             except Exception:
                 pass
     return ret
예제 #4
0
 def call(self):
     '''
     Call the module
     '''
     ret = {}
     fun = self.opts['fun']
     ret['jid'] = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())
     proc_fn = os.path.join(salt.minion.get_proc_dir(self.opts['cachedir']),
                            ret['jid'])
     if fun not in self.minion.functions:
         sys.stderr.write('Function {0} is not available\n'.format(fun))
         sys.exit(1)
     try:
         args, kwargs = salt.minion.detect_kwargs(
             self.minion.functions[fun], self.opts['arg'])
         sdata = {
             'fun': fun,
             'pid': os.getpid(),
             'jid': ret['jid'],
             'tgt': 'salt-call'
         }
         with salt.utils.fopen(proc_fn, 'w+') as fp_:
             fp_.write(self.serial.dumps(sdata))
         ret['return'] = self.minion.functions[fun](*args, **kwargs)
     except (TypeError, CommandExecutionError) as exc:
         msg = 'Error running \'{0}\': {1}\n'
         active_level = LOG_LEVELS.get(self.opts['log_level'].lower(),
                                       logging.ERROR)
         if active_level <= logging.DEBUG:
             sys.stderr.write(traceback.format_exc())
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     except CommandNotFoundError as exc:
         msg = 'Command required for \'{0}\' not found: {1}\n'
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     try:
         os.remove(proc_fn)
     except (IOError, OSError):
         pass
     if hasattr(self.minion.functions[fun], '__outputter__'):
         oput = self.minion.functions[fun].__outputter__
         if isinstance(oput, string_types):
             ret['out'] = oput
         if oput == 'highstate':
             ret['return'] = {'local': ret['return']}
     if self.opts.get('return', ''):
         ret['id'] = self.opts['id']
         ret['fun'] = fun
         for returner in self.opts['return'].split(','):
             try:
                 self.minion.returners['{0}.returner'.format(returner)](ret)
             except Exception:
                 pass
     return ret
예제 #5
0
파일: caller.py 프로젝트: jslatts/salt
 def call(self):
     """
     Call the module
     """
     ret = {}
     fun = self.opts["fun"]
     ret["jid"] = "{0:%Y%m%d%H%M%S%f}".format(datetime.datetime.now())
     proc_fn = os.path.join(salt.minion.get_proc_dir(self.opts["cachedir"]), ret["jid"])
     if fun not in self.minion.functions:
         sys.stderr.write("Function {0} is not available\n".format(fun))
         sys.exit(-1)
     try:
         args, kwargs = salt.minion.parse_args_and_kwargs(self.minion.functions[fun], self.opts["arg"])
         sdata = {"fun": fun, "pid": os.getpid(), "jid": ret["jid"], "tgt": "salt-call"}
         try:
             with salt.utils.fopen(proc_fn, "w+") as fp_:
                 fp_.write(self.serial.dumps(sdata))
         except NameError:
             # Don't require msgpack with local
             pass
         func = self.minion.functions[fun]
         ret["return"] = func(*args, **kwargs)
         ret["retcode"] = sys.modules[func.__module__].__context__.get("retcode", 0)
     except (CommandExecutionError) as exc:
         msg = "Error running '{0}': {1}\n"
         active_level = LOG_LEVELS.get(self.opts["log_level"].lower(), logging.ERROR)
         if active_level <= logging.DEBUG:
             sys.stderr.write(traceback.format_exc())
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     except CommandNotFoundError as exc:
         msg = "Command required for '{0}' not found: {1}\n"
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     try:
         os.remove(proc_fn)
     except (IOError, OSError):
         pass
     if hasattr(self.minion.functions[fun], "__outputter__"):
         oput = self.minion.functions[fun].__outputter__
         if isinstance(oput, string_types):
             ret["out"] = oput
         if oput == "highstate":
             ret["return"] = {"local": ret["return"]}
     if self.opts.get("return", ""):
         ret["id"] = self.opts["id"]
         ret["fun"] = fun
         for returner in self.opts["return"].split(","):
             try:
                 self.minion.returners["{0}.returner".format(returner)](ret)
             except Exception:
                 pass
     return ret
예제 #6
0
파일: caller.py 프로젝트: AFriemann/salt
    def call(self):
        '''
        Call the module
        '''
        ret = {}
        fun = self.opts['fun']
        ret['jid'] = salt.utils.jid.gen_jid(self.opts)
        proc_fn = os.path.join(
            salt.minion.get_proc_dir(self.opts['cachedir']),
            ret['jid']
        )
        if fun not in self.minion.functions:
            docs = self.minion.functions['sys.doc']('{0}*'.format(fun))
            if docs:
                docs[fun] = self.minion.functions.missing_fun_string(fun)
                ret['out'] = 'nested'
                ret['return'] = docs
                return ret
            sys.stderr.write(self.minion.functions.missing_fun_string(fun))
            mod_name = fun.split('.')[0]
            if mod_name in self.minion.function_errors:
                sys.stderr.write(' Possible reasons: {0}\n'.format(self.minion.function_errors[mod_name]))
            else:
                sys.stderr.write('\n')
            sys.exit(-1)
        metadata = self.opts.get('metadata')
        if metadata is not None:
            metadata = salt.utils.args.yamlify_arg(metadata)
        try:
            sdata = {
                'fun': fun,
                'pid': os.getpid(),
                'jid': ret['jid'],
                'tgt': 'salt-call'}
            if metadata is not None:
                sdata['metadata'] = metadata
            args, kwargs = salt.minion.load_args_and_kwargs(
                self.minion.functions[fun],
                salt.utils.args.parse_input(
                    self.opts['arg'],
                    no_parse=self.opts.get('no_parse', [])),
                data=sdata)
            try:
                with salt.utils.files.fopen(proc_fn, 'w+b') as fp_:
                    fp_.write(self.serial.dumps(sdata))
            except NameError:
                # Don't require msgpack with local
                pass
            except IOError:
                sys.stderr.write(
                    'Cannot write to process directory. '
                    'Do you have permissions to '
                    'write to {0} ?\n'.format(proc_fn))
            func = self.minion.functions[fun]
            try:
                ret['return'] = func(*args, **kwargs)
            except TypeError as exc:
                sys.stderr.write('\nPassed invalid arguments: {0}.\n\nUsage:\n'.format(exc))
                salt.utils.print_cli(func.__doc__)
                active_level = LOG_LEVELS.get(
                    self.opts['log_level'].lower(), logging.ERROR)
                if active_level <= logging.DEBUG:
                    trace = traceback.format_exc()
                    sys.stderr.write(trace)
                sys.exit(salt.defaults.exitcodes.EX_GENERIC)
            try:
                ret['retcode'] = sys.modules[
                    func.__module__].__context__.get('retcode', 0)
            except AttributeError:
                ret['retcode'] = 1
        except (CommandExecutionError) as exc:
            msg = 'Error running \'{0}\': {1}\n'
            active_level = LOG_LEVELS.get(
                self.opts['log_level'].lower(), logging.ERROR)
            if active_level <= logging.DEBUG:
                sys.stderr.write(traceback.format_exc())
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(salt.defaults.exitcodes.EX_GENERIC)
        except CommandNotFoundError as exc:
            msg = 'Command required for \'{0}\' not found: {1}\n'
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(salt.defaults.exitcodes.EX_GENERIC)
        try:
            os.remove(proc_fn)
        except (IOError, OSError):
            pass
        if hasattr(self.minion.functions[fun], '__outputter__'):
            oput = self.minion.functions[fun].__outputter__
            if isinstance(oput, six.string_types):
                ret['out'] = oput
        is_local = self.opts['local'] or self.opts.get(
            'file_client', False) == 'local' or self.opts.get(
            'master_type') == 'disable'
        returners = self.opts.get('return', '').split(',')
        if (not is_local) or returners:
            ret['id'] = self.opts['id']
            ret['fun'] = fun
            ret['fun_args'] = self.opts['arg']
            if metadata is not None:
                ret['metadata'] = metadata

        for returner in returners:
            if not returner:  # if we got an empty returner somehow, skip
                continue
            try:
                ret['success'] = True
                self.minion.returners['{0}.returner'.format(returner)](ret)
            except Exception:
                pass

        # return the job infos back up to the respective minion's master
        if not is_local:
            try:
                mret = ret.copy()
                mret['jid'] = 'req'
                self.return_pub(mret)
            except Exception:
                pass
        elif self.opts['cache_jobs']:
            # Local job cache has been enabled
            salt.utils.minion.cache_jobs(self.opts, ret['jid'], ret)

        # close raet channel here
        return ret
예제 #7
0
파일: caller.py 프로젝트: zfouts/salt
    def call(self):
        """
        Call the module
        """
        ret = {}
        fun = self.opts["fun"]
        ret["jid"] = salt.utils.jid.gen_jid(self.opts)
        proc_fn = os.path.join(salt.minion.get_proc_dir(self.opts["cachedir"]),
                               ret["jid"])
        if fun not in self.minion.functions:
            docs = self.minion.functions["sys.doc"]("{}*".format(fun))
            if docs:
                docs[fun] = self.minion.functions.missing_fun_string(fun)
                ret["out"] = "nested"
                ret["return"] = docs
                return ret
            sys.stderr.write(self.minion.functions.missing_fun_string(fun))
            mod_name = fun.split(".")[0]
            if mod_name in self.minion.function_errors:
                sys.stderr.write(" Possible reasons: {}\n".format(
                    self.minion.function_errors[mod_name]))
            else:
                sys.stderr.write("\n")
            sys.exit(-1)
        metadata = self.opts.get("metadata")
        if metadata is not None:
            metadata = salt.utils.args.yamlify_arg(metadata)
        try:
            sdata = {
                "fun": fun,
                "pid": os.getpid(),
                "jid": ret["jid"],
                "tgt": "salt-call",
            }
            if metadata is not None:
                sdata["metadata"] = metadata
            args, kwargs = salt.minion.load_args_and_kwargs(
                self.minion.functions[fun],
                salt.utils.args.parse_input(self.opts["arg"],
                                            no_parse=self.opts.get(
                                                "no_parse", [])),
                data=sdata,
            )
            try:
                with salt.utils.files.fopen(proc_fn, "w+b") as fp_:
                    fp_.write(self.serial.dumps(sdata))
            except NameError:
                # Don't require msgpack with local
                pass
            except OSError:
                sys.stderr.write("Cannot write to process directory. "
                                 "Do you have permissions to "
                                 "write to {} ?\n".format(proc_fn))
            func = self.minion.functions[fun]
            data = {"arg": args, "fun": fun}
            data.update(kwargs)
            executors = getattr(self.minion, "module_executors",
                                []) or salt.utils.args.yamlify_arg(
                                    self.opts.get("module_executors",
                                                  "[direct_call]"))
            if self.opts.get("executor_opts", None):
                data["executor_opts"] = salt.utils.args.yamlify_arg(
                    self.opts["executor_opts"])
            if isinstance(executors, str):
                executors = [executors]
            try:
                for name in executors:
                    fname = "{}.execute".format(name)
                    if fname not in self.minion.executors:
                        raise SaltInvocationError(
                            "Executor '{}' is not available".format(name))
                    ret["return"] = self.minion.executors[fname](self.opts,
                                                                 data, func,
                                                                 args, kwargs)
                    if ret["return"] is not None:
                        break
            except TypeError as exc:
                sys.stderr.write(
                    "\nPassed invalid arguments: {}.\n\nUsage:\n".format(exc))
                salt.utils.stringutils.print_cli(func.__doc__)
                active_level = LOG_LEVELS.get(self.opts["log_level"].lower(),
                                              logging.ERROR)
                if active_level <= logging.DEBUG:
                    trace = traceback.format_exc()
                    sys.stderr.write(trace)
                sys.exit(salt.defaults.exitcodes.EX_GENERIC)
            try:
                retcode = sys.modules[func.__module__].__context__.get(
                    "retcode", 0)
            except AttributeError:
                retcode = salt.defaults.exitcodes.EX_GENERIC

            if retcode == 0:
                # No nonzero retcode in __context__ dunder. Check if return
                # is a dictionary with a "result" or "success" key.
                try:
                    func_result = all(ret["return"].get(x, True)
                                      for x in ("result", "success"))
                except Exception:  # pylint: disable=broad-except
                    # return data is not a dict
                    func_result = True
                if not func_result:
                    retcode = salt.defaults.exitcodes.EX_GENERIC

            ret["retcode"] = retcode
        except (CommandExecutionError) as exc:
            msg = "Error running '{0}': {1}\n"
            active_level = LOG_LEVELS.get(self.opts["log_level"].lower(),
                                          logging.ERROR)
            if active_level <= logging.DEBUG:
                sys.stderr.write(traceback.format_exc())
            sys.stderr.write(msg.format(fun, exc))
            sys.exit(salt.defaults.exitcodes.EX_GENERIC)
        except CommandNotFoundError as exc:
            msg = "Command required for '{0}' not found: {1}\n"
            sys.stderr.write(msg.format(fun, exc))
            sys.exit(salt.defaults.exitcodes.EX_GENERIC)
        try:
            os.remove(proc_fn)
        except OSError:
            pass
        if hasattr(self.minion.functions[fun], "__outputter__"):
            oput = self.minion.functions[fun].__outputter__
            if isinstance(oput, str):
                ret["out"] = oput
        is_local = (self.opts["local"]
                    or self.opts.get("file_client", False) == "local"
                    or self.opts.get("master_type") == "disable")
        returners = self.opts.get("return", "").split(",")
        if (not is_local) or returners:
            ret["id"] = self.opts["id"]
            ret["fun"] = fun
            ret["fun_args"] = self.opts["arg"]
            if metadata is not None:
                ret["metadata"] = metadata

        for returner in returners:
            if not returner:  # if we got an empty returner somehow, skip
                continue
            try:
                ret["success"] = True
                self.minion.returners["{}.returner".format(returner)](ret)
            except Exception:  # pylint: disable=broad-except
                pass

        # return the job infos back up to the respective minion's master
        if not is_local:
            try:
                mret = ret.copy()
                mret["jid"] = "req"
                self.return_pub(mret)
            except Exception:  # pylint: disable=broad-except
                pass
        elif self.opts["cache_jobs"]:
            # Local job cache has been enabled
            salt.utils.minion.cache_jobs(self.opts, ret["jid"], ret)

        return ret
예제 #8
0
    def call(self):
        '''
        Call the module
        '''
        ret = {}
        fun = self.opts['fun']
        ret['jid'] = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())
        proc_fn = os.path.join(salt.minion.get_proc_dir(self.opts['cachedir']),
                               ret['jid'])
        if fun not in self.minion.functions:
            sys.stderr.write('Function {0} is not available\n'.format(fun))
            sys.exit(-1)
        try:
            sdata = {
                'fun': fun,
                'pid': os.getpid(),
                'jid': ret['jid'],
                'tgt': 'salt-call'
            }
            args, kwargs = salt.minion.load_args_and_kwargs(
                self.minion.functions[fun],
                salt.utils.args.parse_input(self.opts['arg']),
                data=sdata)
            try:
                with salt.utils.fopen(proc_fn, 'w+b') as fp_:
                    fp_.write(self.serial.dumps(sdata))
            except NameError:
                # Don't require msgpack with local
                pass
            except IOError:
                sys.stderr.write('Cannot write to process directory. '
                                 'Do you have permissions to '
                                 'write to {0} ?\n'.format(proc_fn))
            func = self.minion.functions[fun]
            try:
                ret['return'] = func(*args, **kwargs)
            except TypeError as exc:
                trace = traceback.format_exc()
                sys.stderr.write('Passed invalid arguments: {0}\n'.format(exc))
                active_level = LOG_LEVELS.get(self.opts['log_level'].lower(),
                                              logging.ERROR)
                if active_level <= logging.DEBUG:
                    sys.stderr.write(trace)
                sys.exit(salt.exitcodes.EX_GENERIC)
            try:
                ret['retcode'] = sys.modules[func.__module__].__context__.get(
                    'retcode', 0)
            except AttributeError:
                ret['retcode'] = 1
        except (CommandExecutionError) as exc:
            msg = 'Error running \'{0}\': {1}\n'
            active_level = LOG_LEVELS.get(self.opts['log_level'].lower(),
                                          logging.ERROR)
            if active_level <= logging.DEBUG:
                sys.stderr.write(traceback.format_exc())
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(salt.exitcodes.EX_GENERIC)
        except CommandNotFoundError as exc:
            msg = 'Command required for \'{0}\' not found: {1}\n'
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(salt.exitcodes.EX_GENERIC)
        try:
            os.remove(proc_fn)
        except (IOError, OSError):
            pass
        if hasattr(self.minion.functions[fun], '__outputter__'):
            oput = self.minion.functions[fun].__outputter__
            if isinstance(oput, string_types):
                ret['out'] = oput
        is_local = self.opts['local'] or self.opts.get('file_client',
                                                       False) == 'local'
        returners = self.opts.get('return', '').split(',')
        if (not is_local) or returners:
            ret['id'] = self.opts['id']
            ret['fun'] = fun
            ret['fun_args'] = self.opts['arg']

        for returner in returners:
            try:
                ret['success'] = True
                self.minion.returners['{0}.returner'.format(returner)](ret)
            except Exception:
                pass

        # return the job infos back up to the respective minion's master

        if not is_local:
            try:
                mret = ret.copy()
                mret['jid'] = 'req'
                self.return_pub(mret)
            except Exception:
                pass
        return ret
예제 #9
0
    def call(self):
        '''
        Call the module
        '''
        ret = {}
        fun = self.opts['fun']
        ret['jid'] = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())
        proc_fn = os.path.join(
            salt.minion.get_proc_dir(self.opts['cachedir']),
            ret['jid']
        )
        if fun not in self.minion.functions:
            sys.stderr.write('Function {0} is not available\n'.format(fun))
            sys.exit(-1)
        try:
            sdata = {
                'fun': fun,
                'pid': os.getpid(),
                'jid': ret['jid'],
                'tgt': 'salt-call'}
            args, kwargs = salt.minion.load_args_and_kwargs(
                self.minion.functions[fun],
                salt.utils.args.parse_input(self.opts['arg']),
                data=sdata)
            try:
                with salt.utils.fopen(proc_fn, 'w+b') as fp_:
                    fp_.write(self.serial.dumps(sdata))
            except NameError:
                # Don't require msgpack with local
                pass
            except IOError:
                sys.stderr.write(
                    'Cannot write to process directory. '
                    'Do you have permissions to '
                    'write to {0} ?\n'.format(proc_fn))
            func = self.minion.functions[fun]
            try:
                ret['return'] = func(*args, **kwargs)
            except TypeError as exc:
                trace = traceback.format_exc()
                sys.stderr.write('Passed invalid arguments: {0}\n'.format(exc))
                active_level = LOG_LEVELS.get(
                    self.opts['log_level'].lower(), logging.ERROR)
                if active_level <= logging.DEBUG:
                    sys.stderr.write(trace)
                sys.exit(1)
            ret['retcode'] = sys.modules[
                func.__module__].__context__.get('retcode', 0)
        except (CommandExecutionError) as exc:
            msg = 'Error running \'{0}\': {1}\n'
            active_level = LOG_LEVELS.get(
                self.opts['log_level'].lower(), logging.ERROR)
            if active_level <= logging.DEBUG:
                sys.stderr.write(traceback.format_exc())
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(1)
        except CommandNotFoundError as exc:
            msg = 'Command required for \'{0}\' not found: {1}\n'
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(1)
        try:
            os.remove(proc_fn)
        except (IOError, OSError):
            pass
        if hasattr(self.minion.functions[fun], '__outputter__'):
            oput = self.minion.functions[fun].__outputter__
            if isinstance(oput, string_types):
                ret['out'] = oput
        is_local = self.opts['local'] or self.opts.get(
            'file_client', False) == 'local'
        returners = self.opts.get('return', '').split(',')
        if (not is_local) or returners:
            ret['id'] = self.opts['id']
            ret['fun'] = fun
            ret['fun_args'] = self.opts['arg']

        for returner in returners:
            try:
                ret['success'] = True
                self.minion.returners['{0}.returner'.format(returner)](ret)
            except Exception:
                pass

        # return the job infos back up to the respective minion's master

        if not is_local:
            try:
                mret = ret.copy()
                mret['jid'] = 'req'
                self.return_pub(mret)
            except Exception:
                pass
        return ret
예제 #10
0
파일: caller.py 프로젝트: DaveQB/salt
    def call(self):
        '''
        Call the module
        '''
        ret = {}
        fun = self.opts['fun']
        ret['jid'] = salt.utils.jid.gen_jid()
        proc_fn = os.path.join(
            salt.minion.get_proc_dir(self.opts['cachedir']),
            ret['jid']
        )
        if fun not in self.minion.functions:
            sys.stderr.write(self.minion.functions.missing_fun_string(fun))
            mod_name = fun.split('.')[0]
            if mod_name in self.minion.function_errors:
                sys.stderr.write(' Possible reasons: {0}\n'.format(self.minion.function_errors[mod_name]))
            else:
                sys.stderr.write('\n')
            sys.exit(-1)
        try:
            sdata = {
                'fun': fun,
                'pid': os.getpid(),
                'jid': ret['jid'],
                'tgt': 'salt-call'}
            args, kwargs = salt.minion.load_args_and_kwargs(
                self.minion.functions[fun],
                salt.utils.args.parse_input(self.opts['arg']),
                data=sdata)
            try:
                with salt.utils.fopen(proc_fn, 'w+b') as fp_:
                    fp_.write(self.serial.dumps(sdata))
            except NameError:
                # Don't require msgpack with local
                pass
            except IOError:
                sys.stderr.write(
                    'Cannot write to process directory. '
                    'Do you have permissions to '
                    'write to {0} ?\n'.format(proc_fn))
            func = self.minion.functions[fun]
            try:
                ret['return'] = func(*args, **kwargs)
            except TypeError as exc:
                sys.stderr.write('\nPassed invalid arguments: {0}.\n\nUsage:\n'.format(exc))
                print_cli(func.__doc__)
                active_level = LOG_LEVELS.get(
                    self.opts['log_level'].lower(), logging.ERROR)
                if active_level <= logging.DEBUG:
                    trace = traceback.format_exc()
                    sys.stderr.write(trace)
                sys.exit(salt.defaults.exitcodes.EX_GENERIC)
            try:
                ret['retcode'] = sys.modules[
                    func.__module__].__context__.get('retcode', 0)
            except AttributeError:
                ret['retcode'] = 1
        except (CommandExecutionError) as exc:
            msg = 'Error running \'{0}\': {1}\n'
            active_level = LOG_LEVELS.get(
                self.opts['log_level'].lower(), logging.ERROR)
            if active_level <= logging.DEBUG:
                sys.stderr.write(traceback.format_exc())
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(salt.defaults.exitcodes.EX_GENERIC)
        except CommandNotFoundError as exc:
            msg = 'Command required for \'{0}\' not found: {1}\n'
            sys.stderr.write(msg.format(fun, str(exc)))
            sys.exit(salt.defaults.exitcodes.EX_GENERIC)
        try:
            os.remove(proc_fn)
        except (IOError, OSError):
            pass
        if hasattr(self.minion.functions[fun], '__outputter__'):
            oput = self.minion.functions[fun].__outputter__
            if isinstance(oput, six.string_types):
                ret['out'] = oput
        is_local = self.opts['local'] or self.opts.get(
            'file_client', False) == 'local'
        returners = self.opts.get('return', '').split(',')
        if (not is_local) or returners:
            ret['id'] = self.opts['id']
            ret['fun'] = fun
            ret['fun_args'] = self.opts['arg']

        for returner in returners:
            if not returner:  # if we got an empty returner somehow, skip
                continue
            try:
                ret['success'] = True
                self.minion.returners['{0}.returner'.format(returner)](ret)
            except Exception:
                pass

        # return the job infos back up to the respective minion's master

        if not is_local:
            try:
                mret = ret.copy()
                mret['jid'] = 'req'
                self.return_pub(mret)
            except Exception:
                pass
        # close raet channel here
        return ret
예제 #11
0
 def call(self):
     '''
     Call the module
     '''
     ret = {}
     fun = self.opts['fun']
     ret['jid'] = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())
     proc_fn = os.path.join(salt.minion.get_proc_dir(self.opts['cachedir']),
                            ret['jid'])
     if fun not in self.minion.functions:
         sys.stderr.write('Function {0} is not available\n'.format(fun))
         sys.exit(-1)
     try:
         sdata = {
             'fun': fun,
             'pid': os.getpid(),
             'jid': ret['jid'],
             'tgt': 'salt-call'
         }
         args, kwargs = salt.minion.parse_args_and_kwargs(
             self.minion.functions[fun], self.opts['arg'], data=sdata)
         try:
             with salt.utils.fopen(proc_fn, 'w+b') as fp_:
                 fp_.write(self.serial.dumps(sdata))
         except NameError:
             # Don't require msgpack with local
             pass
         func = self.minion.functions[fun]
         try:
             ret['return'] = func(*args, **kwargs)
         except TypeError as exc:
             sys.stderr.write(
                 ('Passed invalid arguments: {0}\n').format(exc))
             sys.exit(1)
         ret['retcode'] = sys.modules[func.__module__].__context__.get(
             'retcode', 0)
     except (CommandExecutionError) as exc:
         msg = 'Error running \'{0}\': {1}\n'
         active_level = LOG_LEVELS.get(self.opts['log_level'].lower(),
                                       logging.ERROR)
         if active_level <= logging.DEBUG:
             sys.stderr.write(traceback.format_exc())
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     except CommandNotFoundError as exc:
         msg = 'Command required for \'{0}\' not found: {1}\n'
         sys.stderr.write(msg.format(fun, str(exc)))
         sys.exit(1)
     try:
         os.remove(proc_fn)
     except (IOError, OSError):
         pass
     if hasattr(self.minion.functions[fun], '__outputter__'):
         oput = self.minion.functions[fun].__outputter__
         if isinstance(oput, string_types):
             ret['out'] = oput
     if self.opts.get('return', ''):
         ret['id'] = self.opts['id']
         ret['fun'] = fun
         ret['fun_args'] = self.opts['arg']
         for returner in self.opts['return'].split(','):
             try:
                 ret['success'] = True
                 self.minion.returners['{0}.returner'.format(returner)](ret)
             except Exception:
                 pass
     return ret