forked from ungleich-public/cdist
Add pre-exists state to __file (#641)
This allows checking whether a file exists, but erroring out instead of creating the file if it does not (or is not a regular file).
This commit is contained in:
parent
2b85e4f14b
commit
a9a359d542
3 changed files with 24 additions and 1 deletions
|
@ -25,6 +25,20 @@ type="$(cat "$__object/explorer/type")"
|
||||||
|
|
||||||
[ "$state_should" = "exists" -a "$type" = "file" ] && exit 0 # nothing to do
|
[ "$state_should" = "exists" -a "$type" = "file" ] && exit 0 # nothing to do
|
||||||
|
|
||||||
|
if [ "$state_should" = "pre-exists" ]; then
|
||||||
|
if [ -f "$__object/parameter/source" ]; then
|
||||||
|
echo "--source cannot be used with --state pre-exists"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$type" = "file" ]; then
|
||||||
|
exit 0 # nothing to do
|
||||||
|
else
|
||||||
|
echo "File \"$destination\" does not exist"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
upload_file=
|
upload_file=
|
||||||
create_file=
|
create_file=
|
||||||
if [ "$state_should" = "present" -o "$state_should" = "exists" ]; then
|
if [ "$state_should" = "present" -o "$state_should" = "exists" ]; then
|
||||||
|
|
|
@ -59,7 +59,7 @@ set_mode() {
|
||||||
|
|
||||||
set_attributes=
|
set_attributes=
|
||||||
case "$state_should" in
|
case "$state_should" in
|
||||||
present|exists)
|
present|exists|pre-exists)
|
||||||
# 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
|
||||||
# clearing S_ISUID and S_ISGID bits (see chown(2))
|
# clearing S_ISUID and S_ISGID bits (see chown(2))
|
||||||
for attribute in group owner mode; do
|
for attribute in group owner mode; do
|
||||||
|
|
|
@ -23,6 +23,10 @@ symlink
|
||||||
directory
|
directory
|
||||||
replace it with the source file
|
replace it with the source file
|
||||||
|
|
||||||
|
One exception is that when state is pre-exists, an error is raised if
|
||||||
|
the file would have been created otherwise (e.g. it is not present or
|
||||||
|
not a regular file).
|
||||||
|
|
||||||
In any case, make sure that the file attributes are as specified.
|
In any case, make sure that the file attributes are as specified.
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,6 +45,9 @@ state
|
||||||
the file does not exist
|
the file does not exist
|
||||||
exists
|
exists
|
||||||
the file from source but only if it doesn't already exist
|
the file from source but only if it doesn't already exist
|
||||||
|
pre-exists
|
||||||
|
check that the file exists and is a regular file, but do not
|
||||||
|
create or modify it
|
||||||
|
|
||||||
group
|
group
|
||||||
Group to chgrp to.
|
Group to chgrp to.
|
||||||
|
@ -93,6 +100,8 @@ EXAMPLES
|
||||||
__file /home/frodo/.bashrc --source "/etc/skel/.bashrc" \
|
__file /home/frodo/.bashrc --source "/etc/skel/.bashrc" \
|
||||||
--state exists \
|
--state exists \
|
||||||
--owner frodo --mode 0600
|
--owner frodo --mode 0600
|
||||||
|
# Check that the file is present, show an error when it is not
|
||||||
|
__file /etc/somefile --state pre-exists
|
||||||
# Take file content from stdin
|
# Take file content from stdin
|
||||||
__file /tmp/whatever --owner root --group root --mode 644 --source - << DONE
|
__file /tmp/whatever --owner root --group root --mode 644 --source - << DONE
|
||||||
Here goes the content for /tmp/whatever
|
Here goes the content for /tmp/whatever
|
||||||
|
|
Loading…
Reference in a new issue