"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "salt/modules/cmdmod.py" between
salt-2019.2.0.tar.gz and salt-2019.2.1.tar.gz

About: SaltStack is a systems management software for data center automation, cloud orchestration, server provisioning, configuration management and more. Community version.

cmdmod.py  (salt-2019.2.0):cmdmod.py  (salt-2019.2.1)
skipping to change at line 276 skipping to change at line 276
password=None, password=None,
bg=False, bg=False,
encoded_cmd=False, encoded_cmd=False,
success_retcodes=None, success_retcodes=None,
**kwargs): **kwargs):
''' '''
Do the DRY thing and only call subprocess.Popen() once Do the DRY thing and only call subprocess.Popen() once
''' '''
if 'pillar' in kwargs and not pillar_override: if 'pillar' in kwargs and not pillar_override:
pillar_override = kwargs['pillar'] pillar_override = kwargs['pillar']
if _is_valid_shell(shell) is False: if output_loglevel != 'quiet' and _is_valid_shell(shell) is False:
log.warning( log.warning(
'Attempt to run a shell command with what may be an invalid shell! ' 'Attempt to run a shell command with what may be an invalid shell! '
'Check to ensure that the shell <%s> is valid for this user.', 'Check to ensure that the shell <%s> is valid for this user.',
shell shell
) )
output_loglevel = _check_loglevel(output_loglevel) output_loglevel = _check_loglevel(output_loglevel)
log_callback = _check_cb(log_callback) log_callback = _check_cb(log_callback)
use_sudo = False use_sudo = False
skipping to change at line 400 skipping to change at line 400
if not HAS_WIN_RUNAS: if not HAS_WIN_RUNAS:
msg = 'missing salt/utils/win_runas.py' msg = 'missing salt/utils/win_runas.py'
raise CommandExecutionError(msg) raise CommandExecutionError(msg)
if isinstance(cmd, (list, tuple)): if isinstance(cmd, (list, tuple)):
cmd = ' '.join(cmd) cmd = ' '.join(cmd)
return win_runas(cmd, runas, password, cwd) return win_runas(cmd, runas, password, cwd)
if runas and salt.utils.platform.is_darwin(): if runas and salt.utils.platform.is_darwin():
# we need to insert the user simulation into the command itself and not # We need to insert the user simulation into the command itself and not
# just run it from the environment on macOS as that # just run it from the environment on macOS as that method doesn't work
# method doesn't work properly when run as root for certain commands. # properly when run as root for certain commands.
if isinstance(cmd, (list, tuple)): if isinstance(cmd, (list, tuple)):
cmd = ' '.join(map(_cmd_quote, cmd)) cmd = ' '.join(map(_cmd_quote, cmd))
cmd = 'su -l {0} -c "{1}"'.format(runas, cmd) # Ensure directory is correct before running command
# set runas to None, because if you try to run `su -l` as well as cmd = 'cd -- {dir} && {{ {cmd}\n }}'.format(dir=_cmd_quote(cwd), cmd=cmd
# simulate the environment macOS will prompt for the password of the )
# user and will cause salt to hang.
# Ensure environment is correct for a newly logged-in user by running
# the command under bash as a login shell
cmd = '/bin/bash -l -c {cmd}'.format(cmd=_cmd_quote(cmd))
# Ensure the login is simulated correctly (note: su runs sh, not bash,
# which causes the environment to be initialised incorrectly, which is
# fixed by the previous line of code)
cmd = 'su -l {0} -c {1}'.format(_cmd_quote(runas), _cmd_quote(cmd))
# Set runas to None, because if you try to run `su -l` after changing
# user, su will prompt for the password of the user and cause salt to
# hang.
runas = None runas = None
if runas: if runas:
# Save the original command before munging it # Save the original command before munging it
try: try:
pwd.getpwnam(runas) pwd.getpwnam(runas)
except KeyError: except KeyError:
raise CommandExecutionError( raise CommandExecutionError(
'User \'{0}\' is not available'.format(runas) 'User \'{0}\' is not available'.format(runas)
) )
skipping to change at line 452 skipping to change at line 463
import uuid import uuid
marker = '<<<' + str(uuid.uuid4()) + '>>>' marker = '<<<' + str(uuid.uuid4()) + '>>>'
marker_b = marker.encode(__salt_system_encoding__) marker_b = marker.encode(__salt_system_encoding__)
py_code = ( py_code = (
'import sys, os, itertools; ' 'import sys, os, itertools; '
'sys.stdout.write(\"' + marker + '\"); ' 'sys.stdout.write(\"' + marker + '\"); '
'sys.stdout.write(\"\\0\".join(itertools.chain(*os.environ.items ()))); ' 'sys.stdout.write(\"\\0\".join(itertools.chain(*os.environ.items ()))); '
'sys.stdout.write(\"' + marker + '\");' 'sys.stdout.write(\"' + marker + '\");'
) )
if use_sudo or __grains__['os'] in ['MacOS', 'Darwin']: if use_sudo:
env_cmd = ['sudo'] env_cmd = ['sudo']
# runas is optional if use_sudo is set. # runas is optional if use_sudo is set.
if runas: if runas:
env_cmd.extend(['-u', runas]) env_cmd.extend(['-u', runas])
if group: if group:
env_cmd.extend(['-g', group]) env_cmd.extend(['-g', group])
if shell != DEFAULT_SHELL: if shell != DEFAULT_SHELL:
env_cmd.extend(['-s', '--', shell, '-c']) env_cmd.extend(['-s', '--', shell, '-c'])
else: else:
env_cmd.extend(['-i', '--']) env_cmd.extend(['-i', '--'])
skipping to change at line 563 skipping to change at line 574
env.setdefault('LANGUAGE', 'C') env.setdefault('LANGUAGE', 'C')
else: else:
# On Windows set the codepage to US English. # On Windows set the codepage to US English.
if python_shell: if python_shell:
cmd = 'chcp 437 > nul & ' + cmd cmd = 'chcp 437 > nul & ' + cmd
if clean_env: if clean_env:
run_env = env run_env = env
else: else:
run_env = os.environ.copy() if salt.utils.platform.is_windows():
import nt
run_env = nt.environ.copy()
else:
run_env = os.environ.copy()
run_env.update(env) run_env.update(env)
if prepend_path: if prepend_path:
run_env['PATH'] = ':'.join((prepend_path, run_env['PATH'])) run_env['PATH'] = ':'.join((prepend_path, run_env['PATH']))
if python_shell is None: if python_shell is None:
python_shell = False python_shell = False
new_kwargs = {'cwd': cwd, new_kwargs = {'cwd': cwd,
'shell': python_shell, 'shell': python_shell,
skipping to change at line 3009 skipping to change at line 3024
.. versionadded:: 2019.2.0 .. versionadded:: 2019.2.0
CLI Example: CLI Example:
.. code-block:: bash .. code-block:: bash
salt '*' cmd.run_chroot /var/lib/lxc/container_name/rootfs 'sh /tmp/boot strap.sh' salt '*' cmd.run_chroot /var/lib/lxc/container_name/rootfs 'sh /tmp/boot strap.sh'
''' '''
__salt__['mount.mount']( __salt__['mount.mount'](
os.path.join(root, 'dev'), os.path.join(root, 'dev'),
'udev', 'devtmpfs',
fstype='devtmpfs') fstype='devtmpfs')
__salt__['mount.mount']( __salt__['mount.mount'](
os.path.join(root, 'proc'), os.path.join(root, 'proc'),
'proc', 'proc',
fstype='proc') fstype='proc')
__salt__['mount.mount'](
os.path.join(root, 'sys'),
'sysfs',
fstype='sysfs')
# Execute chroot routine # Execute chroot routine
sh_ = '/bin/sh' sh_ = '/bin/sh'
if os.path.isfile(os.path.join(root, 'bin/bash')): if os.path.isfile(os.path.join(root, 'bin/bash')):
sh_ = '/bin/bash' sh_ = '/bin/bash'
if isinstance(cmd, (list, tuple)): if isinstance(cmd, (list, tuple)):
cmd = ' '.join([six.text_type(i) for i in cmd]) cmd = ' '.join([six.text_type(i) for i in cmd])
cmd = 'chroot {0} {1} -c {2}'.format(root, sh_, _cmd_quote(cmd)) cmd = 'chroot {0} {1} -c {2}'.format(root, sh_, _cmd_quote(cmd))
skipping to change at line 3066 skipping to change at line 3085
break break
for pid in pids: for pid in pids:
# use sig 15 (TERM) for first 3 attempts, then 9 (KILL) # use sig 15 (TERM) for first 3 attempts, then 9 (KILL)
sig = 15 if i < 3 else 9 sig = 15 if i < 3 else 9
os.kill(pid, sig) os.kill(pid, sig)
if _chroot_pids(root): if _chroot_pids(root):
log.error('Processes running in chroot could not be killed, ' log.error('Processes running in chroot could not be killed, '
'filesystem will remain mounted') 'filesystem will remain mounted')
__salt__['mount.umount'](os.path.join(root, 'sys'))
__salt__['mount.umount'](os.path.join(root, 'proc')) __salt__['mount.umount'](os.path.join(root, 'proc'))
__salt__['mount.umount'](os.path.join(root, 'dev')) __salt__['mount.umount'](os.path.join(root, 'dev'))
if hide_output: if hide_output:
ret['stdout'] = ret['stderr'] = '' ret['stdout'] = ret['stderr'] = ''
return ret return ret
def _is_valid_shell(shell): def _is_valid_shell(shell):
''' '''
Attempts to search for valid shells on a system and Attempts to search for valid shells on a system and
see if a given shell is in the list see if a given shell is in the list
skipping to change at line 3250 skipping to change at line 3270
'error': 'Salt does not know how to get the version number for ' 'error': 'Salt does not know how to get the version number for '
'{0}'.format(shell), '{0}'.format(shell),
'installed': None 'installed': None
} }
shell_data = regex_shells[shell] shell_data = regex_shells[shell]
pattern = shell_data.pop(0) pattern = shell_data.pop(0)
# We need to make sure HOME set, so shells work correctly # We need to make sure HOME set, so shells work correctly
# salt-call will general have home set, the salt-minion service may not # salt-call will general have home set, the salt-minion service may not
# We need to assume ports of unix shells to windows will look after # We need to assume ports of unix shells to windows will look after
# themselves in setting HOME as they do it in many different ways # themselves in setting HOME as they do it in many different ways
newenv = os.environ if salt.utils.platform.is_windows():
import nt
newenv = nt.environ
else:
newenv = os.environ
if ('HOME' not in newenv) and (not salt.utils.platform.is_windows()): if ('HOME' not in newenv) and (not salt.utils.platform.is_windows()):
newenv['HOME'] = os.path.expanduser('~') newenv['HOME'] = os.path.expanduser('~')
log.debug('HOME environment set to %s', newenv['HOME']) log.debug('HOME environment set to %s', newenv['HOME'])
try: try:
proc = salt.utils.timed_subprocess.TimedProc( proc = salt.utils.timed_subprocess.TimedProc(
shell_data, shell_data,
stdin=None, stdin=None,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
timeout=10, timeout=10,
 End of changes. 9 change blocks. 
12 lines changed or deleted 38 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)