Skip to content

Commit

Permalink
cmd: Add root parameter for wait and run states
Browse files Browse the repository at this point in the history
Add root parameter for a chroot environment to `wait` and `run`
states. In the case of `run`, we delegate the execution to the
`run_chroot` module.

(cherry picked from commit cd7b5b2)
  • Loading branch information
aplanas committed Dec 3, 2019
1 parent d418dac commit 5947392
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
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:
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

0 comments on commit 5947392

Please sign in to comment.