1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-23 10:59:06 +01:00

Support for global plugin parameters

This commit is contained in:
Lukas Macura 2016-12-11 13:06:05 +01:00
parent 63b00e70aa
commit a9a0ff9528
4 changed files with 80 additions and 8 deletions

View File

@ -40,14 +40,15 @@ zaf_plugin_info() {
local items
! [ -f "$control" ] && zaf_err "Control file $control not found."
plugin=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Plugin)
pdescription=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_moption Description)
pmaintainer=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Maintainer)
pversion=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Version)
purl=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Url)
phome=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Home)
plugin=$(zaf_ctrl_get_global_option "${control}" Plugin)
pdescription=$(zaf_ctrl_get_global_option "${control}" Description)
pmaintainer=$(zaf_ctrl_get_global_option "${control}" Maintainer)
pversion=$(zaf_ctrl_get_global_option "${control}" Version)
purl=$(zaf_ctrl_get_global_option "${control}" Url)
phome=$(zaf_ctrl_get_global_option "${control}" Home)
pitems=$(zaf_ctrl_get_items <"${control}")
peitems=$(zaf_ctrl_get_extitems <"${control}")
params=$(zaf_ctrl_get_global_option "${control}" Parameters)
echo
echo -n "Plugin '$plugin' "; [ -n "$pversion" ] && echo -n "version ${pversion}"; echo ":"
echo "$pdescription"; echo
@ -57,18 +58,41 @@ zaf_plugin_info() {
echo
if zaf_is_plugin "$(basename $plugin)"; then
items=$(zaf_list_plugin_items $plugin)
if [ -n "$params" ]; then
printf "%b" "Plugin parameters: (name,default,actual value)\n"
zaf_ctrl_get_global_option "${control}" Parameters | while read param default ; do
printf "%b" "$param\t$default\t$(zaf_get_plugin_parameter $(dirname $1) $param)\n"
done
echo;
fi
[ -n "$items" ] && { echo -n "Defined items: "; echo $items; }
items=$(zaf_list_plugin_items $plugin test)
[ -n "$items" ] && { echo -n "Test items: "; echo $items; }
items=$(zaf_list_plugin_items $plugin precache)
[ -n "$items" ] && { echo -n "Precache items: "; echo $items; }
[ -n "$peitems" ] && { echo -n "External check items: "; echo $peitems; }
else
echo "Items: $pitems"
fi
echo
}
# Set plugin global parameter
# $1 plugindir
# $2 parameter
# $3 value
zaf_set_plugin_parameter() {
printf "%s" "$3" >"${INSTALL_PREFIX}/${1}/${2}.value"
}
# Get plugin global parameter
# $1 plugindir
# $2 parameter
zaf_get_plugin_parameter() {
[ -f "${1}/${2}.value" ] && cat "${1}/${2}.value"
}
# Prepare plugin into dir
# $1 is url, directory or plugin name (will be searched in default plugin dir).
# $2 is directory to prepare.
@ -104,6 +128,9 @@ zaf_install_plugin() {
local tmpplugindir
local control
local version
local eparam
local param
local default
plugin=$(basename "$1")
plugindir="${ZAF_PLUGINS_DIR}/${plugin}"
@ -125,6 +152,24 @@ zaf_install_plugin() {
zaf_touch "${plugindir}/postinst.need"
fi
zaf_ctrl_install "$url" "${control}" "${plugindir}"
rm "${plugindir}/params"
zaf_touch "${plugindir}/params"
(zaf_ctrl_get_global_option "${control}" "Parameters"; echo) | \
while read param default; do
[ -z "$param" ] && continue
echo $param >>"${plugindir}/params"
eval eparam=\$ZAF_${plugin}_${param}
if [ -z "$eparam" ] && ! zaf_get_plugin_parameter "$plugindir" "$param" >/dev/null; then
zaf_wrn "Do not forget to set parameter $param. Use zaf plugin-set $plugin $param value. Default is $default."
zaf_set_plugin_parameter "$plugindir" "$param" "$default"
else
if [ -n "$eparam" ]; then
zaf_dbg "Setting $param to $eparam from env."
zaf_set_plugin_parameter "$plugindir" "$param" "$eparam"
fi
fi
done
else
zaf_err "Cannot install plugin '$plugin' to $plugindir!"
fi

View File

@ -9,6 +9,14 @@
. ${ZAF_LIB_DIR}/zbxapi.lib.sh
. ${ZAF_LIB_DIR}/cache.lib.sh
# Global plugin parameters
[ -f ./params ] && for p in $(cat ./params); do
var=$p
value="$(cat ${p}.value)"
zaf_trc "Global $p parameter $var=$value"
eval export $var="$value"
done
# Plugin specific functions if exists
[ -f ./functions.sh ] && . ./functions.sh
@ -30,8 +38,12 @@ export PATH
if [ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ]; then
tmpf=$(zaf_tmpfile preload)
export tmpf
$@ 2>$tmpf
[ -s $tmpf ] && zaf_wrn <$tmpf
else
tmpf=$(zaf_tmpfile preload)
export tmpf
fi

View File

@ -78,8 +78,8 @@ zaf_agentparm(){
echo "$value" | grep -qE "$regexp" || zaf_err "$ITEM_KEY: Bad parameter '$name' value '$value' (not in regexp '$regexp')."
fi
fi
eval $name=$value
zaf_trc "$ITEM_KEY: Param $name set to $value"
eval $name=\"$value\"
}
# Fetch url to stdout

17
zaf
View File

@ -235,7 +235,7 @@ reinstall)
done
[ -n "$reinstalled" ] && zaf_is_root && zaf_restart_agent
;;
remove)
remove|uninstall)
shift
[ -z "$1" ] && echo "$0 remove plugin [plugin]..."
for p in $@; do
@ -246,6 +246,21 @@ remove)
done
[ -n "$removed" ] && zaf_is_root && zaf_restart_agent
;;
plugin-set)
plugin="$2"
param="$3"
value="$4"
if [ -z "$plugin" ] || [ -z "$param" ]; then
zaf_err "$0 plugin-set plugin parameter value"
else
if zaf_is_plugin "$plugin"; then
plugindir="${ZAF_PLUGINS_DIR}/${plugin}"
zaf_set_plugin_parameter "$plugindir" "$param" "$value"
else
zaf_err "Plugin $plugin is not installed."
fi
fi
;;
###### Zaf related commands
self-upgrade)