Fix file.managed traceback when source is a list

If file.managed state has a source param which is a list, and none of
the URIs in that list are found, a traceback occurs. This commit
modifies salt.modules.file.source_list to raise an exception when source
is a list and no matches are found, and then catches that exception in
the file.managed state so that Salt can return gracefully with a False
result and meaningful error message.
This commit is contained in:
Erik Johnson 2014-01-07 19:12:58 -06:00
parent 8a5b4ae780
commit fa7e9fc6b3
2 changed files with 38 additions and 26 deletions

View File

@ -1967,6 +1967,7 @@ def source_list(source, source_hash, saltenv):
mdirs += ['{0}?saltenv={1}'.format(d, senv) mdirs += ['{0}?saltenv={1}'.format(d, senv)
for d in __salt__['cp.list_master_dirs'](senv)] for d in __salt__['cp.list_master_dirs'](senv)]
ret = None
for single in source: for single in source:
if isinstance(single, dict): if isinstance(single, dict):
# check the proto, if it is http or ftp then download the file # check the proto, if it is http or ftp then download the file
@ -1985,14 +1986,21 @@ def source_list(source, source_hash, saltenv):
fn_ = __salt__['cp.get_url'](single_src, dest) fn_ = __salt__['cp.get_url'](single_src, dest)
os.remove(fn_) os.remove(fn_)
if fn_: if fn_:
source = single_src ret = (single_src, single_hash)
source_hash = single_hash
break break
elif isinstance(single, salt._compat.string_types): elif isinstance(single, salt._compat.string_types):
if single[7:] in mfiles or single[7:] in mdirs: if single[7:] in mfiles or single[7:] in mdirs:
source = single ret = (single, source_hash)
break break
return source, source_hash if ret is None:
# None of the list items matched
raise CommandExecutionError(
'none of the specified sources were found'
)
else:
return ret
else:
return source, source_hash
def get_managed( def get_managed(
@ -2016,8 +2024,7 @@ def get_managed(
salt '*' file.get_managed /etc/httpd/conf.d/httpd.conf jinja salt://http/httpd.conf '{hash_type: 'md5', 'hsum': <md5sum>}' root root '755' base None None salt '*' file.get_managed /etc/httpd/conf.d/httpd.conf jinja salt://http/httpd.conf '{hash_type: 'md5', 'hsum': <md5sum>}' root root '755' base None None
''' '''
# If the file is a template and the contents is managed # Copy the file to the minion and templatize it
# then make sure to copy it down and templatize things.
sfn = '' sfn = ''
source_sum = {} source_sum = {}
if template and source: if template and source:

View File

@ -1186,31 +1186,36 @@ def managed(name,
context = {} context = {}
context['accumulator'] = _ACCUMULATORS[name] context['accumulator'] = _ACCUMULATORS[name]
if __opts__['test']: try:
ret['result'], ret['comment'] = __salt__['file.check_managed']( if __opts__['test']:
name, ret['result'], ret['comment'] = __salt__['file.check_managed'](
name,
source,
source_hash,
user,
group,
mode,
template,
makedirs,
context,
defaults,
__env__,
contents,
**kwargs
)
return ret
# If the source is a list then find which file exists
source, source_hash = __salt__['file.source_list'](
source, source,
source_hash, source_hash,
user, __env__
group,
mode,
template,
makedirs,
context,
defaults,
__env__,
contents,
**kwargs
) )
except CommandExecutionError as exc:
ret['result'] = False
ret['comment'] = 'Unable to manage file: {0}'.format(exc)
return ret return ret
# If the source is a list then find which file exists
source, source_hash = __salt__['file.source_list'](
source,
source_hash,
__env__
)
# Gather the source file from the server # Gather the source file from the server
try: try:
sfn, source_sum, comment_ = __salt__['file.get_managed']( sfn, source_sum, comment_ = __salt__['file.get_managed'](