add option to look for failed outout in retry function in so-common. look for Err: when running soapt-get update in setup

This commit is contained in:
m0duspwnens
2021-12-29 18:15:16 -05:00
parent b414e22e95
commit 200736a118
2 changed files with 41 additions and 31 deletions

View File

@@ -294,32 +294,42 @@ require_manager() {
} }
retry() { retry() {
maxAttempts=$1 maxAttempts=$1
sleepDelay=$2 sleepDelay=$2
cmd=$3 cmd=$3
expectedOutput=$4 expectedOutput=$4
attempt=0 failedOutput=$5
local exitcode=0 attempt=0
while [[ $attempt -lt $maxAttempts ]]; do local exitcode=0
attempt=$((attempt+1)) while [[ $attempt -lt $maxAttempts ]]; do
echo "Executing command with retry support: $cmd" attempt=$((attempt+1))
output=$(eval "$cmd") echo "Executing command with retry support: $cmd"
exitcode=$? output=$(eval "$cmd")
echo "Results: $output ($exitcode)" exitcode=$?
if [ -n "$expectedOutput" ]; then echo "Results: $output ($exitcode)"
if [[ "$output" =~ "$expectedOutput" ]]; then if [ -n "$expectedOutput" ]; then
return $exitCode if [[ "$output" =~ "$expectedOutput" ]]; then
else return $exitCode
echo "Expected '$expectedOutput' but got '$output'" else
fi echo "Expected '$expectedOutput' but got '$output'"
elif [[ $exitcode -eq 0 ]]; then fi
return $exitCode elif [ -n "$failedOutput" ]; then
fi if [[ "$output" =~ "$failedOutput" ]]; then
echo "Command failed with exit code $exitcode; will retry in $sleepDelay seconds ($attempt / $maxAttempts)..." echo "Found failed output '$failedOutput' in '$output'"
sleep $sleepDelay if [[ $exitcode -eq 0 ]]; then
done exitcode="${exitcode} (This is 0, but we found '$failedOutput' in the output.)"
echo "Command continues to fail; giving up." fi
return $exitcode else
return $exitCode
fi
elif [[ $exitcode -eq 0 ]]; then
return $exitCode
fi
echo "Command failed with exit code $exitcode; will retry in $sleepDelay seconds ($attempt / $maxAttempts)..."
sleep $sleepDelay
done
echo "Command continues to fail; giving up."
return $exitcode
} }
run_check_net_err() { run_check_net_err() {

View File

@@ -1147,7 +1147,7 @@ installer_prereq_packages() {
logCmd "systemctl start NetworkManager" logCmd "systemctl start NetworkManager"
elif [ "$OS" == ubuntu ]; then elif [ "$OS" == ubuntu ]; then
# Print message to stdout so the user knows setup is doing something # Print message to stdout so the user knows setup is doing something
retry 50 10 "apt-get update" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get update" "" "Err:" >> "$setup_log" 2>&1 || exit 1
# Install network manager so we can do interface stuff # Install network manager so we can do interface stuff
if ! command -v nmcli > /dev/null 2>&1; then if ! command -v nmcli > /dev/null 2>&1; then
retry 50 10 "apt-get -y install network-manager" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get -y install network-manager" >> "$setup_log" 2>&1 || exit 1
@@ -1204,12 +1204,12 @@ docker_install() {
else else
case "$install_type" in case "$install_type" in
'MANAGER' | 'EVAL' | 'STANDALONE' | 'MANAGERSEARCH' | 'IMPORT') 'MANAGER' | 'EVAL' | 'STANDALONE' | 'MANAGERSEARCH' | 'IMPORT')
retry 50 10 "apt-get update" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get update" "" "Err:" >> "$setup_log" 2>&1 || exit 1
;; ;;
*) *)
retry 50 10 "apt-key add $temp_install_dir/gpg/docker.pub" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-key add $temp_install_dir/gpg/docker.pub" >> "$setup_log" 2>&1 || exit 1
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" >> "$setup_log" 2>&1 add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" >> "$setup_log" 2>&1
retry 50 10 "apt-get update" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get update" "" "Err:" >> "$setup_log" 2>&1 || exit 1
;; ;;
esac esac
if [ $OSVER == "bionic" ]; then if [ $OSVER == "bionic" ]; then
@@ -2259,7 +2259,7 @@ saltify() {
# Add repo # Add repo
echo "deb https://packages.wazuh.com/3.x/apt/ stable main" > /etc/apt/sources.list.d/wazuh.list 2>> "$setup_log" echo "deb https://packages.wazuh.com/3.x/apt/ stable main" > /etc/apt/sources.list.d/wazuh.list 2>> "$setup_log"
retry 50 10 "apt-get update" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get update" "" "Err:" >> "$setup_log" 2>&1 || exit 1
set_progress_str 6 'Installing various dependencies' set_progress_str 6 'Installing various dependencies'
retry 50 10 "apt-get -y install sqlite3 libssl-dev" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get -y install sqlite3 libssl-dev" >> "$setup_log" 2>&1 || exit 1
set_progress_str 7 'Installing salt-master' set_progress_str 7 'Installing salt-master'
@@ -2279,7 +2279,7 @@ saltify() {
;; ;;
esac esac
retry 50 10 "apt-get update" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get update" "" "Err:" >> "$setup_log" 2>&1 || exit 1
set_progress_str 8 'Installing salt-minion & python modules' set_progress_str 8 'Installing salt-minion & python modules'
retry 50 10 "apt-get -y install salt-minion=3003+ds-1 salt-common=3003+ds-1" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-get -y install salt-minion=3003+ds-1 salt-common=3003+ds-1" >> "$setup_log" 2>&1 || exit 1
retry 50 10 "apt-mark hold salt-minion salt-common" >> "$setup_log" 2>&1 || exit 1 retry 50 10 "apt-mark hold salt-minion salt-common" >> "$setup_log" 2>&1 || exit 1