From 0a1418f4d311db9847f0697a5270347b14a78050 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 12 Oct 2011 23:01:41 +0200 Subject: [PATCH 1/8] forward env Signed-off-by: Steven Armstrong --- lib/cdist/exec/remote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cdist/exec/remote.py b/lib/cdist/exec/remote.py index 7821e993..63551e7a 100644 --- a/lib/cdist/exec/remote.py +++ b/lib/cdist/exec/remote.py @@ -94,7 +94,7 @@ class Remote(object): cmd = self._exec.split() cmd.append(self.target_host) cmd.extend(command) - return self.run_command(cmd, env=None) + return self.run_command(cmd, env=env) def run_command(self, command, env=None): """Run the given command with the given environment. From d47039e91e3a76233233029745b4911279bc945d Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 12 Oct 2011 23:49:42 +0200 Subject: [PATCH 2/8] prepend variables to remote commands Signed-off-by: Steven Armstrong --- lib/cdist/exec/remote.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/cdist/exec/remote.py b/lib/cdist/exec/remote.py index 63551e7a..2ffc73fd 100644 --- a/lib/cdist/exec/remote.py +++ b/lib/cdist/exec/remote.py @@ -102,9 +102,18 @@ class Remote(object): """ assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: %s" % command + + # can't pass environment to remote side, so prepend command with + # variable declarations + if env: + cmd = ["%s=%s" % item for item in env.items()] + cmd.extend(command) + else: + cmd = command + self.log.debug("Remote run: %s", command) try: - return subprocess.check_output(command, env=env).decode() + return subprocess.check_output(cmd).decode() except subprocess.CalledProcessError: raise cdist.Error("Command failed: " + " ".join(command)) except OSError as error: @@ -117,6 +126,12 @@ class Remote(object): """ command = self._exec.split() command.append(self.target_host) + + # can't pass environment to remote side, so prepend command with + # variable declarations + if env: + command.extend(["%s=%s" % item for item in env.items()]) + command.extend(["/bin/sh", "-e"]) command.append(script) @@ -125,7 +140,7 @@ class Remote(object): self.log.debug("Remote run script env: %s", env) try: - return subprocess.check_output(command, env=env).decode() + return subprocess.check_output(command).decode() except subprocess.CalledProcessError as error: script_content = self.run(["cat", script]) self.log.error("Code that raised the error:\n%s", script_content) From 1d85d10f4f9806385f39411bc51c3734ff38e43b Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 00:16:02 +0200 Subject: [PATCH 3/8] consistent naming: /type_base_path/type_path/ Signed-off-by: Steven Armstrong --- lib/cdist/exec/local.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cdist/exec/local.py b/lib/cdist/exec/local.py index 521d56c3..8192bf0c 100644 --- a/lib/cdist/exec/local.py +++ b/lib/cdist/exec/local.py @@ -59,7 +59,7 @@ class Local(object): self.conf_path = os.path.join(self.base_path, "conf") self.global_explorer_path = os.path.join(self.conf_path, "explorer") self.manifest_path = os.path.join(self.conf_path, "manifest") - self.type_base_path = os.path.join(self.conf_path, "type") + self.type_path = os.path.join(self.conf_path, "type") # FIXME: should not be needed anywhere self.lib_path = os.path.join(self.base_path, "lib") @@ -123,7 +123,7 @@ class Local(object): def link_emulator(self, exec_path): """Link emulator to types""" src = os.path.abspath(exec_path) - for cdist_type in core.Type.list_types(self.type_base_path): + for cdist_type in core.Type.list_types(self.type_path): dst = os.path.join(self.bin_path, cdist_type.name) self.log.debug("Linking emulator: %s to %s", src, dst) From 8545221787266302ddbe71eef58c147f2c6b7e4e Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 00:16:30 +0200 Subject: [PATCH 4/8] consistent naming: /object_base_path/object_path/ Signed-off-by: Steven Armstrong --- lib/cdist/exec/local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cdist/exec/local.py b/lib/cdist/exec/local.py index 8192bf0c..1f253f09 100644 --- a/lib/cdist/exec/local.py +++ b/lib/cdist/exec/local.py @@ -67,7 +67,7 @@ class Local(object): self.out_path = out_path self.bin_path = os.path.join(self.out_path, "bin") self.global_explorer_out_path = os.path.join(self.out_path, "explorer") - self.object_base_path = os.path.join(self.out_path, "object") + self.object_path = os.path.join(self.out_path, "object") self.log = logging.getLogger(self.target_host) From 67de9d8c7379c3074b8fce2aeac7bb2e57c8c8a2 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 00:20:55 +0200 Subject: [PATCH 5/8] tests for Local paths Signed-off-by: Steven Armstrong --- lib/cdist/test/exec/test_local.py | 38 ++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/cdist/test/exec/test_local.py b/lib/cdist/test/exec/test_local.py index b74f412e..8585f87a 100644 --- a/lib/cdist/test/exec/test_local.py +++ b/lib/cdist/test/exec/test_local.py @@ -50,12 +50,45 @@ class LocalTestCase(unittest.TestCase): def setUp(self): self.temp_dir = self.mkdtemp() target_host = 'localhost' - out_path = self.temp_dir - self.local = local.Local(target_host, local_base_path, out_path) + self.out_path = self.temp_dir + self.base_path = local_base_path + self.local = local.Local(target_host, self.base_path, self.out_path) def tearDown(self): shutil.rmtree(self.temp_dir) + ### test api + + def test_cache_path(self): + self.assertEqual(self.local.cache_path, os.path.join(self.base_path, "cache")) + + def test_conf_path(self): + self.assertEqual(self.local.conf_path, os.path.join(self.base_path, "conf")) + + def test_global_explorer_path(self): + self.assertEqual(self.local.global_explorer_path, os.path.join(self.base_path, "conf", "explorer")) + + def test_manifest_path(self): + self.assertEqual(self.local.manifest_path, os.path.join(self.base_path, "conf", "manifest")) + + def test_type_path(self): + self.assertEqual(self.local.type_path, os.path.join(self.base_path, "conf", "type")) + + def test_out_path(self): + self.assertEqual(self.local.out_path, self.out_path) + + def test_bin_path(self): + self.assertEqual(self.local.bin_path, os.path.join(self.out_path, "bin")) + + def test_global_explorer_out_path(self): + self.assertEqual(self.local.global_explorer_out_path, os.path.join(self.out_path, "explorer")) + + def test_object_path(self): + self.assertEqual(self.local.object_path, os.path.join(self.out_path, "object")) + + ### /test api + + def test_run_success(self): self.local.run(['/bin/true']) @@ -96,6 +129,5 @@ class LocalTestCase(unittest.TestCase): def test_create_directories(self): self.local.create_directories() - print('self.local.bin_path: %s' % self.local.bin_path) self.assertTrue(os.path.isdir(self.local.out_path)) self.assertTrue(os.path.isdir(self.local.bin_path)) From 5d2827f66ec9152e4e7928e9d4dd47f803f3d7ff Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 00:24:52 +0200 Subject: [PATCH 6/8] tests for Remote paths Signed-off-by: Steven Armstrong --- lib/cdist/test/exec/test_remote.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/cdist/test/exec/test_remote.py b/lib/cdist/test/exec/test_remote.py index 1fdb5833..ea9a17aa 100644 --- a/lib/cdist/test/exec/test_remote.py +++ b/lib/cdist/test/exec/test_remote.py @@ -42,15 +42,31 @@ class RemoteTestCase(unittest.TestCase): def setUp(self): self.temp_dir = self.mkdtemp() target_host = 'localhost' - remote_base_path = self.temp_dir + self.base_path = self.temp_dir user = getpass.getuser() remote_exec = "ssh -o User=%s -q" % user remote_copy = "scp -o User=%s -q" % user - self.remote = remote.Remote(target_host, remote_base_path, remote_exec, remote_copy) + self.remote = remote.Remote(target_host, self.base_path, remote_exec, remote_copy) def tearDown(self): shutil.rmtree(self.temp_dir) + ### test api + + def test_conf_path(self): + self.assertEqual(self.remote.conf_path, os.path.join(self.base_path, "conf")) + + def test_object_path(self): + self.assertEqual(self.remote.object_path, os.path.join(self.base_path, "object")) + + def test_type_path(self): + self.assertEqual(self.remote.type_path, os.path.join(self.base_path, "conf", "type")) + + def test_global_explorer_path(self): + self.assertEqual(self.remote.global_explorer_path, os.path.join(self.base_path, "conf", "explorer")) + + ### /test api + def test_run_success(self): self.remote.run(['/bin/true']) From 48eb996a2e5e150c49705bb3531503a9b9e2f828 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 00:26:11 +0200 Subject: [PATCH 7/8] remove useless filename prefix Signed-off-by: Steven Armstrong --- lib/cdist/test/exec/{test_local.py => local.py} | 0 lib/cdist/test/exec/{test_remote.py => remote.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename lib/cdist/test/exec/{test_local.py => local.py} (100%) rename lib/cdist/test/exec/{test_remote.py => remote.py} (100%) diff --git a/lib/cdist/test/exec/test_local.py b/lib/cdist/test/exec/local.py similarity index 100% rename from lib/cdist/test/exec/test_local.py rename to lib/cdist/test/exec/local.py diff --git a/lib/cdist/test/exec/test_remote.py b/lib/cdist/test/exec/remote.py similarity index 100% rename from lib/cdist/test/exec/test_remote.py rename to lib/cdist/test/exec/remote.py From a8ec91c80484cf248b49559408cf010db99095a2 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 00:35:14 +0200 Subject: [PATCH 8/8] -e /type_base_path/type_path/ -e /object_base_path/object_path/ Signed-off-by: Steven Armstrong --- lib/cdist/core/manifest.py | 6 +++--- lib/cdist/test/manifest/__init__.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/cdist/core/manifest.py b/lib/cdist/core/manifest.py index 8f9fc7ae..e97ba095 100644 --- a/lib/cdist/core/manifest.py +++ b/lib/cdist/core/manifest.py @@ -38,7 +38,7 @@ common: __global: full qualified path to the global output dir == local.out_path __cdist_manifest: full qualified path of the manifest == script __cdist_type_base_path: full qualified path to the directory where types are defined for use in type emulator - == local.type_base_path + == local.type_path initial manifest is: script: full qualified path to the initial manifest @@ -72,7 +72,7 @@ class Manifest(object): 'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']), '__target_host': self.target_host, '__global': self.local.out_path, - '__cdist_type_base_path': self.local.type_base_path, # for use in type emulator + '__cdist_type_base_path': self.local.type_path, # for use in type emulator } def run_initial_manifest(self, script): @@ -90,5 +90,5 @@ class Manifest(object): '__object_fq': cdist_object.path, '__type': cdist_object.type.absolute_path, }) - script = os.path.join(self.local.type_base_path, cdist_object.type.manifest_path) + script = os.path.join(self.local.type_path, cdist_object.type.manifest_path) return self.local.run_script(script, env=env) diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py index ffbbff29..341f4893 100644 --- a/lib/cdist/test/manifest/__init__.py +++ b/lib/cdist/test/manifest/__init__.py @@ -72,12 +72,12 @@ class ManifestTestCase(unittest.TestCase): self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) self.assertEqual(output_dict['__target_host'], self.local.target_host) self.assertEqual(output_dict['__global'], self.local.out_path) - self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_base_path) + self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path) self.assertEqual(output_dict['__manifest'], self.local.manifest_path) def test_type_manifest_environment(self): - cdist_type = core.Type(self.local.type_base_path, '__dump_environment') - cdist_object = core.Object(cdist_type, self.local.object_base_path, 'whatever') + cdist_type = core.Type(self.local.type_path, '__dump_environment') + cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever') output_string = self.manifest.run_type_manifest(cdist_object) output_dict = {} @@ -88,7 +88,7 @@ class ManifestTestCase(unittest.TestCase): self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) self.assertEqual(output_dict['__target_host'], self.local.target_host) self.assertEqual(output_dict['__global'], self.local.out_path) - self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_base_path) + self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path) self.assertEqual(output_dict['__type'], cdist_type.absolute_path) self.assertEqual(output_dict['__object'], cdist_object.absolute_path) self.assertEqual(output_dict['__object_id'], cdist_object.object_id)