From 9ea71a7a19524f488e26f1bcab6fbd0b0843746a Mon Sep 17 00:00:00 2001 From: fcaffieri Date: Thu, 21 Jul 2022 20:59:00 -0300 Subject: [PATCH 1/2] Add try catch implementation to unattended installer --- unattended_installer/builder.sh | 6 ++++++ unattended_installer/install_functions/error.sh | 15 +++++++++++++++ .../install_functions/installMain.sh | 12 ++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 unattended_installer/install_functions/error.sh diff --git a/unattended_installer/builder.sh b/unattended_installer/builder.sh index 867b5f541b..2e89e00804 100755 --- a/unattended_installer/builder.sh +++ b/unattended_installer/builder.sh @@ -99,6 +99,12 @@ function buildInstaller() { ## JAVA_HOME echo "export JAVA_HOME=\"/usr/share/wazuh-indexer/jdk/\"" >> "${output_script_path}" + echo "set -o pipefail" >> "${output_script_path}" + echo "shopt -s expand_aliases" >> "${output_script_path}" + + echo "alias try=\"(set -e;\"" >> "${output_script_path}" + echo "alias catch=\" ); Error \\\$? || \"" >> "${output_script_path}" + ## Functions for all install function modules install_modules=($(find "${resources_installer}" -type f)) install_modules_names=($(eval "echo \"${install_modules[*]}\" | sed 's,${resources_installer}/,,g'")) diff --git a/unattended_installer/install_functions/error.sh b/unattended_installer/install_functions/error.sh new file mode 100644 index 0000000000..14b51b5b85 --- /dev/null +++ b/unattended_installer/install_functions/error.sh @@ -0,0 +1,15 @@ +# Wazuh installer - dashboard.sh functions. +# Copyright (C) 2015, Wazuh Inc. +# +# This program is a free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public +# License (version 2) as published by the FSF - Free Software +# Foundation. + +function Error() { + local retVal=$1 + if [[ $retVal -gt 0 ]] + then + return 1 + fi +} \ No newline at end of file diff --git a/unattended_installer/install_functions/installMain.sh b/unattended_installer/install_functions/installMain.sh index fd49ae3d19..0815606a35 100755 --- a/unattended_installer/install_functions/installMain.sh +++ b/unattended_installer/install_functions/installMain.sh @@ -291,10 +291,14 @@ function main() { manager_startCluster fi installCommon_startService "wazuh-manager" - filebeat_install - filebeat_configure - installCommon_changePasswords - installCommon_startService "filebeat" + try { + filebeat_install + filebeat_configure + installCommon_changePasswords + installCommon_startService "filebeat" + } catch { + installCommon_rollBack + } fi # -------------- AIO case ------------------------------------------ From 1bb6441e5381a8c2200fc59f439edcc5891f549d Mon Sep 17 00:00:00 2001 From: fcaffieri Date: Fri, 22 Jul 2022 19:42:30 -0300 Subject: [PATCH 2/2] Fix problem when grep cannot get value, its return status error, so all script failed --- .../common_functions/common.sh | 16 ++-- .../install_functions/installMain.sh | 89 ++++++++++++------- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/unattended_installer/common_functions/common.sh b/unattended_installer/common_functions/common.sh index 199d7ef630..bbba33f55a 100644 --- a/unattended_installer/common_functions/common.sh +++ b/unattended_installer/common_functions/common.sh @@ -68,9 +68,9 @@ function common_checkInstalled() { dashboard_installed="" if [ "${sys_type}" == "yum" ]; then - wazuh_installed=$(yum list installed 2>/dev/null | grep wazuh-manager) + wazuh_installed=$(yum list installed 2>/dev/null | grep wazuh-manager || true) elif [ "${sys_type}" == "apt-get" ]; then - wazuh_installed=$(apt list --installed 2>/dev/null | grep wazuh-manager) + wazuh_installed=$(apt list --installed 2>/dev/null | grep wazuh-manager || true) fi if [ -d "/var/ossec" ]; then @@ -78,9 +78,9 @@ function common_checkInstalled() { fi if [ "${sys_type}" == "yum" ]; then - indexer_installed=$(yum list installed 2>/dev/null | grep wazuh-indexer) + indexer_installed=$(yum list installed 2>/dev/null | grep wazuh-indexer || true) elif [ "${sys_type}" == "apt-get" ]; then - indexer_installed=$(apt list --installed 2>/dev/null | grep wazuh-indexer) + indexer_installed=$(apt list --installed 2>/dev/null | grep wazuh-indexer || true) fi if [ -d "/var/lib/wazuh-indexer/" ] || [ -d "/usr/share/wazuh-indexer" ] || [ -d "/etc/wazuh-indexer" ] || [ -f "${base_path}/search-guard-tlstool*" ]; then @@ -88,9 +88,9 @@ function common_checkInstalled() { fi if [ "${sys_type}" == "yum" ]; then - filebeat_installed=$(yum list installed 2>/dev/null | grep filebeat) + filebeat_installed=$(yum list installed 2>/dev/null | grep filebeat || true) elif [ "${sys_type}" == "apt-get" ]; then - filebeat_installed=$(apt list --installed 2>/dev/null | grep filebeat) + filebeat_installed=$(apt list --installed 2>/dev/null | grep filebeat || true) fi if [ -d "/var/lib/filebeat/" ] || [ -d "/usr/share/filebeat" ] || [ -d "/etc/filebeat" ]; then @@ -98,9 +98,9 @@ function common_checkInstalled() { fi if [ "${sys_type}" == "yum" ]; then - dashboard_installed=$(yum list installed 2>/dev/null | grep wazuh-dashboard) + dashboard_installed=$(yum list installed 2>/dev/null | grep wazuh-dashboard || true) elif [ "${sys_type}" == "apt-get" ]; then - dashboard_installed=$(apt list --installed 2>/dev/null | grep wazuh-dashboard) + dashboard_installed=$(apt list --installed 2>/dev/null | grep wazuh-dashboard || true) fi if [ -d "/var/lib/wazuh-dashboard/" ] || [ -d "/usr/share/wazuh-dashboard" ] || [ -d "/etc/wazuh-dashboard" ] || [ -d "/run/wazuh-dashboard/" ]; then diff --git a/unattended_installer/install_functions/installMain.sh b/unattended_installer/install_functions/installMain.sh index 0815606a35..1a757f224a 100755 --- a/unattended_installer/install_functions/installMain.sh +++ b/unattended_installer/install_functions/installMain.sh @@ -254,11 +254,17 @@ function main() { # -------------- Wazuh indexer case ------------------------------- if [ -n "${indexer}" ]; then - common_logger "--- Wazuh indexer ---" - indexer_install - indexer_configure - installCommon_startService "wazuh-indexer" - indexer_initialize + try { + common_logger "--- Wazuh indexer ---" + indexer_install + indexer_configure + installCommon_startService "wazuh-indexer" + indexer_initialize + } catch { + common_logger "Wazuh-indexer installation failed" + indexer_installed="true" + installCommon_rollBack + } fi # -------------- Start Wazuh indexer cluster case ------------------ @@ -273,11 +279,17 @@ function main() { if [ -n "${dashboard}" ]; then common_logger "--- Wazuh dashboard ----" - dashboard_install - dashboard_configure - installCommon_startService "wazuh-dashboard" - installCommon_changePasswords - dashboard_initialize + try { + dashboard_install + dashboard_configure + installCommon_startService "wazuh-dashboard" + installCommon_changePasswords + dashboard_initialize + } catch { + common_logger "Wazuh-dashboard installation failed" + dashboard_installed="true" + installCommon_rollBack + } fi @@ -285,18 +297,26 @@ function main() { if [ -n "${wazuh}" ]; then common_logger "--- Wazuh server ---" + try { + manager_install + if [ -n "${server_node_types[*]}" ]; then + manager_startCluster + fi + installCommon_startService "wazuh-manager" + } catch { + common_logger "Wazuh-manager installation failed" + wazuh_installed="true" + installCommon_rollBack + } - manager_install - if [ -n "${server_node_types[*]}" ]; then - manager_startCluster - fi - installCommon_startService "wazuh-manager" try { filebeat_install filebeat_configure installCommon_changePasswords installCommon_startService "filebeat" } catch { + common_logger "Filebeat installation failed" + filebeat_installed="true" installCommon_rollBack } fi @@ -305,23 +325,28 @@ function main() { if [ -n "${AIO}" ]; then - common_logger "--- Wazuh indexer ---" - indexer_install - indexer_configure - installCommon_startService "wazuh-indexer" - indexer_initialize - common_logger "--- Wazuh server ---" - manager_install - installCommon_startService "wazuh-manager" - filebeat_install - filebeat_configure - installCommon_startService "filebeat" - common_logger "--- Wazuh dashboard ---" - dashboard_install - dashboard_configure - installCommon_startService "wazuh-dashboard" - installCommon_changePasswords - dashboard_initializeAIO + try { + common_logger "--- Wazuh indexer ---" + indexer_install + indexer_configure + installCommon_startService "wazuh-indexer" + indexer_initialize + common_logger "--- Wazuh server ---" + manager_install + installCommon_startService "wazuh-manager" + filebeat_install + filebeat_configure + installCommon_startService "filebeat" + common_logger "--- Wazuh dashboard ---" + dashboard_install + dashboard_configure + installCommon_startService "wazuh-dashboard" + installCommon_changePasswords + dashboard_initializeAIO + } catch { + common_logger "AIO installation failed" + installCommon_rollBack + } fi