Skip to content

Commit 7a85ed9

Browse files
authored
Merge pull request #534 from imperva/fix-azure-new
fix: use [[ ]] in bash conditionals and improve yum retry logic
2 parents 1480ad7 + b420e20 commit 7a85ed9

2 files changed

Lines changed: 35 additions & 24 deletions

File tree

.github/workflows/dsf_poc_cli_azure.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ jobs:
289289
run: |
290290
terraform -chdir=$EXAMPLE_DIR workspace list
291291
# Only pass DEPLOYMENT_VERSION if it's not empty
292-
if [ -n "${{ env.DEPLOYMENT_VERSION }}" && ${{ env.DEPLOYMENT_VERSION }} != $'\n' ]; then
293-
terraform -chdir=$EXAMPLE_DIR plan -var dam_license=license.mprv -var ${{ env.DEPLOYMENT_VERSION }}
294-
else
295-
terraform -chdir=$EXAMPLE_DIR plan -var dam_license=license.mprv
296-
fi
292+
if [[ -n "${{ env.DEPLOYMENT_VERSION }}" && "${{ env.DEPLOYMENT_VERSION }}" != $'\n' ]]; then
293+
terraform -chdir=$EXAMPLE_DIR plan -var dam_license=license.mprv -var "${{ env.DEPLOYMENT_VERSION }}"
294+
else
295+
terraform -chdir=$EXAMPLE_DIR plan -var dam_license=license.mprv
296+
fi
297297
298298

299299
# On push to "main", build or change infrastructure according to Terraform configuration files
@@ -302,8 +302,8 @@ jobs:
302302
id: apply
303303
# if: github.ref == 'refs/heads/"master"' && github.event_name == 'push' || github.event_name == 'workflow_dispatch'
304304
run: |
305-
if [ -n "${{ env.DEPLOYMENT_VERSION }}" && ${{ env.DEPLOYMENT_VERSION }} != $'\n' ]; then
306-
terraform -chdir=$EXAMPLE_DIR apply -var dam_license=license.mprv -var ${{ env.DEPLOYMENT_VERSION }}
305+
if [[ -n "${{ env.DEPLOYMENT_VERSION }}" && "${{ env.DEPLOYMENT_VERSION }}" != $'\n' ]]; then
306+
terraform -chdir=$EXAMPLE_DIR apply -var dam_license=license.mprv -var "${{ env.DEPLOYMENT_VERSION }}" -auto-approve
307307
else
308308
terraform -chdir=$EXAMPLE_DIR apply -var dam_license=license.mprv -auto-approve
309309
fi
@@ -348,7 +348,7 @@ jobs:
348348
id: destroy
349349
if: always()
350350
run: |
351-
if [ -n "${{ env.DEPLOYMENT_VERSION }}" && "${{ env.DEPLOYMENT_VERSION }}" != $'\n' ]; then
351+
if [[ -n "${{ env.DEPLOYMENT_VERSION }}" && "${{ env.DEPLOYMENT_VERSION }}" != $'\n' ]]; then
352352
terraform -chdir=$EXAMPLE_DIR destroy -var dam_license=license.mprv -var "${{ env.DEPLOYMENT_VERSION }}" -auto-approve
353353
else
354354
terraform -chdir=$EXAMPLE_DIR destroy -var dam_license=license.mprv -auto-approve

