diff --git a/salt/cli/__init__.py b/salt/cli/__init__.py index 1a792578d9..020a6743ac 100644 --- a/salt/cli/__init__.py +++ b/salt/cli/__init__.py @@ -38,6 +38,20 @@ class SaltCMD(object): action='store_true' help='Instead of using shell globs to evaluate the target'\ + ' servers, use pcre regular expressions') + parser.add_option('-Q', + '--query', + dest='query', + default=False, + action='store_true', + help='Execute a salt command query, this can be used to find'\ + + ' previous function calls, of to look up a call that'\ + + ' occured at a specific time.') + parser.add_option('-c', + '--cmd', + dest='cmd', + default='', + help='Used with the Query option, pass in the command to get'\ + + ' results from.') options, args = parser.parse_args() @@ -45,9 +59,13 @@ class SaltCMD(object): opts['timeout'] = options.timeout opts['pcre'] = options.pcre - opts['tgt'] = args[0] - opts['fun'] = args[1] - opts['arg'] = args[2:] + if options.query: + opts['query'] = options.query + opts['cmd'] = options.cmd + else: + opts['tgt'] = args[0] + opts['fun'] = args[1] + opts['arg'] = args[2:] return opts @@ -55,6 +73,8 @@ class SaltCMD(object): ''' Execute the salt command line ''' + if opts['query']: + cli = local = salt.client.LocalClient() args = [self.opts['tgt'], self.opts['fun'], diff --git a/salt/client.py b/salt/client.py index c53c4b674a..a86f58c580 100644 --- a/salt/client.py +++ b/salt/client.py @@ -117,6 +117,34 @@ class LocalClient(object): return ret time.sleep(0.02) + def find_cmd(self, cmd): + ''' + Hunt through the old salt calls for when cmd was run, return a dict: + {'': } + ''' + job_dir = os.path.join(self.opts['cachedir'], 'jobs') + ret = {} + for jid in os.listdir(job_dir): + jid_dir = os.path.join(job_dir, jid) + loadp = os.path.join(jid_dir, '.load.p') + if os.path.isfile(loadp): + try: + load = pickle.load(open(loadp, 'r')) + if load['fun'] == cmd: + # We found a match! Add the return values + ret[jid] = {} + for host in os.listdir(jid_dir): + host_dir = os.path.join(jid_dir, host) + retp = os.path.join(host_dir, 'return.p') + if not os.path.isfile(retp): + continue + ret[jid][host] = pickle.load(open(retp)) + except: + continue + else: + continue + return ret + def check_minions(self, expr, expr_form='glob'): ''' Check the passed regex against the available minions' public