From 205e256ef646d272bd8854eaed3d1761e8af3126 Mon Sep 17 00:00:00 2001 From: Daniel Heule Date: Mon, 24 Feb 2014 21:42:45 +0100 Subject: [PATCH] rewrite of __key_value, now support nerarly every value --- cdist/conf/type/__key_value/explorer/state | 70 +++++++++------- cdist/conf/type/__key_value/gencode-remote | 93 +++++++++++++++------- 2 files changed, 108 insertions(+), 55 deletions(-) diff --git a/cdist/conf/type/__key_value/explorer/state b/cdist/conf/type/__key_value/explorer/state index 94a5ea7f..071bc862 100755 --- a/cdist/conf/type/__key_value/explorer/state +++ b/cdist/conf/type/__key_value/explorer/state @@ -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 . # -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 diff --git a/cdist/conf/type/__key_value/gencode-remote b/cdist/conf/type/__key_value/gencode-remote index b79d9688..0e963b7c 100755 --- a/cdist/conf/type/__key_value/gencode-remote +++ b/cdist/conf/type/__key_value/gencode-remote @@ -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 . # +#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 <"\$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