From b7fb973fa5de1e2fc2bdc8f1fdb46572b6416d10 Mon Sep 17 00:00:00 2001 From: Daniel Heule Date: Fri, 21 Mar 2014 09:35:47 +0100 Subject: [PATCH] bugfix for whitespace stripping, its no more simple --- cdist/conf/type/__key_value/explorer/state | 4 +- .../type/__key_value/files/remote_script.sh | 104 +++++++++++++++ cdist/conf/type/__key_value/gencode-remote | 121 +----------------- 3 files changed, 107 insertions(+), 122 deletions(-) create mode 100644 cdist/conf/type/__key_value/files/remote_script.sh diff --git a/cdist/conf/type/__key_value/explorer/state b/cdist/conf/type/__key_value/explorer/state index 6dfb2031..b990733d 100755 --- a/cdist/conf/type/__key_value/explorer/state +++ b/cdist/conf/type/__key_value/explorer/state @@ -21,8 +21,8 @@ export key="$(cat "$__object/parameter/key" 2>/dev/null \ || echo "$__object_id")" -export state="$(cat "$__object/parameter/state" 2>/dev/null \ - || echo "present")" +export state="$(cat "$__object/parameter/state")" + file="$(cat "$__object/parameter/file")" if [ ! -f "$file" ]; then diff --git a/cdist/conf/type/__key_value/files/remote_script.sh b/cdist/conf/type/__key_value/files/remote_script.sh new file mode 100644 index 00000000..2ed43366 --- /dev/null +++ b/cdist/conf/type/__key_value/files/remote_script.sh @@ -0,0 +1,104 @@ +#!/bin/sh + +export key="$(cat "$__object/parameter/key" 2>/dev/null \ + || echo "$__object_id")" +export state="$(cat "$__object/parameter/state")" + +file="$(cat "$__object/parameter/file")" + +export delimiter="$(cat "$__object/parameter/delimiter")" +export value="$(cat "$__object/parameter/value" 2>/dev/null \ + || echo "__CDIST_NOTSET__")" +if [ -f "$__object/parameter/exact_delimiter" ]; then + export exact_delimiter=1 +else + export exact_delimiter=0 +fi + +tmpfile=$(mktemp "${file}.cdist.XXXXXXXXXX") +# preserve ownership and permissions by copying existing file over tmpfile +if [ -f "$file" ]; then + cp -p "$file" "$tmpfile" +else + touch "$file" +fi +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"] + comment=ENVIRON["comment"] + exact_delimiter=ENVIRON["exact_delimiter"] + inserted=0 + ll="" + llpopulated=0 + line=key delimiter value +} +# 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,key) + if(i == 1) { + delval = substr($0,length(key)+1) + delpos = index(delval,delimiter) + if(delpos > 1) { + spaces = substr(delval,1,delpos-1) + sub(/[ \t]*/,"",spaces) + if( length(spaces) > 0 ) { + # if there are not only spaces between key and delimiter, + # continue since we we are on the wrong line + if(llpopulated == 1) { + print ll + } + ll=$0 + llpopulated=1 + next + } + } + if(state == "absent") { + if(ll == comment) { + # if comment is present, clear llpopulated flag + llpopulated=0 + } + # if absent, simple yump over this line + next + } + else { + # if comment is present and not present in last line + if (llpopulated == 1) { + print ll + if( comment != "" && ll != comment) { + print comment + } + llpopulated=0 + } + inserted=1 + # state is present, so insert correct line here + print line + ll=line + next + } + } + else { + if(llpopulated == 1) { + print ll + } + ll=$0 + llpopulated=1 + } +} +END { + if(llpopulated == 1) { + print ll + } + if(inserted == 0 && state == "present" ) { + if(comment != "" && ll != comment){ + print comment + } + print line + } +} +AWK_EOF +mv -f "$tmpfile" "$file" diff --git a/cdist/conf/type/__key_value/gencode-remote b/cdist/conf/type/__key_value/gencode-remote index f1744ac1..e6815cb6 100755 --- a/cdist/conf/type/__key_value/gencode-remote +++ b/cdist/conf/type/__key_value/gencode-remote @@ -28,15 +28,6 @@ if [ "$state_is" = "$state_should" ]; then exit 0 fi -file="$(cat "$__object/parameter/file")" -key="$__object_id" -[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")" -if [ -f "$__object/parameter/exact_delimiter" ]; then - export exact_delimiter=1 -else - export exact_delimiter=0 -fi - # here we check only if the states are valid, # emmit messages and # let awk do the work ... @@ -81,114 +72,4 @@ case "$state_should" in ;; esac -cat <<__CDIST_HEREDOC_END_HERE_MARKER -IFS='\n' read -r state <<'__CDIST_INPUT_END_HERE_MARKER' -$state_should -__CDIST_INPUT_END_HERE_MARKER -export state -IFS='\n' read -r key <<'__CDIST_INPUT_END_HERE_MARKER' -$key -__CDIST_INPUT_END_HERE_MARKER -export key -IFS='\n' read -r value <<'__CDIST_INPUT_END_HERE_MARKER' -$(cat "$__object/parameter/value") -__CDIST_INPUT_END_HERE_MARKER -export value -IFS='\n' read -r delimiter <<'__CDIST_INPUT_END_HERE_MARKER' -$(cat "$__object/parameter/delimiter") -__CDIST_INPUT_END_HERE_MARKER -export delimiter -IFS='\n' read -r comment <<'__CDIST_INPUT_END_HERE_MARKER' -$(cat "$__object/parameter/comment") -__CDIST_INPUT_END_HERE_MARKER -export comment -export exact_delimiter="$exact_delimiter" - -tmpfile=\$(mktemp "${file}.cdist.XXXXXXXXXX") -# preserve ownership and permissions by copying existing file over tmpfile -if [ -f "$file" ]; then - cp -p "$file" "\$tmpfile" -else - touch "$file" -fi -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"] - comment=ENVIRON["comment"] - exact_delimiter=ENVIRON["exact_delimiter"] - inserted=0 - ll="" - llpopulated=0 - line=key delimiter value -} -# 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,key) - if(i == 1) { - delval = substr(\$0,length(key)+1) - delpos = index(delval,delimiter) - if(delpos > 1) { - spaces = substr(delval,1,delpos-1) - sub(/[ \t]*/,"",spaces) - if( length(spaces) > 0 ) { - # if there are not only spaces between key and delimiter, - # continue since we we are on the wrong line - if(llpopulated == 1) { - print ll - } - ll=\$0 - llpopulated=1 - next - } - } - if(state == "absent") { - if(ll == comment) { - # if comment is present, clear llpopulated flag - llpopulated=0 - } - # if absent, simple yump over this line - next - } - else { - # if comment is present and not present in last line - if (llpopulated == 1) { - print ll - if( comment != "" && ll != comment) { - print comment - } - llpopulated=0 - } - inserted=1 - # state is present, so insert correct line here - print line - ll=line - next - } - } - else { - if(llpopulated == 1) { - print ll - } - ll=\$0 - llpopulated=1 - } -} -END { - if(llpopulated == 1) { - print ll - } - if(inserted == 0 && state == "present" ) { - if(comment != "" && ll != comment){ - print comment - } - print line - } -} -AWK_EOF -mv -f "\$tmpfile" "$file" -__CDIST_HEREDOC_END_HERE_MARKER +cat "$__type/files/remote_script.sh"