blob: a80c178f2a6af074be3ac161701f5760db3a27d0 [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"}
Sean Daguef625ffe2014-06-05 07:04:41 -040041LOCAL_PIP="$FILES/$(basename $PIP_GET_PIP_URL)"
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 {
Sean Dague0280f6f2015-10-07 09:19:53 -040060 # If get-pip.py isn't python, delete it. This was probably an
61 # outage on the server.
62 if [[ -r $LOCAL_PIP ]]; then
63 if ! head -1 $LOCAL_PIP | grep -q '#!/usr/bin/env python'; then
64 echo "WARNING: Corrupt $LOCAL_PIP found removing"
65 rm $LOCAL_PIP
66 fi
67 fi
68
Dean Troyerdc97cb72015-03-28 08:20:50 -050069 # The OpenStack gate and others put a cached version of get-pip.py
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110070 # for this to find, explicitly to avoid download issues.
71 #
Dean Troyerdc97cb72015-03-28 08:20:50 -050072 # However, if DevStack *did* download the file, we want to check
73 # for updates; people can leave their stacks around for a long
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110074 # time and in the mean-time pip might get upgraded.
75 #
76 # Thus we use curl's "-z" feature to always check the modified
77 # since and only download if a new version is out -- but only if
78 # it seems we downloaded the file originally.
79 if [[ ! -r $LOCAL_PIP || -r $LOCAL_PIP.downloaded ]]; then
Sean Daguea0cc2912015-10-07 09:06:42 -040080 # only test freshness if LOCAL_PIP is actually there,
81 # otherwise we generate a scary warning.
82 local timecond=""
83 if [[ -r $LOCAL_PIP ]]; then
84 timecond="-z $LOCAL_PIP"
85 fi
86
Sean Daguefa41b5b2015-10-08 06:05:20 -040087 curl -f --retry 6 --retry-delay 5 \
Sean Daguea0cc2912015-10-07 09:06:42 -040088 $timecond -o $LOCAL_PIP $PIP_GET_PIP_URL || \
Sean Daguef625ffe2014-06-05 07:04:41 -040089 die $LINENO "Download of get-pip.py failed"
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110090 touch $LOCAL_PIP.downloaded
Dean Troyer62d1d692013-08-01 17:40:40 -050091 fi
Ian Wienand6b9a5642021-07-28 11:19:57 +100092 sudo -H -E python${PYTHON3_VERSION} $LOCAL_PIP
Dean Troyer62d1d692013-08-01 17:40:40 -050093}
94
Dean Troyer62d1d692013-08-01 17:40:40 -050095
Franck Yelles683ff422014-06-19 02:14:42 -070096function configure_pypi_alternative_url {
97 PIP_ROOT_FOLDER="$HOME/.pip"
98 PIP_CONFIG_FILE="$PIP_ROOT_FOLDER/pip.conf"
99 if [[ ! -d $PIP_ROOT_FOLDER ]]; then
100 echo "Creating $PIP_ROOT_FOLDER"
101 mkdir $PIP_ROOT_FOLDER
102 fi
103 if [[ ! -f $PIP_CONFIG_FILE ]]; then
104 echo "Creating $PIP_CONFIG_FILE"
105 touch $PIP_CONFIG_FILE
106 fi
107 if ! ini_has_option "$PIP_CONFIG_FILE" "global" "index-url"; then
Dean Troyerdc97cb72015-03-28 08:20:50 -0500108 # It means that the index-url does not exist
Franck Yelles683ff422014-06-19 02:14:42 -0700109 iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE"
110 fi
111
112}
113
Dean Troyer62d1d692013-08-01 17:40:40 -0500114# Show starting versions
115get_versions
116
Jens Harbottf1ed7c72020-06-11 05:51:26 +0000117if [[ -n $PYPI_ALTERNATIVE_URL ]]; then
118 configure_pypi_alternative_url
119fi
120
Ian Wienanda2097182021-08-10 14:11:12 +1000121if is_fedora && [[ ${DISTRO} == f* ]]; then
122 # get-pip.py will not install over the python3-pip package in
123 # Fedora 34 any more.
124 # https://bugzilla.redhat.com/show_bug.cgi?id=1988935
125 # https://github.com/pypa/pip/issues/9904
126 # You can still install using get-pip.py if python3-pip is *not*
127 # installed; this *should* remain separate under /usr/local and not break
128 # if python3-pip is later installed.
129 # For general sanity, we just use the packaged pip. It should be
Ian Wienand8dac1352021-08-11 14:56:05 +1000130 # recent enough anyway. This is included via rpms/general
131 continue
Ian Wienanda2097182021-08-10 14:11:12 +1000132else
Ian Wienanda2097182021-08-10 14:11:12 +1000133 install_get_pip
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100134fi
Dean Troyer62d1d692013-08-01 17:40:40 -0500135
Doug Hellmannddc38392015-05-07 21:06:24 +0000136set -x
Ian Wienand1e7f7382017-02-27 11:19:40 +1100137
138# Note setuptools is part of requirements.txt and we want to make sure
139# we obey any versioning as described there.
140pip_install_gr setuptools
Mathieu Gagné76ed4272014-06-05 16:50:40 -0400141
Monty Taylordace92f2013-08-10 23:49:47 -0300142get_versions