Skip to content

Commit

Permalink
Merge pull request #2577 from wazuh/enhancement/1493-yum-apt-lock-man…
Browse files Browse the repository at this point in the history
…agement

APT and YUM lock logic added to WIA
  • Loading branch information
c-bordon authored Nov 3, 2023
2 parents 222ff2d + baf5e4f commit fc8ff97
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
6 changes: 5 additions & 1 deletion unattended_installer/common_functions/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function common_checkInstalled() {
dashboard_installed=""

if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
wazuh_installed=$(yum list installed 2>/dev/null | grep wazuh-manager)
elif [ "${sys_type}" == "apt-get" ]; then
wazuh_installed=$(apt list --installed 2>/dev/null | grep wazuh-manager)
Expand All @@ -81,6 +82,7 @@ function common_checkInstalled() {
fi

if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
indexer_installed=$(yum list installed 2>/dev/null | grep wazuh-indexer)
elif [ "${sys_type}" == "apt-get" ]; then
indexer_installed=$(apt list --installed 2>/dev/null | grep wazuh-indexer)
Expand All @@ -92,6 +94,7 @@ function common_checkInstalled() {
fi

if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
filebeat_installed=$(yum list installed 2>/dev/null | grep filebeat)
elif [ "${sys_type}" == "apt-get" ]; then
filebeat_installed=$(apt list --installed 2>/dev/null | grep filebeat)
Expand All @@ -103,6 +106,7 @@ function common_checkInstalled() {
fi

if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
dashboard_installed=$(yum list installed 2>/dev/null | grep wazuh-dashboard)
elif [ "${sys_type}" == "apt-get" ]; then
dashboard_installed=$(apt list --installed 2>/dev/null | grep wazuh-dashboard)
Expand Down Expand Up @@ -170,7 +174,7 @@ function common_remove_gpg_key() {
if [ "${sys_type}" == "yum" ]; then
if { rpm -q gpg-pubkey --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n' | grep "Wazuh"; } >/dev/null ; then
key=$(rpm -q gpg-pubkey --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n' | grep "Wazuh Signing Key" | awk '{print $1}' )
rpm -e "${key}" "${debug}"
rpm -e "${key}"
else
common_logger "Wazuh GPG key not found in the system"
return 1
Expand Down
82 changes: 66 additions & 16 deletions unattended_installer/install_functions/installCommon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,13 @@ function installCommon_aptInstall() {
installer=${package}
fi
command="DEBIAN_FRONTEND=noninteractive apt-get install ${installer} -y -q"
seconds=30
apt_output=$(eval "${command} 2>&1")
install_result="${PIPESTATUS[0]}"
eval "echo \${apt_output} ${debug}"
eval "tail -n 2 ${logfile} | grep -q 'Could not get lock'"
grep_result="${PIPESTATUS[0]}"
while [ "${grep_result}" -eq 0 ] && [ "${attempt}" -lt 10 ]; do
attempt=$((attempt+1))
common_logger "An external process is using APT. This process has to end to proceed with the Wazuh installation. Next retry in ${seconds} seconds (${attempt}/10)"
sleep "${seconds}"
installCommon_checkAptLock

if [ "${attempt}" -ne "${max_attempts}" ]; then
apt_output=$(eval "${command} 2>&1")
install_result="${PIPESTATUS[0]}"
eval "echo \${apt_output} ${debug}"
eval "tail -n 2 ${logfile} | grep -q 'Could not get lock'"
grep_result="${PIPESTATUS[0]}"
done
fi

}

Expand Down Expand Up @@ -189,6 +180,34 @@ function installCommon_checkOptionalInstallation() {

}

function installCommon_checkAptLock() {

attempt=0
seconds=30
max_attempts=10

while fuser "${apt_lockfile}" >/dev/null 2>&1 && [ "${attempt}" -lt "${max_attempts}" ]; do
attempt=$((attempt+1))
common_logger "Another process is using APT. Waiting for it to release the lock. Next retry in ${seconds} seconds (${attempt}/${max_attempts})"
sleep "${seconds}"
done

}

function installCommon_checkYumLock() {

attempt=0
seconds=30
max_attempts=10

while [ -f "${yum_lockfile}" ] && [ "${attempt}" -lt "${max_attempts}" ]; do
attempt=$((attempt+1))
common_logger "Another process is using YUM. Waiting for it to release the lock. Next retry in ${seconds} seconds (${attempt}/${max_attempts})"
sleep "${seconds}"
done

}

function installCommon_createCertificates() {

common_logger -d "Creating Wazuh certificates."
Expand Down Expand Up @@ -298,6 +317,7 @@ function installCommon_changePasswords() {
function installCommon_checkChromium() {

if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
if (! yum list installed 2>/dev/null | grep -q -E ^"google-chrome-stable"\\.) && (! yum list installed 2>/dev/null | grep -q -E ^"chromium"\\.); then
if [ "${DIST_NAME}" == "amzn" ]; then
installCommon_installChrome
Expand Down Expand Up @@ -609,8 +629,10 @@ function installCommon_rollBack() {
if [[ -n "${wazuh_installed}" && ( -n "${wazuh}" || -n "${AIO}" || -n "${uninstall}" ) ]];then
common_logger "Removing Wazuh manager."
if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
eval "yum remove wazuh-manager -y ${debug}"
elif [ "${sys_type}" == "apt-get" ]; then
installCommon_checkAptLock
eval "apt-get remove --purge wazuh-manager -y ${debug}"
fi
common_logger "Wazuh manager removed."
Expand All @@ -623,8 +645,10 @@ function installCommon_rollBack() {
if [[ -n "${indexer_installed}" && ( -n "${indexer}" || -n "${AIO}" || -n "${uninstall}" ) ]]; then
common_logger "Removing Wazuh indexer."
if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
eval "yum remove wazuh-indexer -y ${debug}"
elif [ "${sys_type}" == "apt-get" ]; then
installCommon_checkAptLock
eval "apt-get remove --purge wazuh-indexer -y ${debug}"
fi
common_logger "Wazuh indexer removed."
Expand All @@ -639,8 +663,10 @@ function installCommon_rollBack() {
if [[ -n "${filebeat_installed}" && ( -n "${wazuh}" || -n "${AIO}" || -n "${uninstall}" ) ]]; then
common_logger "Removing Filebeat."
if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
eval "yum remove filebeat -y ${debug}"
elif [ "${sys_type}" == "apt-get" ]; then
installCommon_checkAptLock
eval "apt-get remove --purge filebeat -y ${debug}"
fi
common_logger "Filebeat removed."
Expand All @@ -655,8 +681,10 @@ function installCommon_rollBack() {
if [[ -n "${dashboard_installed}" && ( -n "${dashboard}" || -n "${AIO}" || -n "${uninstall}" ) ]]; then
common_logger "Removing Wazuh dashboard."
if [ "${sys_type}" == "yum" ]; then
installCommon_checkYumLock
eval "yum remove wazuh-dashboard -y ${debug}"
elif [ "${sys_type}" == "apt-get" ]; then
installCommon_checkAptLock
eval "apt-get remove --purge wazuh-dashboard -y ${debug}"
fi
common_logger "Wazuh dashboard removed."
Expand Down Expand Up @@ -758,6 +786,7 @@ function installCommon_yumInstallList(){
dependencies=("$@")
not_installed=()
for dep in "${dependencies[@]}"; do
installCommon_checkYumLock
if ! yum list installed 2>/dev/null | grep -q -E ^"${dep}"\\.;then
not_installed+=("${dep}")
fi
Expand All @@ -767,14 +796,35 @@ function installCommon_yumInstallList(){
common_logger "--- Dependencies ---"
for dep in "${not_installed[@]}"; do
common_logger "Installing $dep."
yum_output=$(yum install ${dep} -y 2>&1)
installCommon_yumInstall "${dep}"
yum_code="${PIPESTATUS[0]}"

eval "echo \${yum_output} ${debug}"
if [ "${yum_code}" != 0 ]; then
if [ "${install_result}" != 0 ]; then
installCommon_checkOptionalInstallation
fi
done
fi

}

function installCommon_yumInstall() {

package="${1}"
version="${2}"
install_result=1
if [ -n "${version}" ]; then
installer="${package}-${version}"
else
installer="${package}"
fi

command="yum install ${installer} -y"
installCommon_checkYumLock

if [ "${attempt}" -ne "${max_attempts}" ]; then
yum_output=$(eval "${command} 2>&1")
install_result="${PIPESTATUS[0]}"
eval "echo \${yum_output} ${debug}"
fi

}
2 changes: 2 additions & 0 deletions unattended_installer/install_functions/installVariables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ readonly indexer_cert_path="/etc/wazuh-indexer/certs"

readonly logfile="/var/log/wazuh-install.log"
debug=">> ${logfile} 2>&1"
readonly yum_lockfile="/var/run/yum.pid"
readonly apt_lockfile="/var/lib/dpkg/lock"

## Offline Installation vars
readonly base_dest_folder="wazuh-offline"
Expand Down

0 comments on commit fc8ff97

Please sign in to comment.