forked from ungleich-public/cdist
[type/__uci] Implement "real" transactions using batch files
This commit is contained in:
parent
a09120977f
commit
d8f20a6a20
3 changed files with 40 additions and 6 deletions
|
@ -20,11 +20,27 @@
|
||||||
|
|
||||||
in_list() { printf '%s\n' "$@" | { grep -qxF "$(read -r NDL; echo "${NDL}")"; } }
|
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:?}
|
config=${__object_id:?}
|
||||||
|
|
||||||
state_is=$(cat "${__object:?}/explorer/state")
|
state_is=$(cat "${__object:?}/explorer/state")
|
||||||
state_should=$(cat "${__object:?}/parameter/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}
|
case ${state_should}
|
||||||
in
|
in
|
||||||
(present)
|
(present)
|
||||||
|
@ -44,12 +60,12 @@ in
|
||||||
|
|
||||||
while read -r value
|
while read -r value
|
||||||
do
|
do
|
||||||
printf "uci add_list '%s'='%s'\n" "${config}" "${value}"
|
uci_cmd "add_list '%s'='%s'" "${config}" "${value}"
|
||||||
done <"${__object:?}/parameter/value"
|
done <"${__object:?}/parameter/value"
|
||||||
else
|
else
|
||||||
# "should" is a scalar
|
# "should" is a scalar
|
||||||
value=$(cat "${__object:?}/parameter/value")
|
value=$(cat "${__object:?}/parameter/value")
|
||||||
printf "uci set '%s'='%s'\n" "${config}" "${value}"
|
uci_cmd "set '%s'='%s'" "${config}" "${value}"
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
(absent)
|
(absent)
|
||||||
|
@ -58,7 +74,7 @@ in
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "uci delete '%s'\n" "${config}"
|
uci_cmd "delete '%s'" "${config}"
|
||||||
;;
|
;;
|
||||||
(*)
|
(*)
|
||||||
printf 'Invalid --state: %s\n' "${state_should}" >&2
|
printf 'Invalid --state: %s\n' "${state_should}" >&2
|
|
@ -13,8 +13,7 @@ This cdist type can be used to alter configuration options in OpenWrt's UCI
|
||||||
system.
|
system.
|
||||||
|
|
||||||
Options can be applied in batches if the `--transaction` parameter is used.
|
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
|
REQUIRED PARAMETERS
|
||||||
-------------------
|
-------------------
|
||||||
|
|
|
@ -18,4 +18,23 @@
|
||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
# 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'
|
||||||
|
|
Loading…
Reference in a new issue