From 781f1f0bc0bc238000a9e2301c04f04b87da66dc Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Thu, 14 Apr 2016 09:57:59 +0200 Subject: [PATCH] Reworked parameters parsing Repaired install libraries loading Repaired installation error handling --- install.sh | 6 +++-- lib/ctrl.lib.sh | 60 +++++++++++++++++++++++++++++++----------- zaf | 70 ++++++++++++++++--------------------------------- 3 files changed, 72 insertions(+), 64 deletions(-) diff --git a/install.sh b/install.sh index a09132b..08e207e 100755 --- a/install.sh +++ b/install.sh @@ -25,7 +25,7 @@ zaf_download_files() { } if ! [ -f README.md ]; then - # We are runing from stdin + # We are runing from stdin if ! which curl >/dev/null; then zaf_err "Curl not found. Cannot continue. Please install it." @@ -41,6 +41,8 @@ if ! type zaf_version >/dev/null; then . lib/zaf.lib.sh . lib/os.lib.sh . lib/ctrl.lib.sh +. lib/cache.lib.sh +. lib/zbxapi.lib.sh fi # Read options as config for ZAF @@ -305,7 +307,7 @@ zaf_postconfigure() { else [ "${ZAF_GIT}" = 1 ] && [ -n "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}" fi - zaf_wrn "Install done. Use 'zaf' to get started. Do not forget to do 'zaf upgrade' to upgrade plugins too!" + zaf_wrn "Install done. Use 'zaf' to get started." true } diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index e8179d2..975f6e4 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -55,16 +55,32 @@ 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" + local ctrlopt + + eval ctrlopt=\$ZAF_CTRL_$(echo $2| tr '-' '_') + if [ -n "$ctrlopt" ]; then + zaf_wrn "Overriding $2 from env" + echo $ctrlopt + else + zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \ + || zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2" + fi } # 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" + local ctrlopt + + eval ctrlopt=\$ZAF_CTRL_$2_$(echo $3| tr '-' '_') + if [ -n "$ctrlopt" ]; then + zaf_wrn "Overriding item $2 option $3 from env" + echo $ctrlopt + else + zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \ + || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" + fi } # Check dependencies based on control file @@ -93,19 +109,21 @@ zaf_ctrl_sudo() { local cmd local parms - if ! which sudo >/dev/null; then - zaf_wrn "Sudo needed bud not installed?" - fi pdir="$3" plugin=$1 zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" sudo=$(zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}") + [ -z "$sudo" ] && return # Nothing to install + if ! which sudo >/dev/null; then + zaf_wrn "Sudo needed bud not installed?" + fi cmd=$(echo $sudo | cut -d ' ' -f 1) parms=$(echo $sudo | cut -d ' ' -f 2-) if which $cmd >/dev/null ; then - (echo "zabbix ALL=NOPASSWD:SETENV: $(which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin + (echo "zabbix ALL=NOPASSWD:SETENV: $(which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin || zaf_err "Error during zaf_ctrl_sudo" + chmod 0440 $ZAF_SUDOERSD/zaf_$plugin else - zaf_wrn "Cannot find binary $cmd for sudo. Ignoring sudo." + zaf_err "Cannot find binary '$cmd' to put into sudoers." fi } @@ -116,14 +134,17 @@ zaf_ctrl_sudo() { zaf_ctrl_cron() { local pdir local plugin + local cron pdir="$3" plugin=$1 zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin" - zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin + cron=$(zaf_ctrl_get_global_option $2 "Cron") + [ -z "$cron" ] && return # Nothing to install + zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin || zaf_err "Error during zaf_ctrl_cron" } -# Install sudo options from control +# Install files defined to be installed in control to plugun directory # $1 pluginurl # $2 control # $3 plugindir @@ -131,21 +152,28 @@ zaf_ctrl_install() { local binaries local pdir local script - local cmd + local files + local f + local b pdir="$3" + (set -e binaries=$(zaf_ctrl_get_global_option $2 "Install-bin") for b in $binaries; do zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" done + files=$(zaf_ctrl_get_global_option $2 "Install-files") + for f in $files; do + zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" + zaf_install "${ZAF_TMP_DIR}/$b" "$pdir" + done script=$(zaf_ctrl_get_global_option $2 "Install-script") [ -n "$script" ] && eval "$script" - cmd=$(zaf_ctrl_get_global_option $2 "Install-cmd") - [ -n "$cmd" ] && $cmd + true + ) || zaf_err "Error during zaf_ctrl_install" } - # Generates zabbix cfg from control file # $1 control # $2 pluginname @@ -158,6 +186,7 @@ zaf_ctrl_generate_cfg() { local cache items=$(zaf_ctrl_get_items <"$1") + (set -e for i in $items; do iscript=$(echo $i | tr -d '[]*&;:') params=$(zaf_ctrl_get_item_option $1 $i "Parameters") @@ -199,6 +228,7 @@ zaf_ctrl_generate_cfg() { fi zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" done + ) || zaf_err "Error during zaf_ctrl_generate_cfg" } diff --git a/zaf b/zaf index 3a9834d..533830d 100755 --- a/zaf +++ b/zaf @@ -1,20 +1,29 @@ #!/bin/sh +if [ -z "$secondstage" ]; then ZAF_CFG_FILE="/etc/zaf.conf" -if [ -f $ZAF_CFG_FILE ]; then - . $ZAF_CFG_FILE -else - echo "Missing config file $ZAF_CFG_FILE! Exiting." - exit 2 -fi + if [ -f $ZAF_CFG_FILE ]; then + . $ZAF_CFG_FILE + else + echo "Missing config file $ZAF_CFG_FILE! Exiting." + exit 2 + fi -# Read options as config for ZAF -for pair in "$@"; do - echo $pair | grep -q '^ZAF\_' || continue - option=$(echo $pair|cut -d '=' -f 1) - value=$(echo $pair|cut -d '=' -f 2-) - eval "${option}='$value'" -done + # Read options as config for ZAF + for pair in "$@"; do + if echo $pair | grep -qE '^ZAF\_(.*)='; then + option=$(echo $pair|cut -d '=' -f 1) + value=$(echo $pair|cut -d '=' -f 2-) + eval "${option}='$value'" + export secondstage=1 + else + params="$params $pair" + fi + done + export $(set |grep ^ZAF_ | cut -d '=' -f 1) + # If some variables in cmd were stripped, rerun only with right arguments and exported variables + [ -n "$secondstage" ] && exec $0 $params +fi [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}" @@ -54,25 +63,6 @@ if zaf_is_root; then chmod g+w "${ZAF_CACHE_DIR}" fi -zaf_shift(){ - local s - - if echo $1 |grep -q "="; then - if echo $2 |grep -q "="; then - if echo $3 |grep -q "="; then - s=3 - else - s=2 - fi - else - s=1 - fi - else - s=0 - fi - echo $s -} - case $1 in check-agent-config) @@ -110,7 +100,6 @@ upgrade) ;; show) shift; - shift $(zaf_shift "$@") if [ -z "$1" ]; then zaf_list_plugins | while read plugin; do zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf @@ -135,7 +124,6 @@ plugins) items) shift - shift $(zaf_shift "$@") if [ -z "$1" ]; then zaf_list_items else @@ -146,7 +134,6 @@ items) test) [ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!" shift - shift $(zaf_shift "$@") if echo $1|grep -q '\.'; then zaf_test_item "$1" exit @@ -166,7 +153,6 @@ test) ;; get) shift - shift $(zaf_shift "$@") if echo $1|grep -q '\.'; then zaf_get_item "$1" exit @@ -186,7 +172,6 @@ get) precache) shift - shift $(zaf_shift "$@") for i in $*; do if zaf_is_plugin $i; then for j in $(zaf_list_plugin_items $i precache); do @@ -204,7 +189,6 @@ precache) install) shift - shift $(zaf_shift "$@") [ -z "$1" ] && echo "$0 install plugin [plugin]..." for p in $@; do if zaf_is_plugin "$(basename $p)"; then @@ -219,7 +203,6 @@ install) reinstall) shift - shift $(zaf_shift "$@") [ -z "$1" ] && echo "$0 reinstall plugin [plugin]..." for p in $@; do if zaf_is_plugin "$p"; then @@ -234,7 +217,6 @@ reinstall) remove) shift - shift $(zaf_shift "$@") [ -z "$1" ] && echo "$0 remove plugin [plugin]..." for p in $@; do if zaf_is_plugin "$p"; then @@ -247,7 +229,6 @@ remove) self-upgrade) shift - shift $(zaf_shift "$@") [ -z "$1" ] && auto=auto zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade." rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer @@ -261,7 +242,6 @@ self-upgrade) self-remove) shift - shift $(zaf_shift "$@") zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove." . /etc/zaf.conf if [ "$1" = "force" ]; then @@ -294,11 +274,8 @@ api) zaf_zbxapi_gethostsingroup $gid ;; backup-group) - set -e - shift $(zaf_shift) shift; shift gid=$(zaf_zbxapi_gethostgroupid "$1") - shift $(zaf_shift) shift hosts=$(zaf_zbxapi_gethostsingroup $gid) dir="." @@ -314,7 +291,6 @@ api) wait ;; backup-host) - shift $(zaf_shift) shift; shift hostid=$(zaf_zbxapi_gethostid "$1") zaf_wrn "Exporting host $3($hostid)..." @@ -336,7 +312,7 @@ api) *) echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:" - echo "$0 Cmd [ZAF_OPTION=value] ..." + echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] ..." echo "Commands:" echo "$0 update To update repo (not plugins, similar to apt-get update)" echo "$0 upgrade To upgrade installed plugins from repo"