[type/__uci] Implement "real" transactions using batch files

This commit is contained in:
Dennis Camera 2020-06-12 19:39:19 +02:00
parent a09120977f
commit d8f20a6a20
3 changed files with 40 additions and 6 deletions

View file

@ -20,11 +20,27 @@
in_list() { printf '%s\n' "$@" | { grep -qxF "$(read -r NDL; echo "${NDL}")"; } }
uci_cmd() {
printf 'printf "%s\n"' "$1"
shift
printf " '%s'" "$@"
printf " >>'%s'\n" "${tmpfile}"
}
config=${__object_id:?}
state_is=$(cat "${__object:?}/explorer/state")
state_should=$(cat "${__object:?}/parameter/state")
transaction_name=$(cat "${__object:?}/parameter/transaction")
tmpdir="${__global:?}/tmp/__uci"
# HACK
mkdir -p "${tmpdir}"
tmpfile="${tmpdir}/${transaction_name}.txt"
case ${state_should}
in
(present)
@ -44,12 +60,12 @@ in
while read -r value
do
printf "uci add_list '%s'='%s'\n" "${config}" "${value}"
uci_cmd "add_list '%s'='%s'" "${config}" "${value}"
done <"${__object:?}/parameter/value"
else
# "should" is a scalar
value=$(cat "${__object:?}/parameter/value")
printf "uci set '%s'='%s'\n" "${config}" "${value}"
uci_cmd "set '%s'='%s'" "${config}" "${value}"
fi
;;
(absent)
@ -58,7 +74,7 @@ in
exit 0
fi
printf "uci delete '%s'\n" "${config}"
uci_cmd "delete '%s'" "${config}"
;;
(*)
printf 'Invalid --state: %s\n' "${state_should}" >&2

View file

@ -13,8 +13,7 @@ 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
-------------------

View file

@ -18,4 +18,23 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
echo 'uci commit'
transaction_name=${__object_id:?}
batchfile="${__global:?}/tmp/__uci/${transaction_name}.txt"
test -s "${batchfile}" || exit 0
cat <<'EOF'
rollback() {
uci changes \
| sed -e 's/\..*$//' -e 's/^-//' \
| while read -r package
do
uci revert "${package}"
done
}
EOF
echo "uci batch <<'EOF' && uci commit || rollback"
cat "${batchfile}"
echo 'EOF'