From 2fa941c4732c6103d713e24ce10f99f523b3e0cd Mon Sep 17 00:00:00 2001 From: Samuel M Smith Date: Tue, 1 Oct 2013 17:21:45 -0600 Subject: [PATCH] Added List of minions that ran a particular job to the job list cache data returned by runners runner.jobs.list_jobs runner.jobs.list_job Also added function runner.jobs.list_job to list a single job given by jid --- salt/client/api.py | 2 +- salt/master.py | 5 +++++ salt/runner.py | 10 +++++----- salt/runners/jobs.py | 42 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/salt/client/api.py b/salt/client/api.py index 8875b2de52..a91fa6dc53 100644 --- a/salt/client/api.py +++ b/salt/client/api.py @@ -130,7 +130,7 @@ class APIClient(object): ''' return self.localClient.run_job(**kwargs) - def minion_sync(self, *args, **kwargs): + def minion_sync(self, **kwargs): ''' Wrap LocalClient for running :ref:`execution modules ` diff --git a/salt/master.py b/salt/master.py index e8c935c5d5..6811760342 100644 --- a/salt/master.py +++ b/salt/master.py @@ -2359,6 +2359,11 @@ class ClearFuncs(object): clear_load, salt.utils.fopen(os.path.join(jid_dir, '.load.p'), 'w+') ) + # save the minions to a cache so we can see in the UI + self.serial.dump( + minions, + salt.utils.fopen(os.path.join(jid_dir, '.minions.p'), 'w+') + ) if self.opts['ext_job_cache']: try: fstr = '{0}.save_load'.format(self.opts['ext_job_cache']) diff --git a/salt/runner.py b/salt/runner.py index 253de2f874..0bebb965ca 100644 --- a/salt/runner.py +++ b/salt/runner.py @@ -30,7 +30,7 @@ class RunnerClient(object): self.opts = opts self.functions = salt.loader.runner(opts) - def _proc_runner(self, tag, fun, low, user): + def _proc_runner(self, fun, low, user, tag, jid): ''' Run this method in a multiprocess target to execute the runner in a multiprocess and fire the return data on the event bus @@ -38,7 +38,7 @@ class RunnerClient(object): salt.utils.daemonize() event = salt.utils.event.MasterEvent(self.opts['sock_dir']) data = {'fun': "runner.{0}".format(fun), - 'jid': low['jid'], + 'jid': jid, 'user': user, } event.fire_event(data, tagify('new', base=tag)) @@ -101,12 +101,12 @@ class RunnerClient(object): ''' jid = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now()) tag = tagify(jid, prefix='run') - low['tag'] = tag - low['jid'] = jid + #low['tag'] = tag + #low['jid'] = jid proc = multiprocessing.Process( target=self._proc_runner, - args=(tag, fun, low, user)) + args=(fun, low, user, tag, jid)) proc.start() return tag diff --git a/salt/runners/jobs.py b/salt/runners/jobs.py index 100cad5bc8..86e99547dc 100644 --- a/salt/runners/jobs.py +++ b/salt/runners/jobs.py @@ -97,6 +97,40 @@ def lookup_jid(jid, ext_source=None): return ret +def list_job(jid): + ''' + List a specific job given by its jid + + CLI Example: + + .. code-block:: bash + + salt-run jobs.list_job 20130916125524463507 + ''' + serial = salt.payload.Serial(__opts__) + ret = {} + jid_dir = salt.utils.jid_dir(jid, __opts__['cachedir'], __opts__['hash_type']) + loadpath = os.path.join(jid_dir, '.load.p') + minionspath = os.path.join(jid_dir, '.minions.p') + if os.path.isfile(loadpath): + load = serial.load(salt.utils.fopen(loadpath, 'rb')) + jid = load['jid'] + ret = { 'jid': jid, + 'Start Time': salt.utils.jid_to_time(jid), + 'Function': load['fun'], + 'Arguments': list(load['arg']), + 'Target': load['tgt'], + 'Target-type': load['tgt_type'], + 'User': load.get('user', 'root')} + if os.path.isfile(minionspath): + minions = serial.load(salt.utils.fopen(minionspath, 'rb')) + ret['Minions'] = minions + + salt.output.display_output(ret, 'yaml', __opts__) + return ret + + + def list_jobs(): ''' List all detectable jobs and associated functions @@ -114,16 +148,22 @@ def list_jobs(): t_path = os.path.join(job_dir, top) for final in os.listdir(t_path): loadpath = os.path.join(t_path, final, '.load.p') + minionspath = os.path.join(t_path, final, '.minions.p') if not os.path.isfile(loadpath): continue load = serial.load(salt.utils.fopen(loadpath, 'rb')) jid = load['jid'] - ret[jid] = {'Start Time': salt.utils.jid_to_time(jid), + ret[jid] = {'jid': jid, + 'Start Time': salt.utils.jid_to_time(jid), 'Function': load['fun'], 'Arguments': list(load['arg']), 'Target': load['tgt'], 'Target-type': load['tgt_type'], 'User': load.get('user', 'root')} + if os.path.isfile(minionspath): + minions = serial.load(salt.utils.fopen(minionspath, 'rb')) + ret[jid]['Minions'] = minions + salt.output.display_output(ret, 'yaml', __opts__) return ret