Cleanup the salt-run(salt.cli.SaltRun) binary parser.

Reused the timeout mix-in. All cli tools parsers also reuse the config mixin.
This commit finishes cleaning up the cli tool parsers.
This commit is contained in:
Pedro Algarvio 2012-08-04 23:25:37 +01:00
parent 261450e90f
commit ac1ee79fa6
4 changed files with 58 additions and 84 deletions

View File

@ -214,69 +214,21 @@ class SaltCall(parsers.SaltCallOptionParser):
caller.run()
class SaltRun(object):
'''
Used to execute salt convenience functions
'''
def __init__(self):
self.opts = self.__parse()
def __parse(self):
'''
Parse the command line arguments
'''
parser = optparse.OptionParser(version="%%prog %s" % VERSION)
parser.add_option('-c',
'--config',
dest='conf_file',
default='/etc/salt/master',
help=('Change the location of the master configuration; '
'default=/etc/salt/master'))
parser.add_option('-t',
'--timeout',
dest='timeout',
default='1',
help=('Change the timeout, if applicable, for the salt runner; '
'default=1'))
parser.add_option('-d',
'--doc',
'--documentation',
dest='doc',
default=False,
action='store_true',
help=('Display documentation for runners, pass a module or '
'a runner to see documentation on only that '
'module/runner.'))
options, args = parser.parse_args()
opts = {}
opts.update(salt.config.master_config(options.conf_file))
opts['conf_file'] = options.conf_file
opts['doc'] = options.doc
if len(args) > 0:
opts['fun'] = args[0]
else:
opts['fun'] = ''
if len(args) > 1:
opts['arg'] = args[1:]
else:
opts['arg'] = []
return opts
class SaltRun(parsers.SaltRunOptionParser):
def run(self):
'''
Execute salt-run
'''
runner = salt.runner.Runner(self.opts)
# Run this here so SystemExit isn't raised
# anywhere else when someone tries to use
# the runners via the python api
try:
runner.run()
except SaltClientError as exc:
raise SystemExit(str(exc))
self.parse_args()
runner = salt.runner.Runner(self.config)
if self.options.doc:
runner._print_docs()
else:
# Run this here so SystemExit isn't raised anywhere else when
# someone tries to use the runners via the python api
try:
runner.run()
except SaltClientError as exc:
raise SystemExit(str(exc))

View File

@ -888,3 +888,34 @@ class SaltCallOptionParser(OptionParser, ConfigDirMixIn, LogLevelMixIn,
def process_module_dirs(self):
if self.options.module_dirs:
self.config['module_dirs'] = self.options.module_dirs.split(',')
class SaltRunOptionParser(OptionParser, ConfigDirMixIn, TimeoutMixIn):
__metaclass__ = OptionParserMeta
default_timeout = 1
usage = "%prog [options]"
def _mixin_setup(self):
self.add_option(
'-d', '--doc', '--documentation',
dest='doc',
default=False,
action='store_true',
help=('Display documentation for runners, pass a module or a '
'runner to see documentation on only that module/runner.')
)
def _mixin_after_parsed(self):
if len(self.args) > 0:
self.config['fun'] = self.args[0]
else:
self.config['fun'] = ''
if len(self.args) > 1:
self.config['arg'] = self.args[1:]
else:
self.config['arg'] = []
def setup_config(self):
return config.master_config(self.get_config_file_path('master'))

View File

@ -237,10 +237,7 @@ class ModuleCase(TestCase):
Generate the tools to test a module
'''
self.client = salt.client.LocalClient(
os.path.join(
INTEGRATION_TEST_DIR,
'files', 'conf', 'master'
)
os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf', 'master')
)
def run_function(self, function, arg=(), **kwargs):
@ -248,11 +245,9 @@ class ModuleCase(TestCase):
Run a single salt function and condition the return down to match the
behavior of the raw function call
'''
orig = self.client.cmd('minion',
function,
arg,
timeout=100,
kwarg=kwargs)
orig = self.client.cmd(
'minion', function, arg, timeout=100, kwarg=kwargs
)
return orig['minion']
def state_result(self, ret):
@ -273,10 +268,7 @@ class ModuleCase(TestCase):
Return the options used for the minion
'''
return salt.config.minion_config(
os.path.join(
INTEGRATION_TEST_DIR,
'files', 'conf', 'minion'
)
os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf', 'minion')
)
@property
@ -285,10 +277,7 @@ class ModuleCase(TestCase):
Return the options used for the minion
'''
return salt.config.minion_config(
os.path.join(
INTEGRATION_TEST_DIR,
'files', 'conf', 'master'
)
os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf', 'master')
)
@ -353,7 +342,7 @@ class ShellCase(TestCase):
'''
Execute salt-run
'''
mconf = os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf', 'master')
mconf = os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf')
arg_str = '-c {0} {1}'.format(mconf, arg_str)
return self.run_script('salt-run', arg_str)
@ -367,10 +356,9 @@ class ShellCase(TestCase):
'{0} {1} {2}'.format(options, fun, ' '.join(arg))
)
opts = salt.config.master_config(
os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf', 'master'))
opts.update({'doc': False,
'fun': fun,
'arg': arg})
os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf', 'master')
)
opts.update({'doc': False, 'fun': fun, 'arg': arg})
runner = salt.runner.Runner(opts)
ret['fun'] = runner.run()
return ret

View File

@ -10,10 +10,13 @@ import integration
from integration import TestDaemon
class RunTest(integration.ShellCase):
class RunTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
'''
Test the salt-run command
'''
_call_binary_ = 'salt-run'
def test_in_docs(self):
'''
test the salt-run docs system