diff --git a/unattended_installer/install_functions/checks.sh b/unattended_installer/install_functions/checks.sh index e6a52fa4c4..a0a9815245 100644 --- a/unattended_installer/install_functions/checks.sh +++ b/unattended_installer/install_functions/checks.sh @@ -207,8 +207,11 @@ function check_dist() { if [ "${DIST_NAME}" == "centos" ] && { [ "${DIST_VER}" -ne "7" ] && [ "${DIST_VER}" -ne "8" ]; }; then notsupported=1 fi - if [ "${DIST_NAME}" == "rhel" ] && { [ "${DIST_VER}" -ne "7" ] && [ "${DIST_VER}" -ne "8" ] && [ "${DIST_VER}" -ne "9" ]; }; then - notsupported=1 + if [ "${DIST_NAME}" == "rhel" ]; then + if [ "${DIST_VER}" -ne "7" ] && [ "${DIST_VER}" -ne "8" ] && [ "${DIST_VER}" -ne "9" ]; then + notsupported=1 + fi + need_centos_repos=1 fi if [ "${DIST_NAME}" == "amzn" ]; then @@ -361,11 +364,11 @@ function checks_previousCertificate() { function checks_specialDepsAL2023() { # Change curl for curl-minimal - wia_yum_dependencies=( "${wia_yum_dependencies[@]/curl/curl-minimal}" ) + assistant_yum_dependencies=( "${assistant_yum_dependencies[@]/curl/curl-minimal}" ) # In containers, coreutils is replaced for coreutils-single if [ -f "/.dockerenv" ]; then - wia_yum_dependencies=( "${wia_yum_dependencies[@]/coreutils/coreutils-single}" ) + assistant_yum_dependencies=( "${assistant_yum_dependencies[@]/coreutils/coreutils-single}" ) fi } @@ -377,18 +380,7 @@ function checks_specifications() { } function checks_ports() { - - dep="lsof" - if [ "${sys_type}" == "yum" ]; then - installCommon_yumInstallList "${dep}" - elif [ "${sys_type}" == "apt-get" ]; then - installCommon_aptInstallList "${dep}" - fi - - if [ "${#not_installed[@]}" -gt 0 ]; then - wia_dependencies_installed+=("${dep}") - fi - + common_logger -d "Checking ports availability." used_port=0 ports=("$@") diff --git a/unattended_installer/install_functions/installCommon.sh b/unattended_installer/install_functions/installCommon.sh index d31ca6edc9..8297ce4ece 100644 --- a/unattended_installer/install_functions/installCommon.sh +++ b/unattended_installer/install_functions/installCommon.sh @@ -115,37 +115,6 @@ function installCommon_aptInstall() { } -function installCommon_aptInstallList(){ - - dependencies=("$@") - not_installed=() - - for dep in "${dependencies[@]}"; do - if ! dpkg -l "${dep}" 2>/dev/null | grep -q -E '^ii\s'; then - not_installed+=("${dep}") - for wia_dep in "${wia_apt_dependencies[@]}"; do - if [ "${wia_dep}" == "${dep}" ]; then - wia_dependencies_installed+=("${dep}") - fi - done - fi - done - - if [ "${#not_installed[@]}" -gt 0 ]; then - common_logger "--- Dependencies ----" - for dep in "${not_installed[@]}"; do - common_logger "Installing $dep." - installCommon_aptInstall "${dep}" - if [ "${install_result}" != 0 ]; then - common_logger -e "Cannot install dependency: ${dep}." - installCommon_rollBack - exit 1 - fi - done - fi - -} - function installCommon_changePasswordApi() { common_logger -d "Changing API passwords." @@ -228,18 +197,6 @@ function installCommon_createInstallFiles() { if eval "mkdir /tmp/wazuh-install-files ${debug}"; then common_logger "Generating configuration files." - - dep="openssl" - if [ "${sys_type}" == "yum" ]; then - installCommon_yumInstallList "${dep}" - elif [ "${sys_type}" == "apt-get" ]; then - installCommon_aptInstallList "${dep}" - fi - - if [ "${#not_installed[@]}" -gt 0 ]; then - wia_dependencies_installed+=("${dep}") - fi - if [ -n "${configurations}" ]; then cert_checkOpenSSL fi @@ -253,7 +210,7 @@ function installCommon_createInstallFiles() { eval "chown root:root /tmp/wazuh-install-files/* ${debug}" eval "tar -zcf '${tar_file}' -C '/tmp/' wazuh-install-files/ ${debug}" eval "rm -rf '/tmp/wazuh-install-files' ${debug}" - eval "rm -rf ${config_file} ${debug}" + eval "rm -rf ${config_file} ${debug}" common_logger "Created ${tar_file_name}. It contains the Wazuh cluster key, certificates, and passwords necessary for installation." else common_logger -e "Unable to create /tmp/wazuh-install-files" @@ -308,7 +265,6 @@ function installCommon_changePasswords() { # Adds the CentOS repository to install lsof. function installCommon_configureCentOSRepositories() { - centos_repos_configured=1 centos_key="/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial" eval "common_curl -sLo ${centos_key} 'https://www.centos.org/keys/RPM-GPG-KEY-CentOS-Official' --max-time 300 --retry 5 --retry-delay 5 --fail" @@ -333,6 +289,21 @@ function installCommon_configureCentOSRepositories() { } +function installCommon_determinePorts { + + used_ports=() + + if [ -n "${AIO}" ]; then + used_ports+=( "${wazuh_aio_ports[@]}" ) + elif [ -n "${wazuh}" ]; then + used_ports+=( "${wazuh_manager_ports[@]}" ) + elif [ -n "${indexer}" ]; then + used_ports+=( "${wazuh_indexer_ports[@]}" ) + elif [ -n "${dashboard}" ]; then + used_ports+=( "${wazuh_dashboard_port[@]}" ) + fi +} + function installCommon_extractConfig() { common_logger -d "Extracting Wazuh configuration." @@ -369,62 +340,50 @@ function installCommon_getPass() { done } -function installCommon_installCheckDependencies() { - - common_logger -d "Installing check dependencies." - if [ "${sys_type}" == "yum" ]; then - if [[ "${DIST_NAME}" == "rhel" ]] && [[ "${DIST_VER}" == "8" || "${DIST_VER}" == "9" ]]; then - installCommon_configureCentOSRepositories - fi - installCommon_yumInstallList "${wia_yum_dependencies[@]}" +function installCommon_installDependencies() { - # In RHEL cases, remove the CentOS repositories configuration - if [ "${centos_repos_configured}" == 1 ]; then - installCommon_removeCentOSrepositories - fi - - elif [ "${sys_type}" == "apt-get" ]; then - eval "apt-get update -q ${debug}" - installCommon_aptInstallList "${wia_apt_dependencies[@]}" + if [ "${1}" == "assistant" ]; then + installing_assistant_deps=1 + assistant_deps_installed=() + installCommon_installList "${assistant_deps_to_install[@]}" + else + installing_assistant_deps=0 + installCommon_installList "${wazuh_deps_to_install[@]}" fi - } -function installCommon_installPrerequisites() { +function installCommon_installList(){ + + dependencies=("$@") + if [ "${#dependencies[@]}" -gt 0 ]; then - message="Installing prerequisites dependencies." - if [ "${sys_type}" == "yum" ]; then - if [ "${1}" == "AIO" ]; then - deps=($(echo "${indexer_yum_dependencies[@]}" "${dashboard_yum_dependencies[@]}" | tr ' ' '\n' | sort -u)) - common_logger -d "${message}" - installCommon_yumInstallList "${deps[@]}" - fi - if [ "${1}" == "indexer" ]; then - common_logger -d "${message}" - installCommon_yumInstallList "${indexer_yum_dependencies[@]}" - fi - if [ "${1}" == "dashboard" ]; then - common_logger -d "${message}" - installCommon_yumInstallList "${dashboard_yum_dependencies[@]}" - fi - elif [ "${sys_type}" == "apt-get" ]; then - eval "apt-get update -q ${debug}" - if [ "${1}" == "AIO" ]; then - deps=($(echo "${wazuh_apt_dependencies[@]}" "${indexer_apt_dependencies[@]}" "${dashboard_apt_dependencies[@]}" | tr ' ' '\n' | sort -u)) - common_logger -d "${message}" - installCommon_aptInstallList "${deps[@]}" - fi - if [ "${1}" == "indexer" ]; then - common_logger -d "${message}" - installCommon_aptInstallList "${indexer_apt_dependencies[@]}" + if [ -n "${need_centos_repos}" ]; then + installCommon_configureCentOSRepositories fi - if [ "${1}" == "dashboard" ]; then - common_logger -d "${message}" - installCommon_aptInstallList "${dashboard_apt_dependencies[@]}" + if [ "${sys_type}" == "apt-get" ]; then + eval "apt-get update -q ${debug}" fi - if [ "${1}" == "wazuh" ]; then - common_logger -d "${message}" - installCommon_aptInstallList "${wazuh_apt_dependencies[@]}" + + common_logger "--- Dependencies ----" + for dep in "${dependencies[@]}"; do + common_logger "Installing $dep." + if [ "${sys_type}" = "apt-get" ]; then + installCommon_aptInstall "${dep}" + else + installCommon_yumInstall "${dep}" + fi + if [ "${install_result}" != 0 ]; then + common_logger -e "Cannot install dependency: ${dep}." + installCommon_rollBack + exit 1 + fi + if [ "${installing_assistant_deps}" == 1 ]; then + assistant_deps_installed+=("${dep}") + fi + done + # In RHEL cases, remove the CentOS repositories configuration + if [ -n "${need_centos_repos}" ]; then + installCommon_removeCentOSrepositories fi fi @@ -571,7 +530,6 @@ function installCommon_removeCentOSrepositories() { eval "rm -f ${centos_repo} ${debug}" eval "rm -f ${centos_key} ${debug}" eval "yum clean all ${debug}" - centos_repos_configured=0 common_logger -d "CentOS repositories and key deleted." } @@ -590,6 +548,11 @@ function installCommon_rollBack() { eval "rm /etc/apt/sources.list.d/wazuh.list ${debug}" fi + # In RHEL cases, remove the CentOS repositories configuration + if [ -n "${need_centos_repos}" ]; then + installCommon_removeCentOSrepositories + fi + if [[ -n "${wazuh_installed}" && ( -n "${wazuh}" || -n "${AIO}" || -n "${uninstall}" ) ]];then common_logger "Removing Wazuh manager." if [ "${sys_type}" == "yum" ]; then @@ -722,7 +685,7 @@ function installCommon_rollBack() { common_remove_gpg_key - installCommon_removeWIADependencies + installCommon_removeAssistantDependencies eval "systemctl daemon-reload ${debug}" @@ -736,6 +699,90 @@ function installCommon_rollBack() { } +function installCommon_scanDependencies() { + + wazuh_deps=() + if [ -n "${AIO}" ]; then + if [ "${sys_type}" == "yum" ]; then + wazuh_deps+=( "${indexer_yum_dependencies[@]}" "${wazuh_yum_dependencies[@]}" "${dashboard_yum_dependencies[@]}" ) + else + wazuh_deps+=( "${indexer_apt_dependencies[@]}" "${wazuh_apt_dependencies[@]}" "${dashboard_apt_dependencies[@]}" ) + fi + elif [ -n "${indexer}" ]; then + if [ "${sys_type}" == "yum" ]; then + wazuh_deps+=( "${indexer_yum_dependencies[@]}" ) + else + wazuh_deps+=( "${indexer_apt_dependencies[@]}" ) + fi + elif [ -n "${wazuh}" ]; then + if [ "${sys_type}" == "yum" ]; then + wazuh_deps+=( "${wazuh_yum_dependencies[@]}" ) + else + wazuh_deps+=( "${wazuh_apt_dependencies[@]}" ) + fi + elif [ -n "${dashboard}" ]; then + if [ "${sys_type}" == "yum" ]; then + wazuh_deps+=( "${dashboard_yum_dependencies[@]}" ) + else + wazuh_deps+=( "${dashboard_apt_dependencies[@]}" ) + fi + fi + + all_deps=( "${wazuh_deps[@]}" ) + if [ "${sys_type}" == "apt-get" ]; then + assistant_deps+=( "${assistant_apt_dependencies[@]}" ) + command='! apt list --installed 2>/dev/null | grep -q -E ^"${dep}"\/' + else + assistant_deps+=( "${assistant_yum_dependencies[@]}" ) + command='! rpm -q ${dep} --quiet' + fi + + # Remove openssl dependency if not necessary + if [ -z "${configurations}" ] && [ -z "${AIO}" ]; then + assistant_deps=( "${assistant_deps[@]/openssl}" ) + fi + + # Remove lsof dependency if not necessary + if [ -z "${AIO}" ] && [ -z "${wazuh}" ] && [ -z "${indexer}" ] && [ -z "${dashboard}" ]; then + assistant_deps=( "${assistant_deps[@]/lsof}" ) + fi + + # Delete duplicates and sort + all_deps+=( "${assistant_deps[@]}" ) + all_deps=( $(echo "${all_deps[@]}" | tr ' ' '\n' | sort -u) ) + assistant_deps_to_install=() + deps_to_install=() + + # Get not installed dependencies of Assistant and Wazuh + for dep in "${all_deps[@]}"; do + if eval "${command}"; then + deps_to_install+=("${dep}") + if [[ "${assistant_deps[*]}" =~ "${dep}" ]]; then + assistant_deps_to_install+=("${dep}") + else + wazuh_deps_to_install+=("${dep}") + fi + fi + done + + # Format and print the message if the option is not specified + if [ -z "${install_dependencies}" ] && [ "${#deps_to_install[@]}" -gt 0 ]; then + printf -v joined_deps_not_installed '%s, ' "${deps_to_install[@]}" + printf -v joined_assistant_not_installed '%s, ' "${assistant_deps_to_install[@]}" + joined_deps_not_installed="${joined_deps_not_installed%, }" + joined_assistant_not_installed="${joined_assistant_not_installed%, }" + + message="To perform the installation, the following package/s must be installed: ${joined_deps_not_installed}." + if [ "${#assistant_deps_to_install[@]}" -gt 0 ]; then + message+=" The following package/s will be removed after the installation: ${joined_assistant_not_installed}." + fi + message+=" Add the -id|--install-dependencies parameter to install them automatically or install them manually." + common_logger -w "${message}" + exit 1 + fi + +} + function installCommon_startService() { if [ "$#" -ne 1 ]; then @@ -794,82 +841,22 @@ function installCommon_startService() { } -function installCommon_yumInstallList(){ - - dependencies=("$@") - not_installed=() - for dep in "${dependencies[@]}"; do - if ! rpm -q "${dep}" --quiet;then - not_installed+=("${dep}") - for wia_dep in "${wia_yum_dependencies[@]}"; do - if [ "${wia_dep}" == "${dep}" ]; then - wia_dependencies_installed+=("${dep}") - fi - done - fi - done +function installCommon_removeAssistantDependencies(){ - if [ "${#not_installed[@]}" -gt 0 ]; then + if [ "${#assistant_deps_installed[@]}" -gt 0 ]; then common_logger "--- Dependencies ---" - for dep in "${not_installed[@]}"; do - common_logger "Installing $dep." - installCommon_yumInstall "${dep}" - yum_code="${PIPESTATUS[0]}" - - eval "echo \${yum_output} ${debug}" - if [ "${yum_code}" != 0 ]; then - common_logger -e "Cannot install dependency: ${dep}." - installCommon_rollBack - exit 1 - fi - done - fi - -} - -function installCommon_removeWIADependencies() { - - if [ "${sys_type}" == "yum" ]; then - installCommon_yumRemoveWIADependencies - elif [ "${sys_type}" == "apt-get" ]; then - installCommon_aptRemoveWIADependencies - fi - -} - -function installCommon_yumRemoveWIADependencies(){ - - if [ "${#wia_dependencies_installed[@]}" -gt 0 ]; then - common_logger "--- Dependencies ---" - for dep in "${wia_dependencies_installed[@]}"; do + for dep in "${assistant_deps_installed[@]}"; do if [ "${dep}" != "systemd" ]; then common_logger "Removing $dep." - yum_output=$(yum remove ${dep} -y 2>&1) - yum_code="${PIPESTATUS[0]}" - eval "echo \${yum_output} ${debug}" - if [ "${yum_code}" != 0 ]; then - common_logger -e "Cannot remove dependency: ${dep}." - exit 1 + if [ "${sys_type}" == "yum" ]; then + pm_output=$(yum remove ${dep} -y 2>&1) + else + pm_output=$(apt-get remove --purge ${dep} -y 2>&1) fi - fi - done - fi - -} - -function installCommon_aptRemoveWIADependencies(){ - - if [ "${#wia_dependencies_installed[@]}" -gt 0 ]; then - common_logger "--- Dependencies ----" - for dep in "${wia_dependencies_installed[@]}"; do - if [ "${dep}" != "systemd" ]; then - common_logger "Removing $dep." - apt_output=$(apt-get remove --purge ${dep} -y 2>&1) - apt_code="${PIPESTATUS[0]}" - - eval "echo \${apt_output} ${debug}" - if [ "${apt_code}" != 0 ]; then + pm_code="${PIPESTATUS[0]}" + eval "echo \${pm_output} ${debug}" + if [ "${pm_code}" != 0 ]; then common_logger -e "Cannot remove dependency: ${dep}." exit 1 fi @@ -878,6 +865,7 @@ function installCommon_aptRemoveWIADependencies(){ fi } + function installCommon_yumInstall() { package="${1}" diff --git a/unattended_installer/install_functions/installMain.sh b/unattended_installer/install_functions/installMain.sh index 39f82ce430..b3cbe649ca 100755 --- a/unattended_installer/install_functions/installMain.sh +++ b/unattended_installer/install_functions/installMain.sh @@ -37,6 +37,9 @@ function getHelp() { echo -e " -i, --ignore-check" echo -e " Ignore the check for minimum hardware requirements." echo -e "" + echo -e " -id, --install-dependencies" + echo -e " Installs automatically the necessary dependencies for the installation." + echo -e "" echo -e " -o, --overwrite" echo -e " Overwrites previously installed components. This will erase all the existing configuration and data." echo -e "" @@ -113,6 +116,10 @@ function main() { ignore=1 shift 1 ;; + "-id"|"--install-dependencies") + install_dependencies=1 + shift 1 + ;; "-o"|"--overwrite") overwrite=1 shift 1 @@ -228,12 +235,6 @@ function main() { check_dist fi - if [ -z "${uninstall}" ] && [ -z "${offline_install}" ]; then - installCommon_installCheckDependencies - elif [ -n "${offline_install}" ]; then - offline_checkDependencies - fi - common_checkInstalled checks_arguments if [ -n "${uninstall}" ]; then @@ -242,23 +243,6 @@ function main() { fi checks_arch - if [ -n "${ignore}" ]; then - common_logger -w "Hardware checks ignored." - else - common_logger "Verifying that your system meets the recommended minimum hardware requirements." - checks_health - fi - -# -------------- Preliminary checks and Prerequisites -------------------------------- - - if [ -z "${uninstall}" ]; then - installCommon_installCheckDependencies - fi - - if [ -z "${configurations}" ] && [ -z "${AIO}" ] && [ -z "${download}" ]; then - checks_previousCertificate - fi - if [ -n "${port_specified}" ]; then checks_available_port "${port_number}" "${wazuh_aio_ports[@]}" dashboard_changePort "${port_number}" @@ -266,27 +250,34 @@ function main() { dashboard_changePort "${http_port}" fi - if [ -n "${AIO}" ]; then - rm -f "${tar_file}" - checks_ports "${wazuh_aio_ports[@]}" - installCommon_installPrerequisites "AIO" + if [ -z "${uninstall}" ] && [ -z "${offline_install}" ]; then + installCommon_scanDependencies + installCommon_installDependencies "assistant" + installCommon_determinePorts + elif [ -n "${offline_install}" ]; then + offline_checkDependencies fi - if [ -n "${indexer}" ]; then - checks_ports "${wazuh_indexer_ports[@]}" - installCommon_installPrerequisites "indexer" + if [ -n "${ignore}" ]; then + common_logger -w "Hardware checks ignored." + else + common_logger "Verifying that your system meets the recommended minimum hardware requirements." + checks_health fi - if [ -n "${wazuh}" ]; then - checks_ports "${wazuh_manager_ports[@]}" - installCommon_installPrerequisites "wazuh" - fi +# -------------- Preliminary checks and Prerequisites -------------------------------- - if [ -n "${dashboard}" ]; then - checks_ports "${wazuh_dashboard_port}" - installCommon_installPrerequisites "dashboard" + if [ -z "${configurations}" ] && [ -z "${AIO}" ] && [ -z "${download}" ]; then + checks_previousCertificate fi + if [ -n "${AIO}" ] || [ -n "${indexer}" ] || [ -n "${wazuh}" ] || [ -n "${dashboard}" ]; then + if [ -n "${AIO}" ]; then + rm -f "${tar_file}" + fi + checks_ports "${used_ports[@]}" + installCommon_installDependencies + fi # -------------- Wazuh repo ---------------------- @@ -323,7 +314,7 @@ function main() { fi if [ -n "${configurations}" ]; then - installCommon_removeWIADependencies + installCommon_removeAssistantDependencies fi # -------------- Wazuh indexer case ------------------------------- @@ -334,7 +325,7 @@ function main() { indexer_configure installCommon_startService "wazuh-indexer" indexer_initialize - installCommon_removeWIADependencies + installCommon_removeAssistantDependencies fi # -------------- Start Wazuh indexer cluster case ------------------ @@ -342,7 +333,7 @@ function main() { if [ -n "${start_indexer_cluster}" ]; then indexer_startCluster installCommon_changePasswords - installCommon_removeWIADependencies + installCommon_removeAssistantDependencies fi # -------------- Wazuh dashboard case ------------------------------ @@ -354,7 +345,7 @@ function main() { installCommon_startService "wazuh-dashboard" installCommon_changePasswords dashboard_initialize - installCommon_removeWIADependencies + installCommon_removeAssistantDependencies fi @@ -372,7 +363,7 @@ function main() { filebeat_configure installCommon_changePasswords installCommon_startService "filebeat" - installCommon_removeWIADependencies + installCommon_removeAssistantDependencies fi # -------------- AIO case ------------------------------------------ @@ -397,7 +388,7 @@ function main() { installCommon_startService "wazuh-dashboard" installCommon_changePasswords dashboard_initializeAIO - installCommon_removeWIADependencies + installCommon_removeAssistantDependencies fi diff --git a/unattended_installer/install_functions/installVariables.sh b/unattended_installer/install_functions/installVariables.sh index 9a411aa7c7..21939cfbc6 100644 --- a/unattended_installer/install_functions/installVariables.sh +++ b/unattended_installer/install_functions/installVariables.sh @@ -56,13 +56,12 @@ wazuh_aio_ports=( 9200 9300 1514 1515 1516 55000 "${http_port}") readonly wazuh_indexer_ports=( 9200 9300 ) readonly wazuh_manager_ports=( 1514 1515 1516 55000 ) wazuh_dashboard_port="${http_port}" -# `lsof` and `openssl` are installed separately -wia_yum_dependencies=( systemd grep tar coreutils sed procps-ng gawk curl ) -readonly wia_apt_dependencies=( systemd grep tar coreutils sed procps gawk curl ) +assistant_yum_dependencies=( systemd grep tar coreutils sed procps-ng gawk curl lsof openssl ) +readonly assistant_apt_dependencies=( systemd grep tar coreutils sed procps gawk curl lsof openssl ) readonly wazuh_yum_dependencies=( libcap ) readonly wazuh_apt_dependencies=( apt-transport-https libcap2-bin software-properties-common gnupg ) readonly indexer_yum_dependencies=( coreutils ) readonly indexer_apt_dependencies=( debconf adduser procps gnupg apt-transport-https ) readonly dashboard_yum_dependencies=( libcap ) readonly dashboard_apt_dependencies=( debhelper tar curl libcap2-bin gnupg apt-transport-https ) -wia_dependencies_installed=() +assistant_deps_to_install=()