[type/__mail_alias] Externalise AWK update script to separate file

This commit is contained in:
Dennis Camera 2020-09-28 17:29:41 +02:00
parent 6ae0808560
commit b48b48e404
2 changed files with 119 additions and 94 deletions

View file

@ -0,0 +1,98 @@
#!/usr/bin/awk -f
#
# 2020 Dennis Camera (dennis.camera@ssrq-sds-fds.ch)
#
# 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/>.
#
function getvalue(path) {
# Reads the first line of the file located at path and returns it.
getline < path
close(path)
return $0
}
function sepafter(f, def, _) {
# finds the separator between field $f and $(f+1)
_ = substr($0, length($f)+1, index(substr($0, length($f)+1), $(f+1))-1)
return _ ? _ : def
}
function write_aliases() {
if (aliases_written) return
# print aliases line
printf "%s%s", ENVIRON["__object_id"], sepafter(1, ": ")
while ((getline < aliases_should_file) > 0) {
if (aliases_written) printf ", "
printf "%s", $0
aliases_written = 1
}
printf "\n"
close(aliases_should_file)
}
BEGIN {
FS = ":[ \t]*"
parameter_dir = ENVIRON["__object"] "/parameter/"
mode = (getvalue(parameter_dir "state") != "absent")
aliases_should_file = (parameter_dir "/alias")
}
/^[ \t]*\#/ {
# comment line (leave alone)
select = 0; cont = 0 # comments terminate alias lists and continuations
print
next
}
{
# is this line a continuation line?
# (the prev. line ended in a backslash or the line starts with whitespace)
is_cont = /^[ \t]/ || cont
# detect if the line is a line to be continued (ends with a backslash)
cont = ($0 ~ /\\$/)
# if it is, we drop the backslash from the line.
if (cont) sub(/[ \t]*\\$/, "", $0)
}
is_cont {
# we ignore the line as it has been rewritten previously or is not
# interesting
next
}
$1 == ENVIRON["__object_id"] {
# "target" user -> rewrite aliases list
select = 1
if (mode) write_aliases()
next
}
{
# other user
select = 0
print
}
END {
# if the last line was an alias, the separator will be reused (looks better)
if (mode && !aliases_written)
write_aliases()
}

View file

@ -18,6 +18,16 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
# #
drop_awk_comments() { sed '/^[[:blank:]]*#.*$/d;/^$/d' "$@"; }
aliases_file=$(cat "${__object}/explorer/aliases_file")
test -n "${aliases_file}" || {
echo 'Could not determine aliases file path.' >&2
exit 1
}
state_should=$(cat "${__object}/parameter/state") state_should=$(cat "${__object}/parameter/state")
case $state_should case $state_should
@ -35,119 +45,36 @@ in
else else
echo "add aliases" >>"$__messages_out" echo "add aliases" >>"$__messages_out"
fi fi
mode=1
;; ;;
(absent) (absent)
# nothing to do if no aliases found. # nothing to do if no aliases found.
test -s "${__object}/explorer/aliases" || exit 0 test -s "${__object}/explorer/aliases" || exit 0
echo "delete aliases" >>"$__messages_out" echo "delete aliases" >>"$__messages_out"
mode=0
;; ;;
(*) (*)
printf 'Invalid --state: %s.\n' "$state_should" >&2 printf 'Invalid --state: %s.\n' "${state_should}" >&2
printf 'Acceptable values are: present, absent.\n' >&2 printf 'Acceptable values are: present, absent.\n' >&2
exit 1 exit 1
esac esac
aliases_file=$(cat "${__object}/explorer/aliases_file") cat <<EOF
test -f '${aliases_file}' || touch '${aliases_file}'
test -n "${aliases_file}" || { awk '$(drop_awk_comments "${__type}/files/update_aliases.awk")' <'${aliases_file}' >'${aliases_file}.tmp' \
echo 'Could not determine aliases file path.' >&2 || {
exit 1 rm -f '${aliases_file}.tmp'
}
# "export" variables to remote
printf 'mode=%u\n' "${mode}"
printf "aliases_file='%s'\n" "${aliases_file}"
cat <<'EOF'
test -f "${aliases_file}" || touch "${aliases_file}"
awk -F ':[ \t]*' -v mode=$mode '
function sepafter(f, def, _) {
# finds the separator between field $f and $(f+1)
_ = substr($0, length($f)+1, index(substr($0, length($f)+1), $(f+1))-1)
return _ ? _ : def
}
function write_aliases() {
if (aliases_written) return
# print aliases line
printf "%s%s", ENVIRON["__object_id"], sepafter(1, ": ")
while ((getline < aliases_should_file) > 0) {
if (aliases_written) printf ", "
printf "%s", $0
aliases_written = 1
}
printf "\n"
close(aliases_should_file)
}
BEGIN {
aliases_should_file = (ENVIRON["__object"] "/parameter/alias")
}
/^[ \t]*\#/ {
# comment line (leave alone)
select = 0; cont = 0 # comments terminate alias lists and continuations
print
next
}
{
# is this line a continuation line?
# (the prev. line ended in a backslash or the line starts with whitespace)
is_cont = /^[ \t]/ || cont
# detect if the line is a line to be continued (ends with a backslash)
cont = ($0 ~ /\\$/)
# if it is, we drop the backslash from the line.
if (cont) sub(/[ \t]*\\$/, "", $0)
}
is_cont {
# we ignore the line as it has been rewritten previously or is not
# interesting
next
}
$1 == ENVIRON["__object_id"] {
# "target" user -> rewrite aliases list
select = 1
if (mode) write_aliases()
next
}
{
# other user
select = 0
print
}
END {
# if the last line was an alias, the separator will be reused (looks better)
if (mode && !aliases_written)
write_aliases()
}
' <"${aliases_file}" >"${aliases_file}.tmp" || {
rm -f "${aliases_file}.tmp"
echo 'Generating new aliases file failed!' >&2 echo 'Generating new aliases file failed!' >&2
exit 1 exit 1
} }
if ! cmp -s "${aliases_file}" "${aliases_file}.tmp" if ! cmp -s '${aliases_file}' '${aliases_file}.tmp'
then then
# aliases file was modified, replace and run `newaliases`. # aliases file was modified, replace:
mv "${aliases_file}.tmp" "${aliases_file}" cat '${aliases_file}.tmp' >'${aliases_file}'
# run newaliases if present # then, run newaliases if present ("missing" on Alpine Linux because of typo)
command -v newaliases >/dev/null 2>&1 && newaliases || true command -v newaliases >/dev/null 2>&1 && newaliases || true
else
# no modifications were made, delete the temp file.
rm "${aliases_file}.tmp"
fi fi
rm -f '${aliases_file}.tmp'
EOF EOF