mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Simplify the marker parsing logic
This commit is contained in:
parent
a09f20ab45
commit
35ad828ab8
1 changed files with 10 additions and 36 deletions
|
@ -424,9 +424,9 @@ def _run(cmd,
|
|||
marker_b = marker.encode(__salt_system_encoding__)
|
||||
py_code = (
|
||||
'import sys, os, itertools; '
|
||||
'sys.stdout.write(\"' + marker + '\\n\"); '
|
||||
'sys.stdout.write(\"' + marker + '\"); '
|
||||
'sys.stdout.write(\"\\0\".join(itertools.chain(*os.environ.items()))); '
|
||||
'sys.stdout.write(\"\\n' + marker + '\\n\");'
|
||||
'sys.stdout.write(\"' + marker + '\");'
|
||||
)
|
||||
if __grains__['os'] in ['MacOS', 'Darwin']:
|
||||
env_cmd = ('sudo', '-i', '-u', runas, '--',
|
||||
|
@ -440,47 +440,21 @@ def _run(cmd,
|
|||
env_cmd = ('su', '-', runas, '-c', sys.executable)
|
||||
else:
|
||||
env_cmd = ('su', '-s', shell, '-', runas, '-c', sys.executable)
|
||||
env_encoded = subprocess.Popen(
|
||||
env_encoded, env_encoded_err = subprocess.Popen(
|
||||
env_cmd,
|
||||
stderr=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stdin=subprocess.PIPE
|
||||
).communicate(py_code.encode(__salt_system_encoding__))
|
||||
env_encoded_err = env_encoded[1]
|
||||
env_encoded = env_encoded[0]
|
||||
if isinstance(env_encoded_err, str):
|
||||
env_encoded_err = env_encoded_err.encode(__salt_system_encoding__)
|
||||
if isinstance(env_encoded, str):
|
||||
env_encoded = env_encoded.encode(__salt_system_encoding__)
|
||||
env_encoded_org = env_encoded
|
||||
env_mark = env_encoded.find(marker_b + b'\n')
|
||||
first_marker_found = False
|
||||
if env_mark >= 0:
|
||||
# strip first marker line plus newline
|
||||
env_encoded = env_encoded[env_mark + len(marker) + 1:]
|
||||
first_marker_found = True
|
||||
env_mark = env_encoded.find(b'\n' + marker_b + b'\n')
|
||||
second_marker_found = False
|
||||
if env_mark >= 0:
|
||||
# strip second marker line plus leading newline
|
||||
env_encoded = env_encoded[0:env_mark]
|
||||
second_marker_found = True
|
||||
if first_marker_found == second_marker_found:
|
||||
if not first_marker_found:
|
||||
log.error('Unable to retrieve environment for \'%s\': err=%s out=%s',
|
||||
runas,
|
||||
repr(env_encoded_err),
|
||||
repr(env_encoded_org)
|
||||
)
|
||||
else:
|
||||
if env_encoded.count(marker_b) != 2:
|
||||
raise CommandExecutionError(
|
||||
'Environment could not be retrieved for User \'{0}\':'
|
||||
' missing a marker, got err={1} out={2}'.format(
|
||||
runas,
|
||||
repr(env_encoded_err),
|
||||
repr(env_encoded_org)
|
||||
)
|
||||
'Environment could not be retrieved for user \'{0}\'',
|
||||
info={'stderr': repr(env_encoded_err),
|
||||
'stdout': repr(env_encoded)}
|
||||
)
|
||||
else:
|
||||
# Strip the marker
|
||||
env_encoded = env_encoded.split(marker_b)[1]
|
||||
|
||||
if six.PY2:
|
||||
import itertools
|
||||
|
|
Loading…
Add table
Reference in a new issue