From 63eee391e0b49f240ec385ab3936ed321c0a088b Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 01:25:31 +0200 Subject: [PATCH 1/3] implement Explorer analog to Manifest + tests Signed-off-by: Steven Armstrong --- lib/cdist/core/explorer.py | 109 ++++++++++++++++++ lib/cdist/test/explorer/__init__.py | 87 ++++++++++++++ .../explorer/fixtures/conf/explorer/global | 2 + .../conf/type/__test_type/explorer/world | 2 + .../explorer/test_parameter | 3 + 5 files changed, 203 insertions(+) create mode 100644 lib/cdist/core/explorer.py create mode 100644 lib/cdist/test/explorer/__init__.py create mode 100755 lib/cdist/test/explorer/fixtures/conf/explorer/global create mode 100755 lib/cdist/test/explorer/fixtures/conf/type/__test_type/explorer/world create mode 100755 lib/cdist/test/explorer/fixtures/conf/type/__test_type_object_parameter/explorer/test_parameter diff --git a/lib/cdist/core/explorer.py b/lib/cdist/core/explorer.py new file mode 100644 index 00000000..7e1d48de --- /dev/null +++ b/lib/cdist/core/explorer.py @@ -0,0 +1,109 @@ +# -*- coding: utf-8 -*- +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# 2011 Nico Schottelius (nico-cdist at schottelius.org) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import logging +import os + +import cdist + +log = logging.getLogger(__name__) + + +''' +common: + runs only remotely, needs local and remote + + env: + __explorer: full qualified path to other global explorers on remote side == remote.global_explorer_path + +global explorer is: + folder full of scripts which have to be: + (- copied to remote) + - executed one by one on remote + - output saved to local files + + env: + + creates: local files with explorer output + +type explorer is: + folder full of scripts which have to be: + (- copied to remote) + - executed one by one on remote for each object instance + - output saved into object instance + + env: + __object: full qualified path to the object's remote dir + __object_id: the objects id + __object_fq: full qualified object id, iow: $type.name + / + object_id + __type_explorer: full qualified path to the other type explorers on remote side + + creates: nothing, all output is handled by the object instances +''' + + +class Explorer(object): + """Executes cdist explorers. + + """ + def __init__(self, target_host, local, remote): + self.target_host = target_host + self.local = local + self.remote = remote + self.env = { + '__target_host': self.target_host, + '__explorer': self.remote.global_explorer_path, + } + + def transfer_global_explorers(self): + """Transfer the global explorers to the remote side.""" + self.remote.mkdir(self.remote.global_explorer_path) + self.remote.transfer(self.local.global_explorer_path, self.remote.global_explorer_path) + + def run_global_explorer(self, explorer): + """Run the given global explorer and return it's output.""" + script = os.path.join(self.remote.global_explorer_path, explorer) + return self.remote.run_script(script, env=self.env) + + def transfer_type_explorers(self, cdist_type): + """Transfer the type explorers for the given type to the remote side.""" + source = os.path.join(self.local.type_path, cdist_type.explorer_path) + destination = os.path.join(self.remote.type_path, cdist_type.explorer_path) + self.remote.mkdir(destination) + self.remote.transfer(source, destination) + + # FIXME: should i do this? probably not + def transfer_object_parameters(self, cdist_object): + pass + + def run_type_explorer(self, explorer, cdist_object): + """Run the given type explorer for the given object and return it's output.""" + cdist_type = cdist_object.type + env = self.env.copy() + env.update({ + '__object': cdist_object.absolute_path, + '__object_id': cdist_object.object_id, + '__object_fq': cdist_object.path, + '__type_explorer': os.path.join(self.remote.type_path, cdist_type.explorer_path) + }) + script = os.path.join(self.remote.type_path, cdist_type.explorer_path, explorer) + return self.remote.run_script(script, env=env) diff --git a/lib/cdist/test/explorer/__init__.py b/lib/cdist/test/explorer/__init__.py new file mode 100644 index 00000000..6fad157b --- /dev/null +++ b/lib/cdist/test/explorer/__init__.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +# +# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import os +import tempfile +import unittest +import shutil +import getpass + +import cdist +from cdist import core +from cdist import test +from cdist.exec import local +from cdist.exec import remote +from cdist.core import explorer + +import os.path as op +my_dir = op.abspath(op.dirname(__file__)) +fixtures = op.join(my_dir, 'fixtures') +local_base_path = fixtures + +class ExplorerClassTestCase(unittest.TestCase): + + def mkdtemp(self, **kwargs): + return tempfile.mkdtemp(prefix='tmp.cdist.test.', **kwargs) + + def mkstemp(self, **kwargs): + return tempfile.mkstemp(prefix='tmp.cdist.test.', **kwargs) + + def setUp(self): + target_host = 'localhost' + + self.local_base_path = local_base_path + self.out_path = self.mkdtemp() + self.local = local.Local(target_host, self.local_base_path, self.out_path) + self.local.create_directories() + + self.remote_base_path = self.mkdtemp() + self.user = getpass.getuser() + remote_exec = "ssh -o User=%s -q" % self.user + remote_copy = "scp -o User=%s -q" % self.user + self.remote = remote.Remote(target_host, self.remote_base_path, remote_exec, remote_copy) + + self.explorer = explorer.Explorer(target_host, self.local, self.remote) + + def tearDown(self): + shutil.rmtree(self.out_path) + shutil.rmtree(self.remote_base_path) + + def test_transfer_global_explorers(self): + # FIXME: test result + self.explorer.transfer_global_explorers() + + def test_run_global_explorer(self): + # FIXME: test result + self.explorer.transfer_global_explorers() + self.explorer.run_global_explorer('global') + + def test_transfer_type_explorers(self): + # FIXME: test result + cdist_type = core.Type(self.local.type_path, '__test_type') + self.explorer.transfer_type_explorers(cdist_type) + + def test_run_type_explorer(self): + cdist_type = core.Type(self.local.type_path, '__test_type') + cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever') + self.explorer.transfer_type_explorers(cdist_type) + self.assertEqual(self.explorer.run_type_explorer('world', cdist_object), 'hello\n') + diff --git a/lib/cdist/test/explorer/fixtures/conf/explorer/global b/lib/cdist/test/explorer/fixtures/conf/explorer/global new file mode 100755 index 00000000..39c16ea8 --- /dev/null +++ b/lib/cdist/test/explorer/fixtures/conf/explorer/global @@ -0,0 +1,2 @@ +#!/bin/sh +echo global diff --git a/lib/cdist/test/explorer/fixtures/conf/type/__test_type/explorer/world b/lib/cdist/test/explorer/fixtures/conf/type/__test_type/explorer/world new file mode 100755 index 00000000..21ba6825 --- /dev/null +++ b/lib/cdist/test/explorer/fixtures/conf/type/__test_type/explorer/world @@ -0,0 +1,2 @@ +#!/bin/sh +echo hello diff --git a/lib/cdist/test/explorer/fixtures/conf/type/__test_type_object_parameter/explorer/test_parameter b/lib/cdist/test/explorer/fixtures/conf/type/__test_type_object_parameter/explorer/test_parameter new file mode 100755 index 00000000..0778907c --- /dev/null +++ b/lib/cdist/test/explorer/fixtures/conf/type/__test_type_object_parameter/explorer/test_parameter @@ -0,0 +1,3 @@ +#!/bin/sh + +cat "$__object/parameter/test" From c15673aef79b0805597d326667a9bb99db6b4e9a Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 11:05:39 +0200 Subject: [PATCH 2/3] +docstring Signed-off-by: Steven Armstrong --- lib/cdist/core/manifest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cdist/core/manifest.py b/lib/cdist/core/manifest.py index e97ba095..fc82b1cc 100644 --- a/lib/cdist/core/manifest.py +++ b/lib/cdist/core/manifest.py @@ -62,7 +62,7 @@ type manifeste is: class Manifest(object): - """Represents a cdist manifest. + """Executes cdist manifests. """ def __init__(self, target_host, local): From 841b54c6d039e0f1b68b9e3ea22d481d1414b4ef Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 11:06:03 +0200 Subject: [PATCH 3/3] +devnotes, +FIXME Signed-off-by: Steven Armstrong --- lib/cdist/core/explorer.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/cdist/core/explorer.py b/lib/cdist/core/explorer.py index 7e1d48de..c79124b3 100644 --- a/lib/cdist/core/explorer.py +++ b/lib/cdist/core/explorer.py @@ -30,26 +30,25 @@ log = logging.getLogger(__name__) ''' common: - runs only remotely, needs local and remote + runs only remotely, needs local and remote to construct paths env: - __explorer: full qualified path to other global explorers on remote side == remote.global_explorer_path + __explorer: full qualified path to other global explorers on remote side + -> remote.global_explorer_path -global explorer is: - folder full of scripts which have to be: - (- copied to remote) - - executed one by one on remote - - output saved to local files +a global explorer is: + - a script + - executed on the remote side + - returns its output as a string env: - creates: local files with explorer output + creates: nothing, returns output type explorer is: - folder full of scripts which have to be: - (- copied to remote) - - executed one by one on remote for each object instance - - output saved into object instance + - a script + - executed on the remote side for each object instance + - returns its output as a string env: __object: full qualified path to the object's remote dir @@ -57,7 +56,8 @@ type explorer is: __object_fq: full qualified object id, iow: $type.name + / + object_id __type_explorer: full qualified path to the other type explorers on remote side - creates: nothing, all output is handled by the object instances + creates: nothing, returns output + ''' @@ -74,6 +74,7 @@ class Explorer(object): '__explorer': self.remote.global_explorer_path, } + # FIXME: should i do this? def transfer_global_explorers(self): """Transfer the global explorers to the remote side.""" self.remote.mkdir(self.remote.global_explorer_path) @@ -84,6 +85,7 @@ class Explorer(object): script = os.path.join(self.remote.global_explorer_path, explorer) return self.remote.run_script(script, env=self.env) + # FIXME: should i do this? def transfer_type_explorers(self, cdist_type): """Transfer the type explorers for the given type to the remote side.""" source = os.path.join(self.local.type_path, cdist_type.explorer_path)