Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stat_file when keep is set, instead of mirroring all file permissions #40609

Merged
merged 1 commit into from Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
stat file when placing it on server instead of caching
remove chmod of files in the fileserver
add stat_file to cp module
use cp.stat_file to get file mode in file module
add a note about keep not working with ssh
  • Loading branch information
gtmanfred committed Apr 10, 2017
commit 6e34c2b5e5e849302af7ccd00509929c3809c658
75 changes: 1 addition & 74 deletions salt/fileclient.py
Expand Up @@ -787,22 +787,6 @@ def get_file(self,
if not fnd_path:
return ''

try:
fnd_mode = fnd.get('stat', [])[0]
except (IndexError, TypeError):
fnd_mode = None

if not salt.utils.is_windows():
if fnd_mode is not None:
try:
if os.stat(dest).st_mode != fnd_mode:
try:
os.chmod(dest, fnd_mode)
except OSError as exc:
log.warning('Failed to chmod %s: %s', dest, exc)
except Exception:
pass

return fnd_path

def file_list(self, saltenv='base', prefix=''):
Expand Down Expand Up @@ -1054,47 +1038,7 @@ def get_file(self,
mode_local = None

if hash_local == hash_server:
if not salt.utils.is_windows():
if mode_server is None:
log.debug('No file mode available for \'%s\'', path)
elif mode_local is None:
log.debug(
'No file mode available for \'%s\'',
dest2check
)
else:
if mode_server == mode_local:
log.info(
'Fetching file from saltenv \'%s\', '
'** skipped ** latest already in cache '
'\'%s\', mode up-to-date', saltenv, path
)
else:
try:
os.chmod(dest2check, mode_server)
log.info(
'Fetching file from saltenv \'%s\', '
'** updated ** latest already in cache, '
'\'%s\', mode updated from %s to %s',
saltenv,
path,
salt.utils.st_mode_to_octal(mode_local),
salt.utils.st_mode_to_octal(mode_server)
)
except OSError as exc:
log.warning(
'Failed to chmod %s: %s', dest2check, exc
)
# We may not have been able to check/set the mode, but we
# don't want to re-download the file because of a failure
# in mode checking. Return the cached path.
return dest2check
else:
log.info(
'Fetching file from saltenv \'%s\', ** skipped ** '
'latest already in cache \'%s\'', saltenv, path
)
return dest2check
return dest2check

log.debug(
'Fetching file from saltenv \'%s\', ** attempting ** \'%s\'',
Expand Down Expand Up @@ -1211,23 +1155,6 @@ def get_file(self,
saltenv, path
)

if not salt.utils.is_windows():
if mode_server is not None:
try:
if os.stat(dest).st_mode != mode_server:
try:
os.chmod(dest, mode_server)
log.info(
'Fetching file from saltenv \'%s\', '
'** done ** \'%s\', mode set to %s',
saltenv,
path,
salt.utils.st_mode_to_octal(mode_server)
)
except OSError:
log.warning('Failed to chmod %s: %s', dest, exc)
except OSError:
pass
return dest

def file_list(self, saltenv='base', prefix=''):
Expand Down
22 changes: 22 additions & 0 deletions salt/modules/cp.py
Expand Up @@ -631,6 +631,28 @@ def hash_file(path, saltenv='base'):
return _client().hash_file(path, saltenv)


def stat_file(path, saltenv='base', octal=True):
'''
Return the permissions of a file, to get the permissions of a file on the
salt master file server prepend the path with salt://<file on server>
otherwise, prepend the file with / for a local file.

CLI Example:

.. code-block:: bash

salt '*' cp.stat_file salt://path/to/file
'''
path, senv = salt.utils.url.split_env(path)
if senv:
saltenv = senv

stat = _client().hash_and_stat_file(path, saltenv)[1]
if stat is None:
return stat
return salt.utils.st_mode_to_octal(stat[0]) if octal is True else stat[0]


def push(path, keep_symlinks=False, upload_path=None, remove_source=False):
'''
WARNING Files pushed to the master will have global read permissions..
Expand Down
11 changes: 9 additions & 2 deletions salt/modules/file.py
Expand Up @@ -4349,7 +4349,7 @@ def check_managed_changes(
if _urlparse(source).scheme in ('salt', 'file') \
or source.startswith('/'):
try:
mode = salt.utils.st_mode_to_octal(os.stat(sfn).st_mode)
mode = __salt__['cp.stat_file'](source, saltenv=saltenv, octal=True)
except Exception as exc:
log.warning('Unable to stat %s: %s', sfn, exc)
changes = check_file_meta(name, sfn, source, source_sum, user,
Expand Down Expand Up @@ -4607,6 +4607,13 @@ def manage_file(name,
a local file on the minion), the mode of the destination file will be
set to the mode of the source file.

.. note:: keep_mode does not work with salt-ssh.

As a consequence of how the files are transfered to the minion, and
the inability to connect back to the master with salt-ssh, salt is
unable to stat the file as it exists on the fileserver and thus
cannot mirror the mode on the salt-ssh minion

CLI Example:

.. code-block:: bash
Expand Down Expand Up @@ -4641,7 +4648,7 @@ def manage_file(name,
if _urlparse(source).scheme in ('salt', 'file') \
or source.startswith('/'):
try:
mode = salt.utils.st_mode_to_octal(os.stat(sfn).st_mode)
mode = __salt__['cp.stat_file'](source, saltenv=saltenv, octal=True)
except Exception as exc:
log.warning('Unable to stat %s: %s', sfn, exc)

Expand Down
7 changes: 7 additions & 0 deletions salt/states/file.py
Expand Up @@ -1336,6 +1336,13 @@ def managed(name,
the ``contents`` options, setting the ``mode`` to ``keep`` is also
incompatible with the ``contents`` options.

.. note:: keep does not work with salt-ssh.

As a consequence of how the files are transfered to the minion, and
the inability to connect back to the master with salt-ssh, salt is
unable to stat the file as it exists on the fileserver and thus
cannot mirror the mode on the salt-ssh minion

template
If this setting is applied, the named templating engine will be used to
render the downloaded file. The following templates are supported:
Expand Down