mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 17:09:03 +00:00
Merge pull request #47580 from sastorsl/file.symlink-updates
File.symlink updates
This commit is contained in:
commit
f5c3e2caec
@ -1269,6 +1269,9 @@ def symlink(
|
||||
renamed to the backupname. If the backupname already
|
||||
exists and force is False, the state will fail. Otherwise, the
|
||||
backupname will be removed first.
|
||||
An absolute path OR a basename file/directory name must be provided.
|
||||
The latter will be placed relative to the symlink destination's parent
|
||||
directory.
|
||||
|
||||
makedirs
|
||||
If the location of the symlink does not already have a parent directory
|
||||
@ -1400,15 +1403,32 @@ def symlink(
|
||||
elif os.path.isfile(name) or os.path.isdir(name):
|
||||
# It is not a link, but a file or dir
|
||||
if backupname is not None:
|
||||
if not os.path.isabs(backupname):
|
||||
if backupname == os.path.basename(backupname):
|
||||
backupname = os.path.join(
|
||||
os.path.dirname(os.path.normpath(name)),
|
||||
backupname)
|
||||
else:
|
||||
return _error(ret, (('Backupname must be an absolute path '
|
||||
'or a file name: {0}').format(backupname)))
|
||||
# Make a backup first
|
||||
if os.path.lexists(backupname):
|
||||
if not force:
|
||||
return _error(ret, ((
|
||||
'File exists where the backup target {0} should go'
|
||||
).format(backupname)))
|
||||
return _error(ret, (('Symlink & backup dest exists and Force not set.'
|
||||
' {0} -> {1} - backup: {2}').format(
|
||||
name, target, backupname)))
|
||||
else:
|
||||
__salt__['file.remove'](backupname)
|
||||
os.rename(name, backupname)
|
||||
try:
|
||||
__salt__['file.move'](name, backupname)
|
||||
except Exception as exc:
|
||||
ret['changes'] = {}
|
||||
log.debug(
|
||||
'Encountered error renaming %s to %s',
|
||||
name, backupname, exc_info=True
|
||||
)
|
||||
return _error(ret, ('Unable to rename {0} to backup {1} -> '
|
||||
': {2}'.format(name, backupname, exc)))
|
||||
elif force:
|
||||
# Remove whatever is in the way
|
||||
if __salt__['file.is_link'](name):
|
||||
|
@ -262,6 +262,26 @@ class TestFileState(TestCase, LoaderModuleMockMixin):
|
||||
group=group, backupname='SALT'),
|
||||
ret)
|
||||
|
||||
with patch.dict(filestate.__salt__, {'config.manage_mode': mock_t,
|
||||
'file.user_to_uid': mock_uid,
|
||||
'file.group_to_gid': mock_gid,
|
||||
'file.is_link': mock_f,
|
||||
'file.readlink': mock_target,
|
||||
'user.info': mock_empty,
|
||||
'user.current': mock_user}):
|
||||
with patch.dict(filestate.__opts__, {'test': False}):
|
||||
with patch.object(os.path, 'isabs', mock_t):
|
||||
with patch.object(os.path, 'isabs', mock_f):
|
||||
comt = ('Backupname must be an absolute path '
|
||||
'or a file name: {0}').format('tmp/SALT')
|
||||
ret.update({'comment': comt,
|
||||
'result': False,
|
||||
'pchanges': {'new': name}})
|
||||
self.assertDictEqual(filestate.symlink
|
||||
(name, target, user=user,
|
||||
group=group, backupname='tmp/SALT'),
|
||||
ret)
|
||||
|
||||
with patch.dict(filestate.__salt__, {'config.manage_mode': mock_t,
|
||||
'file.user_to_uid': mock_uid,
|
||||
'file.group_to_gid': mock_gid,
|
||||
|
Loading…
Reference in New Issue
Block a user