Merge remote-tracking branch 'telmich/master'

This commit is contained in:
Steven Armstrong 2011-10-21 15:39:17 +02:00
commit 6ec4cefe09
2 changed files with 33 additions and 2 deletions

View File

@ -111,6 +111,10 @@ class Remote(object):
"""
assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: %s" % command
# export target_host for use in __remote_{exec,copy} scripts
os_environ = os.environ.copy()
os_environ['__target_host'] = self.target_host
# can't pass environment to remote side, so prepend command with
# variable declarations
if env:
@ -122,7 +126,7 @@ class Remote(object):
self.log.debug("Remote run: %s", command)
try:
if return_output:
return subprocess.check_output(command).decode()
return subprocess.check_output(command, env=os_environ).decode()
else:
subprocess.check_call(command)
except subprocess.CalledProcessError:
@ -140,6 +144,10 @@ class Remote(object):
command = self._exec.split()
command.append(self.target_host)
# export target_host for use in __remote_{exec,copy} scripts
os_environ = os.environ.copy()
os_environ['__target_host'] = self.target_host
# can't pass environment to remote side, so prepend command with
# variable declarations
if env:
@ -154,7 +162,7 @@ class Remote(object):
try:
if return_output:
return subprocess.check_output(command).decode()
return subprocess.check_output(command, env=os_environ).decode()
else:
subprocess.check_call(command)
except subprocess.CalledProcessError as error:

View File

@ -117,3 +117,26 @@ class RemoteTestCase(test.CdistTestCase):
self.remote.create_directories()
self.assertTrue(os.path.isdir(self.remote.base_path))
self.assertTrue(os.path.isdir(self.remote.conf_path))
def test_run_target_host_in_env(self):
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, 'w') as fd:
fd.writelines(["#!/bin/sh\n", "echo $__target_host"])
os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path
remote_copy = "echo"
r = remote.Remote(self.target_host, self.base_path, remote_exec, remote_copy)
self.assertEqual(r.run('/bin/true', return_output=True), "%s\n" % self.target_host)
def test_run_script_target_host_in_env(self):
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, 'w') as fd:
fd.writelines(["#!/bin/sh\n", "echo $__target_host"])
os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path
remote_copy = "echo"
r = remote.Remote(self.target_host, self.base_path, remote_exec, remote_copy)
handle, script = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/true"])
self.assertEqual(r.run_script(script, return_output=True), "%s\n" % self.target_host)