modules/azurerm/sonar-base-instance/setup.tftpl

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ function internet_access() {
3737
# cache and make every retry fail the same way.
3838
function yum_retry() {
3939
local attempt
40-
for attempt in 1 2 3 4 5; do
40+
for attempt in {1..10}; do
4141
if "$@"; then
4242
return 0
4343
fi
44-
echo "Command failed (attempt $attempt): $*. Cleaning yum/dnf cache and retrying..."
44+
echo "Command failed (attempt $attempt/10): $*"
45+
echo "Cleaning yum/dnf cache and retrying..."
4546
yum clean all || true
4647
rm -rf /var/cache/yum /var/cache/dnf || true
47-
sleep $((attempt * 10))
48+
sleep $((attempt < 6 ? attempt * 10 : 60))
4849
done
50+
echo "All $attempt attempts failed for: $*"
51+
echo "RHUI diagnostics:"
52+
curl -sI https://rhui-1.microsoft.com 2>&1 | head -5 || true
4953
return 1
5054
}
5155

@@ -57,9 +61,8 @@ function install_yum_dep_from_internet() {
5761
local package="$1"
5862
local package_name="$${2:-$1}"
5963

60-
# yum_retry takes care of cache repair between attempts, so a transient
61-
# Microsoft RHUI 400 / repomd.xml download glitch heals automatically.
62-
if ! yum list installed "$${package_name}"; then
64+
# Use rpm -q to check if installed without contacting RHUI repos
65+
if ! rpm -q "$${package_name}" &>/dev/null; then
6366
yum_retry yum install "$${package}" -y
6467
fi
6568
}
@@ -70,22 +73,33 @@ function install_azcli_from_internet() {
7073
exit 1
7174
fi
7275
yum_retry rpm --import https://packages.microsoft.com/keys/microsoft.asc
76+
77+
local msrepo_url
7378
if [ "$RHEL_MAJOR_VERSION" -eq 8 ]; then
74-
yum_retry dnf install -y https://packages.microsoft.com/config/rhel/8/packages-microsoft-prod.rpm
79+
msrepo_url="https://packages.microsoft.com/config/rhel/8/packages-microsoft-prod.rpm"
7580
elif [ "$RHEL_MAJOR_VERSION" -eq 9 ]; then
76-
yum_retry dnf install -y https://packages.microsoft.com/config/rhel/9.0/packages-microsoft-prod.rpm
81+
msrepo_url="https://packages.microsoft.com/config/rhel/9.0/packages-microsoft-prod.rpm"
7782
else
7883
echo "Unsupported RHEL version: $RHEL_MAJOR_VERSION"
7984
exit 1
8085
fi
81-
yum_retry dnf install azure-cli -y
86+
87+
# rpm -Uvh avoids dnf's mandatory metadata refresh, which fails when
88+
# Azure RHUI returns 400 on rhel-*-baseos-rhui-rpms.
89+
yum_retry rpm -Uvh --replacepkgs "$msrepo_url"
90+
91+
# Fall back to disabling RHUI if it's degraded; azure-cli's RHEL-side deps
92+
# are already in the Azure RHEL PAYG base image.
93+
yum_retry dnf install azure-cli -y \
94+
|| yum_retry dnf install azure-cli -y --disablerepo='rhui-*'
95+
8296
az login --identity --allow-no-subscriptions
8397
}
8498

8599
function wait_for_systemd() {
86100
for i in $(seq 1 10); do
87-
is_running="$(systemctl is-system-running; echo "")"
88-
if "$is_running" == "running" || "$is_running" == "degraded"; then
101+
is_running="$(systemctl is-system-running || true)"
102+
if [[ "$is_running" == "running" || "$is_running" == "degraded" ]]; then
89103
return 0
90104
fi
91105
sleep 10
@@ -95,9 +109,6 @@ function wait_for_systemd() {
95109
function install_deps() {
96110
install_yum_dep_from_internet unzip
97111
install_yum_dep_from_internet lvm2
98-
install_yum_dep_from_internet "https://dl.fedoraproject.org/pub/epel/epel-release-latest-$RHEL_MAJOR_VERSION.noarch.rpm" epel-release
99-
install_yum_dep_from_internet jq
100-
install_yum_dep_from_internet wget
101112
command -v az || install_azcli_from_internet
102113
}
103114

@@ -187,7 +198,7 @@ function install_tarball() {
187198
# Download installation tarball
188199
if [[ -n "${tarball_url}" ]]; then
189200
TARBALL_FILE=$APPS_DIR/$(basename "${tarball_url}")
190-
wget "${tarball_url}" -O "$TARBALL_FILE" --progress=dot:giga
201+
curl -fL --retry 5 --retry-delay 10 -o "$TARBALL_FILE" "${tarball_url}"
191202
else
192203
TARBALL_FILE=$APPS_DIR/$(basename "${az_blob}")
193204
az storage blob download --account-name "${az_storage_account}" --container-name "${az_container}" --name "${az_blob}" --file "$TARBALL_FILE" --auth-mode login >/dev/null
@@ -201,7 +212,7 @@ function install_tarball() {
201212
}
202213

203214
function set_instance_fqdn() {
204-
instance_fqdn=$(cloud-init query -a | jq -r .ds.meta_data.imds.network.interface[0].ipv4.ipAddress[0].privateIpAddress)
215+
instance_fqdn=$(cloud-init query -a | python3 -c "import sys,json; print(json.load(sys.stdin)['ds']['meta_data']['imds']['network']['interface'][0]['ipv4']['ipAddress'][0]['privateIpAddress'])")
205216
if [ -z "$instance_fqdn" ]; then
206217
echo "Failed to extract instance private FQDN"
207218
exit 1

0 commit comments

Comments
 (0)