forked from ungleich-public/cdist
[type/__uci_section] Add type
This commit is contained in:
parent
3ef638a611
commit
4da3968118
9 changed files with 326 additions and 0 deletions
68
cdist/conf/type/__uci_section/explorer/match
Normal file
68
cdist/conf/type/__uci_section/explorer/match
Normal file
|
@ -0,0 +1,68 @@
|
|||
#!/bin/sh -e
|
||||
#
|
||||
# 2020 Dennis Camera (dennis.camera at 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/>.
|
||||
#
|
||||
# This explorer the "prefix" of the section matching --match.
|
||||
|
||||
squote_values() {
|
||||
sed -e '/=".*"$/{s/="/='\''/;s/"$/'\''/}' \
|
||||
-e "/='.*'$/"'!{s/=/='\''/;s/$/'\''/}'
|
||||
}
|
||||
|
||||
RS=$(printf '\036')
|
||||
|
||||
if ! test -e "${__object:?}/parameter/match"
|
||||
then
|
||||
if echo "${__object_id:?}" | grep -qvE '^[^.]+\.[^.]+$'
|
||||
then
|
||||
echo 'Section identifiers are a package and section name separated by a "." (period).' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# If no --match is given, we take the __object_id as the section identifier.
|
||||
echo "${__object_id:?}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
test -s "${__object:?}/parameter/match" \
|
||||
&& test -s "${__object:?}/parameter/type" \
|
||||
|| {
|
||||
echo 'Parameters --match and --type must be used together.' >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Find by match
|
||||
match=$(cat "${__object:?}/parameter/match")
|
||||
sect_type_filter=$(cat "${__object:?}/parameter/type")
|
||||
|
||||
package_filter=${sect_type_filter%%.*}
|
||||
section_filter=${sect_type_filter##*.}
|
||||
regex="^${package_filter}\.@${section_filter}\[[0-9]\{1,\}\]\.${match%%=*}="
|
||||
|
||||
matched_sections=$(
|
||||
uci -s -N -d "${RS}" show "${package_filter}" 2>/dev/null \
|
||||
| grep -e "${regex}" \
|
||||
| sed -e 's/\.[^.]*=.*$//')
|
||||
|
||||
if test "$(echo "${matched_sections}" | wc -l)" -gt 1
|
||||
then
|
||||
printf 'Found multiple matching sections:\n%s\n' "${matched_sections}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "${matched_sections}"
|
28
cdist/conf/type/__uci_section/explorer/options
Normal file
28
cdist/conf/type/__uci_section/explorer/options
Normal file
|
@ -0,0 +1,28 @@
|
|||
#!/bin/sh -e
|
||||
#
|
||||
# 2020 Dennis Camera (dennis.camera at 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/>.
|
||||
#
|
||||
# This explorer retrieves the current options of the configuration section.
|
||||
|
||||
RS=$(printf '\036')
|
||||
|
||||
section=$("${__type_explorer:?}/match")
|
||||
test -n "${section}" || exit 0
|
||||
|
||||
uci -s -N -d "${RS}" show "${section}" 2>/dev/null \
|
||||
| grep -v -e "^${section}=" || true
|
25
cdist/conf/type/__uci_section/explorer/type
Normal file
25
cdist/conf/type/__uci_section/explorer/type
Normal file
|
@ -0,0 +1,25 @@
|
|||
#!/bin/sh -e
|
||||
#
|
||||
# 2020 Dennis Camera (dennis.camera at 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/>.
|
||||
#
|
||||
# This explorer retrieves the current section type.
|
||||
|
||||
section=$("${__type_explorer:?}/match")
|
||||
test -n "${section}" || exit 0
|
||||
|
||||
uci -s -N get "${section}" 2>/dev/null || true
|
76
cdist/conf/type/__uci_section/man.rst
Normal file
76
cdist/conf/type/__uci_section/man.rst
Normal file
|
@ -0,0 +1,76 @@
|
|||
cdist-type__uci_section(7)
|
||||
==========================
|
||||
|
||||
NAME
|
||||
----
|
||||
cdist-type__uci_section - Manage configuration sections in OpenWrt's
|
||||
Unified Configuration Interface (UCI)
|
||||
|
||||
|
||||
DESCRIPTION
|
||||
-----------
|
||||
This cdist type can be used to replace whole configuration sections in OpenWrt's
|
||||
UCI system.
|
||||
It can be thought of as syntactic sugar for `cdist-type__uci`\ (7), as this type
|
||||
will generate the required `__uci` objects to make the section contain exactly
|
||||
the options specified via ``--option``.
|
||||
|
||||
Since many default UCI sections are unnamed, this type allows to find the
|
||||
matching section by one of its options using the ``--match`` parameter.
|
||||
|
||||
|
||||
REQUIRED PARAMETERS
|
||||
-------------------
|
||||
None.
|
||||
|
||||
|
||||
OPTIONAL PARAMETERS
|
||||
-------------------
|
||||
match
|
||||
Allows to find a section to "replace" through one of its parameters.
|
||||
The value to this parameter is a ``<option>=<string>`` string.
|
||||
option
|
||||
An option that should be present in the section.
|
||||
This parameter can be used multiple times to specify multiple options.
|
||||
The value to this parameter is a ``<option>=<string>`` string.
|
||||
|
||||
Lists can be expressed by repeatedly using the same key.
|
||||
state
|
||||
`present` or `absent`, defaults to `present`.
|
||||
transaction
|
||||
The name of the transaction this option belongs to.
|
||||
The value will be forwarded to `cdist-type__uci`\ (7).
|
||||
type
|
||||
The type of the section in the format: ``<config>.<section-type>``
|
||||
|
||||
|
||||
BOOLEAN PARAMETERS
|
||||
------------------
|
||||
None.
|
||||
|
||||
|
||||
EXAMPLES
|
||||
--------
|
||||
|
||||
.. code-block:: sh
|
||||
|
||||
# TODO
|
||||
__uci_section ...
|
||||
|
||||
|
||||
SEE ALSO
|
||||
--------
|
||||
:strong:`cdist-type__uci`\ (7)
|
||||
|
||||
|
||||
AUTHORS
|
||||
-------
|
||||
Dennis Camera <dennis.camera@ssrq-sds-fds.ch>
|
||||
|
||||
|
||||
COPYING
|
||||
-------
|
||||
Copyright \(C) 2020 Dennis Camera. 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.
|
122
cdist/conf/type/__uci_section/manifest
Executable file
122
cdist/conf/type/__uci_section/manifest
Executable file
|
@ -0,0 +1,122 @@
|
|||
#!/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/>.
|
||||
#
|
||||
|
||||
grep_line() { echo "$2" | grep -qxF "$1"; }
|
||||
unquote_lines() {
|
||||
sed -e '/^".*"$/{s/^"//;s/"$//}' \
|
||||
-e '/'"^'.*'"'$/{s/'"^'"'//;s/'"'$"'//}'
|
||||
}
|
||||
|
||||
append_values() {
|
||||
while read -r _value
|
||||
do
|
||||
set -- "$@" --value "${_value}"
|
||||
done
|
||||
unset _value
|
||||
"$@" </dev/null
|
||||
}
|
||||
|
||||
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 -f "${__object:?}/parameter/option"
|
||||
then
|
||||
optnames_should=$(
|
||||
sed -e 's/=.*$//' "${__object:?}/parameter/option" | sort -u)
|
||||
fi
|
||||
|
||||
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
|
||||
|
||||
# Make sure the section itself is present
|
||||
__uci "${section}" --state present --transaction "${transaction_name}" \
|
||||
--value "${sect_type}"
|
||||
export require=__uci/"${section}"
|
||||
|
||||
# Delete options not in "should"
|
||||
sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \
|
||||
| while read -r optname
|
||||
do
|
||||
if ! grep_line "${optname}" "${optnames_should}"
|
||||
then
|
||||
__uci "${section}.${optname}" --state absent \
|
||||
--transaction "${transaction_name}" </dev/null
|
||||
fi
|
||||
done
|
||||
|
||||
# Set "should" options
|
||||
echo "${optnames_should}" \
|
||||
| while read -r optname
|
||||
do
|
||||
test -n "${optname}" || continue # ignore empty lines
|
||||
|
||||
grep "^${optname}=" <"${__object:?}/parameter/option" \
|
||||
| sed -e 's/^.*=//' \
|
||||
| unquote_lines \
|
||||
| append_values \
|
||||
__uci "${section}.${optname}" --state present \
|
||||
--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
|
1
cdist/conf/type/__uci_section/parameter/default/state
Normal file
1
cdist/conf/type/__uci_section/parameter/default/state
Normal file
|
@ -0,0 +1 @@
|
|||
present
|
|
@ -0,0 +1 @@
|
|||
default
|
4
cdist/conf/type/__uci_section/parameter/optional
Normal file
4
cdist/conf/type/__uci_section/parameter/optional
Normal file
|
@ -0,0 +1,4 @@
|
|||
match
|
||||
state
|
||||
transaction
|
||||
type
|
|
@ -0,0 +1 @@
|
|||
option
|
Loading…
Reference in a new issue