From 6d438d4ec5cbb738283fa37c1f96e61834287412 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 13 Oct 2011 14:29:20 +0200 Subject: [PATCH] implement run_gencode_local Signed-off-by: Steven Armstrong --- lib/cdist/core/code.py | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 lib/cdist/core/code.py diff --git a/lib/cdist/core/code.py b/lib/cdist/core/code.py new file mode 100644 index 00000000..de735cb4 --- /dev/null +++ b/lib/cdist/core/code.py @@ -0,0 +1,110 @@ +# -*- 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 locally, does not need remote + + env: + PATH: prepend directory with type emulator symlinks == local.bin_path + __target_host: the target host we are working on + __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_path + +gencode-local + script: full qualified path to a types gencode-local + + env: + __target_host: the target host we are working on + __global: full qualified path to the global output dir == local.out_path + __object: full qualified path to the object's dir + __object_id: the objects id + __object_fq: full qualified object id, iow: $type.name + / + object_id + __type: full qualified path to the type's dir + + returns: string containing the generated code or None + +gencode-remote + script: full qualified path to a types gencode-remote + + env: + __target_host: the target host we are working on + __global: full qualified path to the global output dir == local.out_path + __object: full qualified path to the object's dir + __object_id: the objects id + __object_fq: full qualified object id, iow: $type.name + / + object_id + __type: full qualified path to the type's dir + + returns: string containing the generated code or None + + +code-local + script: full qualified path to object's code-local + - run script localy + returns: string containing the output + +code-remote + script: full qualified path to object's code-remote + - copy script to remote + - run script remotely + returns: string containing the output +''' + + +class Code(object): + """Generates and executes cdist code scripts. + + """ + 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, + '__global': self.local.out_path, + } + + def _get_env_for_object(self, cdist_object): + return { + '__type': cdist_object.type.absolute_path, + '__object': cdist_object.absolute_path, + '__object_id': cdist_object.object_id, + '__object_fq': cdist_object.path, + } + + def run_gencode_local(self, cdist_object): + cdist_type = cdist_object.type + script = os.path.join(self.local.type_path, cdist_type.gencode_local_path) + env = os.environ.copy() + env.update(self.env) + env.update(self._get_env_for_object(cdist_object)) + return self.local.run_script(script, env=env) +