Fixes multiple issues with retry function in rhel-registration.

There were multiple issues in retry() in rhel-registration:
 - There was no need for it to be recursive (local variables
   got overwritten)
 - There was no delay between multiple attempts, leading to faster but
   more frequent failures.
 - The max number of attempts was set too low for some environements.

With this patch, rhel-registration now works more reliably with slow-links
for portal registration and does not attempt to DDos the portal or your
satellite server.

Change-Id: I594d3c94867b45a7a58766dbcc66edead78d6a4e
This commit is contained in:
Vincent S. Cojot 2017-03-13 14:39:16 -04:00
parent a681b98564
commit 038eae0891

View File

@ -11,7 +11,7 @@ if [ -e $OK ] ; then
exit 0
fi
retryCount=0
retry_max_count=10
opts=
config_opts=
attach_opts=
@ -157,27 +157,41 @@ else
fi
function retry() {
if [[ $retryCount < 3 ]]; then
$@
if ! [[ $? == 0 ]]; then
retryCount=$(echo $retryCount + 1 | bc)
echo "WARN: Failed to connect when running '$@', retrying..."
retry $@
else
retryCount=0
# Inhibit -e since we want to retry without exiting..
set +e
# Retry delay (seconds)
retry_delay=2.0
retry_count=0
mycli="$@"
while [ $retry_count -lt ${retry_max_count} ]
do
echo "INFO: Sleeping ${retry_delay} ..."
sleep ${retry_delay}
echo "INFO: Executing '${mycli}' ..."
${mycli}
if [ $? -eq 0 ]; then
echo "INFO: Ran '${mycli}' successfully, not retrying..."
break
else
echo "WARN: Failed to connect when running '${mycli}', retrying (attempt #$retry_count )..."
retry_count=$(echo $retry_count + 1 | bc)
fi
done
if [ $retry_count -ge ${retry_max_count} ]; then
echo "ERROR: Failed to connect after ${retry_max_count} attempts when running '${mycli}'"
exit 1
fi
else
echo "ERROR: Failed to connect after 3 attempts when running '$@'"
exit 1
fi
# Re-enable -e when exiting retry()
set -e
}
function detect_satellite_version {
ping_api=$REG_SAT_URL/katello/api/ping
if curl --retry 3 --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $ping_api | grep "200 OK"; then
if curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $ping_api | grep "200 OK"; then
echo Satellite 6 detected at $REG_SAT_URL
satellite_version=6
elif curl --retry 3 --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $REG_SAT_URL/rhn/Login.do | grep "200 OK"; then
elif curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -s -D - -o /dev/null $REG_SAT_URL/rhn/Login.do | grep "200 OK"; then
echo Satellite 5 detected at $REG_SAT_URL
satellite_version=5
else
@ -220,7 +234,7 @@ case "${REG_METHOD:-}" in
detect_satellite_version
if [ "$satellite_version" = "6" ]; then
repos="$repos --enable ${satellite_repo}"
curl --retry 3 --retry-delay 10 --max-time 30 -L -k -O "$REG_SAT_URL/pub/katello-ca-consumer-latest.noarch.rpm"
curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -L -k -O "$REG_SAT_URL/pub/katello-ca-consumer-latest.noarch.rpm"
rpm -Uvh katello-ca-consumer-latest.noarch.rpm || true
retry subscription-manager register $opts
retry subscription-manager $repos
@ -229,7 +243,7 @@ case "${REG_METHOD:-}" in
retry subscription-manager repos --disable ${satellite_repo}
else
pushd /usr/share/rhn/
curl --retry 3 --retry-delay 10 --max-time 30 -k -O $REG_SAT_URL/pub/RHN-ORG-TRUSTED-SSL-CERT
curl --retry ${retry_max_count} --retry-delay 10 --max-time 30 -k -O $REG_SAT_URL/pub/RHN-ORG-TRUSTED-SSL-CERT
popd
retry rhnreg_ks --serverUrl=$REG_SAT_URL/XMLRPC $sat5_opts
fi