From 36513997d9aba2b39dde1b448a127b25e73c4444 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Mon, 4 Jun 2012 22:01:32 +0200 Subject: [PATCH 1/3] =?UTF-8?q?implement=20multiple=20parameters=20based?= =?UTF-8?q?=20on=20https://github.com/telmich/cdist/pull/71=20by=20S=C3=A9?= =?UTF-8?q?bastien=20Gross?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Steven Armstrong --- doc/man/man7/cdist-type.text | 19 ++++++++++++++++--- lib/cdist/core/cdist_type.py | 34 ++++++++++++++++++++++++++++++++++ lib/cdist/emulator.py | 12 +++++++++--- lib/cdist/util/fsproperty.py | 6 +++++- 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/doc/man/man7/cdist-type.text b/doc/man/man7/cdist-type.text index 92a2b36d..725c583e 100644 --- a/doc/man/man7/cdist-type.text +++ b/doc/man/man7/cdist-type.text @@ -74,14 +74,19 @@ DEFINING PARAMETERS ------------------- Every type consists of required, optional and boolean parameters, which must be created in a newline seperated file in ***parameter/required***, -***parameter/optional*** and ***parameter/boolean***. If either is missing, -the type will have no required, no optional, no boolean or no parameters at -all. +***parameter/required_multiple***, ***parameter/optional***, +***parameter/optional_multiple*** and ***parameter/boolean***. +Parameters which are allowed multiple times should be listed in +required_multiple or optional_multiple respectively. For all other parameters +the standard unix behaviour of the last given wins is applied. +If either is missing, the type will have no required, no optional, no boolean +or no parameters at all. Example: -------------------------------------------------------------------------------- echo servername >> conf/type/__nginx_vhost/parameter/required echo logdirectory >> conf/type/__nginx_vhost/parameter/optional +echo server_alias >> conf/type/__nginx_vhost/parameter/optional_multiple echo use_ssl >> conf/type/__nginx_vhost/parameter/boolean -------------------------------------------------------------------------------- @@ -108,6 +113,14 @@ if [ -f "$__object/parameter/use_ssl" ]; then # file exists -> True # do some fancy ssl stuff fi + +# parameter with multiple values +if [ -f "$__object/parameter/server_alias" ]; then + for alias in $(cat "$__object/parameter/server_alias"); do + echo $alias > /some/where/usefull + done +fi + -------------------------------------------------------------------------------- diff --git a/lib/cdist/core/cdist_type.py b/lib/cdist/core/cdist_type.py index 1d2472c4..86f3ced1 100644 --- a/lib/cdist/core/cdist_type.py +++ b/lib/cdist/core/cdist_type.py @@ -81,7 +81,9 @@ class CdistType(object): self.__explorers = None self.__required_parameters = None + self.__required_multiple_parameters = None self.__optional_parameters = None + self.__optional_multiple_parameters = None self.__boolean_parameters = None def __repr__(self): @@ -130,6 +132,22 @@ class CdistType(object): self.__required_parameters = parameters return self.__required_parameters + @property + def required_multiple_parameters(self): + """Return a list of required multiple parameters""" + if not self.__required_multiple_parameters: + parameters = [] + try: + with open(os.path.join(self.absolute_path, "parameter", "required_multiple")) as fd: + for line in fd: + parameters.append(line.strip()) + except EnvironmentError: + # error ignored + pass + finally: + self.__required_multiple_parameters = parameters + return self.__required_multiple_parameters + @property def optional_parameters(self): """Return a list of optional parameters""" @@ -146,6 +164,22 @@ class CdistType(object): self.__optional_parameters = parameters return self.__optional_parameters + @property + def optional_multiple_parameters(self): + """Return a list of optional multiple parameters""" + if not self.__optional_multiple_parameters: + parameters = [] + try: + with open(os.path.join(self.absolute_path, "parameter", "optional_multiple")) as fd: + for line in fd: + parameters.append(line.strip()) + except EnvironmentError: + # error ignored + pass + finally: + self.__optional_multiple_parameters = parameters + return self.__optional_multiple_parameters + @property def boolean_parameters(self): """Return a list of boolean parameters""" diff --git a/lib/cdist/emulator.py b/lib/cdist/emulator.py index 39d8ca40..95f29a7b 100644 --- a/lib/cdist/emulator.py +++ b/lib/cdist/emulator.py @@ -89,12 +89,18 @@ class Emulator(object): parser = argparse.ArgumentParser(add_help=False, argument_default=argparse.SUPPRESS) - for parameter in self.cdist_type.optional_parameters: - argument = "--" + parameter - parser.add_argument(argument, dest=parameter, action='store', required=False) for parameter in self.cdist_type.required_parameters: argument = "--" + parameter parser.add_argument(argument, dest=parameter, action='store', required=True) + for parameter in self.cdist_type.required_multiple_parameters: + argument = "--" + parameter + parser.add_argument(argument, dest=parameter, action='append', required=True) + for parameter in self.cdist_type.optional_parameters: + argument = "--" + parameter + parser.add_argument(argument, dest=parameter, action='store', required=False) + for parameter in self.cdist_type.optional_multiple_parameters: + argument = "--" + parameter + parser.add_argument(argument, dest=parameter, action='append', required=False) for parameter in self.cdist_type.boolean_parameters: argument = "--" + parameter parser.add_argument(argument, dest=parameter, action='store_const', const='') diff --git a/lib/cdist/util/fsproperty.py b/lib/cdist/util/fsproperty.py index 55428c4d..5814b2b4 100644 --- a/lib/cdist/util/fsproperty.py +++ b/lib/cdist/util/fsproperty.py @@ -134,7 +134,11 @@ class DirectoryDict(collections.MutableMapping): def __setitem__(self, key, value): try: with open(os.path.join(self.path, key), "w") as fd: - fd.write(str(value)) + if type(value) == type([]): + for v in value: + fd.write(str(v) + '\n') + else: + fd.write(str(value)) except EnvironmentError as e: raise cdist.Error(str(e)) From 06c87f92477c62b7e0315e1f790596f277dbee72 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 5 Jun 2012 18:28:47 +0200 Subject: [PATCH 2/3] changes(2.0.13) += multiple parameter support Signed-off-by: Nico Schottelius --- doc/changelog | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/changelog b/doc/changelog index 9eff3efa..0d44af54 100644 --- a/doc/changelog +++ b/doc/changelog @@ -9,6 +9,7 @@ Changelog * Bugfix __addifnosuchline: Fixed quotes/interpolation bug ("a b" became "a b") * New Explorer: interfaces (Sébastien Gross) * Feature core: Support reading from stdin in types (Steven Armstrong) + * Feature core: Support multiple parameters for types (Steven Armstrong) * Feature __file: Support reading from stdin with - syntax (Steven Armstrong) 2.0.12: 2012-05-29 From 365b320e7cf5714a9386fcd6a8a11d36692c2d03 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 5 Jun 2012 18:32:09 +0200 Subject: [PATCH 3/3] ++version = 2.0.13 Signed-off-by: Nico Schottelius --- doc/changelog | 2 +- lib/cdist/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/changelog b/doc/changelog index 0d44af54..60b21f90 100644 --- a/doc/changelog +++ b/doc/changelog @@ -4,7 +4,7 @@ Changelog * Changes are always commented with their author in (braces) * Exception: No braces means author == Nico Schottelius -2.0.13: +2.0.13: 2012-06-05 * Bugfix __ssh_authorized_key: Ensure it sets proper group (contradict) * Bugfix __addifnosuchline: Fixed quotes/interpolation bug ("a b" became "a b") * New Explorer: interfaces (Sébastien Gross) diff --git a/lib/cdist/__init__.py b/lib/cdist/__init__.py index 38a5f602..02df2f2f 100644 --- a/lib/cdist/__init__.py +++ b/lib/cdist/__init__.py @@ -29,7 +29,7 @@ try: 'cd "%s" && git describe' % here, stderr=devnull, shell=True).decode('utf-8') except: - VERSION = "2.0.12" + VERSION = "2.0.13" BANNER = """ .. . .x+=:. s