|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This is technically redundant, since all consumers of this lib will have enabled these, |
| 4 | +# however, it helps Shellcheck realise the options under which these functions will run. |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +DEFAULT_S3_BASE_URL="https://heroku-buildpack-python.s3.us-east-1.amazonaws.com" |
| 8 | + |
| 9 | +function python::install() { |
| 10 | + local build_dir="${1}" |
| 11 | + local stack="${2}" |
| 12 | + local python_full_version="${3}" |
| 13 | + local python_major_version="${4}" |
| 14 | + local python_version_origin="${5}" |
| 15 | + |
| 16 | + local install_python_start_time |
| 17 | + install_python_start_time=$(nowms) |
| 18 | + local install_dir="${build_dir}/.heroku/python" |
| 19 | + |
| 20 | + if [[ -f "${install_dir}/bin/python" ]]; then |
| 21 | + output::step "Using cached install of Python ${python_full_version}" |
| 22 | + else |
| 23 | + output::step "Installing Python ${python_full_version}" |
| 24 | + |
| 25 | + mkdir -p "${install_dir}" |
| 26 | + |
| 27 | + # Note: This can't be used via app config vars, since it doesn't reference the value from ENV_DIR. |
| 28 | + # TODO: Remove this for parity with the Python CNB, if metrics show it to be unused on Heroku. |
| 29 | + if [[ -v BUILDPACK_S3_BASE_URL ]]; then |
| 30 | + local s3_base_url="${BUILDPACK_S3_BASE_URL}" |
| 31 | + meta_set "custom_s3_base_url" "true" |
| 32 | + else |
| 33 | + local s3_base_url="${DEFAULT_S3_BASE_URL}" |
| 34 | + fi |
| 35 | + |
| 36 | + # Calculating the Ubuntu version from the stack name saves having to shell out to `lsb_release`. |
| 37 | + local ubuntu_version="${stack/heroku-/}.04" |
| 38 | + local arch |
| 39 | + arch=$(dpkg --print-architecture) |
| 40 | + # e.g.: https://heroku-buildpack-python.s3.us-east-1.amazonaws.com/python-3.13.0-ubuntu-24.04-amd64.tar.zst |
| 41 | + local python_url="${s3_base_url}/python-${python_full_version}-ubuntu-${ubuntu_version}-${arch}.tar.zst" |
| 42 | + |
| 43 | + local error_log |
| 44 | + error_log=$(mktemp) |
| 45 | + |
| 46 | + # shellcheck disable=SC2310 # This function is invoked in an 'if' condition so set -e will be disabled. |
| 47 | + if ! { |
| 48 | + { |
| 49 | + # We set max-time for improved UX/metrics for hanging downloads compared to relying |
| 50 | + # on the build system timeout. The Python archives are only ~10 MB so take < 1s to |
| 51 | + # download on Heroku's build system, however, we use much higher timeouts so that |
| 52 | + # the buildpack works in non-Heroku environments that may be far from `us-east-1` |
| 53 | + # or have a slower connection. We don't use `--speed-limit` since it gives worse |
| 54 | + # error messages when used with retries and piping to tar. |
| 55 | + curl \ |
| 56 | + --connect-timeout 10 \ |
| 57 | + --fail \ |
| 58 | + --max-time 120 \ |
| 59 | + --retry-max-time 120 \ |
| 60 | + --retry 3 \ |
| 61 | + --retry-connrefused \ |
| 62 | + --show-error \ |
| 63 | + --silent \ |
| 64 | + "${python_url}" \ |
| 65 | + | tar \ |
| 66 | + --directory "${install_dir}" \ |
| 67 | + --extract \ |
| 68 | + --zstd |
| 69 | + } \ |
| 70 | + |& tee "${error_log}" \ |
| 71 | + |& output::indent |
| 72 | + }; then |
| 73 | + local latest_known_patch_version |
| 74 | + latest_known_patch_version="$(python_version::resolve_python_version "${python_major_version}" "${python_version_origin}")" |
| 75 | + # Ideally we would inspect the HTTP status code directly instead of grepping, however: |
| 76 | + # 1. We want to pipe to tar (since it's faster than performing the download and |
| 77 | + # decompression/extraction as separate steps), so can't write to stdout. |
| 78 | + # 2. We want to display the original stderr to the user, so can't write to stderr. |
| 79 | + # 3. Curl's `--write-out` feature only supports outputting to a file (as opposed to |
| 80 | + # stdout/stderr) as of curl v8.3.0, which is newer than the curl on Heroku-20/22. |
| 81 | + # This has an integration test run against all stacks, which will mean we will know |
| 82 | + # if future versions of curl change the error message string. |
| 83 | + # |
| 84 | + # We have to check for HTTP 403s too, since S3 will return a 403 instead of a 404 for |
| 85 | + # missing files, if the S3 bucket does not have public list permissions enabled. |
| 86 | + if [[ "${python_full_version}" != "${latest_known_patch_version}" ]] && grep --quiet "The requested URL returned error: 40[34]" "${error_log}"; then |
| 87 | + output::error <<-EOF |
| 88 | + Error: The requested Python version isn't available. |
| 89 | +
|
| 90 | + Your app's ${python_version_origin} file specifies a Python version |
| 91 | + of ${python_full_version}, however, we couldn't find that version on S3. |
| 92 | +
|
| 93 | + Check that this Python version has been released upstream, |
| 94 | + and that the Python buildpack has added support for it: |
| 95 | + https://www.python.org/downloads/ |
| 96 | + https://github.com/heroku/heroku-buildpack-python/blob/main/CHANGELOG.md |
| 97 | +
|
| 98 | + If it has, make sure that you are using the latest version |
| 99 | + of this buildpack, and haven't pinned to an older release: |
| 100 | + https://devcenter.heroku.com/articles/managing-buildpacks#view-your-buildpacks |
| 101 | + https://devcenter.heroku.com/articles/managing-buildpacks#classic-buildpacks-references |
| 102 | +
|
| 103 | + We also strongly recommend that you do not pin your app to an |
| 104 | + exact Python version such as ${python_full_version}, and instead only specify |
| 105 | + the major Python version of ${python_major_version} in your ${python_version_origin} file. |
| 106 | + This will allow your app to receive the latest available Python |
| 107 | + patch version automatically, and prevent this type of error. |
| 108 | + EOF |
| 109 | + meta_set "failure_reason" "python-version::unknown-patch" |
| 110 | + meta_set "failure_detail" "${python_full_version}" |
| 111 | + else |
| 112 | + output::error <<-EOF |
| 113 | + Error: Unable to download/install Python. |
| 114 | +
|
| 115 | + An error occurred while downloading/installing the Python |
| 116 | + runtime archive from: |
| 117 | + ${python_url} |
| 118 | +
|
| 119 | + In some cases, this happens due to a temporary issue with |
| 120 | + the network connection or server. |
| 121 | +
|
| 122 | + First, make sure that you are using the latest version |
| 123 | + of this buildpack, and haven't pinned to an older release: |
| 124 | + https://devcenter.heroku.com/articles/managing-buildpacks#view-your-buildpacks |
| 125 | + https://devcenter.heroku.com/articles/managing-buildpacks#classic-buildpacks-references |
| 126 | +
|
| 127 | + Then try building again to see if the error resolves itself. |
| 128 | + EOF |
| 129 | + meta_set "failure_reason" "install-python" |
| 130 | + # e.g.: 'curl: (6) Could not resolve host: heroku-buildpack-python.s3.us-east-1.amazonaws.com' |
| 131 | + meta_set "failure_detail" "$(head --lines=1 "${error_log}" || true)" |
| 132 | + fi |
| 133 | + |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | + fi |
| 137 | + |
| 138 | + meta_time "python_install_duration" "${install_python_start_time}" |
| 139 | +} |
0 commit comments