rewrite of __key_value, now support nerarly every value
This commit is contained in:
parent
95f38d9d44
commit
205e256ef6
2 changed files with 108 additions and 55 deletions
|
@ -1,6 +1,7 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
#
|
#
|
||||||
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
||||||
|
# 2014 Daniel Heule (hda at sfs.biz)
|
||||||
#
|
#
|
||||||
# This file is part of cdist.
|
# This file is part of cdist.
|
||||||
#
|
#
|
||||||
|
@ -18,36 +19,49 @@
|
||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
# 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")"
|
|| echo "$__object_id")"
|
||||||
state="$(cat "$__object/parameter/state" 2>/dev/null \
|
export state="$(cat "$__object/parameter/state" 2>/dev/null \
|
||||||
|| echo "present")"
|
|| echo "present")"
|
||||||
file="$(cat "$__object/parameter/file")"
|
file="$(cat "$__object/parameter/file")"
|
||||||
delimiter="$(cat "$__object/parameter/delimiter")"
|
export delimiter="$(cat "$__object/parameter/delimiter")"
|
||||||
value="$(cat "$__object/parameter/value" 2>/dev/null \
|
export value="$(cat "$__object/parameter/value" 2>/dev/null \
|
||||||
|| echo "__CDIST_NOTSET__")"
|
|| echo "__CDIST_NOTSET__")"
|
||||||
|
|
||||||
case "$state" in
|
awk -f - "$file" <<"AWK_EOF"
|
||||||
absent)
|
BEGIN {
|
||||||
if grep -q -E "^$key$delimiter+" "$file"; then
|
state=ENVIRON["state"]
|
||||||
# if the key exists, with whatever value, we will have to remove it
|
key=ENVIRON["key"]
|
||||||
# so report it as present
|
delimiter=ENVIRON["delimiter"]
|
||||||
echo present
|
value=ENVIRON["value"]
|
||||||
else
|
keydel=key delimiter
|
||||||
# key does not exist
|
line=keydel value
|
||||||
echo absent
|
found=0
|
||||||
fi
|
}
|
||||||
;;
|
# enter the main loop
|
||||||
present)
|
{
|
||||||
if grep -q -E "^$key$delimiter+$value$" "$file"; then
|
i = index($0,keydel)
|
||||||
# key exists and value is same
|
if(i == 1) {
|
||||||
echo present
|
found=1
|
||||||
elif grep -q -E "^$key$delimiter+" "$file"; then
|
if(state == "absent") {
|
||||||
# key exists, but value is empty or different
|
# on state absent, only the ocurance is relevant, so exit here
|
||||||
echo wrongvalue
|
print "present"
|
||||||
else
|
exit
|
||||||
# key does not exist
|
}
|
||||||
echo absent
|
# Key with separator found
|
||||||
fi
|
if($0 == line) {
|
||||||
;;
|
# exact match found, so state is present
|
||||||
esac
|
print "present"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
# not value is wrong ...
|
||||||
|
print "wrongvalue"
|
||||||
|
}
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
END {
|
||||||
|
if(found == 0)
|
||||||
|
print "absent"
|
||||||
|
}
|
||||||
|
AWK_EOF
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#
|
#
|
||||||
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
||||||
# 2012 Nico Schottelius (nico-cdist at schottelius.org)
|
# 2012 Nico Schottelius (nico-cdist at schottelius.org)
|
||||||
|
# 2014 Daniel Heule (hda at sfs.biz)
|
||||||
#
|
#
|
||||||
# This file is part of cdist.
|
# This file is part of cdist.
|
||||||
#
|
#
|
||||||
|
@ -18,6 +19,7 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
#set -x
|
||||||
|
|
||||||
key="$__object_id"
|
key="$__object_id"
|
||||||
[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")"
|
[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")"
|
||||||
|
@ -25,39 +27,15 @@ key="$__object_id"
|
||||||
state_should="$(cat "$__object/parameter/state")"
|
state_should="$(cat "$__object/parameter/state")"
|
||||||
|
|
||||||
file="$(cat "$__object/parameter/file")"
|
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="$(cat "$__object/explorer/state")"
|
||||||
|
|
||||||
[ "$state_is" = "$state_should" ] && exit 0
|
[ "$state_is" = "$state_should" ] && exit 0
|
||||||
|
|
||||||
|
# here we check only if the states are valid, let awk do the work ...
|
||||||
case "$state_should" in
|
case "$state_should" in
|
||||||
absent)
|
absent|present)
|
||||||
# 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)
|
|
||||||
case "$state_is" in
|
case "$state_is" in
|
||||||
absent)
|
absent|wrongvalue|present)
|
||||||
# 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
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Unknown explorer state: $state_is" >&2
|
echo "Unknown explorer state: $state_is" >&2
|
||||||
|
@ -68,3 +46,64 @@ DONE
|
||||||
echo "Unknown state: $state_should" >&2
|
echo "Unknown state: $state_should" >&2
|
||||||
exit 1
|
exit 1
|
||||||
esac
|
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
|
||||||
|
|
Loading…
Reference in a new issue