From 73c77dd2d33e38f4cac6939f7a2739930db20502 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Thu, 5 Mar 2015 15:02:26 +0100 Subject: [PATCH] Let core use random .cdist directory for objects Signed-off-by: Nico Schottelius --- cdist/__init__.py | 4 +--- cdist/config.py | 5 ++-- cdist/core/__init__.py | 2 +- cdist/core/cdist_object.py | 28 +++++++++++++---------- cdist/emulator.py | 14 ++++++++---- cdist/exec/local.py | 21 +++++++++++++++-- cdist/shell.py | 2 +- docs/dev/logs/2015-02-22.allow_dot_cdist | 29 ++++++++++++++++++++++++ 8 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 docs/dev/logs/2015-02-22.allow_dot_cdist diff --git a/cdist/__init__.py b/cdist/__init__.py index 20c76b31..4454a3ac 100644 --- a/cdist/__init__.py +++ b/cdist/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# 2010-2012 Nico Schottelius (nico-cdist at schottelius.org) +# 2010-2015 Nico Schottelius (nico-cdist at schottelius.org) # # This file is part of cdist. # @@ -41,8 +41,6 @@ BANNER = """ "P' "" "" """ -DOT_CDIST = ".cdist" - REMOTE_COPY = "scp -o User=root -q" REMOTE_EXEC = "ssh -o User=root -q" diff --git a/cdist/config.py b/cdist/config.py index a0c787ac..bba9bfcb 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# 2010-2014 Nico Schottelius (nico-cdist at schottelius.org) +# 2010-2015 Nico Schottelius (nico-cdist at schottelius.org) # # This file is part of cdist. # @@ -162,7 +162,8 @@ class Config(object): def object_list(self): """Short name for object list retrieval""" for cdist_object in core.CdistObject.list_objects(self.local.object_path, - self.local.type_path): + self.local.type_path, + self.local.object_marker_name): if cdist_object.cdist_type.is_install: self.log.debug("Running in config mode, ignoring install object: {0}".format(cdist_object)) else: diff --git a/cdist/core/__init__.py b/cdist/core/__init__.py index 5dafd061..d773fc01 100644 --- a/cdist/core/__init__.py +++ b/cdist/core/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc) -# 2014 Nico Schottelius (nico-cdist at schottelius.org) +# 2014-2015 Nico Schottelius (nico-cdist at schottelius.org) # # This file is part of cdist. # diff --git a/cdist/core/cdist_object.py b/cdist/core/cdist_object.py index d13c33dd..ba0fdf11 100644 --- a/cdist/core/cdist_object.py +++ b/cdist/core/cdist_object.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # 2011 Steven Armstrong (steven-cdist at armstrong.cc) -# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org) +# 2011-2015 Nico Schottelius (nico-cdist at schottelius.org) # 2014 Daniel Heule (hda at sfs.biz) # # This file is part of cdist. @@ -63,7 +63,7 @@ class CdistObject(object): STATE_RUNNING = "running" STATE_DONE = "done" - def __init__(self, cdist_type, base_path, object_marker=".cdist", object_id=''): + def __init__(self, cdist_type, base_path, object_marker, object_id): self.cdist_type = cdist_type # instance of Type self.base_path = base_path self.object_id = object_id @@ -75,30 +75,34 @@ class CdistObject(object): self.name = self.join_name(self.cdist_type.name, self.object_id) self.path = os.path.join(self.cdist_type.path, self.object_id, self.object_marker) + self.absolute_path = os.path.join(self.base_path, self.path) self.code_local_path = os.path.join(self.path, "code-local") self.code_remote_path = os.path.join(self.path, "code-remote") self.parameter_path = os.path.join(self.path, "parameter") @classmethod - def list_objects(cls, object_base_path, type_base_path): + def list_objects(cls, object_base_path, type_base_path, object_marker): """Return a list of object instances""" - for object_name in cls.list_object_names(object_base_path): + for object_name in cls.list_object_names(object_base_path, object_marker): type_name, object_id = cls.split_name(object_name) - yield cls(cdist.core.CdistType(type_base_path, type_name), object_base_path, object_id=object_id) + yield cls(cdist.core.CdistType(type_base_path, type_name), + base_path=object_base_path, + object_marker=object_marker, + object_id=object_id) + + @classmethod + def list_object_names(cls, object_base_path, object_marker): + """Return a list of object names""" + for path, dirs, files in os.walk(object_base_path): + if object_marker in dirs: + yield os.path.relpath(path, object_base_path) @classmethod def list_type_names(cls, object_base_path): """Return a list of type names""" return os.listdir(object_base_path) - @classmethod - def list_object_names(cls, object_base_path): - """Return a list of object names""" - for path, dirs, files in os.walk(object_base_path): - if self.object_marker in dirs: - yield os.path.relpath(path, object_base_path) - @staticmethod def split_name(object_name): """split_name('__type_name/the/object_id') -> ('__type_name', 'the/object_id') diff --git a/cdist/emulator.py b/cdist/emulator.py index 41834fbf..abd8c884 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org) +# 2011-2015 Nico Schottelius (nico-cdist at schottelius.org) # 2012 Steven Armstrong (steven-cdist at armstrong.cc) # 2014 Daniel Heule (hda at sfs.biz) # @@ -64,9 +64,10 @@ class Emulator(object): self.global_path = self.env['__global'] self.target_host = self.env['__target_host'] - # Internally only + # Internal variables self.object_source = self.env['__cdist_manifest'] self.type_base_path = self.env['__cdist_type_base_path'] + self.object_marker = self.env['__cdist_object_marker'] except KeyError as e: raise MissingRequiredEnvironmentVariableError(e.args[0]) @@ -131,13 +132,16 @@ class Emulator(object): self.log.debug('Args: %s' % self.args) def setup_object(self): - # Setup object_id - FIXME: unset / do not setup anymore! - if not self.cdist_type.is_singleton: + # Setup object - and ensure it is not in args + if self.cdist_type.is_singleton: + self.object_id = False + else: self.object_id = self.args.object_id[0] del self.args.object_id # Instantiate the cdist object we are defining - self.cdist_object = core.CdistObject(self.cdist_type, self.object_base_path, self.object_id) + self.cdist_object = core.CdistObject(self.cdist_type, + self.object_base_path, self.object_marker, self.object_id) # Create object with given parameters self.parameters = {} diff --git a/cdist/exec/local.py b/cdist/exec/local.py index 2f75ffd4..40f34e25 100644 --- a/cdist/exec/local.py +++ b/cdist/exec/local.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # 2011 Steven Armstrong (steven-cdist at armstrong.cc) -# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org) +# 2011-2015 Nico Schottelius (nico-cdist at schottelius.org) # # This file is part of cdist. # @@ -66,9 +66,9 @@ class Local(object): self._init_log() self._init_permissions() self._init_paths() + self._init_object_marker() self._init_conf_dirs() - @property def dist_conf_dir(self): return os.path.abspath(os.path.join(os.path.dirname(cdist.__file__), "conf")) @@ -103,6 +103,12 @@ class Local(object): self.type_path = os.path.join(self.conf_path, "type") + def _init_object_marker(self): + self.object_marker_file = os.path.join(self.base_path, "object_marker") + + # Does not need to be secure - just randomly different from .cdist + self.object_marker_name = tempfile.mktemp(prefix='.cdist-', dir='') + def _init_conf_dirs(self): self.conf_dirs = [] @@ -125,6 +131,7 @@ class Local(object): def _init_directories(self): self.mkdir(self.conf_path) self.mkdir(self.global_explorer_out_path) + self.mkdir(self.object_path) self.mkdir(self.bin_path) def create_files_dirs(self): @@ -132,6 +139,13 @@ class Local(object): self._create_conf_path_and_link_conf_dirs() self._create_messages() self._link_types_for_emulator() + self._setup_object_marker_file() + + def _setup_object_marker_file(self): + with open(self.object_marker_file, 'w') as fd: + fd.write("%s\n" % self.object_marker_name) + + self.log.debug("Object marker %s saved in %s" % (self.object_marker_name, self.object_marker_file)) def _init_cache_dir(self, cache_dir): @@ -166,6 +180,9 @@ class Local(object): # Export __target_host for use in __remote_{copy,exec} scripts env['__target_host'] = self.target_host + # Export for emulator + env['__cdist_object_marker'] = self.object_marker_name + if message_prefix: message = cdist.message.Message(message_prefix, self.messages_path) env.update(message.env) diff --git a/cdist/shell.py b/cdist/shell.py index 8ca68115..d0921bc9 100644 --- a/cdist/shell.py +++ b/cdist/shell.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# 2013 Nico Schottelius (nico-cdist at schottelius.org) +# 2013-2015 Nico Schottelius (nico-cdist at schottelius.org) # # This file is part of cdist. # diff --git a/docs/dev/logs/2015-02-22.allow_dot_cdist b/docs/dev/logs/2015-02-22.allow_dot_cdist new file mode 100644 index 00000000..58a1b041 --- /dev/null +++ b/docs/dev/logs/2015-02-22.allow_dot_cdist @@ -0,0 +1,29 @@ +- locate code that references .cdist + - cdist_object.py +- need to change code that handles objects? + - setup object marker + exec/local.py + - cdist/emulator.py + - need to know the marker name + - shell.py + - test/manifest/__init__.py + - core/code.py: + - core/manifest.py: + - core/manifest.py: + - list_object_names() needs to know the marker -- used BY: + - list_objects + - cdist/test/cdist_object/__init__.py + - cdist/config.py + - cdist/test/cdist_object/__init__.py + + - list_object_names + - needs to have object_marker + +- or modify object code to load name +- setup a per-run random name + - local.py +- use the per-run random name +- create test + + def __init__(self, cdist_type, base_path, object_marker=".cdist", object_id=''): +