From bf1b995908823fb7e29401d169c4e76a73b953f9 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 1 Nov 2011 22:20:46 +0100 Subject: [PATCH 1/6] implement __lt__ and __eq__ to support sorting a list of types Signed-off-by: Steven Armstrong --- lib/cdist/core/type.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/cdist/core/type.py b/lib/cdist/core/type.py index e9cb375a..20365b8d 100644 --- a/lib/cdist/core/type.py +++ b/lib/cdist/core/type.py @@ -86,6 +86,12 @@ class Type(object): def __repr__(self): return '' % self.name + def __eq__(self, other): + return isinstance(other, self.__class__) and self.name == other.name + + def __lt__(self, other): + return isinstance(other, self.__class__) and self.name < other.name + @property def is_singleton(self): """Check whether a type is a singleton.""" From 70ee0f1681b2495bc429d3ff9af3b947408e4b70 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 1 Nov 2011 22:46:46 +0100 Subject: [PATCH 2/6] implement __lt__ to support sorting a list of objects Signed-off-by: Steven Armstrong --- lib/cdist/core/object.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/cdist/core/object.py b/lib/cdist/core/object.py index 5157d86a..29dc16e9 100644 --- a/lib/cdist/core/object.py +++ b/lib/cdist/core/object.py @@ -115,6 +115,10 @@ class Object(object): """define equality as 'attributes are the same'""" return self.__dict__ == other.__dict__ + def __lt__(self, other): + return isinstance(other, self.__class__) and self.name < other.name + + # FIXME: still needed? @property def explorer_path(self): From afba5e92804e537b372b8548aca0d619aa66488b Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 2 Nov 2011 07:52:08 +0100 Subject: [PATCH 3/6] add utilitly method to split an object name into its type name and object_id Signed-off-by: Steven Armstrong --- lib/cdist/core/object.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/cdist/core/object.py b/lib/cdist/core/object.py index 29dc16e9..b62417b7 100644 --- a/lib/cdist/core/object.py +++ b/lib/cdist/core/object.py @@ -60,9 +60,7 @@ class Object(object): def list_objects(cls, object_base_path, type_base_path): """Return a list of object instances""" for object_name in cls.list_object_names(object_base_path): - type_name = object_name.split(os.sep)[0] - # FIXME: allow object without object_id? e.g. for singleton - object_id = os.sep.join(object_name.split(os.sep)[1:]) + type_name, object_id = cls.split_name(object_name) yield cls(cdist.core.Type(type_base_path, type_name), object_base_path, object_id=object_id) @classmethod @@ -77,20 +75,17 @@ class Object(object): if DOT_CDIST in dirs: yield os.path.relpath(path, object_base_path) - def object_from_name(self, object_name): - """Convenience method for creating an object instance from an object name. + @staticmethod + def split_name(object_name): + """split_name('__type_name/the/object_id') -> ('__type_name', 'the/object_id') - Mainly intended to create objects when resolving requirements. - - e.g: - .object_from_name('__other/object') -> + Split the given object name into it's type and object_id parts. """ - type_path = self.type.base_path - object_path = self.base_path type_name = object_name.split(os.sep)[0] + # FIXME: allow object without object_id? e.g. for singleton object_id = os.sep.join(object_name.split(os.sep)[1:]) - return self.__class__(self.type.__class__(type_path, type_name), object_path, object_id=object_id) + return type_name, object_id def __init__(self, cdist_type, base_path, object_id=None): if object_id: @@ -118,6 +113,19 @@ class Object(object): def __lt__(self, other): return isinstance(other, self.__class__) and self.name < other.name + def object_from_name(self, object_name): + """Convenience method for creating an object instance from an object name. + + Mainly intended to create objects when resolving requirements. + + e.g: + .object_from_name('__other/object') -> + + """ + type_path = self.type.base_path + object_path = self.base_path + type_name, object_id = self.split_name(object_name) + return self.__class__(self.type.__class__(type_path, type_name), object_path, object_id=object_id) # FIXME: still needed? @property From 792ddf0a5bf2b26139373586200550f442994dbe Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 2 Nov 2011 09:13:23 +0100 Subject: [PATCH 4/6] add utility method to join object name from type and object_id Signed-off-by: Steven Armstrong --- lib/cdist/core/object.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/cdist/core/object.py b/lib/cdist/core/object.py index b62417b7..778bebe8 100644 --- a/lib/cdist/core/object.py +++ b/lib/cdist/core/object.py @@ -87,6 +87,15 @@ class Object(object): object_id = os.sep.join(object_name.split(os.sep)[1:]) return type_name, object_id + @staticmethod + def join_name(type_name, object_id): + """join_name('__type_name', 'the/object_id') -> __type_name/the/object_id' + + Join the given type_name and object_id into an object name. + + """ + return os.path.join(type_name, object_id) + def __init__(self, cdist_type, base_path, object_id=None): if object_id: if object_id.startswith('/'): @@ -96,7 +105,7 @@ class Object(object): self.type = cdist_type # instance of Type self.base_path = base_path self.object_id = object_id - self.name = os.path.join(self.type.name, self.object_id) + self.name = self.join_name(self.type.name, self.object_id) self.path = os.path.join(self.type.path, self.object_id, DOT_CDIST) self.absolute_path = os.path.join(self.base_path, self.path) self.code_local_path = os.path.join(self.path, "code-local") From e36e538f819b4991291a24455ece844180497c50 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 2 Nov 2011 09:15:44 +0100 Subject: [PATCH 5/6] sort lists before testing for equality Signed-off-by: Steven Armstrong --- lib/cdist/test/explorer/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cdist/test/explorer/__init__.py b/lib/cdist/test/explorer/__init__.py index 1f7a2e35..cafe34fc 100644 --- a/lib/cdist/test/explorer/__init__.py +++ b/lib/cdist/test/explorer/__init__.py @@ -69,7 +69,7 @@ class ExplorerClassTestCase(test.CdistTestCase): self.explorer.transfer_global_explorers() source = self.local.global_explorer_path destination = self.remote.global_explorer_path - self.assertEqual(os.listdir(source), os.listdir(destination)) + self.assertEqual(sorted(os.listdir(source)), sorted(os.listdir(destination))) def test_run_global_explorer(self): self.explorer.transfer_global_explorers() @@ -79,7 +79,7 @@ class ExplorerClassTestCase(test.CdistTestCase): def test_run_global_explorers(self): out_path = self.mkdtemp() self.explorer.run_global_explorers(out_path) - self.assertEqual(os.listdir(out_path), ['foobar', 'global']) + self.assertEqual(sorted(os.listdir(out_path)), sorted(['foobar', 'global'])) shutil.rmtree(out_path) def test_list_type_explorer_names(self): @@ -116,7 +116,7 @@ class ExplorerClassTestCase(test.CdistTestCase): self.explorer.transfer_object_parameters(cdist_object) source = os.path.join(self.local.object_path, cdist_object.parameter_path) destination = os.path.join(self.remote.object_path, cdist_object.parameter_path) - self.assertEqual(os.listdir(source), os.listdir(destination)) + self.assertEqual(sorted(os.listdir(source)), sorted(os.listdir(destination))) def test_run_type_explorer(self): cdist_type = core.Type(self.local.type_path, '__test_type') From e195eb46c495d42562607bfcf128563412732312 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 2 Nov 2011 16:30:35 +0100 Subject: [PATCH 6/6] new type __cron: installs and manages cron jobs Signed-off-by: Steven Armstrong --- conf/type/__cron/explorer/entry | 39 +++++++++++++++++ conf/type/__cron/gencode-remote | 63 +++++++++++++++++++++++++++ conf/type/__cron/man.text | 61 ++++++++++++++++++++++++++ conf/type/__cron/manifest | 66 +++++++++++++++++++++++++++++ conf/type/__cron/parameter/optional | 6 +++ conf/type/__cron/parameter/required | 2 + 6 files changed, 237 insertions(+) create mode 100755 conf/type/__cron/explorer/entry create mode 100755 conf/type/__cron/gencode-remote create mode 100644 conf/type/__cron/man.text create mode 100755 conf/type/__cron/manifest create mode 100644 conf/type/__cron/parameter/optional create mode 100644 conf/type/__cron/parameter/required diff --git a/conf/type/__cron/explorer/entry b/conf/type/__cron/explorer/entry new file mode 100755 index 00000000..362c96fe --- /dev/null +++ b/conf/type/__cron/explorer/entry @@ -0,0 +1,39 @@ +#!/bin/sh +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# 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 . +# + +name="$__object_id" +user="$(cat "$__object/parameter/user")" + +prefix="#cdist:__cron/$name" +suffix="#/cdist:__cron/$name" + +crontab -u $user -l | awk -v prefix="$prefix" -v suffix="$suffix" ' +{ + if (index($0,prefix)) { + triggered=1 + } + if (triggered) { + if (index($0,suffix)) { + triggered=0 + } + print + } +} +' diff --git a/conf/type/__cron/gencode-remote b/conf/type/__cron/gencode-remote new file mode 100755 index 00000000..90b53451 --- /dev/null +++ b/conf/type/__cron/gencode-remote @@ -0,0 +1,63 @@ +#!/bin/sh +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# 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 . +# + +user="$(cat "$__object/parameter/user")" +state_should="$(cat "$__object/parameter/state")" +state_is=$(cmp --quiet "$__object/parameter/entry" "$__object/explorer/entry" \ + && echo present \ + || echo absent +) + +if [ "$state_is" != "$state_should" ]; then + case "$state_should" in + present) + cat << DONE +tmp=\$(mktemp) +crontab -u $user -l > \$tmp +cat >> \$tmp << EOC +$(cat "$__object/parameter/entry")" +EOC +crontab -u $user \$tmp +rm \$tmp +DONE + ;; + absent) + # defined in type manifest + prefix="$(cat "$__object/parameter/prefix")" + suffix="$(cat "$__object/parameter/suffix")" + cat << DONE +crontab -u $user -l | awk -v prefix="$prefix" -v suffix="$suffix" ' +{ + if (index(\$0,prefix)) { + triggered=1 + } + if (triggered) { + if (index(\$0,suffix)) { + triggered=0 + } + } else { + print + } +} +' | crontab -u $user - +DONE + ;; + esac +fi diff --git a/conf/type/__cron/man.text b/conf/type/__cron/man.text new file mode 100644 index 00000000..c4852b7f --- /dev/null +++ b/conf/type/__cron/man.text @@ -0,0 +1,61 @@ +cdist-type__cron(7) +=================== +Steven Armstrong + + +NAME +---- +cdist-type__cron - installs and manages cron jobs + + +DESCRIPTION +----------- +This cdist type allows you to manage entries in a users crontab. + + +REQUIRED PARAMETERS +------------------- +user:: + The user who's crontab is edited +command:: + The command to run. + + +OPTIONAL PARAMETERS +------------------- +state:: + Either present or absent. Defaults to present. +minute:: + See crontab(5). Defaults to * +hour:: + See crontab(5). Defaults to * +day_of_month:: + See crontab(5). Defaults to * +month:: + See crontab(5). Defaults to * +day_of_week:: + See crontab(5). Defaults to * + + +EXAMPLES +-------- + +-------------------------------------------------------------------------------- +# add cronjob +__cron some-id --user root --command "/path/to/script" + +# remove cronjob +__cron some-id --user root --command "/path/to/script" --state absent +-------------------------------------------------------------------------------- + + +SEE ALSO +-------- +- cdist-type(7) +- crontab(5) + + +COPYING +------- +Copyright \(C) 2011 Steven Armstrong. Free use of this software is +granted under the terms of the GNU General Public License version 3 (GPLv3). diff --git a/conf/type/__cron/manifest b/conf/type/__cron/manifest new file mode 100755 index 00000000..01c4358c --- /dev/null +++ b/conf/type/__cron/manifest @@ -0,0 +1,66 @@ +#!/bin/sh +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# 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 . +# + +name="$__object_id" +user="$(cat "$__object/parameter/user")" +command="$(cat "$__object/parameter/command")" + +# set defaults +if [ ! -f "$__object/parameter/state" ]; then + echo "present" > "$__object/parameter/state" +fi +if [ -f "$__object/parameter/minute" ]; then + minute="$(cat "$__object/parameter/minute")" +else + minute="*" + echo "$minute" > "$__object/parameter/minute" +fi +if [ -f "$__object/parameter/hour" ]; then + hour="$(cat "$__object/parameter/hour")" +else + hour="*" + echo "$hour" > "$__object/parameter/hour" +fi +if [ -f "$__object/parameter/day_of_month" ]; then + day_of_month="$(cat "$__object/parameter/day_of_month")" +else + day_of_month="*" + echo "$day_of_month" > "$__object/parameter/day_of_month" +fi +if [ -f "$__object/parameter/month" ]; then + month="$(cat "$__object/parameter/month")" +else + month="*" + echo "$month" > "$__object/parameter/month" +fi +if [ -f "$__object/parameter/day_of_week" ]; then + day_of_week="$(cat "$__object/parameter/day_of_week")" +else + day_of_week="*" + echo "$day_of_week" > "$__object/parameter/day_of_week" +fi + +# NOTE: if changed, also change in explorers +prefix="#cdist:__cron/$name" +suffix="#/cdist:__cron/$name" +echo "$prefix" | tee "$__object/parameter/prefix" > "$__object/parameter/entry" +echo "$minute $hour $day_of_month $month $day_of_week $command" >> "$__object/parameter/entry" +echo "$suffix" | tee "$__object/parameter/suffix" >> "$__object/parameter/entry" + diff --git a/conf/type/__cron/parameter/optional b/conf/type/__cron/parameter/optional new file mode 100644 index 00000000..1a4aae3d --- /dev/null +++ b/conf/type/__cron/parameter/optional @@ -0,0 +1,6 @@ +state +minute +hour +day_of_month +month +day_of_week diff --git a/conf/type/__cron/parameter/required b/conf/type/__cron/parameter/required new file mode 100644 index 00000000..711a59ab --- /dev/null +++ b/conf/type/__cron/parameter/required @@ -0,0 +1,2 @@ +user +command