Skip to content

Commit

Permalink
Fix passing of environment variables with spaces #1300
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Peng committed Sep 27, 2019
1 parent 7744e0b commit 8f1f9ac
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/sos/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,17 @@ def _get_receive_cmd(self, rename=False):
return '''rsync -a --no-g -e 'ssh ''' + self.cm_opts + ''' -p {port}' {host}:{source:e} "{dest:adep}"'''

def _get_execute_cmd(self, under_workdir=True, pass_env=False) -> str:
# #1300
# due to the limitations of command line arguments of ssh, we cannot pass variables
# with values containing space and qutation marks, perhaps also other special characters.
# The following is not a robust solution to #1300 but I cannot think of a better method.
env_vars = ' '.join( [f'{x}={os.environ[x]}' for x in os.environ.keys() if not any(y in os.environ[x] for y in (' ', '"', "'" ))])
return self.config.get(
'execute_cmd', 'ssh ' + self.cm_opts +
""" -q {host} -p {port} "bash --login -c '""" +
('set -e $(env);' if pass_env else '') +
('[ -d {workdir} ] || mkdir -p {workdir}; cd {workdir} && '
if under_workdir else '') + ''' {cmd}'" ''')
(env_vars if pass_env else '') +
(' [ -d {workdir} ] || mkdir -p {workdir}; cd {workdir} && '
if under_workdir else ' ') + ''' {cmd}'" ''')

def _get_query_cmd(self):
return self.config.get(
Expand Down

0 comments on commit 8f1f9ac

Please sign in to comment.