bbfdm/utilities/files/usr/libexec/rpcd/bbf.config
2024-06-04 12:18:02 +02:00

86 lines
1.9 KiB
Bash
Executable file

#!/bin/sh
. /usr/share/libubox/jshn.sh
BBFDM_DMMAP_CONFIG="/etc/bbfdm/dmmap"
BBFDM_DMMAP_SAVEDIR="/tmp/.bbfdm"
LOGLEVEL="$(uci -q get bbfdm.bbfdmd.loglevel)"
log() {
local level
level="${LOGLEVEL:-0}"
if [ "${level}" -gt 2 ]; then
echo "$@" | logger -t bbf.config -p info
fi
}
check_result() {
local res="$1"
local service="$2"
local action="$3"
if [ "${res}" -ne 0 ]; then
echo "{ \"error\": \"Failed to ${action} ${service} service\" }"
exit "${res}"
fi
}
apply_config_changes() {
local service="$1"
local action="$3"
# Check if either service or action is empty
if [ -z "$service" ] || [ -z "$action" ]; then
return
fi
log "Applying $action configuration for service: $service"
# Commit/Revert config changes
ubus -t 1 call uci ${action} "{'config': '${service}'}"
check_result "$?" "${service}" "${action}"
}
case "$1" in
list)
echo '{ "commit": { "services": [] }, "revert": { "services": [] } }'
;;
call)
# Read input JSON from standard input
read -r input
# Parse input JSON
json_load "${input}"
# Check if 'services' array is provided
json_get_type type "services"
if [ -z "${type}" ]; then
echo '{ "error": "Services array should be defined !!!" }'
exit 1
fi
# Check if 'services' is array
if [ "${type}" != "array" ]; then
echo '{ "error": "Services argument should be array of strings !!!" }'
exit 1
fi
# Iterate over each service and apply config changes
json_for_each_item "apply_config_changes" "services" "$2"
# Commit/Revert bbfdm dmmap config changes
for file in "${BBFDM_DMMAP_CONFIG}"/*; do
file_name=$(basename "${file}")
log "Applying $2 configuration for file: $file_name"
uci -q -c "${BBFDM_DMMAP_CONFIG}" -t "${BBFDM_DMMAP_SAVEDIR}" "$2" "${file_name}"
check_result "$?" "${file_name}" "$2"
done
# Send 'bbf.config.change' event to run refresh instances
ubus send bbf.config.change
echo '{ "status": "ok" }'
;;
esac