Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # **install_pip.sh** |
| 4 | |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 5 | # Update pip and friends to a known common version |
| 6 | |
| 7 | # Assumptions: |
Jens Harbott | f1ed7c7 | 2020-06-11 05:51:26 +0000 | [diff] [blame] | 8 | # - PYTHON3_VERSION refers to a version already installed |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 9 | |
Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 10 | set -o errexit |
Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 11 | |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 12 | # Keep track of the current directory |
| 13 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 14 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
| 15 | |
Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 16 | # Change dir to top of DevStack |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 17 | cd $TOP_DIR |
| 18 | |
| 19 | # Import common functions |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 20 | source $TOP_DIR/stackrc |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 21 | |
Sean Dague | 646085d | 2016-03-21 17:00:51 -0400 | [diff] [blame] | 22 | # don't start tracing until after we've sourced the world |
| 23 | set -o xtrace |
| 24 | |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 25 | FILES=$TOP_DIR/files |
| 26 | |
Andreas Scheuring | 0063495 | 2016-08-26 10:29:20 +0200 | [diff] [blame] | 27 | # The URL from where the get-pip.py file gets downloaded. If a local |
| 28 | # get-pip.py mirror is available, PIP_GET_PIP_URL can be set to that |
| 29 | # mirror in local.conf to avoid download timeouts. |
| 30 | # Example: |
| 31 | # PIP_GET_PIP_URL="http://local-server/get-pip.py" |
| 32 | # |
| 33 | # Note that if get-pip.py already exists in $FILES this script will |
| 34 | # not re-download or check for a new version. For example, this is |
| 35 | # done by openstack-infra diskimage-builder elements as part of image |
| 36 | # preparation [1]. This prevents any network access, which can be |
| 37 | # unreliable in CI situations. |
Matt Riedemann | 9b6d2f2 | 2019-06-18 10:43:16 -0400 | [diff] [blame] | 38 | # [1] https://opendev.org/openstack/project-config/src/branch/master/nodepool/elements/cache-devstack/source-repository-pip |
Andreas Scheuring | 0063495 | 2016-08-26 10:29:20 +0200 | [diff] [blame] | 39 | |
| 40 | PIP_GET_PIP_URL=${PIP_GET_PIP_URL:-"https://bootstrap.pypa.io/get-pip.py"} |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 41 | PIP_GET_PIP36_URL=${PIP_GET_PIP36_URL:-"https://bootstrap.pypa.io/pip/3.6/get-pip.py"} |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 42 | |
| 43 | GetDistro |
| 44 | echo "Distro: $DISTRO" |
| 45 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 46 | function get_versions { |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 47 | # FIXME(dhellmann): Deal with multiple python versions here? This |
| 48 | # is just used for reporting, so maybe not? |
Ian Wienand | 2df2aa0 | 2021-08-10 13:50:08 +1000 | [diff] [blame] | 49 | PIP=$(which pip 2>/dev/null || which pip-python 2>/dev/null || which pip3 2>/dev/null || true) |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 50 | if [[ -n $PIP ]]; then |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 51 | PIP_VERSION=$($PIP --version | awk '{ print $2}') |
Monty Taylor | dace92f | 2013-08-10 23:49:47 -0300 | [diff] [blame] | 52 | echo "pip: $PIP_VERSION" |
Attila Fazekas | 46ea723 | 2013-10-07 07:29:27 +0200 | [diff] [blame] | 53 | else |
| 54 | echo "pip: Not Installed" |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 55 | fi |
| 56 | } |
| 57 | |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 58 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 59 | function install_get_pip { |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 60 | if [[ "$PYTHON3_VERSION" = "3.6" ]]; then |
| 61 | _pip_url=$PIP_GET_PIP36_URL |
| 62 | _local_pip="$FILES/$(basename $_pip_url)-py36" |
| 63 | else |
| 64 | _pip_url=$PIP_GET_PIP_URL |
| 65 | _local_pip="$FILES/$(basename $_pip_url)" |
| 66 | fi |
| 67 | |
| 68 | |
Sean Dague | 0280f6f | 2015-10-07 09:19:53 -0400 | [diff] [blame] | 69 | # If get-pip.py isn't python, delete it. This was probably an |
| 70 | # outage on the server. |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 71 | if [[ -r $_local_pip ]]; then |
| 72 | if ! head -1 $_local_pip | grep -q '#!/usr/bin/env python'; then |
| 73 | echo "WARNING: Corrupt $_local_pip found removing" |
| 74 | rm $_local_pip |
Sean Dague | 0280f6f | 2015-10-07 09:19:53 -0400 | [diff] [blame] | 75 | fi |
| 76 | fi |
| 77 | |
Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 78 | # The OpenStack gate and others put a cached version of get-pip.py |
Ian Wienand | 7c4ce9e | 2015-03-10 11:32:26 +1100 | [diff] [blame] | 79 | # for this to find, explicitly to avoid download issues. |
| 80 | # |
Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 81 | # However, if DevStack *did* download the file, we want to check |
| 82 | # for updates; people can leave their stacks around for a long |
Ian Wienand | 7c4ce9e | 2015-03-10 11:32:26 +1100 | [diff] [blame] | 83 | # time and in the mean-time pip might get upgraded. |
| 84 | # |
| 85 | # Thus we use curl's "-z" feature to always check the modified |
| 86 | # since and only download if a new version is out -- but only if |
| 87 | # it seems we downloaded the file originally. |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 88 | if [[ ! -r $_local_pip || -r $_local_pip.downloaded ]]; then |
Sean Dague | a0cc291 | 2015-10-07 09:06:42 -0400 | [diff] [blame] | 89 | # only test freshness if LOCAL_PIP is actually there, |
| 90 | # otherwise we generate a scary warning. |
| 91 | local timecond="" |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 92 | if [[ -r $_local_pip ]]; then |
| 93 | timecond="-z $_local_pip" |
Sean Dague | a0cc291 | 2015-10-07 09:06:42 -0400 | [diff] [blame] | 94 | fi |
| 95 | |
Sean Dague | fa41b5b | 2015-10-08 06:05:20 -0400 | [diff] [blame] | 96 | curl -f --retry 6 --retry-delay 5 \ |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 97 | $timecond -o $_local_pip $_pip_url || \ |
Sean Dague | f625ffe | 2014-06-05 07:04:41 -0400 | [diff] [blame] | 98 | die $LINENO "Download of get-pip.py failed" |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 99 | touch $_local_pip.downloaded |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 100 | fi |
Rodolfo Alonso Hernandez | a756f4b | 2022-01-31 16:38:31 +0000 | [diff] [blame] | 101 | sudo -H -E python${PYTHON3_VERSION} $_local_pip |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 104 | |
Franck Yelles | 683ff42 | 2014-06-19 02:14:42 -0700 | [diff] [blame] | 105 | function configure_pypi_alternative_url { |
| 106 | PIP_ROOT_FOLDER="$HOME/.pip" |
| 107 | PIP_CONFIG_FILE="$PIP_ROOT_FOLDER/pip.conf" |
| 108 | if [[ ! -d $PIP_ROOT_FOLDER ]]; then |
| 109 | echo "Creating $PIP_ROOT_FOLDER" |
| 110 | mkdir $PIP_ROOT_FOLDER |
| 111 | fi |
| 112 | if [[ ! -f $PIP_CONFIG_FILE ]]; then |
| 113 | echo "Creating $PIP_CONFIG_FILE" |
| 114 | touch $PIP_CONFIG_FILE |
| 115 | fi |
| 116 | if ! ini_has_option "$PIP_CONFIG_FILE" "global" "index-url"; then |
Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 117 | # It means that the index-url does not exist |
Franck Yelles | 683ff42 | 2014-06-19 02:14:42 -0700 | [diff] [blame] | 118 | iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE" |
| 119 | fi |
| 120 | |
| 121 | } |
| 122 | |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 123 | # Show starting versions |
| 124 | get_versions |
| 125 | |
Jens Harbott | f1ed7c7 | 2020-06-11 05:51:26 +0000 | [diff] [blame] | 126 | if [[ -n $PYPI_ALTERNATIVE_URL ]]; then |
| 127 | configure_pypi_alternative_url |
| 128 | fi |
| 129 | |
Alfredo Moralejo | 5ea4c3c | 2021-11-16 15:13:03 +0100 | [diff] [blame] | 130 | if is_fedora && [[ ${DISTRO} == f* || ${DISTRO} == rhel9 ]]; then |
Ian Wienand | a209718 | 2021-08-10 14:11:12 +1000 | [diff] [blame] | 131 | # get-pip.py will not install over the python3-pip package in |
| 132 | # Fedora 34 any more. |
| 133 | # https://bugzilla.redhat.com/show_bug.cgi?id=1988935 |
| 134 | # https://github.com/pypa/pip/issues/9904 |
| 135 | # You can still install using get-pip.py if python3-pip is *not* |
| 136 | # installed; this *should* remain separate under /usr/local and not break |
| 137 | # if python3-pip is later installed. |
| 138 | # For general sanity, we just use the packaged pip. It should be |
Ian Wienand | 8dac135 | 2021-08-11 14:56:05 +1000 | [diff] [blame] | 139 | # recent enough anyway. This is included via rpms/general |
Michal Berger | bfc79dc | 2021-10-05 15:40:20 +0200 | [diff] [blame] | 140 | : # Simply fall through |
Dr. Jens Harbott | d6909e4 | 2022-01-22 13:54:12 +0100 | [diff] [blame] | 141 | elif is_ubuntu; then |
Martin Kopec | 818d1a2 | 2022-11-28 11:19:45 +0100 | [diff] [blame] | 142 | # pip on Ubuntu 20.04 and higher is new enough, too |
Dr. Jens Harbott | e6e7100 | 2022-06-07 10:12:59 +0200 | [diff] [blame] | 143 | # drop setuptools from u-c |
| 144 | sed -i -e '/setuptools/d' $REQUIREMENTS_DIR/upper-constraints.txt |
Ian Wienand | a209718 | 2021-08-10 14:11:12 +1000 | [diff] [blame] | 145 | else |
Ian Wienand | a209718 | 2021-08-10 14:11:12 +1000 | [diff] [blame] | 146 | install_get_pip |
Dr. Jens Harbott | e6e7100 | 2022-06-07 10:12:59 +0200 | [diff] [blame] | 147 | |
| 148 | # Note setuptools is part of requirements.txt and we want to make sure |
| 149 | # we obey any versioning as described there. |
| 150 | pip_install_gr setuptools |
Attila Fazekas | 9127c1a | 2015-11-05 10:09:02 +0100 | [diff] [blame] | 151 | fi |
Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 152 | |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 153 | set -x |
Ian Wienand | 1e7f738 | 2017-02-27 11:19:40 +1100 | [diff] [blame] | 154 | |
Mathieu Gagné | 76ed427 | 2014-06-05 16:50:40 -0400 | [diff] [blame] | 155 | |
Monty Taylor | dace92f | 2013-08-10 23:49:47 -0300 | [diff] [blame] | 156 | get_versions |