add --state parameter to __directory type which allows removal of directories

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-07-21 23:06:13 +02:00
parent 76f07511a9
commit 37612ff102
4 changed files with 80 additions and 32 deletions

View file

@ -25,39 +25,55 @@
# #
destination="/$__object_id" destination="/$__object_id"
state_should="$(cat "$__object/parameter/state")"
# Include parent directories? case "$state_should" in
if [ -f "$__object/parameter/parents" ]; then present)
# Include parent directories?
if [ -f "$__object/parameter/parents" ]; then
parents="$(cat "$__object/parameter/parents")" parents="$(cat "$__object/parameter/parents")"
if [ yes = "$parents" ]; then if [ yes = "$parents" ]; then
mkdiropt="-p" mkdiropt="-p"
else else
mkdiropt="" mkdiropt=""
fi fi
fi fi
if [ -f "$__object/parameter/recursive" ]; then if [ -f "$__object/parameter/recursive" ]; then
if [ yes = "$(cat "$__object/parameter/recursive")" ]; then if [ yes = "$(cat "$__object/parameter/recursive")" ]; then
recursive="-R" recursive="-R"
fi fi
fi fi
# Only create if not already existent # Only create if not already existent
if [ no = "$(cat "$__object/explorer/exists")" ]; then if [ no = "$(cat "$__object/explorer/exists")" ]; then
echo mkdir $mkdiropt \"$destination\" echo mkdir $mkdiropt \"$destination\"
fi fi
# Mode settings # Mode settings
if [ -f "$__object/parameter/mode" ]; then if [ -f "$__object/parameter/mode" ]; then
echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\" echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\"
fi fi
# Group # Group
if [ -f "$__object/parameter/group" ]; then if [ -f "$__object/parameter/group" ]; then
echo chgrp $recursive \"$(cat "$__object/parameter/group")\" \"$destination\" echo chgrp $recursive \"$(cat "$__object/parameter/group")\" \"$destination\"
fi fi
# Owner # Owner
if [ -f "$__object/parameter/owner" ]; then if [ -f "$__object/parameter/owner" ]; then
echo chown $recursive \"$(cat "$__object/parameter/owner")\" \"$destination\" echo chown $recursive \"$(cat "$__object/parameter/owner")\" \"$destination\"
fi fi
;;
absent)
# Only delete if it exists
if [ yes = "$(cat "$__object/explorer/exists")" ]; then
echo rm -r \"$destination\"
fi
;;
*)
echo "Unknown state" >&2
exit 1
;;
esac

View file

@ -5,12 +5,12 @@ Nico Schottelius <nico-cdist--@--schottelius.org>
NAME NAME
---- ----
cdist-type__directory - Create a directory cdist-type__directory - Manage a directory
DESCRIPTION DESCRIPTION
----------- -----------
This cdist type allows you to create directories on the target. This cdist type allows you to create or remove directories on the target.
REQUIRED PARAMETERS REQUIRED PARAMETERS
@ -20,6 +20,9 @@ None.
OPTIONAL PARAMETERS OPTIONAL PARAMETERS
------------------- -------------------
state::
'present' or 'absent', defaults to 'present'
group:: group::
Group to chgrp to. Group to chgrp to.
@ -45,6 +48,9 @@ EXAMPLES
# A silly example # A silly example
__directory /tmp/foobar __directory /tmp/foobar
# Remove a directory
__directory /tmp/foobar --ensure absent
# Ensure /etc exists correctly # Ensure /etc exists correctly
__directory /etc --owner root --group root --mode 0755 __directory /etc --owner root --group root --mode 0755

25
conf/type/__directory/manifest Executable file
View file

@ -0,0 +1,25 @@
#!/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 <http://www.gnu.org/licenses/>.
#
if [ -f "$__object/parameter/state" ]; then
state="$(cat "$__object/parameter/state")"
else
echo "present" > "$__object/parameter/state"
fi

View file

@ -1,3 +1,4 @@
state
group group
mode mode
owner owner