diff --git a/lib/cdist/config_install.py b/lib/cdist/config_install.py index 418f9fd9..7214cdba 100644 --- a/lib/cdist/config_install.py +++ b/lib/cdist/config_install.py @@ -270,6 +270,33 @@ class ConfigInstall: cdist_object.ran = True def run_type_explorer(self, cdist_object): + + def run_type_explorer(self, cdist_object, remote_global_explorer_path, remote_type_path, remote_object_path, scp-hints, ssh-hints, object_base_path): + + def __init__(self, remote_global_explorer_path, remote_type_path, remote_object_path, exec-hint, object_base_path, global_explorer_out_path): + + def __init__(self, remote_global_explorer_path, remote_type_path, remote_object_path, exec-hint): + + def run_type_explorer(self, cdist_object) + + self.value = .,.. + return value + + def run_global_explorer(self) + + def __init__(self, remote_global_explorer_path, remote_type_path, remote_object_path, exec-hint): + == 1x + + + + + run_type_explorer() + self.instance.value[obej + + c = ConfigInstall("foo") + c.remote_global_explorer_path = "moo" + + def run_type_explorer(self, cdist_object) """Run type specific explorers for objects""" cdist_type = cdist_object.type @@ -301,6 +328,8 @@ class ConfigInstall: cdist.exec.run_or_fail(remote_cmd, stdout=output_fd, remote_prefix=True) output_fd.close() + return outputs + def link_emulator(self): """Link emulator to types""" src = os.path.abspath(self.exec_path) @@ -311,6 +340,8 @@ class ConfigInstall: # FIXME: handle exception / make it more beautiful / Steven: raise except :-) os.symlink(src, dst) + def run_global_explorers(src_path, dst_path, remote_dst_path): + def run_global_explorers(self): """Run global explorers""" log.info("Running global explorers") @@ -413,17 +444,3 @@ class ConfigInstall: # but remote_mkdir uses -p to fix this self.remote_mkdir(dst) self.transfer_path(src, dst) - - def remote_mkdir(self, directory): - """Create directory on remote side""" - cdist.exec.run_or_fail(["mkdir", "-p", directory], remote_prefix=True) - - def remove_remote_path(self, destination): - """Ensure path on remote side vanished""" - cdist.exec.run_or_fail(["rm", "-rf", destination], remote_prefix=True) - - def transfer_path(self, source, destination): - """Transfer directory and previously delete the remote destination""" - self.remove_remote_path(destination) - cdist.exec.run_or_fail(os.environ['__remote_copy'].split() + - ["-r", source, self.target_host + ":" + destination]) diff --git a/lib/cdist/exec.py b/lib/cdist/exec.py index 63a3dfa3..7ec9bc0a 100644 --- a/lib/cdist/exec.py +++ b/lib/cdist/exec.py @@ -27,6 +27,10 @@ log = logging.getLogger(__name__) import cdist +class ExecWrapper(object): + def __init__(self, remote_exec, remote_copy, target_host): + self.remote_exec = remote_exec + def shell_run_or_debug_fail(script, *args, remote_prefix=False, **kargs): # Manually execute /bin/sh, because sh -e does what we want # and sh -c -e does not exit if /bin/false called @@ -75,3 +79,19 @@ def run_or_fail(*args, remote_prefix=False, **kargs): raise cdist.Error("Command failed: " + " ".join(*args)) except OSError as error: raise cdist.Error(" ".join(*args) + ": " + error.args[1]) + + + + def remote_mkdir(self, directory): + """Create directory on remote side""" + cdist.exec.run_or_fail(["mkdir", "-p", directory], remote_prefix=True) + + def remove_remote_path(self, destination): + """Ensure path on remote side vanished""" + cdist.exec.run_or_fail(["rm", "-rf", destination], remote_prefix=True) + + def transfer_path(self, source, destination): + """Transfer directory and previously delete the remote destination""" + self.remove_remote_path(destination) + cdist.exec.run_or_fail(os.environ['__remote_copy'].split() + + ["-r", source, self.target_host + ":" + destination]) diff --git a/lib/cdist/explorer.py b/lib/cdist/explorer.py new file mode 100644 index 00000000..e2c41f8a --- /dev/null +++ b/lib/cdist/explorer.py @@ -0,0 +1,156 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2010-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 io +import logging +#import os +#import stat +#import shutil +#import sys +#import tempfile +#import time +# +#import cdist.core +#import cdist.exec + +log = logging.getLogger(__name__) + +class Explorer: + """Execute explorers""" + + def __init__(self, + object_base_path, + remote_global_explorer_path, + remote_type_path, + remote_object_path, + type_path, + exec_hint): + + self.object_base_path = object_base_path + self.remote_global_explorer_path = remote_global_explorer_path + self.remote_type_path = remote_type_path + self.remote_object_path = remote_object_path + self.exec_hint = exec_hint + + def run_type_explorer(self, cdist_object) + """Run type specific explorers for objects""" + + cdist_type = cdist_object.type + self.transfer_type_explorers(cdist_type) + + cmd = [] + cmd.append("__explorer=" + self.remote_global_explorer_path) + cmd.append("__type_explorer=" + os.path.join( + self.remote_type_path, + cdist_type.explorer_path)) + cmd.append("__object=" + os.path.join( + self.remote_object_path, + cdist_object.path)) + cmd.append("__object_id=" + cdist_object.object_id) + cmd.append("__object_fq=" + cdist_object.name) + + # Need to transfer at least the parameters for objects to be useful + self.transfer_object_parameter(cdist_object) + + outputs = {} + for explorer in cdist_type.explorers: + remote_cmd = cmd + [os.path.join(self.remote_type_path, + cdist_type.explorer_path, explorer)] + outputs[explorer] = io.StringIO() + log.debug("%s exploring %s using %s storing to %s", + cdist_object, explorer, remote_cmd, output) + + cdist.exec.run_or_fail(remote_cmd, stdout=outputs[explorer], remote_prefix=True) + + return outputs + + def run_global_explorers(self): + """Run global explorers""" + log.info("Running global explorers") + + src_path = self.global_explorer_path + dst_path = self.remote_global_explorer_path + + self.transfer_path(src_path, dst_path) + + outputs = {} + for explorer in os.listdir(src_path): + outputs[explorer] = io.StringIO() + cmd = [] + cmd.append("__explorer=" + remote_dst_path) + cmd.append(os.path.join(remote_dst_path, explorer)) + cdist.exec.run_or_fail(cmd, stdout=outputs[explorer], remote_prefix=True) + + def transfer_object_parameter(self, cdist_object): + """Transfer the object parameter to the remote destination""" + src = os.path.join(self.object_base_path, + cdist_object.parameter_path) + dst = os.path.join(self.remote_object_path, + cdist_object.parameter_path) + + # Synchronise parameter dir afterwards + self.remote_mkdir(dst) + self.transfer_path(src, dst) + + def transfer_global_explorers(self): + """Transfer the global explorers""" + self.remote_mkdir(self.remote_global_explorer_path) + self.transfer_path(self.global_explorer_path, + self.remote_global_explorer_path) + + def transfer_type_explorers(self, cdist_type): + """Transfer explorers of a type, but only once""" + if cdist_type.transferred_explorers: + log.debug("Skipping retransfer for explorers of %s", cdist_type) + return + else: + log.debug("Ensure no retransfer for %s", cdist_type) + # Do not retransfer + cdist_type.transferred_explorers = True + + explorers = cdist_type.explorers + + if len(explorers) > 0: + rel_path = cdist_type.explorer_path + src = os.path.join(self.type_base_path, rel_path) + dst = os.path.join(self.remote_type_path, rel_path) + + # Ensure full path until type exists: + # /var/lib/cdist/conf/type/__directory/explorer + # /var/lib/cdist/conf/type/__directory may not exist, + # but remote_mkdir uses -p to fix this + self.remote_mkdir(dst) + self.transfer_path(src, dst) + + def remote_mkdir(self, directory): + """Create directory on remote side""" + cdist.exec.run_or_fail(["mkdir", "-p", directory], remote_prefix=True) + + def remove_remote_path(self, destination): + """Ensure path on remote side vanished""" + cdist.exec.run_or_fail(["rm", "-rf", destination], remote_prefix=True) + + def transfer_path(self, source, destination): + """Transfer directory and previously delete the remote destination""" + self.remove_remote_path(destination) + cdist.exec.run_or_fail(os.environ['__remote_copy'].split() + + ["-r", source, self.target_host + ":" + destination])