blob: 91b180c06f832de883b6f60e222d0142a65b6d2a [file] [log] [blame]
Dean Troyer62d1d692013-08-01 17:40:40 -05001#!/usr/bin/env bash
2
3# **install_pip.sh**
4
Dean Troyer62d1d692013-08-01 17:40:40 -05005# Update pip and friends to a known common version
6
7# Assumptions:
Jens Harbottf1ed7c72020-06-11 05:51:26 +00008# - PYTHON3_VERSION refers to a version already installed
Dean Troyer62d1d692013-08-01 17:40:40 -05009
Adam Spiersc85ade72013-10-01 00:35:16 +010010set -o errexit
Adam Spiersc85ade72013-10-01 00:35:16 +010011
Dean Troyer62d1d692013-08-01 17:40:40 -050012# Keep track of the current directory
13TOOLS_DIR=$(cd $(dirname "$0") && pwd)
14TOP_DIR=`cd $TOOLS_DIR/..; pwd`
15
Dean Troyerdc97cb72015-03-28 08:20:50 -050016# Change dir to top of DevStack
Dean Troyer62d1d692013-08-01 17:40:40 -050017cd $TOP_DIR
18
19# Import common functions
Clark Boylan05aa3842015-08-03 11:14:13 -070020source $TOP_DIR/stackrc
Dean Troyer62d1d692013-08-01 17:40:40 -050021
Sean Dague646085d2016-03-21 17:00:51 -040022# don't start tracing until after we've sourced the world
23set -o xtrace
24
Dean Troyer62d1d692013-08-01 17:40:40 -050025FILES=$TOP_DIR/files
26
Andreas Scheuring00634952016-08-26 10:29:20 +020027# 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 Riedemann9b6d2f22019-06-18 10:43:16 -040038# [1] https://opendev.org/openstack/project-config/src/branch/master/nodepool/elements/cache-devstack/source-repository-pip
Andreas Scheuring00634952016-08-26 10:29:20 +020039
40PIP_GET_PIP_URL=${PIP_GET_PIP_URL:-"https://bootstrap.pypa.io/get-pip.py"}
Rodolfo Alonso Hernandeza756f4b2022-01-31 16:38:31 +000041PIP_GET_PIP36_URL=${PIP_GET_PIP36_URL:-"https://bootstrap.pypa.io/pip/3.6/get-pip.py"}
Dean Troyer62d1d692013-08-01 17:40:40 -050042
43GetDistro
44echo "Distro: $DISTRO"
45
Ian Wienandaee18c72014-02-21 15:35:08 +110046function get_versions {
Doug Hellmannddc38392015-05-07 21:06:24 +000047 # FIXME(dhellmann): Deal with multiple python versions here? This
48 # is just used for reporting, so maybe not?
Ian Wienand2df2aa02021-08-10 13:50:08 +100049 PIP=$(which pip 2>/dev/null || which pip-python 2>/dev/null || which pip3 2>/dev/null || true)
Dean Troyer62d1d692013-08-01 17:40:40 -050050 if [[ -n $PIP ]]; then
Dean Troyer62d1d692013-08-01 17:40:40 -050051 PIP_VERSION=$($PIP --version | awk '{ print $2}')
Monty Taylordace92f2013-08-10 23:49:47 -030052 echo "pip: $PIP_VERSION"
Attila Fazekas46ea7232013-10-07 07:29:27 +020053 else
54 echo "pip: Not Installed"
Dean Troyer62d1d692013-08-01 17:40:40 -050055 fi
56}
57
Dean Troyer62d1d692013-08-01 17:40:40 -050058
Ian Wienandaee18c72014-02-21 15:35:08 +110059function install_get_pip {
Rodolfo Alonso Hernandeza756f4b2022-01-31 16:38:31 +000060 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 Dague0280f6f2015-10-07 09:19:53 -040069 # If get-pip.py isn't python, delete it. This was probably an
70 # outage on the server.
Rodolfo Alonso Hernandeza756f4b2022-01-31 16:38:31 +000071 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 Dague0280f6f2015-10-07 09:19:53 -040075 fi
76 fi
77
Dean Troyerdc97cb72015-03-28 08:20:50 -050078 # The OpenStack gate and others put a cached version of get-pip.py
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110079 # for this to find, explicitly to avoid download issues.
80 #
Dean Troyerdc97cb72015-03-28 08:20:50 -050081 # However, if DevStack *did* download the file, we want to check
82 # for updates; people can leave their stacks around for a long
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110083 # 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 Hernandeza756f4b2022-01-31 16:38:31 +000088 if [[ ! -r $_local_pip || -r $_local_pip.downloaded ]]; then
Sean Daguea0cc2912015-10-07 09:06:42 -040089 # only test freshness if LOCAL_PIP is actually there,
90 # otherwise we generate a scary warning.
91 local timecond=""
Rodolfo Alonso Hernandeza756f4b2022-01-31 16:38:31 +000092 if [[ -r $_local_pip ]]; then
93 timecond="-z $_local_pip"
Sean Daguea0cc2912015-10-07 09:06:42 -040094 fi
95
Sean Daguefa41b5b2015-10-08 06:05:20 -040096 curl -f --retry 6 --retry-delay 5 \
Rodolfo Alonso Hernandeza756f4b2022-01-31 16:38:31 +000097 $timecond -o $_local_pip $_pip_url || \
Sean Daguef625ffe2014-06-05 07:04:41 -040098 die $LINENO "Download of get-pip.py failed"
Rodolfo Alonso Hernandeza756f4b2022-01-31 16:38:31 +000099 touch $_local_pip.downloaded
Dean Troyer62d1d692013-08-01 17:40:40 -0500100 fi
Rodolfo Alonso Hernandeza756f4b2022-01-31 16:38:31 +0000101 sudo -H -E python${PYTHON3_VERSION} $_local_pip
Dean Troyer62d1d692013-08-01 17:40:40 -0500102}
103
Dean Troyer62d1d692013-08-01 17:40:40 -0500104
Franck Yelles683ff422014-06-19 02:14:42 -0700105function 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 Troyerdc97cb72015-03-28 08:20:50 -0500117 # It means that the index-url does not exist
Franck Yelles683ff422014-06-19 02:14:42 -0700118 iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE"
119 fi
120
121}
122
Dean Troyer62d1d692013-08-01 17:40:40 -0500123# Show starting versions
124get_versions
125
Jens Harbottf1ed7c72020-06-11 05:51:26 +0000126if [[ -n $PYPI_ALTERNATIVE_URL ]]; then
127 configure_pypi_alternative_url
128fi
129
Alfredo Moralejo5ea4c3c2021-11-16 15:13:03 +0100130if is_fedora && [[ ${DISTRO} == f* || ${DISTRO} == rhel9 ]]; then
Ian Wienanda2097182021-08-10 14:11:12 +1000131 # 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 Wienand8dac1352021-08-11 14:56:05 +1000139 # recent enough anyway. This is included via rpms/general
Michal Bergerbfc79dc2021-10-05 15:40:20 +0200140 : # Simply fall through
Dr. Jens Harbottd6909e42022-01-22 13:54:12 +0100141elif is_ubuntu; then
Martin Kopec818d1a22022-11-28 11:19:45 +0100142 # pip on Ubuntu 20.04 and higher is new enough, too
Dr. Jens Harbotte6e71002022-06-07 10:12:59 +0200143 # drop setuptools from u-c
144 sed -i -e '/setuptools/d' $REQUIREMENTS_DIR/upper-constraints.txt
Ian Wienanda2097182021-08-10 14:11:12 +1000145else
Ian Wienanda2097182021-08-10 14:11:12 +1000146 install_get_pip
Dr. Jens Harbotte6e71002022-06-07 10:12:59 +0200147
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 Fazekas9127c1a2015-11-05 10:09:02 +0100151fi
Dean Troyer62d1d692013-08-01 17:40:40 -0500152
Doug Hellmannddc38392015-05-07 21:06:24 +0000153set -x
Ian Wienand1e7f7382017-02-27 11:19:40 +1100154
Mathieu Gagné76ed4272014-06-05 16:50:40 -0400155
Monty Taylordace92f2013-08-10 23:49:47 -0300156get_versions