2020-05-29 15:33:52 +00:00
|
|
|
#!/bin/sh -e
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
2020-05-30 15:24:26 +00:00
|
|
|
state_should=$(cat "${__object}/parameter/state")
|
|
|
|
|
|
|
|
case $state_should
|
|
|
|
in
|
|
|
|
(present)
|
2020-05-30 20:07:20 +00:00
|
|
|
if cmp -s "${__object}/explorer/aliases" "${__object}/parameter/alias"
|
2020-05-30 15:24:26 +00:00
|
|
|
then
|
|
|
|
# all good!
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-06-09 19:39:28 +00:00
|
|
|
if test -s "${__object}/explorer/aliases"
|
|
|
|
then
|
|
|
|
echo "update aliases" >>"$__messages_out"
|
|
|
|
else
|
|
|
|
echo "add aliases" >>"$__messages_out"
|
|
|
|
fi
|
|
|
|
|
2020-05-30 15:24:26 +00:00
|
|
|
mode=1
|
|
|
|
;;
|
|
|
|
(absent)
|
|
|
|
# nothing to do if no aliases found.
|
|
|
|
test -s "${__object}/explorer/aliases" || exit 0
|
|
|
|
|
|
|
|
echo "delete aliases" >>"$__messages_out"
|
2020-06-09 19:39:28 +00:00
|
|
|
|
2020-05-30 15:24:26 +00:00
|
|
|
mode=0
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
printf 'Invalid --state given: %s\n' "$state_should" >&2
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
|
|
|
|
aliases_file=$(cat "${__object}/explorer/aliases_file")
|
|
|
|
|
2020-06-09 19:39:28 +00:00
|
|
|
test -n "${aliases_file}" || {
|
2020-05-30 15:24:26 +00:00
|
|
|
echo 'Could not determine aliases file path.' >&2
|
|
|
|
exit 1
|
2020-06-09 19:39:28 +00:00
|
|
|
}
|
2020-05-30 15:24:26 +00:00
|
|
|
|
|
|
|
# "export" variables to remote
|
|
|
|
printf 'mode=%u\n' "${mode}"
|
|
|
|
printf "aliases_file='%s'\n" "${aliases_file}"
|
|
|
|
|
|
|
|
cat <<'EOF'
|
2020-05-30 20:07:20 +00:00
|
|
|
test -f "${aliases_file}" || touch "${aliases_file}"
|
2020-06-09 19:39:28 +00:00
|
|
|
|
|
|
|
awk -F ':[ \t]*' -v mode=$mode '
|
|
|
|
function sepafter(f, default, _) {
|
|
|
|
# finds the separator between field $f and $(f+1)
|
2020-05-30 15:24:26 +00:00
|
|
|
_ = substr($0, length($f) + 1, index(substr($0, length($f)+1), $(f+1)) - 1)
|
2020-06-09 19:39:28 +00:00
|
|
|
return _ ? _ : default
|
2020-05-30 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function write_aliases() {
|
|
|
|
if (aliases_written) return
|
|
|
|
|
2020-06-09 19:39:28 +00:00
|
|
|
# print aliases line
|
2020-05-30 15:24:26 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
/^#/ {
|
2020-06-09 19:39:28 +00:00
|
|
|
# comment line (leave alone)
|
|
|
|
select = 0; cont = 0 # comments terminate alias lists and continuations
|
2020-05-30 15:24:26 +00:00
|
|
|
print
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
2020-06-09 19:39:28 +00:00
|
|
|
/^[ \t]/ || cont {
|
|
|
|
# continuation line (either the previous line ended in a backslash or the
|
|
|
|
# line starts with whitespace)
|
|
|
|
|
|
|
|
# if in the alias list of the "target" user, we drop the line as it has been
|
|
|
|
# rewritten previously
|
|
|
|
if (select) next
|
|
|
|
}
|
|
|
|
|
2020-05-30 15:24:26 +00:00
|
|
|
{
|
2020-06-09 19:39:28 +00:00
|
|
|
# detect if the line is a line to be continued (ends with a backslash)
|
2020-05-30 15:24:26 +00:00
|
|
|
cont = ($0 ~ /\\$/)
|
2020-06-09 19:39:28 +00:00
|
|
|
# if it is, we drop the backslash from the line.
|
2020-05-30 20:07:20 +00:00
|
|
|
if (cont) sub(/[ \t]*\\$/, "", $0)
|
2020-05-30 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$1 == ENVIRON["__object_id"] {
|
2020-06-09 19:39:28 +00:00
|
|
|
# "target" user -> rewrite aliases list
|
|
|
|
select = 1
|
2020-05-30 15:24:26 +00:00
|
|
|
if (mode) write_aliases()
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-06-09 19:39:28 +00:00
|
|
|
# other user
|
|
|
|
select = 0
|
2020-05-30 15:24:26 +00:00
|
|
|
print
|
|
|
|
}
|
|
|
|
|
|
|
|
END {
|
2020-06-09 19:39:28 +00:00
|
|
|
# if the last line was an alias, the separator will be reused (looks better)
|
|
|
|
if (mode && !aliases_written)
|
|
|
|
write_aliases()
|
2020-05-30 15:24:26 +00:00
|
|
|
}
|
|
|
|
' <"${aliases_file}" >"${aliases_file}.tmp" || {
|
|
|
|
echo 'Generating new aliases file failed!' >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:07:20 +00:00
|
|
|
if ! cmp -s "${aliases_file}" "${aliases_file}.tmp"
|
2020-05-30 15:24:26 +00:00
|
|
|
then
|
2020-06-09 19:39:28 +00:00
|
|
|
# aliases file was modified, replace and run `newaliases`
|
2020-05-30 15:24:26 +00:00
|
|
|
mv "${aliases_file}.tmp" "${aliases_file}"
|
|
|
|
newaliases
|
|
|
|
else
|
2020-06-09 19:39:28 +00:00
|
|
|
# no modifications were made, delete the temp file.
|
2020-05-30 15:24:26 +00:00
|
|
|
rm "${aliases_file}.tmp"
|
|
|
|
fi
|
|
|
|
EOF
|