2011-01-18 20:17:20 +00:00
|
|
|
#!/bin/sh
|
2011-01-18 20:21:30 +00:00
|
|
|
#
|
2012-11-07 16:52:05 +00:00
|
|
|
# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
|
2013-09-12 12:02:33 +00:00
|
|
|
# 2013 Steven Armstrong (steven-cdist armstrong.cc)
|
2011-01-18 20:21:30 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
2011-02-16 18:42:10 +00:00
|
|
|
|
2011-03-15 20:18:15 +00:00
|
|
|
destination="/$__object_id"
|
2013-09-12 12:02:33 +00:00
|
|
|
state_should="$(cat "$__object/parameter/state")"
|
|
|
|
type="$(cat "$__object/explorer/type")"
|
|
|
|
stat_file="$__object/explorer/stat"
|
|
|
|
|
|
|
|
|
|
|
|
get_current_value() {
|
|
|
|
if [ -s "$stat_file" ]; then
|
|
|
|
_name="$1"
|
|
|
|
_value="$2"
|
|
|
|
case "$_value" in
|
|
|
|
[0-9]*)
|
|
|
|
_index=2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
_index=3
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
awk '/'"$_name"':/ { print $'$_index' }' "$stat_file"
|
|
|
|
unset _name _value _index
|
|
|
|
fi
|
|
|
|
}
|
2011-02-25 01:13:11 +00:00
|
|
|
|
2013-09-12 12:02:33 +00:00
|
|
|
set_group() {
|
|
|
|
echo chgrp \"$1\" \"$destination\"
|
|
|
|
}
|
|
|
|
|
|
|
|
set_owner() {
|
|
|
|
echo chown \"$1\" \"$destination\"
|
|
|
|
}
|
|
|
|
|
|
|
|
set_mode() {
|
|
|
|
echo chmod \"$1\" \"$destination\"
|
|
|
|
}
|
|
|
|
|
|
|
|
set_attributes=
|
2012-01-09 14:37:16 +00:00
|
|
|
case "$state_should" in
|
2012-11-07 15:28:21 +00:00
|
|
|
present|exists)
|
2013-09-12 12:02:33 +00:00
|
|
|
if [ -f "$__object/files/destination_upload" ]; then
|
|
|
|
# we uploaded a file, move it into place and set all attributes
|
|
|
|
destination_upload="$(cat "$__object/files/destination_upload")"
|
|
|
|
set_attributes=1
|
|
|
|
if [ "$type" = "directory" ]; then
|
|
|
|
# our destination is currently a directory, move it out of the way,
|
|
|
|
cat << DONE
|
|
|
|
destination_old="\$(mktemp "${destination}.cdist.XXXXXXXXXX")"
|
2013-09-18 08:44:52 +00:00
|
|
|
mv "$destination" "\$destination_old"
|
|
|
|
DONE
|
|
|
|
fi
|
|
|
|
|
|
|
|
# move our upload into place
|
|
|
|
cat << DONE
|
2013-09-12 12:02:33 +00:00
|
|
|
mv "$destination_upload" "$destination"
|
|
|
|
DONE
|
2013-09-18 08:44:52 +00:00
|
|
|
|
|
|
|
if [ "$type" = "directory" ]; then
|
|
|
|
# delete the legacy directory
|
|
|
|
cat << DONE
|
|
|
|
rm -rf "\$destination_old"
|
|
|
|
DONE
|
2012-01-09 14:37:16 +00:00
|
|
|
fi
|
2011-07-18 11:34:39 +00:00
|
|
|
fi
|
2012-01-09 15:30:52 +00:00
|
|
|
|
2013-09-12 12:02:33 +00:00
|
|
|
# Note: Mode - needs to happen last as a chown/chgrp can alter mode by
|
|
|
|
# clearing S_ISUID and S_ISGID bits (see chown(2))
|
|
|
|
for attribute in group owner mode; do
|
|
|
|
if [ -f "$__object/parameter/$attribute" ]; then
|
|
|
|
value_should="$(cat "$__object/parameter/$attribute")"
|
|
|
|
value_is="$(get_current_value "$attribute" "$value_should")"
|
|
|
|
if [ "$set_attributes" -o "$value_should" != "$value_is" ]; then
|
|
|
|
"set_$attribute" "$value_should"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2012-01-09 14:37:16 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
absent)
|
2013-09-12 12:02:33 +00:00
|
|
|
# FIXME: only delete if it's a file? or no matter what?
|
|
|
|
if [ "$type" = "file" ]; then
|
2012-01-09 14:37:16 +00:00
|
|
|
echo rm -f \"$destination\"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
echo "Unknown state: $state_should" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|