rewrite of __key_value, now support nerarly every value

This commit is contained in:
Daniel Heule 2014-02-24 21:42:45 +01:00
parent 95f38d9d44
commit 205e256ef6
2 changed files with 108 additions and 55 deletions

View File

@ -1,6 +1,7 @@
#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2014 Daniel Heule (hda at sfs.biz)
#
# This file is part of cdist.
#
@ -18,36 +19,49 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
key="$(cat "$__object/parameter/key" 2>/dev/null \
export key="$(cat "$__object/parameter/key" 2>/dev/null \
|| echo "$__object_id")"
state="$(cat "$__object/parameter/state" 2>/dev/null \
export state="$(cat "$__object/parameter/state" 2>/dev/null \
|| echo "present")"
file="$(cat "$__object/parameter/file")"
delimiter="$(cat "$__object/parameter/delimiter")"
value="$(cat "$__object/parameter/value" 2>/dev/null \
export delimiter="$(cat "$__object/parameter/delimiter")"
export value="$(cat "$__object/parameter/value" 2>/dev/null \
|| echo "__CDIST_NOTSET__")"
case "$state" in
absent)
if grep -q -E "^$key$delimiter+" "$file"; then
# if the key exists, with whatever value, we will have to remove it
# so report it as present
echo present
else
# key does not exist
echo absent
fi
;;
present)
if grep -q -E "^$key$delimiter+$value$" "$file"; then
# key exists and value is same
echo present
elif grep -q -E "^$key$delimiter+" "$file"; then
# key exists, but value is empty or different
echo wrongvalue
else
# key does not exist
echo absent
fi
;;
esac
awk -f - "$file" <<"AWK_EOF"
BEGIN {
state=ENVIRON["state"]
key=ENVIRON["key"]
delimiter=ENVIRON["delimiter"]
value=ENVIRON["value"]
keydel=key delimiter
line=keydel value
found=0
}
# enter the main loop
{
i = index($0,keydel)
if(i == 1) {
found=1
if(state == "absent") {
# on state absent, only the ocurance is relevant, so exit here
print "present"
exit
}
# Key with separator found
if($0 == line) {
# exact match found, so state is present
print "present"
}
else {
# not value is wrong ...
print "wrongvalue"
}
exit
}
}
END {
if(found == 0)
print "absent"
}
AWK_EOF

View File

@ -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