2020-06-03 11:05:40 +00:00
|
|
|
#!/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/>.
|
|
|
|
#
|
|
|
|
|
2020-10-23 09:45:37 +00:00
|
|
|
in_list() {
|
|
|
|
printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
quote() {
|
|
|
|
for _arg
|
|
|
|
do
|
|
|
|
shift
|
|
|
|
if test -n "$(printf %s "${_arg}" | tr -d -c '\t\n \042-\047\050-\052\073-\077\133\\`|~' | tr -c '' '.')"
|
|
|
|
then
|
|
|
|
# needs quoting
|
|
|
|
set -- "$@" "$(printf "'%s'" "$(printf %s "${_arg}" | sed -e "s/'/'\\\\''/g")")"
|
|
|
|
else
|
|
|
|
set -- "$@" "${_arg}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
unset _arg
|
|
|
|
|
|
|
|
# NOTE: Use printf because POSIX echo interprets escape sequences
|
|
|
|
printf '%s' "$*"
|
|
|
|
}
|
2020-06-03 11:05:40 +00:00
|
|
|
|
2020-06-12 17:39:19 +00:00
|
|
|
uci_cmd() {
|
2020-10-23 09:45:37 +00:00
|
|
|
# Usage: uci_cmd [UCI ARGUMENTS]...
|
|
|
|
# Will output a shell command to be executed locally to add the given
|
|
|
|
# command to $tmpfile.
|
|
|
|
printf "printf '%%s\\\\n' %s" "$(quote "$(quote "$@")")"
|
|
|
|
printf ' >>%s\n' "$(quote "${tmpfile}")"
|
2020-06-12 17:39:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 20:03:25 +00:00
|
|
|
uci_validate_name() {
|
|
|
|
# like util.c uci_validate_name()
|
|
|
|
test -n "$*" && test -z "$(echo "$*" | tr -d '[:alnum:]_')"
|
|
|
|
}
|
|
|
|
|
|
|
|
uci_validate_tuple() (
|
|
|
|
tok=${1:?}
|
|
|
|
case $tok
|
|
|
|
in
|
|
|
|
(*.*.*)
|
|
|
|
# check option
|
|
|
|
option=${tok##*.}
|
|
|
|
uci_validate_name "${option}" || {
|
|
|
|
printf 'Invalid option: %s\n' "${option}" >&2
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
tok=${tok%.*}
|
|
|
|
;;
|
|
|
|
(*.*)
|
|
|
|
# no option (section definition)
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
printf 'Invalid tuple: %s\n' "$1" >&2
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case ${tok#*.}
|
|
|
|
in
|
|
|
|
(@*) section=$(expr "${tok#*.}" : '@\(.*\)\[-*[0-9]*\]$') ;;
|
|
|
|
(*) section=${tok#*.} ;;
|
|
|
|
esac
|
|
|
|
uci_validate_name "${section}" || {
|
|
|
|
printf 'Invalid section: %s\n' "${1#*.}" >&2
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
config=${tok%%.*}
|
|
|
|
uci_validate_name "${config}" || {
|
|
|
|
printf 'Invalid config: %s\n' "${config}" >&2
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-10-23 09:45:37 +00:00
|
|
|
|
2020-06-03 11:05:40 +00:00
|
|
|
config=${__object_id:?}
|
2020-10-23 20:03:25 +00:00
|
|
|
uci_validate_tuple "${config}"
|
2020-06-03 11:05:40 +00:00
|
|
|
|
|
|
|
state_is=$(cat "${__object:?}/explorer/state")
|
|
|
|
state_should=$(cat "${__object:?}/parameter/state")
|
|
|
|
|
2020-06-12 17:39:19 +00:00
|
|
|
transaction_name=$(cat "${__object:?}/parameter/transaction")
|
|
|
|
|
|
|
|
tmpdir="${__global:?}/tmp/__uci"
|
|
|
|
# HACK
|
|
|
|
mkdir -p "${tmpdir}"
|
|
|
|
|
|
|
|
tmpfile="${tmpdir}/${transaction_name}.txt"
|
|
|
|
|
|
|
|
|
2020-06-03 11:05:40 +00:00
|
|
|
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
|
|
|
|
|
2020-10-24 10:57:40 +00:00
|
|
|
# Determine type
|
|
|
|
type=$(cat "${__object:?}/parameter/type" 2>/dev/null || true)
|
|
|
|
case ${type}
|
|
|
|
in
|
|
|
|
(option|list) ;;
|
|
|
|
('')
|
|
|
|
# Guess type by the number of values
|
|
|
|
test "$(wc -l "${__object:?}/parameter/value")" -gt 1 \
|
|
|
|
&& type=list \
|
|
|
|
|| type=option
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
printf 'Invalid --type: %s\n' "${type}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case ${type}
|
|
|
|
in
|
|
|
|
(list)
|
|
|
|
printf 'set_list %s\n' "${config}" >>"${__messages_out:?}"
|
|
|
|
|
|
|
|
if test "${state_is}" != 'absent'
|
|
|
|
then
|
|
|
|
uci_cmd delete "${config}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
while read -r value
|
|
|
|
do
|
|
|
|
uci_cmd add_list "${config}"="${value}"
|
|
|
|
done <"${__object:?}/parameter/value"
|
|
|
|
;;
|
|
|
|
(option)
|
|
|
|
printf 'set %s\n' "${config}" >>"${__messages_out:?}"
|
|
|
|
|
|
|
|
value=$(cat "${__object:?}/parameter/value")
|
|
|
|
uci_cmd set "${config}"="${value}"
|
|
|
|
;;
|
|
|
|
esac
|
2020-06-03 11:05:40 +00:00
|
|
|
;;
|
|
|
|
(absent)
|
|
|
|
if in_list "${state_is}" 'absent'
|
|
|
|
then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-06-20 14:43:16 +00:00
|
|
|
printf 'delete %s\n' "${config}" >>"${__messages_out:?}"
|
2020-10-23 09:45:37 +00:00
|
|
|
uci_cmd delete "${config}"
|
2020-06-03 11:05:40 +00:00
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
printf 'Invalid --state: %s\n' "${state_should}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|