Skip to content

[FEAT] Allow selecting mirrors for package download #1153

@JeWe37

Description

@JeWe37

Is this a new feature request?

  • I have searched the existing issues

Name of mod

mod-scripts

Wanted change

Offer the ability to pass an environment variable PACKAGE_MIRRORS that specifies which mirrors should be used during repo package installation by the docker mods on startup.

Reason for change

I've repeatedly run into issues due to the default alpine mirrors being unstable in my environment. I imagine users in countries with restricted internet access would likewise appreciate the ability to easily adjusting the mirrors used.

Proposed code change

Untested (and literally just copied from AI) don't really have the time to test right now (otherwise this would've been a PR...), but should get the idea across. I suspect I'll get back to this the next time this issue decides to annoy me.

--- package-install.v1
+++ package-install.v1
@@ -22,6 +22,35 @@
 
 write_mod_debug "Package install script version ${PKG_SCRIPT_VER}"
 
+set_package_mirror() {
+    if [[ -z ${PACKAGE_MIRRORS} ]]; then
+        return
+    fi
+
+    write_mod_debug "Setting package mirror to ${PACKAGE_MIRRORS}"
+
+    if [[ -f /usr/bin/apt ]]; then
+        if [[ -f /etc/apt/sources.list ]]; then
+            sed -i \
+                -e "s|http://archive.ubuntu.com/ubuntu|${PACKAGE_MIRRORS}|g" \
+                -e "s|http://security.ubuntu.com/ubuntu|${PACKAGE_MIRRORS}|g" \
+                -e "s|http://deb.debian.org/debian|${PACKAGE_MIRRORS}|g" \
+                -e "s|http://security.debian.org/debian-security|${PACKAGE_MIRRORS}|g" \
+                /etc/apt/sources.list
+        fi
+        if compgen -G "/etc/apt/sources.list.d/*.sources" > /dev/null; then
+            sed -i "s|^URIs: .*|URIs: ${PACKAGE_MIRRORS}|g" /etc/apt/sources.list.d/*.sources
+        fi
+    elif [[ -f /sbin/apk ]]; then
+        sed -i "s|https\?://[^ ]*/alpine|${PACKAGE_MIRRORS}|g" /etc/apk/repositories
+    elif [[ -f /usr/sbin/pacman ]]; then
+        sed -i "s|^Server = .*|Server = ${PACKAGE_MIRRORS}|g" /etc/pacman.d/mirrorlist
+    elif [[ -f /usr/bin/dnf ]]; then
+        sed -i \
+            -e "s|^mirrorlist=|#mirrorlist=|g" \
+            -e "s|^#baseurl=http.*/\\\$contentdir|baseurl=${PACKAGE_MIRRORS}|g" \
+            /etc/yum.repos.d/*.repo
+    fi
+}
+
 if [[ -f "/mod-pip-packages-to-install.list" ]]; then
     IFS=' ' read -ra PIP_PACKAGES <<< "$(tr '\n' ' ' < /mod-pip-packages-to-install.list)"
     if [[ ${#PIP_PACKAGES[@]} -ne 0 ]] && [[ ${PIP_PACKAGES[*]} != "" ]]; then
@@ -53,6 +82,7 @@
     if [[ ${#REPO_PACKAGES[@]} -ne 0 ]] && [[ ${REPO_PACKAGES[*]} != "" ]]; then
         write_mod_info "Installing all mod packages"
         write_mod_debug "Defined packages: ${REPO_PACKAGES[*]}"
+        set_package_mirror
         if [[ -f /usr/bin/apt ]]; then
             export DEBIAN_FRONTEND="noninteractive"
             apt-get update

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions