From 2c298c22ea93dbd7556291d16c9394d67df93900 Mon Sep 17 00:00:00 2001 From: Matthias Stecher Date: Sun, 26 Jul 2020 17:54:15 +0200 Subject: [PATCH] Add library access to code-remote script. The code-remote script can now access the library files, too. It gets the same environment variables `$__library` (global library) and `$__type_library` (type specific library) like the type explorers. The library files are copied to the remote host if this is not already done for the type explorers. The method for this was moved to the cdist type class to gain better generalisation and accessability for both parties. --- cdist/core/cdist_type.py | 25 +++++++++++++++++++++++++ cdist/core/code.py | 18 ++++++++++++++---- cdist/core/explorer.py | 15 +++------------ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/cdist/core/cdist_type.py b/cdist/core/cdist_type.py index 044c2b9d..f44a778c 100644 --- a/cdist/core/cdist_type.py +++ b/cdist/core/cdist_type.py @@ -56,6 +56,7 @@ class CdistType: self.absolute_path = os.path.join(self.base_path, self.path) if not os.path.isdir(self.absolute_path): raise InvalidTypeError(self.name, self.path, self.absolute_path) + self.manifest_path = os.path.join(self.name, "manifest") self.explorer_path = os.path.join(self.name, "explorer") self.library_path = os.path.join(self.name, "library") @@ -72,6 +73,8 @@ class CdistType: self.__parameter_defaults = None self.__deprecated_parameters = None + self._transferred_type_library = False + def __hash__(self): return hash(self.name) @@ -297,3 +300,25 @@ class CdistType: finally: self.__deprecated_parameters = deprecated return self.__deprecated_parameters + + # transfer of the library required by type explorers and code-remote + def transfer_type_library(self, target): + """Transfer the type library for the given type to the remote side. + + target: Code | Explorer | Manifest + (must contain .local and .remote properties) + """ + if not self._transferred_type_library: + source = os.path.join(target.local.type_path, + self.library_path) + destination = os.path.join(target.remote.type_path, + self.library_path) + + if os.path.isdir(source) and os.listdir(source): + target.remote.transfer(source, destination) + target.remote.run(["chmod", "0700", "%s/*" % (destination)]) + else: + # at least, the directory should exist + target.remote.mkdir(destination) + + self._transferred_type_library = True diff --git a/cdist/core/code.py b/cdist/core/code.py index 1e76386d..85e689d0 100644 --- a/cdist/core/code.py +++ b/cdist/core/code.py @@ -40,7 +40,7 @@ common: types are defined for use in type emulator == local.type_path __library: full qualified path to the library folder containing - common code == local.library_path + common code == local.global_library_path gencode-local script: full qualified path to a types gencode-local @@ -160,7 +160,12 @@ class Code: def transfer_code_remote(self, cdist_object): """Transfer the code_remote script for the given object to the - remote side.""" + remote side. Will transfer the type library too if not already + copied to the remote side cause of type explorers.""" + # $__type/library (if not already done) + cdist_object.cdist_type.transfer_type_library(self) + + # $__object/code-remote source = os.path.join(self.local.object_path, cdist_object.code_remote_path) destination = os.path.join(self.remote.object_path, @@ -199,11 +204,16 @@ class Code: def run_code_remote(self, cdist_object): """Run the code-remote script for the given cdist object on the remote side.""" - # Put some env vars, to allow read only access to the parameters - # over $__object which is already on the remote side env = { + # Put some env vars, to allow read only access to the parameters + # over $__object which is already on the remote side '__object': os.path.join(self.remote.object_path, cdist_object.path), '__object_id': cdist_object.object_id, + + # The libraries should be over there, too + '__library': self.remote.global_library_path, + '__type_library': os.path.join(self.remote.type_path, + cdist_object.cdist_type.library_path), } return self._run_code(cdist_object, 'remote', env=env) diff --git a/cdist/core/explorer.py b/cdist/core/explorer.py index b3a55da6..1d2f7b98 100644 --- a/cdist/core/explorer.py +++ b/cdist/core/explorer.py @@ -255,8 +255,10 @@ class Explorer: self.log.trace(("Skipping retransfer of type explorers " "for: %s"), cdist_type) else: - self.transfer_type_library(cdist_type) + # transfer library by common code + cdist_type.transfer_type_library(self) + # transfer type explorers source = os.path.join(self.local.type_path, cdist_type.explorer_path) destination = os.path.join(self.remote.type_path, @@ -265,17 +267,6 @@ class Explorer: self.remote.run(["chmod", "0700", "%s/*" % (destination)]) self._type_explorers_transferred.append(cdist_type.name) - def transfer_type_library(self, cdist_type): - """Transfer the type library for the given type to the - remote side.""" - source = os.path.join(self.local.type_path, - cdist_type.library_path) - if os.path.exists(source) and os.listdir(source): - destination = os.path.join(self.remote.type_path, - cdist_type.library_path) - self.remote.transfer(source, destination) - self.remote.run(["chmod", "0700", "%s/*" % (destination)]) - def transfer_object_parameters(self, cdist_object): """Transfer the parameters for the given object to the remote side.""" if cdist_object.parameters: