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

(Backport 50302) cmd: Add root parameter for wait and run states #54956

Merged
merged 1 commit into from
Dec 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions salt/states/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def wait(name,
unless=None,
creates=None,
cwd=None,
root=None,
runas=None,
shell=None,
env=(),
Expand Down Expand Up @@ -436,6 +437,10 @@ def wait(name,
The current working directory to execute the command in, defaults to
/root

root
Path to the root of the jail to use. If this parameter is set, the command
will run inside a chroot

runas
The user name to run the command as

Expand Down Expand Up @@ -674,6 +679,7 @@ def run(name,
unless=None,
creates=None,
cwd=None,
root=None,
runas=None,
shell=None,
env=None,
Expand Down Expand Up @@ -707,6 +713,10 @@ def run(name,
The current working directory to execute the command in, defaults to
/root

root
Path to the root of the jail to use. If this parameter is set, the command
will run inside a chroot

runas
The user name to run the command as

Expand Down Expand Up @@ -882,6 +892,7 @@ def run(name,

cmd_kwargs = copy.deepcopy(kwargs)
cmd_kwargs.update({'cwd': cwd,
'root': root,
'runas': runas,
'use_vt': use_vt,
'shell': shell or __grains__['shell'],
Expand Down Expand Up @@ -912,10 +923,11 @@ def run(name,

# Wow, we passed the test, run this sucker!
try:
cmd_all = __salt__['cmd.run_all'](
name, timeout=timeout, python_shell=True, **cmd_kwargs
run_cmd = 'cmd.run_all' if not root else 'cmd.run_chroot'
cmd_all = __salt__[run_cmd](
cmd=name, timeout=timeout, python_shell=True, **cmd_kwargs
)
except CommandExecutionError as err:
except Exception as err:
aplanas marked this conversation as resolved.
Show resolved Hide resolved
ret['comment'] = six.text_type(err)
return ret

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/states/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ def test_run(self):
'skip_watch': True})
self.assertDictEqual(cmd.run(name, onlyif=''), ret)

def test_run_root(self):
'''
Test to run a command with a different root
'''
name = 'cmd.script'

ret = {'name': name,
'result': False,
'changes': {},
'comment': ''}

with patch.dict(cmd.__grains__, {'shell': 'shell'}):
with patch.dict(cmd.__opts__, {'test': False}):
mock = MagicMock(side_effect=[CommandExecutionError,
{'retcode': 1}])
with patch.dict(cmd.__salt__, {'cmd.run_chroot': mock}):
ret.update({'comment': '', 'result': False})
self.assertDictEqual(cmd.run(name, root='/mnt'), ret)

ret.update({'comment': 'Command "cmd.script" run',
'result': False, 'changes': {'retcode': 1}})
self.assertDictEqual(cmd.run(name, root='/mnt'), ret)

# 'script' function tests: 1

def test_script(self):
Expand Down