mirror of
				https://github.com/limosek/zaf.git
				synced 2025-10-31 09:37:37 +01:00 
			
		
		
		
	Reworked parameters parsing
Repaired install libraries loading Repaired installation error handling
This commit is contained in:
		| @@ -41,6 +41,8 @@ if ! type zaf_version >/dev/null; then | |||||||
| . lib/zaf.lib.sh | . lib/zaf.lib.sh | ||||||
| . lib/os.lib.sh | . lib/os.lib.sh | ||||||
| . lib/ctrl.lib.sh  | . lib/ctrl.lib.sh  | ||||||
|  | . lib/cache.lib.sh  | ||||||
|  | . lib/zbxapi.lib.sh  | ||||||
| fi | fi | ||||||
|  |  | ||||||
| # Read options as config for ZAF | # Read options as config for ZAF | ||||||
| @@ -305,7 +307,7 @@ zaf_postconfigure() { | |||||||
| 	else | 	else | ||||||
| 	    [ "${ZAF_GIT}" = 1 ] && [ -n  "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}" | 	    [ "${ZAF_GIT}" = 1 ] && [ -n  "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}" | ||||||
|         fi |         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 | 	true | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -55,16 +55,32 @@ zaf_block_get_option() { | |||||||
| # $1 - control file | # $1 - control file | ||||||
| # $2 - option name | # $2 - option name | ||||||
| zaf_ctrl_get_global_option() { | zaf_ctrl_get_global_option() { | ||||||
| 	zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \ | 	local ctrlopt | ||||||
| 	|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2" |  | ||||||
|  | 	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) | # Get item specific option (single or multiline) | ||||||
| # $1 - control file | # $1 - control file | ||||||
| # $2 - item name | # $2 - item name | ||||||
| # $3 - option name | # $3 - option name | ||||||
| zaf_ctrl_get_item_option() { | zaf_ctrl_get_item_option() { | ||||||
| 	zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \ | 	local ctrlopt | ||||||
| 	|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" |  | ||||||
|  | 	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 | # Check dependencies based on control file | ||||||
| @@ -93,19 +109,21 @@ zaf_ctrl_sudo() { | |||||||
| 	local cmd | 	local cmd | ||||||
| 	local parms | 	local parms | ||||||
|  |  | ||||||
| 	if ! which sudo >/dev/null; then |  | ||||||
| 		zaf_wrn "Sudo needed bud not installed?" |  | ||||||
| 	fi |  | ||||||
| 	pdir="$3" | 	pdir="$3" | ||||||
| 	plugin=$1 | 	plugin=$1 | ||||||
| 	zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" | 	zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" | ||||||
| 	sudo=$(zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}") | 	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) | 	cmd=$(echo $sudo | cut -d ' ' -f 1) | ||||||
| 	parms=$(echo $sudo | cut -d ' ' -f 2-) | 	parms=$(echo $sudo | cut -d ' ' -f 2-) | ||||||
| 	if which $cmd >/dev/null ; then | 	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 | 	else | ||||||
| 		zaf_wrn "Cannot find binary $cmd for sudo. Ignoring sudo." | 		zaf_err "Cannot find binary '$cmd' to put into sudoers." | ||||||
| 	fi | 	fi | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -116,14 +134,17 @@ zaf_ctrl_sudo() { | |||||||
| zaf_ctrl_cron() { | zaf_ctrl_cron() { | ||||||
| 	local pdir | 	local pdir | ||||||
| 	local plugin | 	local plugin | ||||||
|  | 	local cron | ||||||
|  |  | ||||||
| 	pdir="$3" | 	pdir="$3" | ||||||
| 	plugin=$1 | 	plugin=$1 | ||||||
| 	zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin" | 	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 | # $1 pluginurl | ||||||
| # $2 control | # $2 control | ||||||
| # $3 plugindir | # $3 plugindir | ||||||
| @@ -131,21 +152,28 @@ zaf_ctrl_install() { | |||||||
| 	local binaries | 	local binaries | ||||||
| 	local pdir | 	local pdir | ||||||
| 	local script | 	local script | ||||||
| 	local cmd | 	local files | ||||||
|  | 	local f | ||||||
|  | 	local b | ||||||
|  |  | ||||||
| 	pdir="$3" | 	pdir="$3" | ||||||
|  | 	(set -e | ||||||
| 	binaries=$(zaf_ctrl_get_global_option $2 "Install-bin") | 	binaries=$(zaf_ctrl_get_global_option $2 "Install-bin") | ||||||
| 	for b in $binaries; do | 	for b in $binaries; do | ||||||
| 		zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" | 		zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" | ||||||
|                 zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" |                 zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" | ||||||
| 	done | 	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") | 	script=$(zaf_ctrl_get_global_option $2 "Install-script") | ||||||
| 	[ -n "$script" ] && eval "$script" | 	[ -n "$script" ] && eval "$script" | ||||||
| 	cmd=$(zaf_ctrl_get_global_option $2 "Install-cmd") | 	true | ||||||
| 	[ -n "$cmd" ] && $cmd | 	) || zaf_err "Error during zaf_ctrl_install" | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| # Generates zabbix cfg from control file | # Generates zabbix cfg from control file | ||||||
| # $1 control | # $1 control | ||||||
| # $2 pluginname | # $2 pluginname | ||||||
| @@ -158,6 +186,7 @@ zaf_ctrl_generate_cfg() { | |||||||
| 	local cache | 	local cache | ||||||
|  |  | ||||||
| 	items=$(zaf_ctrl_get_items <"$1") | 	items=$(zaf_ctrl_get_items <"$1") | ||||||
|  | 	(set -e | ||||||
| 	for i in $items; do | 	for i in $items; do | ||||||
|             iscript=$(echo $i | tr -d '[]*&;:') |             iscript=$(echo $i | tr -d '[]*&;:') | ||||||
| 	    params=$(zaf_ctrl_get_item_option $1 $i "Parameters") | 	    params=$(zaf_ctrl_get_item_option $1 $i "Parameters") | ||||||
| @@ -199,6 +228,7 @@ zaf_ctrl_generate_cfg() { | |||||||
|             fi |             fi | ||||||
| 	    zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" | 	    zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" | ||||||
| 	done | 	done | ||||||
|  | 	) || zaf_err "Error during zaf_ctrl_generate_cfg" | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										70
									
								
								zaf
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								zaf
									
									
									
									
									
								
							| @@ -1,20 +1,29 @@ | |||||||
| #!/bin/sh | #!/bin/sh | ||||||
|  |  | ||||||
|  | if [ -z "$secondstage" ]; then | ||||||
| ZAF_CFG_FILE="/etc/zaf.conf" | ZAF_CFG_FILE="/etc/zaf.conf" | ||||||
| if [ -f $ZAF_CFG_FILE ]; then | 	if [ -f $ZAF_CFG_FILE ]; then | ||||||
| 	. $ZAF_CFG_FILE | 		. $ZAF_CFG_FILE | ||||||
| else | 	else | ||||||
| 	echo "Missing config file $ZAF_CFG_FILE! Exiting." | 		echo "Missing config file $ZAF_CFG_FILE! Exiting." | ||||||
| 	exit 2 | 		exit 2 | ||||||
| fi | 	fi | ||||||
|  |  | ||||||
| # Read options as config for ZAF | 	# Read options as config for ZAF | ||||||
| for pair in "$@"; do | 	for pair in "$@"; do | ||||||
|     echo $pair | grep -q '^ZAF\_' || continue |     		if echo $pair | grep -qE '^ZAF\_(.*)='; then | ||||||
|     option=$(echo $pair|cut -d '=' -f 1) |     			option=$(echo $pair|cut -d '=' -f 1) | ||||||
|     value=$(echo $pair|cut -d '=' -f 2-) |     			value=$(echo $pair|cut -d '=' -f 2-) | ||||||
|     eval "${option}='$value'" |     			eval "${option}='$value'" | ||||||
| done | 			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 | [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf | ||||||
| ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}" | ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}" | ||||||
| @@ -54,25 +63,6 @@ if zaf_is_root; then | |||||||
| 	chmod g+w "${ZAF_CACHE_DIR}" | 	chmod g+w "${ZAF_CACHE_DIR}" | ||||||
| fi | 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 | case $1 in | ||||||
|  |  | ||||||
| check-agent-config) | check-agent-config) | ||||||
| @@ -110,7 +100,6 @@ upgrade) | |||||||
| 	;; | 	;; | ||||||
| show) | show) | ||||||
| 	shift; | 	shift; | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	if [ -z "$1" ]; then | 	if [ -z "$1" ]; then | ||||||
| 		zaf_list_plugins | while read plugin; do | 		zaf_list_plugins | while read plugin; do | ||||||
| 			zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf | 			zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf | ||||||
| @@ -135,7 +124,6 @@ plugins) | |||||||
|  |  | ||||||
| items) | items) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	if [ -z "$1" ]; then | 	if [ -z "$1" ]; then | ||||||
| 		zaf_list_items | 		zaf_list_items | ||||||
| 	else | 	else | ||||||
| @@ -146,7 +134,6 @@ items) | |||||||
| test) | test) | ||||||
| 	[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!" | 	[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!" | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	if echo $1|grep -q '\.'; then | 	if echo $1|grep -q '\.'; then | ||||||
| 		zaf_test_item "$1" | 		zaf_test_item "$1" | ||||||
| 		exit | 		exit | ||||||
| @@ -166,7 +153,6 @@ test) | |||||||
| 	;; | 	;; | ||||||
| get) | get) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	if echo $1|grep -q '\.'; then | 	if echo $1|grep -q '\.'; then | ||||||
| 		zaf_get_item "$1" | 		zaf_get_item "$1" | ||||||
| 		exit | 		exit | ||||||
| @@ -186,7 +172,6 @@ get) | |||||||
|  |  | ||||||
| precache) | precache) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	for i in $*; do | 	for i in $*; do | ||||||
| 		if zaf_is_plugin $i; then | 		if zaf_is_plugin $i; then | ||||||
| 			for j in $(zaf_list_plugin_items $i precache); do | 			for j in $(zaf_list_plugin_items $i precache); do | ||||||
| @@ -204,7 +189,6 @@ precache) | |||||||
|  |  | ||||||
| install) | install) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	[ -z "$1" ] && echo "$0 install plugin [plugin]..." | 	[ -z "$1" ] && echo "$0 install plugin [plugin]..." | ||||||
| 	for p in $@; do | 	for p in $@; do | ||||||
| 		if zaf_is_plugin "$(basename $p)"; then | 		if zaf_is_plugin "$(basename $p)"; then | ||||||
| @@ -219,7 +203,6 @@ install) | |||||||
|  |  | ||||||
| reinstall) | reinstall) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	[ -z "$1" ] && echo "$0 reinstall plugin [plugin]..." | 	[ -z "$1" ] && echo "$0 reinstall plugin [plugin]..." | ||||||
| 	for p in $@; do | 	for p in $@; do | ||||||
| 		if zaf_is_plugin "$p"; then | 		if zaf_is_plugin "$p"; then | ||||||
| @@ -234,7 +217,6 @@ reinstall) | |||||||
|  |  | ||||||
| remove) | remove) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	[ -z "$1" ] && echo "$0 remove plugin [plugin]..." | 	[ -z "$1" ] && echo "$0 remove plugin [plugin]..." | ||||||
| 	for p in $@; do | 	for p in $@; do | ||||||
| 		if zaf_is_plugin "$p"; then | 		if zaf_is_plugin "$p"; then | ||||||
| @@ -247,7 +229,6 @@ remove) | |||||||
|  |  | ||||||
| self-upgrade) | self-upgrade) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	[ -z "$1" ] && auto=auto | 	[ -z "$1" ] && auto=auto | ||||||
| 	zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade." | 	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 | 	rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer | ||||||
| @@ -261,7 +242,6 @@ self-upgrade) | |||||||
| 	 | 	 | ||||||
| self-remove) | self-remove) | ||||||
| 	shift | 	shift | ||||||
| 	shift $(zaf_shift "$@") |  | ||||||
| 	zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove." | 	zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove." | ||||||
| 	. /etc/zaf.conf | 	. /etc/zaf.conf | ||||||
| 	if [ "$1" = "force" ]; then | 	if [ "$1" = "force" ]; then | ||||||
| @@ -294,11 +274,8 @@ api) | |||||||
| 		zaf_zbxapi_gethostsingroup $gid | 		zaf_zbxapi_gethostsingroup $gid | ||||||
| 		;; | 		;; | ||||||
| 	backup-group) | 	backup-group) | ||||||
| 	set -e |  | ||||||
| 		shift $(zaf_shift) |  | ||||||
| 		shift; shift | 		shift; shift | ||||||
| 		gid=$(zaf_zbxapi_gethostgroupid "$1") | 		gid=$(zaf_zbxapi_gethostgroupid "$1") | ||||||
| 		shift $(zaf_shift) |  | ||||||
| 		shift | 		shift | ||||||
| 		hosts=$(zaf_zbxapi_gethostsingroup $gid) | 		hosts=$(zaf_zbxapi_gethostsingroup $gid) | ||||||
| 		dir="." | 		dir="." | ||||||
| @@ -314,7 +291,6 @@ api) | |||||||
| 		wait | 		wait | ||||||
| 		;; | 		;; | ||||||
| 	backup-host) | 	backup-host) | ||||||
| 		shift $(zaf_shift) |  | ||||||
| 		shift; shift | 		shift; shift | ||||||
| 		hostid=$(zaf_zbxapi_gethostid "$1") | 		hostid=$(zaf_zbxapi_gethostid "$1") | ||||||
| 		zaf_wrn "Exporting host $3($hostid)..." | 		zaf_wrn "Exporting host $3($hostid)..." | ||||||
| @@ -336,7 +312,7 @@ api) | |||||||
|  |  | ||||||
| *) | *) | ||||||
| 	echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:" | 	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 "Commands:" | ||||||
| 	echo "$0 update			To update repo (not plugins, similar to apt-get update)" | 	echo "$0 update			To update repo (not plugins, similar to apt-get update)" | ||||||
| 	echo "$0 upgrade		To upgrade installed plugins from repo"  | 	echo "$0 upgrade		To upgrade installed plugins from repo"  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Lukas Macura
					Lukas Macura