Improve installing package with mirror fallback [skip ci]

This commit is contained in:
Shivam Mathur
2026-05-13 23:43:44 +05:30
parent 90487d3de7
commit bc5c9f7652

View File

@@ -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.