Skip to content

Commit fe17b36

Browse files
committed
Retry DNF operations
1 parent ed688a2 commit fe17b36

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

01_install_requirements.sh

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,8 @@ sudo dnf -y clean all
3939

4040
old_version=$(sudo dnf info NetworkManager | grep Version | cut -d ':' -f 2)
4141

42-
# Update to latest packages first
43-
# Number of attempts
44-
MAX_RETRIES=5
45-
# Delay between attempts (in seconds)
46-
_YUM_RETRY_BACKOFF=15
47-
48-
attempt=1
49-
while (( attempt <= MAX_RETRIES )); do
50-
if sudo dnf -y upgrade --nobest; then
51-
echo "System upgraded successfully."
52-
break
53-
else
54-
echo "Upgrade failed (attempt $attempt). Cleaning cache and retrying..."
55-
sudo dnf clean all
56-
sudo rm -rf /var/cache/dnf/*
57-
sleep $(( _YUM_RETRY_BACKOFF * attempt ))
58-
fi
59-
60-
(( attempt++ ))
61-
done
62-
63-
if (( attempt > MAX_RETRIES )); then
64-
echo "ERROR: Failed to upgrade system after $MAX_RETRIES attempts."
65-
exit 1
66-
fi
42+
dnf_with_retries -y upgrade --nobest
43+
echo "System upgraded successfully."
6744

6845
new_version=$(sudo dnf info NetworkManager | grep Version | cut -d ':' -f 2)
6946
# If NetworkManager was upgraded it needs to be restarted
@@ -80,42 +57,42 @@ source /etc/os-release
8057
# All of those are needed because we're still behind for OS support.
8158
# passlib needs to be installed as system dependency
8259
if [[ -x "/usr/libexec/platform-python" ]]; then
83-
sudo /usr/libexec/platform-python -m pip install passlib || sudo dnf -y install python3-pip && sudo /usr/libexec/platform-python -m pip install passlib
60+
sudo /usr/libexec/platform-python -m pip install passlib || sudo dnf_with_retries -y install --nobest python3-pip && sudo /usr/libexec/platform-python -m pip install passlib
8461
fi
8562

8663
# Install ansible, other packages are installed via
8764
# vm-setup/install-package-playbook.yml
8865
case $DISTRO in
8966
"centos8"|"rhel8"|"almalinux8"|"rocky8")
9067
# install network-scripts package to be able to use legacy network commands
91-
sudo dnf install -y network-scripts
68+
dnf_with_retries install -y --nobest network-scripts
9269
if [[ $DISTRO == "centos8" ]] && [[ "$NAME" != *"Stream"* ]]; then
9370
echo "CentOS is not supported, please switch to CentOS Stream / RHEL / Rocky / Alma"
9471
exit 1
9572
fi
9673
if [[ $DISTRO == "centos8" || $DISTRO == "almalinux8" || $DISTRO == "rocky8" ]]; then
97-
sudo dnf -y install epel-release dnf --enablerepo=extras
74+
dnf_with_retries -y install --nobest epel-release dnf --enablerepo=extras
9875
elif [[ $DISTRO == "rhel8" ]]; then
9976
# Enable EPEL for python3-passlib and python3-bcrypt required by metal3-dev-env
100-
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
77+
dnf_with_retries dnf -y install --nobest https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
10178
if sudo subscription-manager repos --list-enabled 2>&1 | grep "ansible-2-for-rhel-8-$(uname -m)-rpms"; then
10279
# The packaged 2.x ansible is too old for compatibility with metal3-dev-env
10380
sudo dnf erase -y ansible
10481
sudo subscription-manager repos --disable=ansible-2-for-rhel-8-$(uname -m)-rpms
10582
fi
10683
fi
10784
# Note recent ansible needs python >= 3.8 so we install 3.9 here
108-
sudo dnf -y install python39
85+
dnf_with_retries -y install --nobest python39
10986
sudo alternatives --set python /usr/bin/python3.9
11087
sudo alternatives --set python3 /usr/bin/python3.9
11188
sudo update-alternatives --install /usr/bin/pip3 pip3 /usr/bin/pip3.9 1
11289
PYTHON_DEVEL="python39-devel"
11390
;;
11491
"centos9"|"rhel9"|"almalinux9"|"rocky9")
115-
sudo dnf -y install python3-pip
92+
dnf_with_retries -y install --nobest python3-pip
11693
if [[ $DISTRO == "centos9" || $DISTRO == "almalinux9" || $DISTRO == "rocky9" ]] ; then
11794
sudo dnf config-manager --set-enabled crb
118-
sudo dnf -y install epel-release
95+
dnf_with_retries -y install --nobest epel-release
11996
elif [[ $DISTRO == "rhel9" ]]; then
12097
# NOTE(raukadah): If a system is subscribed to RHEL subscription then
12198
# sudo subscription-manager identity will return exit 0 else 1.
@@ -124,7 +101,7 @@ case $DISTRO in
124101
# enable the CRB repository
125102
sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
126103
fi
127-
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
104+
dnf_with_retries -y install --nobest https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
128105
fi
129106
sudo ln -s /usr/bin/python3 /usr/bin/python || true
130107
PYTHON_DEVEL="python3-devel"
@@ -150,7 +127,7 @@ GO_VERSION=${GO_VERSION:-1.22.3}
150127
GOARCH=$(uname -m)
151128
if [[ $GOARCH == "aarch64" ]]; then
152129
GOARCH="arm64"
153-
sudo dnf -y install $PYTHON_DEVEL libxml2-devel libxslt-devel
130+
dnf_with_retries -y install --nobest $PYTHON_DEVEL libxml2-devel libxslt-devel
154131
elif [[ $GOARCH == "x86_64" ]]; then
155132
GOARCH="amd64"
156133
fi
@@ -178,16 +155,16 @@ popd
178155

179156
if [ -n "${KNI_INSTALL_FROM_GIT}" ]; then
180157
# zip is required for building the installer from source
181-
sudo dnf -y install zip
158+
dnf_with_retries -y --nobest install zip
182159
fi
183160

184161
# Install nfs for persistent volumes
185162
if [ "${PERSISTENT_IMAGEREG}" == true ] ; then
186-
sudo dnf -y install nfs-utils
163+
dnf_with_retries -y --nobest install nfs-utils
187164
fi
188165

189166
if [[ "${NODES_PLATFORM}" == "baremetal" ]] ; then
190-
sudo dnf -y install ipmitool
167+
dnf_with_retries -y --nobest install ipmitool
191168
fi
192169

193170
# needed if we are using locally built images

agent/01_agent_requirements.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ fi
5151

5252
if [[ "${AGENT_E2E_TEST_BOOT_MODE}" == "ISCSI" ]]; then
5353
# Install shell to administer local storage
54-
sudo dnf -y install targetcli
54+
dnf_with_retries -y --nobest install targetcli
5555
fi
5656

5757
if [[ "${AGENT_E2E_TEST_BOOT_MODE}" == "ISO_NO_REGISTRY" ]]; then
58-
sudo dnf -y install xorriso coreos-installer syslinux skopeo
58+
dnf_with_retries -y --nobest install xorriso coreos-installer syslinux skopeo
5959
fi

utils.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ function retry_with_timeout() {
2828
return $(( exit_code ))
2929
}
3030

31+
# Run a dnf command with retries and cache cleaning
32+
dnf_with_retries() {
33+
local max_retries=5
34+
local delay=15
35+
local attempt=1
36+
37+
while (( attempt <= max_retries )); do
38+
echo "Attempt $attempt of $max_retries: sudo dnf $*"
39+
40+
if sudo dnf "$@"; then
41+
echo "sudo dnf $* succeeded."
42+
return 0
43+
fi
44+
45+
echo "sudo dnf $* failed on attempt $attempt."
46+
if (( attempt < max_retries )); then
47+
echo "Cleaning DNF cache and retrying after $delay seconds..."
48+
sudo dnf clean all || true
49+
sudo rm -rf /var/cache/dnf/* || true
50+
sleep "$delay"
51+
fi
52+
53+
(( attempt++ ))
54+
done
55+
56+
echo "ERROR: sudo dnf $* failed after $max_retries attempts."
57+
return 1
58+
}
59+
3160
function generate_assets() {
3261
rm -rf assets/generated && mkdir assets/generated
3362
for file in $(find assets/templates/ -iname '*.yaml' -type f -printf "%P\n"); do

0 commit comments

Comments
 (0)