Skip to content

Commit

Permalink
Merge pull request #56627 from Ch3LL/set_env_ssh
Browse files Browse the repository at this point in the history
Add new salt-ssh set_path option
  • Loading branch information
dwoz committed Apr 18, 2020
2 parents a3dc9ff + bda906d commit d2ccd09
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .ci/kitchen-macosxhighsierra-py3
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ runTestSuite(
kitchen_platforms_file: '/var/jenkins/workspace/pre-golden-nox-platforms.yml',
kitchen_verifier_file: '/var/jenkins/workspace/nox-verifier.yml',
nox_env_name: 'runtests-zeromq',
nox_passthrough_opts: '',
nox_passthrough_opts: '--ssh-tests',
python_version: 'py3',
run_full: params.runFull,
testrun_timeout: 6,
Expand Down
2 changes: 1 addition & 1 deletion .ci/kitchen-macosxmojave-py3
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ runTestSuite(
kitchen_platforms_file: '/var/jenkins/workspace/pre-golden-nox-platforms.yml',
kitchen_verifier_file: '/var/jenkins/workspace/nox-verifier.yml',
nox_env_name: 'runtests-zeromq',
nox_passthrough_opts: '',
nox_passthrough_opts: '--ssh-tests',
python_version: 'py3',
run_full: params.runFull,
testrun_timeout: 6,
Expand Down
2 changes: 1 addition & 1 deletion .ci/kitchen-macosxsierra-py3
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ runTestSuite(
kitchen_platforms_file: '/var/jenkins/workspace/pre-golden-nox-platforms.yml',
kitchen_verifier_file: '/var/jenkins/workspace/nox-verifier.yml',
nox_env_name: 'runtests-zeromq',
nox_passthrough_opts: '',
nox_passthrough_opts: '--ssh-tests',
python_version: 'py3',
run_full: params.runFull,
testrun_timeout: 6,
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ Versions are `MAJOR.PATCH`.
### Changed

### Fixed

- [#56237](https://github.com/saltstack/salt/pull/56237) - Fix alphabetical ordering and remove duplicates across all documentation indexes - [@myii](https://github.com/myii)
- [#56325](https://github.com/saltstack/salt/pull/56325) - Fix hyperlinks to `salt.serializers` and other documentation issues - [@myii](https://github.com/myii)

### Added
- [#56627](https://github.com/saltstack/salt/pull/56627) - Add new salt-ssh set_path option

## 3000.1

### Removed
Expand Down
19 changes: 18 additions & 1 deletion doc/topics/releases/sodium.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ This grain provides the same information as the ``path`` grain, only formatted
as a list of directories.


================
Salt-SSH updates
================

ssh_pre_flight
--------------

A new Salt-SSH roster option `ssh_pre_flight` has been added. This enables you to run a
script before Salt-SSH tries to run any commands. You can set this option in the roster
for a specific minion or use the `roster_defaults` to set it for all minions.
Expand Down Expand Up @@ -60,3 +62,18 @@ minion. If you want to force the script to run you have the following options:
* Wipe the thin dir on the targeted minion using the -w arg.
* Set ssh_run_pre_flight to True in the config.
* Run salt-ssh with the --pre-flight arg.

set_path
--------

A new salt-ssh roster option `set_path` has been added. This allows you to set
the path environment variable used to run the salt-ssh command on the target minion.
You can set this setting in your roster file like so:

.. code-block:: yaml
minion1:
host: localhost
user: root
passwd: P@ssword
set_path: '$PATH:/usr/local/bin/'
4 changes: 4 additions & 0 deletions doc/topics/ssh/roster.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ The information which can be stored in a roster ``target`` is the following:
# does not exist, unless --pre-flight is passed to salt-ssh
# command or ssh_run_pre_flight is set to true in the config
# Added in Sodium Release.
set_path: # Set the path environment variable, to ensure the expected python
# binary is in the salt-ssh path, when running the command.
# Example: '$PATH:/usr/local/bin/'. Added in Sodium Release.
.. _ssh_pre_flight:

Expand Down
6 changes: 6 additions & 0 deletions salt/client/ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@
if [ -n "$DEBUG" ]
then set -x
fi
SET_PATH="{{SET_PATH}}"
if [ -n "$SET_PATH" ]
then export PATH={{SET_PATH}}
fi
SUDO=""
if [ -n "{{SUDO}}" ]
then SUDO="sudo "
Expand Down Expand Up @@ -907,6 +911,7 @@ def __init__(

self.fun, self.args, self.kwargs = self.__arg_comps()
self.id = id_
self.set_path = kwargs.get("set_path", "")

self.mods = mods if isinstance(mods, dict) else {}
args = {
Expand Down Expand Up @@ -1311,6 +1316,7 @@ def _cmd_str(self):
SUDO_USER=sudo_user,
SSH_PY_CODE=py_code_enc,
HOST_PY_MAJOR=sys.version_info[0],
SET_PATH=self.set_path,
)
else:
cmd = saltwinshell.gen_shim(py_code_enc)
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,10 @@ def prep_ssh(self):
) as roster:
roster.write(" user: {0}\n".format(RUNTIME_VARS.RUNNING_TESTS_USER))
roster.write(
" priv: {0}/{1}".format(RUNTIME_VARS.TMP_CONF_DIR, "key_test")
" priv: {0}/{1}\n".format(RUNTIME_VARS.TMP_CONF_DIR, "key_test")
)
if salt.utils.platform.is_darwin():
roster.write(" set_path: $PATH:/usr/local/bin/\n")
sys.stdout.write(" {LIGHT_GREEN}STARTED!\n{ENDC}".format(**self.colors))

@classmethod
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/ssh/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Import salt testing libs
from tests.support.case import SSHCase
from tests.support.runtests import RUNTIME_VARS


class SSHTest(SSHCase):
Expand All @@ -34,6 +35,18 @@ def test_thin_dir(self):
os.path.exists(os.path.join(thin_dir, "salt-call"))
os.path.exists(os.path.join(thin_dir, "running_data"))

def test_set_path(self):
"""
test setting the path env variable
"""
path = "/pathdoesnotexist/"
roster = os.path.join(RUNTIME_VARS.TMP, "roster-set-path")
self.custom_roster(
roster, data={"set_path": "$PATH:/usr/local/bin/:{0}".format(path)}
)
ret = self.run_function("environ.get", ["PATH"], roster_file=roster)
assert path in ret

def tearDown(self):
"""
make sure to clean up any old ssh directories
Expand Down
1 change: 1 addition & 0 deletions tests/integration/ssh/test_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_service(self, grains):
service = "org.ntp.ntpd"
if int(os_release.split(".")[1]) >= 13:
service = "com.apple.AirPlayXPCHelper"
self.run_function("service.enable", [service])
ret = self.run_function("service.get_all")
self.assertIn(service, ret)
self.run_function("service.stop", [service])
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/client/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import absolute_import, print_function, unicode_literals

import os
import re
import shutil
import tempfile

Expand Down Expand Up @@ -392,3 +393,44 @@ def test_run_ssh_pre_flight(self):
call("/bin/sh '{0}'".format(exp_tmp)),
call("rm '{0}'".format(exp_tmp)),
] == mock_cmd.call_args_list

@skipIf(salt.utils.platform.is_windows(), "SSH_PY_SHIM not set on windows")
def test_cmd_run_set_path(self):
"""
test when set_path is set
"""
target = self.target
target["set_path"] = "$PATH:/tmp/path/"
single = ssh.Single(
self.opts,
self.opts["argv"],
"localhost",
mods={},
fsclient=None,
thin=salt.utils.thin.thin_path(self.opts["cachedir"]),
mine=False,
**self.target
)

ret = single._cmd_str()
assert re.search("\\" + target["set_path"], ret)

@skipIf(salt.utils.platform.is_windows(), "SSH_PY_SHIM not set on windows")
def test_cmd_run_not_set_path(self):
"""
test when set_path is not set
"""
target = self.target
single = ssh.Single(
self.opts,
self.opts["argv"],
"localhost",
mods={},
fsclient=None,
thin=salt.utils.thin.thin_path(self.opts["cachedir"]),
mine=False,
**self.target
)

ret = single._cmd_str()
assert re.search('SET_PATH=""', ret)

0 comments on commit d2ccd09

Please sign in to comment.