Merge pull request #41696 from terminalmage/issue41691

Handle a few edge/corner cases with non-string input to cmd.run
This commit is contained in:
garethgreenaway 2017-06-13 11:48:56 -07:00 committed by GitHub
commit aab55d304a
2 changed files with 30 additions and 10 deletions

View File

@ -987,9 +987,12 @@ class State(object):
errors.append('Missing "name" data')
if data['name'] and not isinstance(data['name'], six.string_types):
errors.append(
'ID \'{0}\' in SLS \'{1}\' is not formed as a string, but is '
'a {2}'.format(
data['name'], data['__sls__'], type(data['name']).__name__)
'ID \'{0}\' {1}is not formed as a string, but is a {2}'.format(
data['name'],
'in SLS \'{0}\' '.format(data['__sls__'])
if '__sls__' in data else '',
type(data['name']).__name__
)
)
if errors:
return errors

View File

@ -2,6 +2,7 @@
'''For running command line executables with a timeout'''
from __future__ import absolute_import
import shlex
import subprocess
import threading
import salt.exceptions
@ -40,6 +41,12 @@ class TimedProc(object):
try:
self.process = subprocess.Popen(args, **kwargs)
except TypeError:
if not kwargs.get('shell', False):
if not isinstance(args, (list, tuple)):
try:
args = shlex.split(args)
except AttributeError:
args = shlex.split(str(args))
str_args = []
for arg in args:
if not isinstance(arg, six.string_types):
@ -47,6 +54,16 @@ class TimedProc(object):
else:
str_args.append(arg)
args = str_args
else:
if not isinstance(args, (list, tuple, six.string_types)):
# Handle corner case where someone does a 'cmd.run 3'
args = str(args)
# Ensure that environment variables are strings
for key, val in six.iteritems(kwargs.get('env', {})):
if not isinstance(val, six.string_types):
kwargs['env'][key] = str(val)
if not isinstance(key, six.string_types):
kwargs['env'][str(key)] = kwargs['env'].pop(key)
self.process = subprocess.Popen(args, **kwargs)
self.command = args