Fix: Improve pip install reliability lf-act-venv 61/73561/3
authorAnil Belur <abelur@linuxfoundation.org>
Mon, 25 Aug 2025 13:43:53 +0000 (23:43 +1000)
committerAnil Belur <abelur@linuxfoundation.org>
Mon, 25 Aug 2025 22:53:43 +0000 (08:53 +1000)
Enhance the lf-activate-venv function with robust error handling for
pip installations. Add trusted host configurations, proxy detection,
and fallback strategies to address common "Could not find a version"
errors caused by SSL certificate issues, firewall restrictions, and
network connectivity problems in CI/CD environments.

Changes include:
- Added trusted host flags for PyPI mirrors
- Implemented proxy detection and configuration
- Created fallback installation with verbose debugging
- Added individual package installation as final fallback
- Improved error messages with actionable troubleshooting guidance

Change-Id: Ice9471c0bbd4bda471259f7345298de7ce61b9a3
Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
jenkins-init-scripts/lf-env.sh
releasenotes/notes/improve-pip-install-network-reliability-b8f3e4c2d7a5946e.yaml [new file with mode: 0644]

index ee50676..319c4ca 100644 (file)
@@ -243,17 +243,83 @@ lf-activate-venv () {
             echo "${FUNCNAME[0]}(): INFO: Save venv in file: $venv_file"
         fi
 
-        # Pin setuptools<66 until the issue with pbr + setuptools >=66 is fixed
-        "$lf_venv/bin/python3" -m pip install --upgrade --quiet \
-                        pip 'setuptools<66' virtualenv || return 1
+        echo "${FUNCNAME[0]}(): INFO: Installing base packages (pip, setuptools, virtualenv)"
+
+        # Define pip install options for network issues
+        local pip_opts="--upgrade --quiet"
+
+        # Add trusted hosts to bypass SSL certificate issues
+        # This is the most common cause of the "Could not find a version" error
+        pip_opts="$pip_opts --trusted-host pypi.org"
+        pip_opts="$pip_opts --trusted-host files.pythonhosted.org"
+        pip_opts="$pip_opts --trusted-host pypi.python.org"
+
+        # If behind a corporate proxy, try to detect and handle it
+        if [[ -n "${HTTP_PROXY:-}" || -n "${HTTPS_PROXY:-}" ]]; then
+            echo "${FUNCNAME[0]}(): INFO: Proxy detected, using proxy settings"
+            if [[ -n "${HTTP_PROXY:-}" ]]; then
+                pip_opts="$pip_opts --proxy $HTTP_PROXY"
+            elif [[ -n "${HTTPS_PROXY:-}" ]]; then
+                pip_opts="$pip_opts --proxy $HTTPS_PROXY"
+            fi
+        fi
+
+        # First, try to install with enhanced options
+        echo "${FUNCNAME[0]}(): INFO: Attempting to install with network-safe options..."
+        if ! "$lf_venv/bin/python3" -m pip install "$pip_opts" \
+                        pip 'setuptools<66' virtualenv; then
+
+            echo "${FUNCNAME[0]}(): WARNING: Initial install failed, trying fallback options..."
+
+            # Fallback 1: Try with verbose output for debugging
+            echo "${FUNCNAME[0]}(): INFO: Trying with verbose output for debugging..."
+            if ! "$lf_venv/bin/python3" -m pip install --verbose --trusted-host pypi.org \
+                            --trusted-host files.pythonhosted.org \
+                            pip 'setuptools<66' virtualenv; then
+
+                # Fallback 2: Try installing packages one by one
+                echo "${FUNCNAME[0]}(): WARNING: Batch install failed, trying individual packages..."
+
+                if ! "$lf_venv/bin/python3" -m pip install --trusted-host pypi.org \
+                                --trusted-host files.pythonhosted.org pip; then
+                    lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Cannot install pip - network connectivity issue"
+                    lf-echo-stderr "This suggests a firewall, proxy, or DNS resolution problem"
+                    lf-echo-stderr "Contact your network administrator or check proxy settings"
+                    return 1
+                fi
+
+                if ! "$lf_venv/bin/python3" -m pip install --trusted-host pypi.org \
+                                --trusted-host files.pythonhosted.org 'setuptools<66'; then
+                    lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Cannot install setuptools"
+                    return 1
+                fi
+
+                if ! "$lf_venv/bin/python3" -m pip install --trusted-host pypi.org \
+                                --trusted-host files.pythonhosted.org virtualenv; then
+                    lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Cannot install virtualenv"
+                    return 1
+                fi
+
+                echo "${FUNCNAME[0]}(): INFO: Individual package installation succeeded"
+            else
+                echo "${FUNCNAME[0]}(): INFO: Fallback install succeeded"
+            fi
+        else
+            echo "${FUNCNAME[0]}(): INFO: Base packages installed successfully"
+        fi
+
+        # Continue with the rest of the package installation
         if [[ -z $pkg_list ]]; then
-            echo "${FUNCNAME[0]}(): WARNING: No packages to install"
+            echo "${FUNCNAME[0]}(): WARNING: No additional packages to install"
         else
-            echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
+            echo "${FUNCNAME[0]}(): INFO: Installing additional packages: $pkg_list"
             # $pkg_list is expected to be unquoted
             # shellcheck disable=SC2086
-            "$lf_venv/bin/python3" -m pip install --upgrade --quiet \
-                        --upgrade-strategy eager $pkg_list || return 1
+            if ! "$lf_venv/bin/python3" -m pip install "$pip_opts" \
+                        --upgrade-strategy eager $pkg_list; then
+                lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Failed to install packages: $pkg_list"
+                return 1
+            fi
         fi
         ;;
     *)
diff --git a/releasenotes/notes/improve-pip-install-network-reliability-b8f3e4c2d7a5946e.yaml b/releasenotes/notes/improve-pip-install-network-reliability-b8f3e4c2d7a5946e.yaml
new file mode 100644 (file)
index 0000000..249b95e
--- /dev/null
@@ -0,0 +1,8 @@
+---
+fixes:
+  - |
+    Improve pip installation reliability in lf-activate-venv function by adding
+    enhanced network error handling, trusted host configuration, proxy detection,
+    and fallback installation strategies. This addresses common "Could not find
+    a version" errors caused by SSL certificate issues, firewall restrictions,
+    and network connectivity problems in CI/CD environments.