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

Add execution module to SCP files on devices where we can't install the Salt Minion #48861

Merged
merged 3 commits into from Aug 6, 2018
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
1 change: 1 addition & 0 deletions doc/ref/modules/all/index.rst
Expand Up @@ -384,6 +384,7 @@ execution modules
saltcloudmod
saltutil
schedule
scp_mod
scsi
sdb
seed
Expand Down
7 changes: 7 additions & 0 deletions doc/ref/modules/all/salt.modules.scp_mod.rst
@@ -0,0 +1,7 @@
=======================
salt.modules.scp module
=======================

.. automodule:: salt.modules.scp_mod
:members:

190 changes: 190 additions & 0 deletions salt/modules/napalm_mod.py
Expand Up @@ -46,6 +46,12 @@
except ImportError:
HAS_CISCOCONFPARSE = False

try:
import scp # pylint: disable=unused-import
HAS_SCP = True
except ImportError:
HAS_SCP = False

# ----------------------------------------------------------------------------------------------------------------------
# module properties
# ----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1765,3 +1771,187 @@ def config_diff_text(source1='candidate',
candidate_path=candidate_path,
running_config=running_cfg,
running_path=running_path)


@depends(HAS_SCP)
def scp_get(remote_path,
local_path='',
recursive=False,
preserve_times=False,
**kwargs):
'''
.. versionadded:: Fluorine

Transfer files and directories from remote network device to the localhost
of the Minion.

.. note::
This function is only available only when the underlying library
`scp <https://github.com/jbardin/scp.py>`_
is installed. See
:mod:`scp module <salt.modules.scp_mod>` for
more details.

remote_path
Path to retrieve from remote host. Since this is evaluated by scp on the
remote host, shell wildcards and environment variables may be used.

recursive: ``False``
Transfer files and directories recursively.

preserve_times: ``False``
Preserve ``mtime`` and ``atime`` of transferred files and directories.

passphrase
Used for decrypting private keys.

pkey
An optional private key to use for authentication.

key_filename
The filename, or list of filenames, of optional private key(s) and/or
certificates to try for authentication.

timeout
An optional timeout (in seconds) for the TCP connect.

socket_timeout: ``10``
The channel socket timeout in seconds.

buff_size: ``16384``
The size of the SCP send buffer.

allow_agent: ``True``
Set to ``False`` to disable connecting to the SSH agent.

look_for_keys: ``True``
Set to ``False`` to disable searching for discoverable private key
files in ``~/.ssh/``

banner_timeout
An optional timeout (in seconds) to wait for the SSH banner to be
presented.

auth_timeout
An optional timeout (in seconds) to wait for an authentication
response.

auto_add_policy: ``False``
Automatically add the host to the ``known_hosts``.

CLI Example:

.. code-block:: bash

salt '*' napalm.scp_get /var/tmp/file /tmp/file auto_add_policy=True
'''
conn_args = netmiko_args(**kwargs)
conn_args['hostname'] = conn_args['host']
kwargs.update(conn_args)
return __salt__['scp.get'](remote_path,
local_path=local_path,
recursive=recursive,
preserve_times=preserve_times,
**kwargs)


@depends(HAS_SCP)
def scp_put(files,
remote_path=None,
recursive=False,
preserve_times=False,
saltenv='base',
**kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the docs here, it's not clear what kwargs could be passed here and what effect they would have.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - I will add some docs on this. Thanks for suggestion!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some more notes into the docstring of these functions, please let me know what you think!

'''
.. versionadded:: Fluorine

Transfer files and directories to remote network device.

.. note::
This function is only available only when the underlying library
`scp <https://github.com/jbardin/scp.py>`_
is installed. See
:mod:`scp module <salt.modules.scp_mod>` for
more details.

files
A single path or a list of paths to be transferred.

remote_path
The path on the remote device where to store the files.

recursive: ``True``
Transfer files and directories recursively.

preserve_times: ``False``
Preserve ``mtime`` and ``atime`` of transferred files and directories.

saltenv: ``base``
The name of the Salt environment. Ignored when ``files`` is not a
``salt://`` URL.

hostname
The hostname of the remote device.

port: ``22``
The port of the remote device.

username
The username required for SSH authentication on the device.

password
Used for password authentication. It is also used for private key
decryption if ``passphrase`` is not given.

passphrase
Used for decrypting private keys.

pkey
An optional private key to use for authentication.

key_filename
The filename, or list of filenames, of optional private key(s) and/or
certificates to try for authentication.

timeout
An optional timeout (in seconds) for the TCP connect.

socket_timeout: ``10``
The channel socket timeout in seconds.

buff_size: ``16384``
The size of the SCP send buffer.

allow_agent: ``True``
Set to ``False`` to disable connecting to the SSH agent.

look_for_keys: ``True``
Set to ``False`` to disable searching for discoverable private key
files in ``~/.ssh/``

banner_timeout
An optional timeout (in seconds) to wait for the SSH banner to be
presented.

auth_timeout
An optional timeout (in seconds) to wait for an authentication
response.

auto_add_policy: ``False``
Automatically add the host to the ``known_hosts``.

CLI Example:

.. code-block:: bash

salt '*' napalm.scp_put /path/to/file /var/tmp/file auto_add_policy=True
'''
conn_args = netmiko_args(**kwargs)
conn_args['hostname'] = conn_args['host']
kwargs.update(conn_args)
return __salt__['scp.put'](files,
remote_path=remote_path,
recursive=recursive,
preserve_times=preserve_times,
saltenv=saltenv,
**kwargs)