Merge pull request #36520 from twangboy/fix_cmd.script_runas

Fix cmd.script runas for Windows
This commit is contained in:
Mike Place 2016-09-28 13:07:00 +09:00 committed by GitHub
commit e7def534b1

View file

@ -18,6 +18,7 @@ import subprocess
import sys
import time
import traceback
import tempfile
from salt.utils import vt
# Import salt libs
@ -25,7 +26,8 @@ import salt.utils
import salt.utils.timed_subprocess
import salt.grains.extra
import salt.ext.six as six
from salt.exceptions import CommandExecutionError, TimedProcTimeoutError
from salt.exceptions import CommandExecutionError, TimedProcTimeoutError, \
SaltInvocationError
from salt.log import LOG_LEVELS
from salt.ext.six.moves import range
from salt.ext.six.moves import shlex_quote as _cmd_quote
@ -1986,8 +1988,8 @@ def script(source,
def _cleanup_tempfile(path):
try:
os.remove(path)
except (IOError, OSError) as exc:
__salt__['file.remove'](path)
except (SaltInvocationError, CommandExecutionError) as exc:
log.error(
'cmd.script: Unable to clean tempfile \'{0}\': {1}'.format(
path,
@ -2004,6 +2006,12 @@ def script(source,
# Backwards compatibility
saltenv = __env__
if salt.utils.is_windows() and runas and cwd is None:
cwd = tempfile.mkdtemp(dir=__opts__['cachedir'])
__salt__['win_dacl.add_ace'](
cwd, 'File', runas, 'READ&EXECUTE', 'ALLOW',
'FOLDER&SUBFOLDERS&FILES')
path = salt.utils.mkstemp(dir=cwd, suffix=os.path.splitext(source)[1])
if template:
@ -2016,7 +2024,10 @@ def script(source,
saltenv,
**kwargs)
if not fn_:
_cleanup_tempfile(path)
if salt.utils.is_windows() and runas:
_cleanup_tempfile(cwd)
else:
_cleanup_tempfile(path)
return {'pid': 0,
'retcode': 1,
'stdout': '',
@ -2025,7 +2036,10 @@ def script(source,
else:
fn_ = __salt__['cp.cache_file'](source, saltenv)
if not fn_:
_cleanup_tempfile(path)
if salt.utils.is_windows() and runas:
_cleanup_tempfile(cwd)
else:
_cleanup_tempfile(path)
return {'pid': 0,
'retcode': 1,
'stdout': '',
@ -2053,7 +2067,10 @@ def script(source,
use_vt=use_vt,
bg=bg,
password=password)
_cleanup_tempfile(path)
if salt.utils.is_windows() and runas:
_cleanup_tempfile(cwd)
else:
_cleanup_tempfile(path)
return ret