forked from ungleich-public/cdist
196 lines
5.3 KiB
Bash
Executable File
196 lines
5.3 KiB
Bash
Executable File
#!/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
|
|
|
|
|
|
state_should="$(cat "$__object/parameter/state")"
|
|
state_is="$(cat "$__object/explorer/state")"
|
|
|
|
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 ...
|
|
case "$state_should" in
|
|
absent)
|
|
case "$state_is" in
|
|
absent|nosuchfile)
|
|
# nothing to do
|
|
;;
|
|
wrongformat|wrongvalue|present)
|
|
echo removed >> "$__messages_out"
|
|
;;
|
|
*)
|
|
echo "Unknown explorer state: $state_is" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
;;
|
|
present)
|
|
case "$state_is" in
|
|
nosuchfile)
|
|
echo created >> "$__messages_out"
|
|
;;
|
|
absent)
|
|
echo inserted >> "$__messages_out"
|
|
;;
|
|
wrongformated|wrongvalue)
|
|
echo changed >> "$__messages_out"
|
|
;;
|
|
present)
|
|
# nothing to do
|
|
;;
|
|
*)
|
|
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
|
|
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
|