|
|
|
@ -2,6 +2,7 @@ |
|
|
|
|
# |
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc) |
|
|
|
|
# 2012 Nico Schottelius (nico-cdist at schottelius.org) |
|
|
|
|
# 2014 Daniel Heule (hda at sfs.biz) |
|
|
|
|
# |
|
|
|
|
# This file is part of cdist. |
|
|
|
|
# |
|
|
|
@ -18,6 +19,7 @@ |
|
|
|
|
# You should have received a copy of the GNU General Public License |
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>. |
|
|
|
|
# |
|
|
|
|
#set -x |
|
|
|
|
|
|
|
|
|
key="$__object_id" |
|
|
|
|
[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")" |
|
|
|
@ -25,39 +27,15 @@ key="$__object_id" |
|
|
|
|
state_should="$(cat "$__object/parameter/state")" |
|
|
|
|
|
|
|
|
|
file="$(cat "$__object/parameter/file")" |
|
|
|
|
delimiter="$(cat "$__object/parameter/delimiter")" |
|
|
|
|
# escape double quotes, as that is what we use ourself below |
|
|
|
|
value_escaped="$(cat "$__object/parameter/value" | sed -e "s/\([\"]\)/\\\\\1/g")" |
|
|
|
|
state_is="$(cat "$__object/explorer/state")" |
|
|
|
|
|
|
|
|
|
[ "$state_is" = "$state_should" ] && exit 0 |
|
|
|
|
|
|
|
|
|
# here we check only if the states are valid, let awk do the work ... |
|
|
|
|
case "$state_should" in |
|
|
|
|
absent) |
|
|
|
|
# remove lines starting with key |
|
|
|
|
cat << DONE |
|
|
|
|
tmpfile=\$(mktemp ${file}.cdist.XXXXXXXXXX) |
|
|
|
|
# preserve ownership and permissions by copying existing file over tmpfile |
|
|
|
|
cp -p "$file" "\$tmpfile" |
|
|
|
|
sed '/^$key\($delimiter\+\)/d' "$file" > "\$tmpfile" |
|
|
|
|
mv -f "\$tmpfile" "$file" |
|
|
|
|
DONE |
|
|
|
|
;; |
|
|
|
|
present) |
|
|
|
|
absent|present) |
|
|
|
|
case "$state_is" in |
|
|
|
|
absent) |
|
|
|
|
# add new key and value |
|
|
|
|
printf 'echo "%s%s%s" >> "%s"' "$key" "$delimiter" "$value_escaped" "$file" |
|
|
|
|
;; |
|
|
|
|
wrongvalue) |
|
|
|
|
# change exisiting value |
|
|
|
|
cat << DONE |
|
|
|
|
tmpfile=\$(mktemp ${file}.cdist.XXXXXXXXXX) |
|
|
|
|
# preserve ownership and permissions by copying existing file over tmpfile |
|
|
|
|
cp -p "$file" "\$tmpfile" |
|
|
|
|
sed "s|^$key\($delimiter\+\).*|$key\\1$value_escaped|" "$file" > "\$tmpfile" |
|
|
|
|
mv -f "\$tmpfile" "$file" |
|
|
|
|
DONE |
|
|
|
|
absent|wrongvalue|present) |
|
|
|
|
;; |
|
|
|
|
*) |
|
|
|
|
echo "Unknown explorer state: $state_is" >&2 |
|
|
|
@ -68,3 +46,64 @@ DONE |
|
|
|
|
echo "Unknown state: $state_should" >&2 |
|
|
|
|
exit 1 |
|
|
|
|
esac |
|
|
|
|
|
|
|
|
|
cat <<CDIST_HEREDOC_END_HERE_MARKER |
|
|
|
|
export state="\$(cat <<"CDIST_INPUT_END_HERE_MARKER" |
|
|
|
|
$state_should |
|
|
|
|
CDIST_INPUT_END_HERE_MARKER |
|
|
|
|
)" |
|
|
|
|
export key="\$(cat <<"CDIST_INPUT_END_HERE_MARKER" |
|
|
|
|
$key |
|
|
|
|
CDIST_INPUT_END_HERE_MARKER |
|
|
|
|
)" |
|
|
|
|
export delimiter="\$(cat <<"CDIST_INPUT_END_HERE_MARKER" |
|
|
|
|
$(cat "$__object/parameter/delimiter") |
|
|
|
|
CDIST_INPUT_END_HERE_MARKER |
|
|
|
|
)" |
|
|
|
|
export value="\$(cat <<"CDIST_INPUT_END_HERE_MARKER" |
|
|
|
|
$(cat "$__object/parameter/value") |
|
|
|
|
CDIST_INPUT_END_HERE_MARKER |
|
|
|
|
)" |
|
|
|
|
|
|
|
|
|
tmpfile=\$(mktemp "${file}.cdist.XXXXXXXXXX") |
|
|
|
|
# preserve ownership and permissions by copying existing file over tmpfile |
|
|
|
|
cp -p "$file" "\$tmpfile" |
|
|
|
|
awk -f - "$file" >"\$tmpfile" <<"AWK_EOF" |
|
|
|
|
BEGIN { |
|
|
|
|
# import variables in a secure way .. |
|
|
|
|
state=ENVIRON["state"] |
|
|
|
|
key=ENVIRON["key"] |
|
|
|
|
delimiter=ENVIRON["delimiter"] |
|
|
|
|
value=ENVIRON["value"] |
|
|
|
|
keydel=key delimiter |
|
|
|
|
line=keydel value |
|
|
|
|
inserted=0 |
|
|
|
|
} |
|
|
|
|
# enter the main loop |
|
|
|
|
{ |
|
|
|
|
# I dont use regex, this is by design, so we can match against every value without special meanings of chars ... |
|
|
|
|
i = index(\$0,keydel) |
|
|
|
|
if(i == 1) { |
|
|
|
|
if(state == "absent") { |
|
|
|
|
# if absent, simple yump over this line |
|
|
|
|
next |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
inserted=1 |
|
|
|
|
# state is present, so insert correct line here |
|
|
|
|
print line |
|
|
|
|
next |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
print \$0 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
END { |
|
|
|
|
if(inserted == 0 && state == "present" ) { |
|
|
|
|
print line |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
AWK_EOF |
|
|
|
|
mv -f "\$tmpfile" "$file" |
|
|
|
|
CDIST_HEREDOC_END_HERE_MARKER |
|
|
|
|