From: Anil Belur Date: Mon, 25 Aug 2025 13:43:53 +0000 (+1000) Subject: Fix: Improve pip install reliability lf-act-venv X-Git-Tag: v0.92.3~1 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F61%2F73561%2F3;p=releng%2Fglobal-jjb.git Fix: Improve pip install reliability lf-act-venv 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 --- diff --git a/jenkins-init-scripts/lf-env.sh b/jenkins-init-scripts/lf-env.sh index ee506766..319c4ca2 100644 --- a/jenkins-init-scripts/lf-env.sh +++ b/jenkins-init-scripts/lf-env.sh @@ -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 index 00000000..249b95e3 --- /dev/null +++ b/releasenotes/notes/improve-pip-install-network-reliability-b8f3e4c2d7a5946e.yaml @@ -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.