From bc5c9f76522c1c3d548e085b275e225f74204035 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Wed, 13 May 2026 23:43:44 +0530 Subject: [PATCH] Improve installing package with mirror fallback [skip ci] --- src/scripts/linux.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 9276484d..953eeaf9 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -29,6 +29,15 @@ fix_broken_packages() { sudo apt --fix-broken install >/dev/null 2>&1 } +# Function to switch the Ubuntu apt mirror from Azure to the main Ubuntu archive. +switch_mirror() { + [ "${ID:-}" = "ubuntu" ] || return 0 + if [ -f /etc/apt/apt-mirrors.txt ] && grep -Eq 'azure\.archive\.ubuntu\.com' /etc/apt/apt-mirrors.txt; then + sudo sed -i -E '/azure\.archive\.ubuntu\.com/d' /etc/apt/apt-mirrors.txt + sudo rm -f /tmp/os_lists + fi +} + # Function to install a package install_packages() { packages=("$@") @@ -36,7 +45,13 @@ install_packages() { echo "force-confnew" | sudo tee /etc/dpkg/dpkg.cfg.d/force-confnew >/dev/null 2>&1 trap "sudo rm -f /etc/dpkg/dpkg.cfg.d/force-confnew 2>/dev/null" exit fi - $apt_install "${packages[@]}" >/dev/null 2>&1 || (update_lists && fix_broken_packages && $apt_install "${packages[@]}" >/dev/null 2>&1) + if $apt_install "${packages[@]}" >/dev/null 2>&1; then + return 0 + fi + if update_lists && fix_broken_packages && $apt_install "${packages[@]}" >/dev/null 2>&1; then + return 0 + fi + switch_mirror && update_lists && $apt_install "${packages[@]}" >/dev/null 2>&1 } # Function to disable an extension.