Add __uci and __uci_commit types

This commit is contained in:
Dennis Camera 2020-06-03 13:05:40 +02:00
parent 48d66b0143
commit e30ecdda53
11 changed files with 357 additions and 0 deletions

View file

@ -0,0 +1,89 @@
#!/bin/sh
#
# 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 state of the configuration option
# The output of this explorer is one of these values:
# present
# The configuration option is present and has the value of the
# parameter --value.
# absent
# The configuration option is not defined.
# different
# The configuration option is present but has a different value than the
# parameter --value.
# rearranged
# The configuration option is present (a list) and has the same values as
# the parameter --value, but in a different order.
RS=$(printf '\036')
option=${__object_id:?}
values_is=$(uci -s -N -d "${RS}" get "${option}" 2>/dev/null) || {
echo absent
exit 0
}
# strip off trailing newline
printf '%s' "${values_is}" \
| awk '
BEGIN {
state = "present" # assume all is fine
}
NR == FNR {
# memoize "should" state
should[FNR] = $0
# go to next line (important!)
next
}
# compare "is" state
$0 == should[FNR] { next }
FNR > length(should) {
# there are more "is" records than "should" -> definitely different
state = "different"
exit
}
{
# see if we can find the value somewhere in should
for (i in should) {
if ($0 == should[i]) {
# ... value found -> rearranged
# FIXME: Duplicate values are not properly handled here. Do they matter?
state = "rearranged"
next
}
}
state = "different"
exit
}
END {
if (FNR < length(should)) {
# "is" was shorter than "should" -> different
state = "different"
}
print state
}
' "${__object:?}/parameter/value" RS="${RS}" -

View file

@ -0,0 +1,67 @@
#!/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/>.
#
in_list() { printf '%s\n' "$@" | { grep -qxF "$(read -r NDL; echo "${NDL}")"; } }
config=${__object_id:?}
state_is=$(cat "${__object:?}/explorer/state")
state_should=$(cat "${__object:?}/parameter/state")
case ${state_should}
in
(present)
if in_list "${state_is}" 'present' 'rearranged'
then
# NOTE: order is ignored so rearranged is also fine.
exit 0
fi
if test "$(wc -l "${__object:?}/parameter/value")" -gt 1
then
# "should" is a list
if test "${state_is}" != 'absent'
then
printf "uci delete '%s'\n" "${config}"
fi
while read -r value
do
printf "uci add_list '%s'='%s'\n" "${config}" "${value}"
done <"${__object:?}/parameter/value"
else
# "should" is a scalar
value=$(cat "${__object:?}/parameter/value")
printf "uci set '%s'='%s'\n" "${config}" "${value}"
fi
;;
(absent)
if in_list "${state_is}" 'absent'
then
exit 0
fi
printf "uci delete '%s'\n" "${config}"
;;
(*)
printf 'Invalid --state: %s\n' "${state_should}" >&2
exit 1
;;
esac

View file

@ -0,0 +1,77 @@
cdist-type__uci(7)
==================
NAME
----
cdist-type__uci - Manage configuration values in OpenWrt's
Unified Configuration Interface (UCI)
DESCRIPTION
-----------
This cdist type can be used to alter configuration options in OpenWrt's UCI
system.
Options can be applied in batches if the `--transaction` parameter is used.
It is important to ensure that the `__uci_commit` object is executed before a
new transaction is started.
REQUIRED PARAMETERS
-------------------
value
The value to be set. Can be used multiple times.
Due to the way cdist handles arguments, values **must not** contain newline
characters.
OPTIONAL PARAMETERS
-------------------
state
`present` or `absent`, defaults to `present`.
transaction
The name of the transaction this option belongs to.
If none is given: "default" is used.
BOOLEAN PARAMETERS
------------------
None.
EXAMPLES
--------
.. code-block:: sh
# Set the system hostname
__uci system.@system[0].hostname --value 'OpenWrt'
# Enable NTP and NTPd (in one transaction)
__uci system.ntp.enabled --value 1 --transaction ntp
__uci system.ntp.enable_server --value 1 --transaction ntp
__uci system.ntp.server --transaction ntp \
--value '0.openwrt.pool.ntp.org' \
--value '1.openwrt.pool.ntp.org' \
--value '2.openwrt.pool.ntp.org' \
--value '3.openwrt.pool.ntp.org'
export require=__uci_commit/ntp
SEE ALSO
--------
- https://openwrt.org/docs/guide-user/base-system/uci
- :strong:`cdist-type__uci_commit`\ (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.

40
cdist/conf/type/__uci/manifest Executable file
View file

@ -0,0 +1,40 @@
#!/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/>.
#
os=$(cat "${__global:?}/explorer/os")
transaction_name=$(cat "${__object:?}/parameter/transaction")
case ${os}
in
(openwrt)
# okay
;;
(*)
printf "Your operating system (%s) is currently not supported by this type (%s)\n" "${os}" "${__type##*/}" >&2
printf "Please contribute an implementation for it if you can.\n" >&2
exit 1
;;
esac
# Make sure the changes are being commited
require=${__object_name:?} __uci_commit "${transaction_name}"

View file

@ -0,0 +1 @@
present

View file

@ -0,0 +1 @@
default

View file

@ -0,0 +1,2 @@
state
transaction

View file

@ -0,0 +1 @@
value

View file

@ -0,0 +1,21 @@
#!/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/>.
#
echo 'uci commit'

View file

@ -0,0 +1,58 @@
cdist-type__uci_commit(7)
=========================
NAME
----
cdist-type__uci_commit - Commit a UCI transaction.
DESCRIPTION
-----------
This type executes the "uci commit" command on the target.
It is usually not required to use this type. Use the `--transaction` parameter
of `cdist-type__uci`\ (7) instead.
REQUIRED PARAMETERS
-------------------
None.
OPTIONAL PARAMETERS
-------------------
None.
BOOLEAN PARAMETERS
------------------
None.
EXAMPLES
--------
.. code-block:: sh
# Commit the default transaction
__uci_commit default
# Commit another transaction
__uci_commit my_transaction
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.

View file