[type/__uci_section] Apply all commands in a single batch
This commit is contained in:
parent
3e5f18d409
commit
9d40500570
5 changed files with 180 additions and 74 deletions
|
@ -1,13 +1,6 @@
|
||||||
# -*- mode: sh; indent-tabs-mode: t -*-
|
# -*- mode: sh; indent-tabs-mode: t -*-
|
||||||
|
|
||||||
append_values() {
|
NL=$(printf '\n '); NL=${NL% }
|
||||||
while read -r _value
|
|
||||||
do
|
|
||||||
set -- "$@" --value "${_value}"
|
|
||||||
done
|
|
||||||
unset _value
|
|
||||||
"$@" </dev/null
|
|
||||||
}
|
|
||||||
|
|
||||||
grep_line() {
|
grep_line() {
|
||||||
{ shift; printf '%s\n' "$@"; } | grep -qxF "$1"
|
{ shift; printf '%s\n' "$@"; } | grep -qxF "$1"
|
||||||
|
@ -37,6 +30,28 @@ print_errors() {
|
||||||
}' >&2
|
}' >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
printf '%s' "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
uci_cmd() {
|
||||||
|
# Usage: uci_cmd [UCI ARGUMENTS]...
|
||||||
|
mkdir -p "${__object:?}/files"
|
||||||
|
printf '%s\n' "$(quote "$@")" >>"${__object:?}/files/uci_batch.txt"
|
||||||
|
}
|
||||||
|
|
||||||
uci_validate_name() {
|
uci_validate_name() {
|
||||||
# like util.c uci_validate_name()
|
# like util.c uci_validate_name()
|
||||||
test -n "$*" && test -z "$(printf %s "$*" | tr -d '[:alnum:]_' | tr -c '' .)"
|
test -n "$*" && test -z "$(printf %s "$*" | tr -d '[:alnum:]_' | tr -c '' .)"
|
||||||
|
|
1
cdist/conf/type/__uci_section/files/uci_apply.sh
Symbolic link
1
cdist/conf/type/__uci_section/files/uci_apply.sh
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../../__uci/files/uci_apply.sh
|
146
cdist/conf/type/__uci_section/gencode-remote
Executable file
146
cdist/conf/type/__uci_section/gencode-remote
Executable file
|
@ -0,0 +1,146 @@
|
||||||
|
#!/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/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
# shellcheck source=files/functions.sh
|
||||||
|
. "${__type:?}/files/functions.sh"
|
||||||
|
|
||||||
|
|
||||||
|
section=$(cat "${__object:?}/explorer/match")
|
||||||
|
|
||||||
|
state_is=$(test -s "${__object:?}/explorer/type" && echo present || echo absent)
|
||||||
|
state_should=$(cat "${__object:?}/parameter/state")
|
||||||
|
|
||||||
|
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 -n "${type_is}"
|
||||||
|
then
|
||||||
|
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
|
||||||
|
|
||||||
|
# Collect option names
|
||||||
|
if test -f "${__object:?}/parameter/list"
|
||||||
|
then
|
||||||
|
listnames_should=$(
|
||||||
|
sed -e 's/=.*$//' "${__object:?}/parameter/list" | sort -u)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if test -f "${__object:?}/parameter/option"
|
||||||
|
then
|
||||||
|
optnames_should=$(
|
||||||
|
sed -e 's/=.*$//' "${__object:?}/parameter/option" | sort -u)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Make sure the section itself is present
|
||||||
|
if test "${state_is}" = absent
|
||||||
|
then
|
||||||
|
printf 'set %s\n' "${section}" >>"${__messages_out:?}"
|
||||||
|
# shellcheck disable=SC2140
|
||||||
|
uci_cmd set "${section}"="${sect_type}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Delete options/lists not in "should"
|
||||||
|
sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \
|
||||||
|
| while read -r _optname
|
||||||
|
do
|
||||||
|
grep_line "${_optname}" "${listnames_should}" "${optnames_should}" || {
|
||||||
|
printf 'delete %s\n' "${section}.${_optname}" >>"${__messages_out:?}"
|
||||||
|
uci_cmd delete "${section}.${_optname}"
|
||||||
|
} </dev/null
|
||||||
|
done
|
||||||
|
|
||||||
|
# Set "should" options
|
||||||
|
prefix_lines option "${optnames_should}" list "${listnames_should}" \
|
||||||
|
| while read -r _type _optname
|
||||||
|
do
|
||||||
|
test -n "${_type}${_optname}" || continue # ignore empty lines
|
||||||
|
|
||||||
|
case $_type
|
||||||
|
in
|
||||||
|
(list)
|
||||||
|
printf 'set_list %s\n' "${section}.${_optname}" >>"${__messages_out:?}"
|
||||||
|
|
||||||
|
if grep -q -e "^${_optname}=" "${__object:?}/explorer/options"
|
||||||
|
then
|
||||||
|
uci_cmd delete "${section}.${_optname}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
grep "^${_optname}=" "${__object:?}/parameter/list" \
|
||||||
|
| sed -e 's/^.*=//' \
|
||||||
|
| unquote_lines \
|
||||||
|
| while read -r _value
|
||||||
|
do
|
||||||
|
# shellcheck disable=SC2140
|
||||||
|
uci_cmd add_list "${section}.${_optname}"="${_value}"
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
(option)
|
||||||
|
printf 'set %s\n' "${section}.${_optname}" >>"${__messages_out:?}"
|
||||||
|
|
||||||
|
# shellcheck disable=SC2140
|
||||||
|
uci_cmd set "${section}.${_optname}"="$(
|
||||||
|
grep "^${_optname}=" "${__object:?}/parameter/option" \
|
||||||
|
| sed -e 's/^.*=//' \
|
||||||
|
| unquote_lines \
|
||||||
|
| head -n 1)"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
(absent)
|
||||||
|
if test "${state_is}" = absent
|
||||||
|
then
|
||||||
|
# if explorer found no section there is nothing to delete
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf 'delete %s\n' "${section}" >>"${__messages_out:?}"
|
||||||
|
uci_cmd delete "${section}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if test -s "${__object:?}/files/uci_batch.txt"
|
||||||
|
then
|
||||||
|
cat "${__type:?}/files/uci_apply.sh"
|
||||||
|
printf "uci_apply <<'EOF'\n"
|
||||||
|
cat "${__object:?}/files/uci_batch.txt"
|
||||||
|
printf '\nEOF\n'
|
||||||
|
fi
|
|
@ -43,8 +43,6 @@ in
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
section=$(cat "${__object:?}/explorer/match")
|
|
||||||
|
|
||||||
state_should=$(cat "${__object:?}/parameter/state")
|
state_should=$(cat "${__object:?}/parameter/state")
|
||||||
|
|
||||||
case $state_should
|
case $state_should
|
||||||
|
@ -57,85 +55,31 @@ in
|
||||||
type_is=$(cat "${__object:?}/explorer/type")
|
type_is=$(cat "${__object:?}/explorer/type")
|
||||||
type_should=$(cat "${__object:?}/parameter/type")
|
type_should=$(cat "${__object:?}/parameter/type")
|
||||||
|
|
||||||
if test -n "${type_is}"
|
if test -n "${type_is}" && test "${type_is}" != "${type_should##*.}"
|
||||||
then
|
|
||||||
if test "${type_is}" != "${type_should##*.}"
|
|
||||||
then
|
then
|
||||||
# Check if section type matches (section exists and --type provided)
|
# Check if section type matches (section exists and --type provided)
|
||||||
printf 'Section type "%s" does not match --type "%s".\n' \
|
printf 'Section type "%s" does not match --type "%s".\n' \
|
||||||
"${type_is}" "${type_should}" >&2
|
"${type_is}" "${type_should}" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
|
||||||
|
|
||||||
# Check options for syntax errors
|
# Check options for syntax errors
|
||||||
validate_options "${__object:?}/parameter/list" "${__object:?}/parameter/object" \
|
validate_options "${__object:?}/parameter/list" "${__object:?}/parameter/object" \
|
||||||
| print_errors 'Found erroneous options in arguments:'
|
| print_errors 'Found erroneous options in arguments:'
|
||||||
|
|
||||||
# Collect option names
|
# Check for duplicate option names
|
||||||
if test -f "${__object:?}/parameter/list"
|
if test -s "${__object:?}/parameter/option"
|
||||||
then
|
then
|
||||||
listnames_should=$(
|
sed -e 's/=.*$//' "${__object:?}/parameter/option" \
|
||||||
sed -e 's/=.*$//' "${__object:?}/parameter/list" | sort -u)
|
| sort \
|
||||||
fi
|
|
||||||
|
|
||||||
if test -f "${__object:?}/parameter/option"
|
|
||||||
then
|
|
||||||
optnames_should=$(sed -e 's/=.*$//' "${__object:?}/parameter/option" | sort)
|
|
||||||
|
|
||||||
echo "${optnames_should}" \
|
|
||||||
| uniq -d \
|
| uniq -d \
|
||||||
| print_errors \
|
| print_errors \
|
||||||
'Found duplicate --options:' \
|
'Found duplicate --options:' \
|
||||||
"$(printf '\nUse --list for lists, instead.')"
|
"$(printf '\nUse --list for lists, instead.')"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Make sure the section itself is present
|
|
||||||
__uci "${section}" --state present --value "${sect_type}"
|
|
||||||
export require=__uci/"${section}"
|
|
||||||
|
|
||||||
# Delete options/lists not in "should"
|
|
||||||
sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \
|
|
||||||
| while read -r optname
|
|
||||||
do
|
|
||||||
grep_line "${optname}" "${listnames_should}" "${optnames_should}" || {
|
|
||||||
__uci "${section}.${optname}" --state absent
|
|
||||||
} </dev/null
|
|
||||||
done
|
|
||||||
|
|
||||||
# Set "should" options
|
|
||||||
prefix_lines option "${optnames_should}" list "${listnames_should}" \
|
|
||||||
| while read -r _type _optname
|
|
||||||
do
|
|
||||||
test -n "${_type}${_optname}" || continue # ignore empty lines
|
|
||||||
|
|
||||||
grep "^${_optname}=" <"${__object:?}/parameter/${_type}" \
|
|
||||||
| sed -e 's/^.*=//' \
|
|
||||||
| unquote_lines \
|
|
||||||
| append_values \
|
|
||||||
__uci "${section}.${_optname}" --state present \
|
|
||||||
--type "${_type}"
|
|
||||||
done
|
|
||||||
;;
|
;;
|
||||||
(absent)
|
(absent)
|
||||||
# if explorer found no section there is nothing to delete
|
:
|
||||||
test -n "${section}" || exit 0
|
|
||||||
|
|
||||||
__uci "${section}" --state absent
|
|
||||||
;;
|
;;
|
||||||
(*)
|
(*)
|
||||||
printf 'Invalid --state: %s\n' "${state_should}" >&2
|
printf 'Invalid --state: %s\n' "${state_should}" >&2
|
||||||
|
|
0
cdist/conf/type/__uci_section/nonparallel
Normal file
0
cdist/conf/type/__uci_section/nonparallel
Normal file
Loading…
Reference in a new issue