cdist/cdist/conf/type/__uci_section/manifest

147 lines
4.1 KiB
Bash
Executable File

#!/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/>.
#
# shellcheck source=files/functions.sh
. "${__type:?}/files/functions.sh"
## Check section name and error if invalid!
case ${__object_id:?}
in
(*.*)
uci_validate_name "${__object_id%%.*}" || {
printf 'Invalid package name: %s\n' "${__object_id%%.*}" >&2
exit 1
}
uci_validate_name "${__object_id#*.}" || {
printf 'Invalid section name: %s\n' "${__object_id#*.}" >&2
exit 1
}
;;
(*)
uci_validate_name "${__object_id:?}" || {
printf 'Invalid section name: %s\n' "${__object_id:?}" >&2
exit 1
}
;;
esac
section=$(cat "${__object:?}/explorer/match")
state_should=$(cat "${__object:?}/parameter/state")
transaction_name=$(cat "${__object:?}/parameter/transaction")
case $state_should
in
(present)
test -f "${__object:?}/parameter/type" || {
echo 'Parameter --type is required.' >&2
exit 1
}
type_is=$(cat "${__object:?}/explorer/type")
type_should=$(cat "${__object:?}/parameter/type")
if test -n "${type_is}"
then
if test "${type_is}" != "${type_should##*.}"
then
# Check if section type matches (section exists and --type provided)
printf 'Section type "%s" does not match --type "%s".\n' \
"${type_is}" "${type_should}" >&2
exit 1
fi
sect_type=${type_is}
else
sect_type=${type_should##*.}
fi
if test -z "${section}"
then
# No section exists and --match was used.
# So we generate a new section identifier from $__object_id.
case ${__object_id:?}
in
(*.*) section=${__object_id:?} ;;
(*) section="${type_should%%.*}.${__object_id:?}" ;;
esac
fi
# Check options for syntax errors
validate_options "${__object:?}/parameter/list" "${__object:?}/parameter/object" \
| print_errors 'Found erroneous options in arguments:'
# Collect option names
if test -f "${__object:?}/parameter/list"
then
listnames_should=$(
sed -e 's/=.*$//' "${__object:?}/parameter/list" | sort -u)
fi
if test -f "${__object:?}/parameter/option"
then
optnames_should=$(sed -e 's/=.*$//' "${__object:?}/parameter/option" | sort)
echo "${optnames_should}" \
| uniq -d \
| print_errors \
'Found duplicate --options:' \
"$(printf '\nUse --list for lists, instead.')"
fi
# Make sure the section itself is present
__uci "${section}" --state present --transaction "${transaction_name}" \
--value "${sect_type}"
export require=__uci/"${section}"
# Delete options/lists not in "should"
sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \
| while read -r optname
do
grep_line "${optname}" "${listnames_should}" "${optnames_should}" || {
__uci "${section}.${optname}" --state absent --transaction "${transaction_name}"
} </dev/null
done
# Set "should" options
prefix_lines option "${optnames_should}" list "${listnames_should}" \
| while read -r _type _optname
do
test -n "${_type}${_optname}" || continue # ignore empty lines
grep "^${_optname}=" <"${__object:?}/parameter/${_type}" \
| sed -e 's/^.*=//' \
| unquote_lines \
| append_values \
__uci "${section}.${_optname}" --state present \
--type "${_type}" --transaction "${transaction_name}"
done
;;
(absent)
# if explorer found no section there is nothing to delete
test -n "${section}" || exit 0
__uci "${section}" --state absent --transaction "${transaction_name}"
;;
(*)
printf 'Invalid --state: %s\n' "${state_should}" >&2
exit 1
;;
esac