1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-21 18:09:06 +01:00

Reworked interface

This commit is contained in:
Lukas Macura 2016-04-01 15:51:45 +02:00
parent d0a3244f79
commit 99bd255e3f
6 changed files with 147 additions and 65 deletions

View File

@ -144,12 +144,18 @@ zaf_configure(){
fi
fi
fi
if which git >/dev/null; then
ZAF_GIT=1
else
ZAF_GIT=""
fi
zaf_get_option ZAF_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$1"
zaf_get_option ZAF_TMP_BASE "Tmp directory prefix (\$USER will be added)" "/tmp/zaf" "$1"
zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$1"
zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$1"
zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1"
zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "https://github.com/limosek/zaf-plugins.git" "$1"
[ "${ZAF_GIT}" -eq 1 ] && zaf_get_option ZAF_PLUGINS_GITURL "Git plugins repository" "https://github.com/limosek/zaf-plugins.git" "$1"
zaf_get_option ZAF_PLUGINS_URL "Plugins http[s] repository" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$1"
zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$1"
zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" "$1"
! [ -d "${ZAF_AGENT_CONFIGD}" ] && [ -d "/etc/zabbix/zabbix_agentd.d" ] && ZAF_AGENT_CONFIGD="/etc/zabbix/zabbix_agentd.d"
@ -160,11 +166,6 @@ zaf_configure(){
if zaf_is_root && ! which $ZAF_AGENT_BIN >/dev/null; then
zaf_err "Zabbix agent not installed? Use ZAF_ZABBIX_AGENT_BIN env variable to specify location. Exiting."
fi
if which git >/dev/null; then
ZAF_GIT=1
else
ZAF_GIT=""
fi
[ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc"
if ! [ -f "${ZAF_CFG_FILE}" ]; then
@ -181,7 +182,8 @@ zaf_configure(){
zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR"
zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR"
zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR"
zaf_set_option ZAF_PLUGINS_REPO "$ZAF_PLUGINS_REPO"
zaf_set_option ZAF_PLUGINS_URL "$ZAF_PLUGINS_URL"
[ "${ZAF_GIT}" -eq 1 ] && zaf_set_option ZAF_PLUGINS_GITURL "$ZAF_PLUGINS_GITURL"
zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR"
zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG"
zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD"
@ -241,7 +243,8 @@ install)
zaf_install_bin $(zaf_getrest zaf) ${ZAF_BIN_DIR}
export INSTALL_PREFIX ZAF_CFG_FILE
if zaf_is_root; then
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf install zaf || zaf_err "Error installing zaf plugin."
[ "${ZAF_GIT}" -eq 1 ] && ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf update
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf reinstall zaf || zaf_err "Error installing zaf plugin."
if zaf_is_root && ! zaf_check_agent_config; then
echo "Something is wrong with zabbix agent config."
echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}"

View File

@ -1,8 +1,6 @@
# Control file related functions
# Get block from stdin
# $1 option
# $2 name
# Get item list from control on stdin
zaf_ctrl_get_items() {
grep '^Item ' | cut -d ' ' -f 2 | cut -d ':' -f 1 | tr '\r\n' ' '
}
@ -57,14 +55,16 @@ zaf_block_get_option() {
# $1 - control file
# $2 - option name
zaf_ctrl_get_global_option() {
zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" || zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \
|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
}
# Get item specific option (single or multiline)
# $1 - control file
# $2 - item name
# $3 - option name
zaf_ctrl_get_item_option() {
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \
|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
}
# Check dependencies based on control file
@ -82,24 +82,24 @@ zaf_ctrl_check_deps() {
}
# Install binaries from control
# $1 control
# $2 plugindir
# $1 pluginurl
# $2 control
# $3 plugindir
zaf_ctrl_install() {
local binaries
local pdir
local script
local cmd
pdir="$2"
export ZAF_PLUGIN_DIR="$pdir"
binaries=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-bin")
pdir="$3"
binaries=$(zaf_ctrl_get_global_block <$2 | zaf_block_get_option "Install-bin")
for b in $binaries; do
zaf_fetch_url "$url/$b" >"${ZAF_TMP_DIR}/$b"
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
done
script=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "Install-script")
script=$(zaf_ctrl_get_global_block <$2 | zaf_block_get_moption "Install-script")
[ -n "$script" ] && eval "$script"
cmd=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-cmd")
cmd=$(zaf_ctrl_get_global_block <$2 | zaf_block_get_option "Install-cmd")
[ -n "$cmd" ] && $cmd
}
@ -109,33 +109,41 @@ zaf_ctrl_install() {
zaf_ctrl_generate_cfg() {
local items
local cmd
local iscript
local ikey
local lock
items=$(zaf_ctrl_get_items <"$1")
for i in $items; do
ikey=$(echo $i | tr -d '[]*&;:')
iscript=$(echo $i | tr -d '[]*&;:')
params=$(zaf_ctrl_get_item_option $1 $i "Parameters")
if [ -n "$params" ]; then
ikey="$2.$i[*]"
else
ikey="$2.$i"
fi
lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock "
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
echo "UserParameter=$2.${i},$lock$cmd";
echo "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd";
continue
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Function")
if [ -n "$cmd" ]; then
echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $lock$cmd";
echo "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd";
continue;
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then
zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${ikey}.sh;
zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${iscript}.sh;
zaf_install_bin ${ZAF_TMP_DIR}/${ikey}.sh ${ZAF_PLUGINS_DIR}/$2/
echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $lock${ZAF_PLUGINS_DIR}/$2/${ikey}.sh";
echo "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh";
continue;
fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
done
}

View File

@ -68,24 +68,34 @@ zaf_is_root(){
# $2 - directory
zaf_install(){
zaf_dbg "Install file $1 to $INSTALL_PREFIX/$2/$(basename $1)"
cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
}
# $1 - src file
# $2 - directory
zaf_install_bin(){
zaf_dbg "Install binary $1 to $INSTALL_PREFIX/$2/$(basename $1)"
cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
}
# $1 - directory
zaf_install_dir(){
zaf_dbg "Install directory $1 to $INSTALL_PREFIX/$1"
mkdir -p "$INSTALL_PREFIX/$1"
$ZAF_DO mkdir -p "$INSTALL_PREFIX/$1"
}
# $1 - file
zaf_touch(){
zaf_dbg "Touch $INSTALL_PREFIX/$1"
touch "$INSTALL_PREFIX/$1"
$ZAF_DO touch "$INSTALL_PREFIX/$1"
}
# $1 - directory
zaf_uninstall(){
if [ -n "$INSTALL_PREFIX" ]; then
zaf_dbg "Removing $INSTALL_PREFIX/$1"
$ZAF_DO rm -rf "$INSTALL_PREFIX/$1"
else
zaf_dbg "Removing $1"
$ZAF_DO rm -rf "$1"
fi
}
# Automaticaly install agent on debian

View File

@ -1,7 +1,20 @@
#!/bin/sh
. /etc/zaf.conf
[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}"
trap "rm -rif ${ZAF_TMP_DIR}" EXIT
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
. ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh
export ZAF_LIB_DIR
export ZAF_TMP_DIR
export ZAF_PLUGINS_DIR
[ -n "$*" ] && $@

View File

@ -1,4 +1,8 @@
# Hardcoded variables
ZAF_VERSION="trunk"
ZAF_URL="https://raw.githubusercontent.com/limosek/zaf/master/"
############################################ Common routines
zaf_msg() {
@ -16,6 +20,10 @@ zaf_err() {
exit 1
}
zaf_version(){
echo $ZAF_VERSION
}
# Fetch url to stdout
# $1 url
# It supports real file, file:// and other schemes known by curl
@ -73,7 +81,7 @@ zaf_discovery_add_row(){
shift;shift
echo " {"
while [ -n "$1" ]; do
echo -n ' "{#'$1'}":"'$2'" '
echo -n ' "'$1'":"'$2'" '
shift;shift
if [ -n "$1" ]; then
echo ","
@ -135,8 +143,12 @@ zaf_check_agent_config() {
# Update repo
zaf_update_repo() {
[ "$ZAF_GIT" != 1 ] && { zaf_err "Git is not installed. Exiting."; }
! [ -d ${ZAF_REPO_DIR} ] && git clone "${ZAF_PLUGINS_REPO}" "${ZAF_REPO_DIR}"
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
if [ -z "${ZAF_PLUGINS_GITURL}" ] || [ -z "${ZAF_REPO_DIR}" ]; then
zaf_err "This system is not configured for git repository."
else
[ ! -d "${ZAF_REPO_DIR}" ] && git clone "${ZAF_PLUGINS_GITURL}" "${ZAF_REPO_DIR}"
(cd ${ZAF_REPO_DIR} && git pull)
fi
}
# Construct url from plugin name
@ -154,7 +166,7 @@ zaf_get_plugin_url() {
if [ -d "${ZAF_REPO_DIR}/$1" ]; then
url="${ZAF_REPO_DIR}/$1"
else
url="${ZAF_PLUGINS_REPO}/$1";
url="${ZAF_PLUGINS_URL}/$1";
fi
fi
fi
@ -206,8 +218,9 @@ zaf_install_plugin() {
if zaf_prepare_plugin "$1" $plugindir; then
[ "$ZAF_DEBUG" -gt 0 ] && zaf_plugin_info "${control}"
zaf_ctrl_check_deps "${control}"
zaf_ctrl_install "${control}" "${plugin}"
zaf_ctrl_generate_cfg "${control}" "${plugin}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_ctrl_install "$1" "${control}" "${plugindir}"
zaf_ctrl_generate_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
else
zaf_err "Cannot install plugin $plugin to $plugindir!"
@ -228,7 +241,6 @@ zaf_list_plugins() {
}
zaf_is_plugin() {
[ -d "$1" ] && [ -f "$1/control.zaf" ] && return
[ -d "$ZAF_PLUGINS_DIR/$1" ] && [ -f "$ZAF_PLUGINS_DIR/$1/control.zaf" ] && return
false
}
@ -252,25 +264,39 @@ zaf_plugin_version() {
}
zaf_list_plugin_items() {
if [ -z "$1" ]; then
echo "Missing plugin name";
exit 1
local items
local i
local p
local key
if ! zaf_is_plugin "$1"; then
zaf_err "Missing plugin name or plugin $1 unknown. ";
fi
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control"
if [ -d "$plugindir" ] ; then
zaf_ctrl_get_option "$cfile" Item
else
echo "Plugin $1 not installed"
fi
cfile="$plugindir/control.zaf"
items=$(zaf_ctrl_get_items <$cfile)
for i in $items; do
p=$(zaf_ctrl_get_item_option $cfile $i "Parameters")
if [ -n "$p" ]; then
key="$1.$i[]"
else
key="$1.$i"
fi
echo -n "$key "
done
echo
}
zaf_list_items() {
for p in $(zaf_list_plugins); do
zaf_list_plugin_items $p
echo $p: $(zaf_list_plugin_items $p)
done
}
zaf_test_item() {
$ZAF_AGENT_BIN -t "$1"
}
zaf_remove_plugin() {
! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { zaf_err "Plugin $1 not installed!"; }
zaf_wrn "Removing plugin $1"

56
zaf
View File

@ -1,9 +1,5 @@
#!/bin/sh
# Hardcoded variables
ZAF_VERSION="trunk"
ZAF_URL="https://raw.githubusercontent.com/limosek/zaf/master/"
ZAF_CFG_FILE="/etc/zaf.conf"
if [ -f $ZAF_CFG_FILE ]; then
. $ZAF_CFG_FILE
@ -11,7 +7,6 @@ else
echo "Missing config file $ZAF_CFG_FILE! Exiting."
exit 2
fi
[ -d $(dirname $0)/.git ] && ZAF_LIB_DIR=$(dirname $0)/lib
[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}"
@ -19,10 +14,17 @@ trap "rm -rif ${ZAF_TMP_DIR}" EXIT
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
if [ -f ./lib/zaf.lib.sh ]; then
. ./lib/zaf.lib.sh
. ./lib/os.lib.sh
. ./lib/ctrl.lib.sh
[ -f ./lib/zaf.${ZAF_OS}.sh ] && . ./lib/zaf.${ZAF_OS}.sh
else
. ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh
[ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh
fi
case $1 in
@ -43,17 +45,21 @@ show)
if zaf_list_plugins | grep -q "^$2"; then
zaf_plugin_info $ZAF_PLUGINS_DIR/$2/control.zaf
else
zaf_prepare_plugin "$2" "$ZAF_TMP_DIR/plugin"
zaf_plugin_info "$ZAF_TMP_DIR/plugin/control.zaf"
if echo $2 |grep -q ^http; then
zaf_prepare_plugin "$2" "$ZAF_TMP_DIR/plugin"
zaf_plugin_info "$ZAF_TMP_DIR/plugin/control.zaf"
else
zaf_err "Plugin $2 not installed."
fi
fi
fi
;;
list)
plugins)
zaf_list_plugins
;;
list-items)
items)
if [ -z "$2" ]; then
zaf_list_items
else
@ -61,8 +67,18 @@ list-items)
fi
;;
test-items)
zaf_show_plugin "$2" tst
test)
if [ -n "$2" ]; then
shift
plugins="$@"
else
plugins="$(zaf_list_plugins)"
fi
for p in $plugins; do
for i in $(zaf_list_plugin_items $p); do
echo $i: $(zaf_test_item $i)
done
done
;;
install)
@ -84,7 +100,7 @@ reinstall)
[ -z "$@" ] && echo "$0 reinstall plugin [plugin]..."
for p in $@; do
if zaf_is_plugin "$p"; then
zaf_remove_plugin "$p"
zaf_remove_plugin "$(basename $p)"
reinstalled=1
fi
zaf_install_plugin "$p"
@ -115,20 +131,26 @@ self-upgrade)
;;
self-remove)
if [ "$2" = "force" ]; then
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} /usr/bin/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
. /etc/zaf.conf
if [ "$2" = "force" ]; then
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
else
echo "This will remove zaf from this computer and erase all configuration."
echo "This command will be executed:"
echo "rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*"
echo
echo "To continue, please do $0 self-remove force"
fi
;;
*)
echo "$0 update To update repo"
echo "$0 list To list installed plugins"
echo "$0 plugins To list installed plugins"
echo "$0 show [plugin] To show installed plugins or plugin info"
echo "$0 list-items [plugin] To list all suported items [for plugin]"
echo "$0 test-items plugin To test all suported items for plugin"
echo "$0 items [plugin] To list all suported items [for plugin]"
echo "$0 test [plugin] To test all suported items [for plugin]"
echo "$0 install plugin To install plugin"
echo "$0 remove plugin To remove plugin"
echo "$0 self-upgrade To self-upgrade zaf"