Merge pull request #10049 from JustinCarmony/saltfile

Saltfile Feature
This commit is contained in:
Thomas S Hatch 2014-02-03 15:08:44 -08:00
commit 7cb424ea98

View File

@ -19,6 +19,7 @@ import getpass
import logging import logging
import optparse import optparse
import traceback import traceback
import yaml
from functools import partial from functools import partial
# Import salt libs # Import salt libs
@ -272,6 +273,109 @@ class MergeConfigMixIn(object):
self.config[option.dest]) self.config[option.dest])
class SaltfileMixIn(object):
__metaclass__ = MixInMeta
_mixin_prio_ = -20
def _mixin_setup(self):
self.add_option(
'--saltfile', default=None,
help='Specify the path to a Saltfile. If not passed, on will be '
'searched for in the current working directory'
)
def process_saltfile(self):
if self.options.saltfile is None:
# No one passed a Saltfile as an option, environment variable!?
self.options.saltfile = os.environ.get('SALT_SALTFILE', None)
if self.options.saltfile is None:
# If we're here, no one passed a Saltfile either to the CLI tool or
# as an environment variable.
# Is there a Saltfile in the current directory?
saltfile = os.path.join(os.getcwd(), 'Saltfile')
if os.path.isfile(saltfile):
self.options.saltfile = saltfile
if not self.options.saltfile:
# There's still no valid Saltfile? No need to continue...
return
if not os.path.isfile(self.options.saltfile):
self.error(
'{0!r} file does not exist.\n'.format(self.options.saltfile
)
)
# Make sure we have an absolute path
self.options.saltfile = os.path.abspath(self.options.saltfile)
# Make sure we let the user know that we will be loading a Saltfile
logging.getLogger(__name__).info(
'Loading Saltfile from {0!r}'.format(self.options.saltfile)
)
saltfile_config = config._read_conf_file(saltfile)
if not saltfile_config:
# No configuration was loaded from the Saltfile
return
if self.get_prog_name() not in saltfile_config:
# There's no configuration specific to the CLI tool. Stop!
return
# We just want our own configuration
cli_config = saltfile_config[self.get_prog_name()]
# If there are any options, who's names match any key from the loaded
# Saltfile, we need to update it's default value
for option in self.option_list:
if option.dest is None:
# --version does not have dest attribute set for example.
continue
if option.dest not in cli_config:
# If we don't have anything in Saltfile for this option, let's
# continue processing right now
continue
# Get the passed value from shell. If empty get the default one
default = self.defaults.get(option.dest)
value = getattr(self.options, option.dest, default)
if value != default:
# The user passed an argument, we won't override it with the
# one from Saltfile, if any
continue
# We reched this far! Set the Saltfile value on the option
setattr(self.options, option.dest, cli_config[option.dest])
# Let's also search for options referred in any option groups
for group in self.option_groups:
for option in group.option_list:
if option.dest is None:
continue
if option.dest not in cli_config:
# If we don't have anything in Saltfile for this option,
# let's continue processing right now
continue
# Get the passed value from shell. If empty get the default one
default = self.defaults.get(option.dest)
value = getattr(self.options, option.dest, default)
if value != default:
# The user passed an argument, we won't override it with
# the one from Saltfile, if any
continue
if option.dest in cli_config:
setattr(self.options,
option.dest,
cli_config[option.dest])
class ConfigDirMixIn(object): class ConfigDirMixIn(object):
__metaclass__ = MixInMeta __metaclass__ = MixInMeta
_mixin_prio_ = -10 _mixin_prio_ = -10
@ -300,7 +404,9 @@ class ConfigDirMixIn(object):
self.options.config_dir = os.path.abspath(self.options.config_dir) self.options.config_dir = os.path.abspath(self.options.config_dir)
if hasattr(self, 'setup_config'): if hasattr(self, 'setup_config'):
self.config = self.setup_config() if not hasattr(self, 'config'):
self.config = {}
self.config.update(self.setup_config())
def get_config_file_path(self, configfile=None): def get_config_file_path(self, configfile=None):
if configfile is None: if configfile is None:
@ -2023,7 +2129,7 @@ class SaltRunOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn,
class SaltSSHOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn, class SaltSSHOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn,
LogLevelMixIn, TargetOptionsMixIn, LogLevelMixIn, TargetOptionsMixIn,
OutputOptionsMixIn): OutputOptionsMixIn, SaltfileMixIn):
__metaclass__ = OptionParserMeta __metaclass__ = OptionParserMeta
usage = '%prog [options]' usage = '%prog [options]'
@ -2081,7 +2187,7 @@ class SaltSSHOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn,
help='Set the number of concurrent minions to communicate with. ' help='Set the number of concurrent minions to communicate with. '
'This value defines how many processes are opened up at a ' 'This value defines how many processes are opened up at a '
'time to manage connections, the more running processes the ' 'time to manage connections, the more running processes the '
'faster communication should be, default is 25') 'faster communication should be, default is %default')
self.add_option( self.add_option(
'-i', '-i',
'--ignore-host-keys', '--ignore-host-keys',