Merge branch 'directory-exists' into 'master'

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

See merge request ungleich-public/cdist!832
This commit is contained in:
poljakowski 2020-02-26 16:33:37 +01:00
commit dd6b07d924
2 changed files with 41 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,26 @@ case "$state_should" in
fi fi
done done
;; ;;
pre-exists)
case $type in
directory)
# all good
exit 0
;;
none)
printf 'Directory "%s" does not exist\n' "$destination" >&2
exit 1
;;
file|symlink)
printf 'File "%s" exists and is a %s, but should be a directory\n' "$destination" "$type" >&2
exit 1
;;
*)
printf 'File or directory "%s" is in an unknown state\n' "$destination" >&2
exit 1
;;
esac
;;
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.