[__directory] Add --state exists and --state pre-exists

This commit is contained in:
Dennis Camera 2020-01-15 19:00:30 +01:00
parent 3258fc98e1
commit 2cf44c66d4
2 changed files with 27 additions and 5 deletions

View file

@ -3,6 +3,7 @@
# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org) # 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
# 2013 Steven Armstrong (steven-cdist armstrong.cc) # 2013 Steven Armstrong (steven-cdist armstrong.cc)
# 2014 Daniel Heule (hda at sfs.biz) # 2014 Daniel Heule (hda at sfs.biz)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -21,8 +22,8 @@
# #
destination="/$__object_id" destination="/$__object_id"
state_should="$(cat "$__object/parameter/state")" state_should=$(cat "$__object/parameter/state")
type="$(cat "$__object/explorer/type")" type=$(cat "$__object/explorer/type")
stat_file="$__object/explorer/stat" stat_file="$__object/explorer/stat"
# variable to keep track if we have to set directory attributes # variable to keep track if we have to set directory attributes
@ -72,7 +73,7 @@ set_mode() {
} }
case "$state_should" in case "$state_should" in
present) present|exists)
if [ "$type" != "directory" ]; then if [ "$type" != "directory" ]; then
set_attributes=1 set_attributes=1
if [ "$type" != "none" ]; then if [ "$type" != "none" ]; then
@ -83,6 +84,10 @@ case "$state_should" in
fi fi
echo "mkdir $mkdiropt '$destination'" echo "mkdir $mkdiropt '$destination'"
echo "create" >> "$__messages_out" echo "create" >> "$__messages_out"
elif [ "$state_should" = 'exists' ]; then
# The type is directory and --state exists. We are done and do not
# check or set the attributes.
exit 0
fi fi
# Note: Mode - needs to happen last as a chown/chgrp can alter mode by # Note: Mode - needs to happen last as a chown/chgrp can alter mode by
@ -103,6 +108,12 @@ case "$state_should" in
fi fi
done done
;; ;;
pre-exists)
if [ "$type" != "directory" ]; then
echo "Directory \"$destination\" does not exist" >&2
exit 1
fi
;;
absent) absent)
if [ "$type" = "directory" ]; then if [ "$type" = "directory" ]; then
echo "rm -rf '$destination'" echo "rm -rf '$destination'"

View file

@ -19,7 +19,18 @@ None.
OPTIONAL PARAMETERS OPTIONAL PARAMETERS
------------------- -------------------
state state
'present' or 'absent', defaults to 'present' 'present', 'absent', 'exists' or 'pre-exists', defaults to 'present' where:
present
the directory exists and the given attributes are set.
absent
the directory does not exist.
exists
the directory exists, but its attributes are not altered if it already
existed.
pre-exists
check that the directory exists and is indeed a directory, but do not
create or modify it.
group group
Group to chgrp to. Group to chgrp to.
@ -36,7 +47,7 @@ BOOLEAN PARAMETERS
parents parents
Whether to create parents as well (mkdir -p behaviour). Whether to create parents as well (mkdir -p behaviour).
Warning: all intermediate directory permissions default Warning: all intermediate directory permissions default
to whatever mkdir -p does. to whatever mkdir -p does.
Usually this means root:root, 0700. Usually this means root:root, 0700.