Merge pull request #36750 from terminalmage/issue36746

Add the CLI client and pub_data as class attributes
This commit is contained in:
Thomas S Hatch 2016-10-13 13:38:33 -06:00 committed by GitHub
commit 10d255c511
3 changed files with 42 additions and 20 deletions

View file

@ -44,7 +44,7 @@ class SaltCMD(parsers.SaltCMDOptionParser):
# process is run with the -a flag
skip_perm_errors = self.options.eauth != ''
local = salt.client.get_local_client(
self.local_client = salt.client.get_local_client(
self.get_config_file_path(),
skip_perm_errors=skip_perm_errors)
except SaltClientError as exc:
@ -55,7 +55,7 @@ class SaltCMD(parsers.SaltCMDOptionParser):
self._run_batch()
else:
if self.options.timeout <= 0:
self.options.timeout = local.opts['timeout']
self.options.timeout = self.local_client.opts['timeout']
kwargs = {
'tgt': self.config['tgt'],
@ -115,20 +115,20 @@ class SaltCMD(parsers.SaltCMDOptionParser):
kwargs['eauth'] = self.options.eauth
if self.config['async']:
jid = local.cmd_async(**kwargs)
jid = self.local_client.cmd_async(**kwargs)
print_cli('Executed command with job ID: {0}'.format(jid))
return
retcodes = []
try:
# local will be None when there was an error
errors = []
if local:
if self.local_client:
if self.options.subset:
cmd_func = local.cmd_subset
cmd_func = self.local_client.cmd_subset
kwargs['sub'] = self.options.subset
kwargs['cli'] = True
else:
cmd_func = local.cmd_cli
cmd_func = self.local_client.cmd_cli
if self.options.progress:
kwargs['progress'] = True
@ -147,7 +147,7 @@ class SaltCMD(parsers.SaltCMDOptionParser):
elif self.config['fun'] == 'sys.doc':
ret = {}
out = ''
for full_ret in local.cmd_cli(**kwargs):
for full_ret in self.local_client.cmd_cli(**kwargs):
ret_, out, retcode = self._format_ret(full_ret)
ret.update(ret_)
self._output_ret(ret, out)

View file

@ -602,7 +602,7 @@ class LocalClient(object):
:returns: A generator
'''
arg = salt.utils.args.condition_input(arg, kwarg)
pub_data = self.run_job(
self.pub_data = self.run_job(
tgt,
fun,
arg,
@ -611,13 +611,13 @@ class LocalClient(object):
timeout,
**kwargs)
if not pub_data:
yield pub_data
if not self.pub_data:
yield self.pub_data
else:
try:
for fn_ret in self.get_cli_event_returns(
pub_data['jid'],
pub_data['minions'],
self.pub_data['jid'],
self.pub_data['minions'],
self._get_timeout(timeout),
tgt,
expr_form,
@ -630,12 +630,16 @@ class LocalClient(object):
yield fn_ret
except KeyboardInterrupt:
msg = ('Exiting on Ctrl-C\nThis job\'s jid is:\n{0}\n'
'The minions may not have all finished running and any '
'remaining minions will return upon completion. To '
'look up the return data for this job later run:\n'
'salt-run jobs.lookup_jid {0}').format(pub_data['jid'])
raise SystemExit(msg)
raise SystemExit(
'\n'
'This job\'s jid is: {0}\n'
'Exiting gracefully on Ctrl-c\n'
'The minions may not have all finished running and any '
'remaining minions will return upon completion. To look '
'up the return data for this job later, run the following '
'command:\n\n'
'salt-run jobs.lookup_jid {0}'.format(self.pub_data['jid'])
)
def cmd_iter(
self,

View file

@ -46,9 +46,27 @@ def _handle_signals(client, signum, sigframe):
hardcrash = client.options.hard_crash
except (AttributeError, KeyError):
hardcrash = False
if signum == signal.SIGINT:
exit_msg = '\nExiting gracefully on Ctrl-c'
try:
jid = client.local_client.pub_data['jid']
exit_msg += (
'\n'
'This job\'s jid is: {0}\n'
'The minions may not have all finished running and any remaining '
'minions will return upon completion. To look up the return data '
'for this job later, run the following command:\n\n'
'salt-run jobs.lookup_jid {0}'.format(jid)
)
except (AttributeError, KeyError):
pass
else:
exit_msg = None
_handle_interrupt(
SystemExit('\nExiting gracefully on Ctrl-c'),
Exception('\nExiting with hard crash Ctrl-c'),
SystemExit(exit_msg),
Exception('\nExiting with hard crash on Ctrl-c'),
hardcrash, trace=trace)