From 8e2b9e4337cbc55996a2b626855dbcaa652b9cbf Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 21 Oct 2011 15:10:41 +0200 Subject: [PATCH 1/3] add test for: __target_host exported to remote exec Signed-off-by: Steven Armstrong --- lib/cdist/test/exec/remote.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/cdist/test/exec/remote.py b/lib/cdist/test/exec/remote.py index 92b68d06..1334e330 100644 --- a/lib/cdist/test/exec/remote.py +++ b/lib/cdist/test/exec/remote.py @@ -118,3 +118,16 @@ 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_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", "echo foobar"]) + self.assertEqual(r.run_script(script, return_output=True), "%s\n" % self.target_host) From 16b118b38e541edc3256bf9f08566ca9b0cc34cb Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 21 Oct 2011 15:16:38 +0200 Subject: [PATCH 2/3] add test for: __target_host exported to remote exec run Signed-off-by: Steven Armstrong --- lib/cdist/test/exec/remote.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/cdist/test/exec/remote.py b/lib/cdist/test/exec/remote.py index 1334e330..a09a334f 100644 --- a/lib/cdist/test/exec/remote.py +++ b/lib/cdist/test/exec/remote.py @@ -119,6 +119,16 @@ class RemoteTestCase(test.CdistTestCase): 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: @@ -129,5 +139,5 @@ class RemoteTestCase(test.CdistTestCase): 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", "echo foobar"]) + fd.writelines(["#!/bin/sh\n", "/bin/true"]) self.assertEqual(r.run_script(script, return_output=True), "%s\n" % self.target_host) From 17e76426158069ab4ba16d765f4b988d64dedb4f Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 21 Oct 2011 15:17:19 +0200 Subject: [PATCH 3/3] implement: export __target_host in remote exec Signed-off-by: Steven Armstrong --- lib/cdist/exec/remote.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/cdist/exec/remote.py b/lib/cdist/exec/remote.py index 2847d89c..259f72d8 100644 --- a/lib/cdist/exec/remote.py +++ b/lib/cdist/exec/remote.py @@ -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: