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-09-28 15:29:41 +00:00
|
|
|
drop_awk_comments() { sed '/^[[:blank:]]*#.*$/d;/^$/d' "$@"; }
|
|
|
|
|
2020-09-28 15:37:41 +00:00
|
|
|
aliases_file=$(cat "${__object:?}/explorer/aliases_file")
|
2020-09-28 15:29:41 +00:00
|
|
|
|
|
|
|
test -n "${aliases_file}" || {
|
|
|
|
echo 'Could not determine aliases file path.' >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-28 15:37:41 +00:00
|
|
|
state_should=$(cat "${__object:?}/parameter/state")
|
2020-05-30 15:24:26 +00:00
|
|
|
|
|
|
|
case $state_should
|
|
|
|
in
|
|
|
|
(present)
|
2020-09-28 15:37:41 +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-10-01 10:55:38 +00:00
|
|
|
test -s "${__object:?}/parameter/alias" || {
|
|
|
|
printf 'The --alias parameter is required if --state present.\n' >&2
|
|
|
|
printf 'Use --state absent to remove all aliases.\n' >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2020-09-28 15:37:41 +00:00
|
|
|
if test -s "${__object:?}/explorer/aliases"
|
2020-06-09 19:39:28 +00:00
|
|
|
then
|
2020-09-28 15:37:41 +00:00
|
|
|
echo "update aliases" >>"${__messages_out:?}"
|
2020-06-09 19:39:28 +00:00
|
|
|
else
|
2020-09-28 15:37:41 +00:00
|
|
|
echo "add aliases" >>"${__messages_out:?}"
|
2020-06-09 19:39:28 +00:00
|
|
|
fi
|
2020-05-30 15:24:26 +00:00
|
|
|
;;
|
|
|
|
(absent)
|
|
|
|
# nothing to do if no aliases found.
|
2020-09-28 15:37:41 +00:00
|
|
|
test -s "${__object:?}/explorer/aliases" || exit 0
|
2020-05-30 15:24:26 +00:00
|
|
|
|
2020-09-28 15:37:41 +00:00
|
|
|
echo "delete aliases" >>"${__messages_out:?}"
|
2020-05-30 15:24:26 +00:00
|
|
|
;;
|
|
|
|
(*)
|
2020-09-28 15:29:41 +00:00
|
|
|
printf 'Invalid --state: %s.\n' "${state_should}" >&2
|
2020-06-11 13:38:34 +00:00
|
|
|
printf 'Acceptable values are: present, absent.\n' >&2
|
2020-05-30 15:24:26 +00:00
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
|
2020-09-28 15:29:41 +00:00
|
|
|
cat <<EOF
|
|
|
|
test -f '${aliases_file}' || touch '${aliases_file}'
|
2020-06-09 19:39:28 +00:00
|
|
|
|
2020-09-28 15:37:41 +00:00
|
|
|
awk '$(drop_awk_comments "${__type:?}/files/update_aliases.awk")' <'${aliases_file}' >'${aliases_file}.tmp' \
|
2020-09-28 15:29:41 +00:00
|
|
|
|| {
|
|
|
|
rm -f '${aliases_file}.tmp'
|
2020-05-30 15:24:26 +00:00
|
|
|
echo 'Generating new aliases file failed!' >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2020-09-28 15:29:41 +00:00
|
|
|
if ! cmp -s '${aliases_file}' '${aliases_file}.tmp'
|
2020-05-30 15:24:26 +00:00
|
|
|
then
|
2020-09-28 15:29:41 +00:00
|
|
|
# aliases file was modified, replace:
|
|
|
|
cat '${aliases_file}.tmp' >'${aliases_file}'
|
2020-06-22 12:02:42 +00:00
|
|
|
|
2020-09-28 15:29:41 +00:00
|
|
|
# then, run newaliases if present ("missing" on Alpine Linux because of typo)
|
2020-06-22 12:02:42 +00:00
|
|
|
command -v newaliases >/dev/null 2>&1 && newaliases || true
|
2020-05-30 15:24:26 +00:00
|
|
|
fi
|
2020-09-28 15:29:41 +00:00
|
|
|
rm -f '${aliases_file}.tmp'
|
2020-05-30 15:24:26 +00:00
|
|
|
EOF
|