mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Merge branch '2015.8' into '2016.3'
Conflicts: - salt/states/file.py - tests/unit/states/file_test.py
This commit is contained in:
commit
2b307b7ea1
@ -45,6 +45,10 @@ minion configuration file:
|
||||
The beacon system, like many others in Salt, can also be configured via the
|
||||
minion pillar, grains, or local config file.
|
||||
|
||||
.. note::
|
||||
The `inotify` beacon only works on OSes that have `inotify` kernel support.
|
||||
Currently this excludes FreeBSD, Mac OS X, and Windows.
|
||||
|
||||
Beacon Monitoring Interval
|
||||
--------------------------
|
||||
|
||||
|
@ -2521,11 +2521,11 @@ def append(path, *args, **kwargs):
|
||||
if ofile.read(len(linesep)) != linesep:
|
||||
ofile.seek(0, os.SEEK_END)
|
||||
ofile.write(linesep)
|
||||
|
||||
# Append lines in text mode
|
||||
with salt.utils.fopen(path, 'r+') as ofile:
|
||||
ofile.seek(0, os.SEEK_END)
|
||||
for line in args:
|
||||
ofile.write('{0}\n'.format(line))
|
||||
with salt.utils.fopen(path, 'a') as ofile:
|
||||
for new_line in args:
|
||||
ofile.write('{0}{1}'.format(new_line, os.linesep))
|
||||
|
||||
return 'Wrote {0} lines to "{1}"'.format(len(args), path)
|
||||
|
||||
|
@ -1372,7 +1372,10 @@ def describe(cwd, rev='HEAD', user=None, ignore_retcode=False):
|
||||
cwd = _expand_path(cwd, user)
|
||||
if not isinstance(rev, six.string_types):
|
||||
rev = str(rev)
|
||||
command = ['git', 'describe', rev]
|
||||
command = ['git', 'describe']
|
||||
if _LooseVersion(version(versioninfo=False)) >= _LooseVersion('1.5.6'):
|
||||
command.append('--always')
|
||||
command.append(rev)
|
||||
return _git_run(command,
|
||||
cwd=cwd,
|
||||
runas=user,
|
||||
|
@ -179,14 +179,14 @@ def _key_present(
|
||||
rc = __salt__['boto_kms.create_key'](
|
||||
policy, description, key_usage, region, key, keyid, profile
|
||||
)
|
||||
key_metadata = rc['key_metadata']
|
||||
kms_key_id = key_metadata['KeyId']
|
||||
if 'error' in rc:
|
||||
ret['result'] = False
|
||||
ret['comment'] = 'Failed to create key: {0}'.format(
|
||||
rc['error']['message']
|
||||
)
|
||||
return ret
|
||||
key_metadata = rc['key_metadata']
|
||||
kms_key_id = key_metadata['KeyId']
|
||||
rn = __salt__['boto_kms.create_alias'](
|
||||
alias, kms_key_id, region, key, keyid, profile
|
||||
)
|
||||
@ -207,13 +207,13 @@ def _key_present(
|
||||
rd = __salt__['boto_kms.describe_key'](
|
||||
alias, region, key, keyid, profile
|
||||
)
|
||||
key_metadata = rd['key_metadata']
|
||||
if 'error' in rd:
|
||||
ret['result'] = False
|
||||
ret['comment'] = 'Failed to update key: {0}.'.format(
|
||||
rd['error']['message']
|
||||
)
|
||||
return ret
|
||||
key_metadata = rd['key_metadata']
|
||||
_ret = _key_description(
|
||||
key_metadata, description, region, key, keyid, profile
|
||||
)
|
||||
|
@ -3536,17 +3536,17 @@ def append(name,
|
||||
|
||||
.. versionadded:: 0.9.5
|
||||
'''
|
||||
name = os.path.expanduser(name)
|
||||
|
||||
ret = {
|
||||
'name': name,
|
||||
ret = {'name': name,
|
||||
'changes': {},
|
||||
'pchanges': {},
|
||||
'result': False,
|
||||
'comment': ''}
|
||||
|
||||
if not name:
|
||||
return _error(ret, 'Must provide name to file.append')
|
||||
|
||||
name = os.path.expanduser(name)
|
||||
|
||||
if sources is None:
|
||||
sources = []
|
||||
|
||||
@ -3595,17 +3595,16 @@ def append(name,
|
||||
|
||||
with salt.utils.fopen(name, 'rb') as fp_:
|
||||
slines = fp_.readlines()
|
||||
slines = [item.rstrip() for item in slines]
|
||||
|
||||
count = 0
|
||||
test_lines = []
|
||||
|
||||
append_lines = []
|
||||
try:
|
||||
for chunk in text:
|
||||
if ignore_whitespace:
|
||||
if __salt__['file.search'](
|
||||
name,
|
||||
salt.utils.build_whitespace_split_regex(chunk),
|
||||
multiline=True):
|
||||
name,
|
||||
salt.utils.build_whitespace_split_regex(chunk),
|
||||
multiline=True):
|
||||
continue
|
||||
elif __salt__['file.search'](
|
||||
name,
|
||||
@ -3613,37 +3612,39 @@ def append(name,
|
||||
multiline=True):
|
||||
continue
|
||||
|
||||
lines = chunk.splitlines()
|
||||
for line_item in chunk.splitlines():
|
||||
append_lines.append('{0}'.format(line_item))
|
||||
|
||||
for line in lines:
|
||||
if __opts__['test']:
|
||||
ret['comment'] = 'File {0} is set to be updated'.format(name)
|
||||
ret['result'] = None
|
||||
test_lines.append('{0}\n'.format(line))
|
||||
else:
|
||||
__salt__['file.append'](name, line)
|
||||
count += 1
|
||||
except TypeError:
|
||||
return _error(ret, 'No text found to append. Nothing appended')
|
||||
|
||||
if __opts__['test']:
|
||||
nlines = slines + test_lines
|
||||
ret['comment'] = 'File {0} is set to be updated'.format(name)
|
||||
ret['result'] = None
|
||||
nlines = list(slines)
|
||||
nlines.extend(append_lines)
|
||||
if slines != nlines:
|
||||
if not salt.utils.istextfile(name):
|
||||
ret['changes']['diff'] = 'Replace binary file'
|
||||
else:
|
||||
# Changes happened, add them
|
||||
ret['changes']['diff'] = (
|
||||
''.join(difflib.unified_diff(slines, nlines))
|
||||
'\n'.join(difflib.unified_diff(slines, nlines))
|
||||
)
|
||||
else:
|
||||
ret['comment'] = 'File {0} is in correct state'.format(name)
|
||||
ret['result'] = True
|
||||
return ret
|
||||
|
||||
if append_lines:
|
||||
__salt__['file.append'](name, args=append_lines)
|
||||
ret['comment'] = 'Appended {0} lines'.format(len(append_lines))
|
||||
else:
|
||||
ret['comment'] = 'File {0} is in correct state'.format(name)
|
||||
|
||||
with salt.utils.fopen(name, 'rb') as fp_:
|
||||
nlines = fp_.readlines()
|
||||
nlines = [item.rstrip(os.linesep) for item in nlines]
|
||||
|
||||
if slines != nlines:
|
||||
if not salt.utils.istextfile(name):
|
||||
@ -3651,14 +3652,10 @@ def append(name,
|
||||
else:
|
||||
# Changes happened, add them
|
||||
ret['changes']['diff'] = (
|
||||
''.join(difflib.unified_diff(slines, nlines))
|
||||
)
|
||||
'\n'.join(difflib.unified_diff(slines, nlines)))
|
||||
|
||||
if count:
|
||||
ret['comment'] = 'Appended {0} lines'.format(count)
|
||||
else:
|
||||
ret['comment'] = 'File {0} is in correct state'.format(name)
|
||||
ret['result'] = True
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
|
@ -1630,31 +1630,18 @@ def latest(
|
||||
|
||||
targets = {}
|
||||
problems = []
|
||||
cmp_func = __salt__.get('pkg.version_cmp')
|
||||
minion_os = __salt__['grains.item']('os')['os']
|
||||
|
||||
if minion_os == 'Gentoo' and watch_flags:
|
||||
for pkg in desired_pkgs:
|
||||
if not avail[pkg] and not cur[pkg]:
|
||||
msg = 'No information found for {0!r}.'.format(pkg)
|
||||
for pkg in desired_pkgs:
|
||||
if not avail[pkg]:
|
||||
if not cur[pkg]:
|
||||
msg = 'No information found for \'{0}\'.'.format(pkg)
|
||||
log.error(msg)
|
||||
problems.append(msg)
|
||||
else:
|
||||
if salt.utils.compare_versions(ver1=cur[pkg], oper='!=', ver2=avail[pkg], cmp_func=cmp_func):
|
||||
targets[pkg] = avail[pkg]
|
||||
else:
|
||||
if not cur[pkg] or __salt__['portage_config.is_changed_uses'](pkg):
|
||||
targets[pkg] = avail[pkg]
|
||||
else:
|
||||
for pkg in desired_pkgs:
|
||||
if pkg not in avail:
|
||||
if not cur.get(pkg):
|
||||
msg = 'No information found for \'{0}\'.'.format(pkg)
|
||||
log.error(msg)
|
||||
problems.append(msg)
|
||||
elif not cur.get(pkg) \
|
||||
or salt.utils.compare_versions(ver1=cur[pkg], oper='<', ver2=avail[pkg], cmp_func=cmp_func):
|
||||
elif watch_flags \
|
||||
and __grains__.get('os') == 'Gentoo' \
|
||||
and __salt__['portage_config.is_changed_uses'](pkg):
|
||||
targets[pkg] = avail[pkg]
|
||||
else:
|
||||
targets[pkg] = avail[pkg]
|
||||
|
||||
if problems:
|
||||
return {
|
||||
|
@ -1079,88 +1079,6 @@ class FileTestCase(TestCase):
|
||||
ret.update({'comment': comt, 'result': True})
|
||||
self.assertDictEqual(filestate.uncomment(name, regex), ret)
|
||||
|
||||
# 'append' function tests: 1
|
||||
|
||||
def test_append(self):
|
||||
'''
|
||||
Test to ensure that some text appears at the end of a file.
|
||||
'''
|
||||
name = '/etc/motd'
|
||||
source = ['salt://motd/hr-messages.tmpl']
|
||||
sources = ['salt://motd/devops-messages.tmpl']
|
||||
text = ['Trust no one unless you have eaten much salt with him.']
|
||||
|
||||
ret = {'name': name,
|
||||
'result': False,
|
||||
'comment': '',
|
||||
'pchanges': {},
|
||||
'changes': {}}
|
||||
|
||||
comt = ('Must provide name to file.append')
|
||||
ret.update({'comment': comt, 'name': ''})
|
||||
self.assertDictEqual(filestate.append(''), ret)
|
||||
|
||||
comt = ('source and sources are mutually exclusive')
|
||||
ret.update({'comment': comt, 'name': name})
|
||||
self.assertDictEqual(filestate.append(name, source=source,
|
||||
sources=sources), ret)
|
||||
|
||||
mock_t = MagicMock(return_value=True)
|
||||
mock_f = MagicMock(return_value=False)
|
||||
mock_err = MagicMock(side_effect=[TypeError, True, True])
|
||||
with patch.dict(filestate.__salt__,
|
||||
{'file.directory_exists': mock_f,
|
||||
'file.makedirs': mock_t,
|
||||
'file.stats': mock_f,
|
||||
'cp.get_template': mock_f,
|
||||
'file.contains_regex_multiline': mock_err,
|
||||
'file.search': mock_err}):
|
||||
with patch.object(os.path, 'isdir', mock_t):
|
||||
comt = ('The following files will be changed:\n/etc:'
|
||||
' directory - new\n')
|
||||
ret.update({'comment': comt, 'name': name, 'pchanges': {'/etc': {'directory': 'new'}}})
|
||||
self.assertDictEqual(filestate.append(name, makedirs=True), ret)
|
||||
|
||||
with patch.object(os.path, 'isabs', mock_f):
|
||||
comt = ('Specified file {0} is not an absolute path'
|
||||
.format(name))
|
||||
ret.update({'comment': comt, 'pchanges': {}})
|
||||
self.assertDictEqual(filestate.append(name), ret)
|
||||
|
||||
with patch.object(os.path, 'isabs', mock_t):
|
||||
with patch.object(os.path, 'exists', mock_t):
|
||||
comt = ("Failed to load template file {0}".format(source))
|
||||
ret.pop('pchanges')
|
||||
ret.update({'comment': comt, 'name': source, 'data': [], })
|
||||
self.assertDictEqual(filestate.append(name, source=source),
|
||||
ret)
|
||||
|
||||
ret.pop('data', None)
|
||||
ret.update({'name': name})
|
||||
with patch.object(salt.utils, 'fopen',
|
||||
MagicMock(mock_open(read_data=''))):
|
||||
comt = ('No text found to append. Nothing appended')
|
||||
ret.update({'comment': comt, 'pchanges': {}})
|
||||
self.assertDictEqual(filestate.append(name, text=text),
|
||||
ret)
|
||||
|
||||
with patch.object(salt.utils, 'istextfile', mock_f):
|
||||
with patch.dict(filestate.__opts__, {'test': True}):
|
||||
change = {'diff': 'Replace binary file'}
|
||||
ret.update({'comment': '', 'result': None,
|
||||
'changes': change})
|
||||
self.assertDictEqual(filestate.append
|
||||
(name, text=text), ret)
|
||||
|
||||
with patch.dict(filestate.__opts__,
|
||||
{'test': False}):
|
||||
comt = ('File {0} is in correct state'
|
||||
.format(name))
|
||||
ret.update({'comment': comt, 'result': True,
|
||||
'changes': {}})
|
||||
self.assertDictEqual(filestate.append
|
||||
(name, text=text), ret)
|
||||
|
||||
# 'prepend' function tests: 1
|
||||
|
||||
def test_prepend(self):
|
||||
|
Loading…
Reference in New Issue
Block a user