1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-13 05:59:06 +01:00
limosek-zaf/install.sh

246 lines
7.4 KiB
Bash
Raw Normal View History

2016-03-23 14:37:51 +01:00
#!/bin/sh
2016-03-24 16:59:49 +01:00
if ! [ "$(basename $0)" = "install.sh" ]; then
# We are runing from stdin
2016-03-30 16:09:02 +02:00
url="https://raw.githubusercontent.com/limosek/zaf/master/"
if ! which curl >/dev/null;
then
echo "Curl not found. Cannot continue. Please install it."
exit 2
fi
echo "Installing from url $url..." >&2
[ -z "$*" ] && silent=silent
2016-03-24 16:59:49 +01:00
set -e
mkdir -p /tmp/zaf-installer \
&& cd /tmp/zaf-installer \
2016-03-30 16:09:02 +02:00
&& (for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh install.sh ; do curl -f -k -s -L -o - "$url/$i") >install.sh \
2016-03-24 16:59:49 +01:00
&& chmod +x install.sh \
2016-03-30 16:09:02 +02:00
&& exec ./install.sh $silent "$@"
exit
2016-03-24 16:59:49 +01:00
fi
2016-03-24 09:38:34 +01:00
ZAF_CFG_FILE=/etc/zaf.conf
2016-03-30 16:09:02 +02:00
. $(dirname $0)/lib/zaf.lib.sh
. $(dirname $0)/lib/os.lib.sh
. $(dirname $0)/lib/ctrl.lib.sh
2016-03-24 09:38:34 +01:00
2016-03-24 15:46:42 +01:00
zaf_msg() {
[ "$ZAF_DEBUG" = "1" ] && echo $@
}
2016-03-24 09:38:34 +01:00
# Read option. If it is already set in zaf.conf, it is skipped. If env variable is set, it is used instead of default
# It sets global variable name on result.
# $1 - option name
# $2 - option description
# $3 - default
# $4 - if $4="silent" , use autoconf. if $4="user", force asking.
zaf_get_option(){
local opt
eval opt=\$$1
if [ -n "$opt" ] && ! [ "$4" = "user" ]; then
eval "$1='$opt'"
2016-03-24 15:46:42 +01:00
zaf_msg "Got $2 <$1> from ENV: $opt" >&2
2016-03-24 09:38:34 +01:00
return
else
opt="$3"
fi
if ! [ "$4" = "silent" ]; then
echo -n "$2 <$1> [$opt]: "
read opt
else
opt=""
fi
if [ -z "$opt" ]; then
opt="$3"
2016-03-24 15:46:42 +01:00
zaf_msg "Got $2 <$1> from Defaults: $opt" >&2
2016-03-24 09:38:34 +01:00
else
2016-03-24 15:46:42 +01:00
zaf_msg "Got $2 <$1> from USER: $opt"
2016-03-24 09:38:34 +01:00
fi
2016-03-30 16:09:02 +02:00
eval "$1='$opt'"
2016-03-24 09:38:34 +01:00
}
# Sets option to zaf.conf
# $1 option name
# $2 option value
zaf_set_option(){
2016-03-30 16:09:02 +02:00
local description
2016-03-24 09:38:34 +01:00
if ! grep -q "^$1=" ${ZAF_CFG_FILE}; then
echo "$1='$2'" >>${ZAF_CFG_FILE}
2016-03-24 15:46:42 +01:00
zaf_msg "Saving $1 to $2 in ${ZAF_CFG_FILE}" >&2
else
zaf_msg "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2
2016-03-24 09:38:34 +01:00
fi
2016-03-23 14:37:51 +01:00
}
2016-03-24 16:59:49 +01:00
zaf_getrest(){
2016-03-23 14:37:51 +01:00
if [ -f "$(dirname $0)/$1" ]; then
echo "$(dirname $0)/$1"
else
2016-03-24 16:59:49 +01:00
curl -f -k -s -L -o - https://raw.githubusercontent.com/limosek/zaf/master/$1 >${ZAF_TMP_DIR}/$(basename $1)
2016-03-23 14:37:51 +01:00
echo ${ZAF_TMP_DIR}/$(basename $1)
fi
}
2016-03-24 16:59:49 +01:00
zaf_install(){
cp "$1" "$2"
}
zaf_install_exe(){
cp "$1" "$2"
2016-03-24 16:59:49 +01:00
chmod +x "$2"
}
2016-03-30 16:09:02 +02:00
# Automaticaly install agent if supported
zaf_install_agent() {
case $ZAF_OS in
Debian)
curl "http://repo.zabbix.com/zabbix/3.0/debian/pool/main/z/zabbix-release/zabbix-release_3.0-1+${ZAF_CODENAME}_all.deb" >"/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_CODENAME}_all.deb" \
&& dpkg -i "/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_CODENAME}_all.deb" \
&& apt-get update \
&& apt-get install $ZAF_AGENT_PKG
;;
esac
}
# Set config option in zabbix agent
# $1 option
# $2 value
zaf_agent_set_option() {
local option="$1"
local value="$2"
if grep ^$option\= $ZAF_AGENT_CONFIG; then
echo "Moving option $option to zaf config part."
sed -i "s/$option=/#$option=/" $ZAF_AGENT_CONFIG
2016-03-24 09:38:34 +01:00
fi
2016-03-30 16:09:02 +02:00
echo "$option=$value" >> "$ZAF_AGENT_CONFIGD/zaf_options.conf"
}
# Automaticaly configure agent if supported
# Parameters are in format zabbixconfvar=value
zaf_configure_agent() {
local pair
local option
local value
touch "$ZAF_AGENT_CONFIGD/zaf_options.conf"
for pair in "$@"; do
echo $pair | grep -q '^Z\_' || continue
option=$(echo $pair|cut -d '=' -f 1|cut -d '_' -f 2)
value=$(echo $pair|cut -d '=' -f 2-)
zaf_agent_set_option "$option" "$value"
done
2016-03-24 09:38:34 +01:00
}
2016-03-23 17:25:18 +01:00
2016-03-24 09:38:34 +01:00
zaf_no_perms(){
echo "No permissions! to $1! Please become root or give perms. Exiting."
exit 2
}
2016-03-23 14:37:51 +01:00
2016-03-24 09:38:34 +01:00
zaf_configure(){
2016-03-30 16:09:02 +02:00
[ "$1" = "interactive" ] && ZAF_DEBUG=1
zaf_detect_system
zaf_get_option ZAF_PKG "Packaging system to use" "$ZAF_PKG" "$1"
zaf_get_option ZAF_OS "Operating system to use" "$ZAF_OS" "$1"
zaf_get_option ZAF_OS_CODENAME "Operating system codename" "$ZAF_OS_CODENAME" "$1"
zaf_get_option ZAF_AGENT_PKG "Zabbix agent package" "$ZAF_AGENT_PKG" "$1"
if [ -n "$ZAF_AGENT_PKG" ]; then
if ! zaf_check_deps "$ZAF_AGENT_PKG"; then
if [ "$1" = "silent" ]; then
zaf_install_agent
fi
fi
fi
2016-03-24 16:59:49 +01:00
zaf_get_option ZAF_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$1"
2016-03-24 15:46:42 +01:00
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_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1"
zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$1"
2016-03-24 15:46:42 +01:00
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"
2016-03-24 17:54:01 +01:00
! [ -d "${ZAF_AGENT_CONFIGD}" ] && [ -d "/etc/zabbix/zabbix_agentd.d" ] && ZAF_AGENT_CONFIGD="/etc/zabbix/zabbix_agentd.d"
2016-03-24 15:46:42 +01:00
zaf_get_option ZAF_AGENT_CONFIGD "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/" "$1"
zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$1"
zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$1"
2016-03-24 09:38:34 +01:00
if ! which $ZAF_AGENT_BIN >/dev/null; then
echo "Zabbix agent not installed? Use ZAF_ZABBIX_AGENT_BIN env variable to specify location. Exiting."
exit 3
2016-03-23 14:37:51 +01:00
fi
2016-03-24 15:46:42 +01:00
if which git >/dev/null; then
ZAF_GIT=1
else
ZAF_GIT=""
fi
2016-03-24 09:38:34 +01:00
if ! [ -f "${ZAF_CFG_FILE}" ]; then
touch "${ZAF_CFG_FILE}" || zaf_no_perms "${ZAF_CFG_FILE}"
2016-03-23 14:37:51 +01:00
fi
2016-03-24 09:38:34 +01:00
zaf_set_option ZAF_PKG "${ZAF_PKG}"
2016-03-30 16:09:02 +02:00
zaf_set_option ZAF_OS "${ZAF_OS}"
zaf_set_option ZAF_OS_CODENAME "${ZAF_OS_CODENAME}"
zaf_set_option ZAF_AGENT_PKG "${ZAF_AGENT_PKG}"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_GIT "${ZAF_GIT}"
2016-03-24 16:59:49 +01:00
zaf_set_option ZAF_CURL_INSECURE "${ZAF_CURL_INSECURE}"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_TMP_BASE "$ZAF_TMP_BASE"
2016-03-24 09:38:34 +01:00
zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR"
zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR"
zaf_set_option ZAF_PLUGINS_REPO "$ZAF_PLUGINS_REPO"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR"
2016-03-24 09:38:34 +01:00
zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG"
zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD"
zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART"
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$"
2016-03-23 14:37:51 +01:00
}
2016-03-24 09:38:34 +01:00
if [ -f "${ZAF_CFG_FILE}" ]; then
. "${ZAF_CFG_FILE}"
fi
2016-03-24 15:46:42 +01:00
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$"
2016-03-24 09:38:34 +01:00
2016-03-23 14:37:51 +01:00
case $1 in
2016-03-30 16:09:02 +02:00
interactive)
zaf_configure interactive
2016-03-24 15:46:42 +01:00
$0 install
;;
2016-03-30 16:09:02 +02:00
silent)
2016-03-24 15:46:42 +01:00
zaf_configure silent
2016-03-30 16:09:02 +02:00
zaf_configure_agent "$@"
2016-03-23 14:37:51 +01:00
rm -rif ${ZAF_TMP_DIR}
2016-03-24 16:59:49 +01:00
mkdir -p ${ZAF_TMP_DIR}
mkdir -p ${ZAF_LIB_DIR}
mkdir -p ${ZAF_PLUGINS_DIR}
zaf_install $(zaf_getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR}/zaf.lib.sh
zaf_install $(zaf_getrest lib/jshn.sh) ${ZAF_LIB_DIR}/jshn.sh
zaf_install_exe $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR}/zaflock
2016-03-24 15:46:42 +01:00
mkdir -p ${ZAF_TMP_DIR}/p/zaf
2016-03-23 14:37:51 +01:00
mkdir -p ${ZAF_PLUGINS_DIR}
zaf_install_exe $(zaf_getrest zaf) /usr/bin/zaf
2016-03-24 20:37:03 +01:00
/usr/bin/zaf install zaf
2016-03-30 16:09:02 +02:00
if ! zaf_check_agent_config; then
2016-03-23 17:25:18 +01:00
echo "Something is wrong with zabbix agent config."
echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}"
echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside."
echo "Does ${ZAF_AGENT_RESTART} work?"
exit 1
fi
rm -rif ${ZAF_TMP_DIR}
2016-03-24 15:46:42 +01:00
echo "Install OK. Use 'zaf' without parameters to continue."
2016-03-23 14:37:51 +01:00
;;
2016-03-30 16:09:02 +02:00
*)
echo
echo "Please specify how to install."
echo "ZAF_CONFIG_OPTION=value [...] install.sh {silent|interactive} Z_option=value [...]"
echo "Example 1 (default install): install.sh silent"
echo 'Example 2 (preconfigure agent options): install.sh silent Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_Hostname=$(hostname)'
echo "Example 3 (preconfigure zaf packaging system to use): ZAF_PKG=dpkg install.sh silent"
echo "Example 4 (interactive): install.sh interactive"
echo
exit 1
2016-03-23 14:37:51 +01:00
esac