From 4ef55ef13f9172ab6bb045d5a6bfb7acec982758 Mon Sep 17 00:00:00 2001 From: Daniel Heule Date: Mon, 27 Jan 2014 16:19:01 +0100 Subject: [PATCH 1/3] allow object overrides with CDIST_ALLOW_OVERRIDE=true --- cdist/core/cdist_object.py | 6 ++++-- cdist/emulator.py | 5 ++++- docs/man/man7/cdist-manifest.text | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cdist/core/cdist_object.py b/cdist/core/cdist_object.py index 45b5e3ff..9ead71c7 100644 --- a/cdist/core/cdist_object.py +++ b/cdist/core/cdist_object.py @@ -2,6 +2,7 @@ # # 2011 Steven Armstrong (steven-cdist at armstrong.cc) # 2011-2013 Nico Schottelius (nico-cdist at schottelius.org) +# 2014 Daniel Heule (hda at sfs.biz) # # This file is part of cdist. # @@ -215,9 +216,10 @@ class CdistObject(object): """Create this cdist object on the filesystem. """ try: - os.makedirs(self.absolute_path, exist_ok=False) + cdexist_ok = True if os.environ.get('CDIST_ALLOW_OVERRIDE',"false") == 'true' else False + os.makedirs(self.absolute_path, exist_ok=cdexist_ok) absolute_parameter_path = os.path.join(self.base_path, self.parameter_path) - os.makedirs(absolute_parameter_path, exist_ok=False) + os.makedirs(absolute_parameter_path, exist_ok=cdexist_ok) except EnvironmentError as error: raise cdist.Error('Error creating directories for cdist object: %s: %s' % (self, error)) diff --git a/cdist/emulator.py b/cdist/emulator.py index 63d3bbbc..0693eb35 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -2,6 +2,7 @@ # # 2011-2013 Nico Schottelius (nico-cdist at schottelius.org) # 2012 Steven Armstrong (steven-cdist at armstrong.cc) +# 2014 Daniel Heule (hda at sfs.biz) # # This file is part of cdist. # @@ -144,12 +145,14 @@ class Emulator(object): if value is not None: self.parameters[key] = value - if self.cdist_object.exists: + if self.cdist_object.exists and os.environ.get('CDIST_ALLOW_OVERRIDE',"false") != 'true': if self.cdist_object.parameters != self.parameters: raise cdist.Error("Object %s already exists with conflicting parameters:\n%s: %s\n%s: %s" % (self.cdist_object.name, " ".join(self.cdist_object.source), self.cdist_object.parameters, self.object_source, self.parameters) ) else: + if self.cdist_object.exists: + self.log.debug('Object %s override forced with CDIST_ALLOW_OVERRIDE=true',self.cdist_object.name) self.cdist_object.create() self.cdist_object.parameters = self.parameters diff --git a/docs/man/man7/cdist-manifest.text b/docs/man/man7/cdist-manifest.text index 92d0b897..84dc36ae 100644 --- a/docs/man/man7/cdist-manifest.text +++ b/docs/man/man7/cdist-manifest.text @@ -161,6 +161,30 @@ __package lighttpd --state present require="__package/lighttpd" __package munin --state present -------------------------------------------------------------------------------- +OVERRIDES +--------- +In some special cases, you would like to create an allready defined object +with different parameters. In normal situations this leads to an error in cdist. +If you whish, you can mark this second definition of an object with +CDIST_ALLOW_OVERRIDE=true to tell cdist, that this object override is +wanted and should be accepted. + +-------------------------------------------------------------------------------- +# for example in the inial manifest + +# reate user account foobar with some hash for password +__user foobar --password 'some_fancy_hash' + +# ... many statements and includes in the manifest later ... +# somewhere in a conditionaly sourced manifest +# (e.g. for example only sourced if a special application is on the target host) + +# this leads to an error ... +__user foobar --password 'some_other_hash' + +# this tells cdist, that you know that this is an override and should be accepted +CDIST_ALLOW_OVERRIDE=true __user foobar --password 'some_other_hash' +-------------------------------------------------------------------------------- SEE ALSO From 197fabf40ac3c84f8bed89e3576b82672496bb61 Mon Sep 17 00:00:00 2001 From: Daniel Heule Date: Fri, 31 Jan 2014 17:56:55 +0100 Subject: [PATCH 2/3] added some ideas from asteven and a bit more description about the order in the manpage --- cdist/core/cdist_object.py | 7 +++---- cdist/emulator.py | 4 +++- docs/man/man7/cdist-manifest.text | 5 ++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cdist/core/cdist_object.py b/cdist/core/cdist_object.py index 9ead71c7..b17bd339 100644 --- a/cdist/core/cdist_object.py +++ b/cdist/core/cdist_object.py @@ -212,14 +212,13 @@ class CdistObject(object): """Checks wether this cdist object exists on the file systems.""" return os.path.exists(self.absolute_path) - def create(self): + def create(self, allow_overwrite=False): """Create this cdist object on the filesystem. """ try: - cdexist_ok = True if os.environ.get('CDIST_ALLOW_OVERRIDE',"false") == 'true' else False - os.makedirs(self.absolute_path, exist_ok=cdexist_ok) + os.makedirs(self.absolute_path, exist_ok=allow_overwrite) absolute_parameter_path = os.path.join(self.base_path, self.parameter_path) - os.makedirs(absolute_parameter_path, exist_ok=cdexist_ok) + os.makedirs(absolute_parameter_path, exist_ok=allow_overwrite) except EnvironmentError as error: raise cdist.Error('Error creating directories for cdist object: %s: %s' % (self, error)) diff --git a/cdist/emulator.py b/cdist/emulator.py index 0693eb35..e32f12e0 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -153,7 +153,9 @@ class Emulator(object): else: if self.cdist_object.exists: self.log.debug('Object %s override forced with CDIST_ALLOW_OVERRIDE=true',self.cdist_object.name) - self.cdist_object.create() + self.cdist_object.create(True) + else + self.cdist_object.create() self.cdist_object.parameters = self.parameters # Record / Append source diff --git a/docs/man/man7/cdist-manifest.text b/docs/man/man7/cdist-manifest.text index 84dc36ae..d9bcb893 100644 --- a/docs/man/man7/cdist-manifest.text +++ b/docs/man/man7/cdist-manifest.text @@ -163,11 +163,14 @@ require="__package/lighttpd" __package munin --state present OVERRIDES --------- -In some special cases, you would like to create an allready defined object +In some special cases, you would like to create an already defined object with different parameters. In normal situations this leads to an error in cdist. If you whish, you can mark this second definition of an object with CDIST_ALLOW_OVERRIDE=true to tell cdist, that this object override is wanted and should be accepted. +ATTENTION: Only use this feature if you are 100% sure in which order +cdist encounter the affected objects, otherwhise this results +into an undefined situation. -------------------------------------------------------------------------------- # for example in the inial manifest From 5fbac8d0baa85197216ed1e3cb3fa7262415fb40 Mon Sep 17 00:00:00 2001 From: Daniel Heule Date: Fri, 31 Jan 2014 17:59:56 +0100 Subject: [PATCH 3/3] forgot the : after the else ... --- cdist/emulator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/emulator.py b/cdist/emulator.py index e32f12e0..ed846e6d 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -154,7 +154,7 @@ class Emulator(object): if self.cdist_object.exists: self.log.debug('Object %s override forced with CDIST_ALLOW_OVERRIDE=true',self.cdist_object.name) self.cdist_object.create(True) - else + else: self.cdist_object.create() self.cdist_object.parameters = self.parameters