cdist/cdist/conf/type/__key_value/gencode-remote

140 lines
3.8 KiB
Plaintext
Raw Normal View History

#!/bin/sh
#
# 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.
#
# 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/>.
#
#set -x
key="$__object_id"
[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")"
state_should="$(cat "$__object/parameter/state")"
file="$(cat "$__object/parameter/file")"
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|present)
case "$state_is" in
absent|wrongvalue|present)
;;
*)
echo "Unknown explorer state: $state_is" >&2
exit 1
esac
;;
*)
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
)"
export comment="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
$(cat "$__object/parameter/comment_line")
__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"]
comment=ENVIRON["comment"]
keydel=key delimiter
line=keydel value
inserted=0
ll=""
llpopulated=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(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