mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Cleanup the salt-call
(salt.cli.SaltCall
) binary parser.
Reused the logging and output mix-ins.
This commit is contained in:
parent
060f33a15a
commit
261450e90f
3 changed files with 100 additions and 107 deletions
|
@ -181,122 +181,36 @@ class SaltKey(parsers.SaltKeyOptionParser):
|
|||
key.run()
|
||||
|
||||
|
||||
class SaltCall(object):
|
||||
class SaltCall(parsers.SaltCallOptionParser):
|
||||
'''
|
||||
Used to locally execute a salt command
|
||||
'''
|
||||
def __init__(self):
|
||||
self.opts = self.__parse()
|
||||
|
||||
def __parse(self):
|
||||
'''
|
||||
Parse the command line arguments
|
||||
'''
|
||||
usage = "%prog [options] <function> [arguments]"
|
||||
parser = optparse.OptionParser(
|
||||
version='salt-call {0}'.format(VERSION),
|
||||
usage=usage
|
||||
)
|
||||
|
||||
parser.add_option('-g',
|
||||
'--grains',
|
||||
dest='grains_run',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help='Return the information generated by the salt grains')
|
||||
parser.add_option('-m',
|
||||
'--module-dirs',
|
||||
dest='module_dirs',
|
||||
default='',
|
||||
help=('Specify an additional directories to pull modules '
|
||||
'from, multiple directories can be delimited by commas'))
|
||||
parser.add_option('-c',
|
||||
'--config',
|
||||
dest='conf_file',
|
||||
default='/etc/salt/minion',
|
||||
help='Pass in an alternative configuration file')
|
||||
parser.add_option('-d',
|
||||
'--doc',
|
||||
dest='doc',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help=('Return the documentation for the specified module of '
|
||||
'for all modules if none are specified'))
|
||||
parser.add_option('-l',
|
||||
'--log-level',
|
||||
default='info',
|
||||
dest='log_level',
|
||||
help='Set the output level for salt-call')
|
||||
parser.add_option('--raw-out',
|
||||
default=False,
|
||||
action='store_true',
|
||||
dest='raw_out',
|
||||
help=('Print the output from the salt command in raw python '
|
||||
'form, this is suitable for re-reading the output into '
|
||||
'an executing python script with eval.'))
|
||||
parser.add_option('--text-out',
|
||||
default=False,
|
||||
action='store_true',
|
||||
dest='txt_out',
|
||||
help=('Print the output from the salt command in the same '
|
||||
'form the shell would.'))
|
||||
parser.add_option('--yaml-out',
|
||||
default=False,
|
||||
action='store_true',
|
||||
dest='yaml_out',
|
||||
help='Print the output from the salt command in yaml.')
|
||||
parser.add_option('--json-out',
|
||||
default=False,
|
||||
action='store_true',
|
||||
dest='json_out',
|
||||
help='Print the output from the salt command in json.')
|
||||
parser.add_option('--no-color',
|
||||
default=False,
|
||||
dest='no_color',
|
||||
action='store_true',
|
||||
help='Disable all colored output')
|
||||
|
||||
options, args = parser.parse_args()
|
||||
|
||||
opts = {}
|
||||
opts.update(salt.config.minion_config(options.conf_file))
|
||||
|
||||
for k, v in options.__dict__.items():
|
||||
if k == 'module_dirs':
|
||||
opts[k] = v.split(',')
|
||||
else:
|
||||
opts[k] = v
|
||||
|
||||
if len(args) >= 1:
|
||||
opts['fun'] = args[0]
|
||||
opts['arg'] = args[1:]
|
||||
elif opts['grains_run']:
|
||||
pass
|
||||
else:
|
||||
# salt-call should not ever be called without arguments
|
||||
parser.print_help()
|
||||
parser.exit(1)
|
||||
|
||||
verify_env([opts['pki_dir'],
|
||||
opts['cachedir'],
|
||||
os.path.dirname(opts['log_file']),
|
||||
],
|
||||
opts['user'],
|
||||
permissive=opts['permissive_pki_access'])
|
||||
|
||||
return opts
|
||||
|
||||
def run(self):
|
||||
'''
|
||||
Execute the salt call!
|
||||
'''
|
||||
import salt.log
|
||||
salt.log.setup_console_logger(
|
||||
self.opts['log_level'],
|
||||
log_format=self.opts['log_fmt_console'],
|
||||
date_format=self.opts['log_datefmt'],
|
||||
self.parse_args()
|
||||
|
||||
verify_env([
|
||||
self.config['pki_dir'],
|
||||
self.config['cachedir'],
|
||||
os.path.dirname(self.config['log_file'])
|
||||
],
|
||||
self.config['user'],
|
||||
permissive=self.config['permissive_pki_access']
|
||||
)
|
||||
caller = salt.cli.caller.Caller(self.opts)
|
||||
|
||||
caller = salt.cli.caller.Caller(self.config)
|
||||
|
||||
if self.options.doc:
|
||||
caller.print_docs()
|
||||
self.exit(0)
|
||||
|
||||
if self.options.grains_run:
|
||||
caller.print_grains()
|
||||
self.exit(0)
|
||||
|
||||
caller.run()
|
||||
|
||||
|
||||
|
|
|
@ -837,3 +837,54 @@ class SaltKeyOptionParser(OptionParser, ConfigDirMixIn, LogLevelMixIn,
|
|||
# It was decided to always set this to info, since it really all is
|
||||
# info or error.
|
||||
self.config['loglevel'] = 'info'
|
||||
|
||||
|
||||
class SaltCallOptionParser(OptionParser, ConfigDirMixIn, LogLevelMixIn,
|
||||
OutputOptionsWithTextMixIn):
|
||||
__metaclass__ = OptionParserMeta
|
||||
|
||||
description = "XXX: Add salt-call description"
|
||||
|
||||
usage = "%prog [options] <function> [arguments]"
|
||||
|
||||
def _mixin_setup(self):
|
||||
self.add_option(
|
||||
'-g', '--grains',
|
||||
dest='grains_run',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help='Return the information generated by the salt grains'
|
||||
)
|
||||
self.add_option(
|
||||
'-m', '--module-dirs',
|
||||
default='',
|
||||
help=('Specify an additional directories to pull modules from, '
|
||||
'multiple directories can be delimited by commas')
|
||||
)
|
||||
self.add_option(
|
||||
'-d', '--doc', '--documentation',
|
||||
dest='doc',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help=('Return the documentation for the specified module or for '
|
||||
'all modules if none are specified.')
|
||||
)
|
||||
|
||||
def _mixin_after_parsed(self):
|
||||
if not self.args and not self.options.grains_run and not self.options.doc:
|
||||
self.print_help()
|
||||
self.exit(1)
|
||||
|
||||
elif len(self.args) >= 1:
|
||||
if self.options.grains_run:
|
||||
self.error("-g/--grains does not accept any arguments")
|
||||
|
||||
self.config['fun'] = self.args[0]
|
||||
self.config['arg'] = self.args[1:]
|
||||
|
||||
def setup_config(self):
|
||||
return config.minion_config(self.get_config_file_path('minion'))
|
||||
|
||||
def process_module_dirs(self):
|
||||
if self.options.module_dirs:
|
||||
self.config['module_dirs'] = self.options.module_dirs.split(',')
|
||||
|
|
28
tests/integration/shell/call.py
Normal file
28
tests/integration/shell/call.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
tests.integration.shell.call
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
:copyright: © 2012 UfSoft.org - :email:`Pedro Algarvio (pedro@algarvio.me)`
|
||||
:license: Apache 2.0, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
# Import salt libs
|
||||
from saltunittest import TestLoader, TextTestRunner
|
||||
import integration
|
||||
from integration import TestDaemon
|
||||
|
||||
|
||||
class CallTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
|
||||
_call_binary_ = 'salt-call'
|
||||
|
||||
if __name__ == "__main__":
|
||||
loader = TestLoader()
|
||||
tests = loader.loadTestsFromTestCase(CallTest)
|
||||
print('Setting up Salt daemons to execute tests')
|
||||
with TestDaemon():
|
||||
runner = TextTestRunner(verbosity=1).run(tests)
|
||||
sys.exit(runner.wasSuccessful())
|
Loading…
Add table
Reference in a new issue