blob: 517669e282542e37613dba3ea2600fdf23555fb1 [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?
Attila Fazekas46ea7232013-10-07 07:29:27 +020049 PIP=$(which pip 2>/dev/null || which pip-python 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
Jens Harbottf1ed7c72020-06-11 05:51:26 +000056 # Show python3 module version
57 python${PYTHON3_VERSION} -m pip --version
Dean Troyer62d1d692013-08-01 17:40:40 -050058}
59
Dean Troyer62d1d692013-08-01 17:40:40 -050060
Ian Wienandaee18c72014-02-21 15:35:08 +110061function install_get_pip {
Sean Dague0280f6f2015-10-07 09:19:53 -040062 # If get-pip.py isn't python, delete it. This was probably an
63 # outage on the server.
64 if [[ -r $LOCAL_PIP ]]; then
65 if ! head -1 $LOCAL_PIP | grep -q '#!/usr/bin/env python'; then
66 echo "WARNING: Corrupt $LOCAL_PIP found removing"
67 rm $LOCAL_PIP
68 fi
69 fi
70
Dean Troyerdc97cb72015-03-28 08:20:50 -050071 # The OpenStack gate and others put a cached version of get-pip.py
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110072 # for this to find, explicitly to avoid download issues.
73 #
Dean Troyerdc97cb72015-03-28 08:20:50 -050074 # However, if DevStack *did* download the file, we want to check
75 # for updates; people can leave their stacks around for a long
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110076 # time and in the mean-time pip might get upgraded.
77 #
78 # Thus we use curl's "-z" feature to always check the modified
79 # since and only download if a new version is out -- but only if
80 # it seems we downloaded the file originally.
81 if [[ ! -r $LOCAL_PIP || -r $LOCAL_PIP.downloaded ]]; then
Sean Daguea0cc2912015-10-07 09:06:42 -040082 # only test freshness if LOCAL_PIP is actually there,
83 # otherwise we generate a scary warning.
84 local timecond=""
85 if [[ -r $LOCAL_PIP ]]; then
86 timecond="-z $LOCAL_PIP"
87 fi
88
Sean Daguefa41b5b2015-10-08 06:05:20 -040089 curl -f --retry 6 --retry-delay 5 \
Sean Daguea0cc2912015-10-07 09:06:42 -040090 $timecond -o $LOCAL_PIP $PIP_GET_PIP_URL || \
Sean Daguef625ffe2014-06-05 07:04:41 -040091 die $LINENO "Download of get-pip.py failed"
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110092 touch $LOCAL_PIP.downloaded
Dean Troyer62d1d692013-08-01 17:40:40 -050093 fi
Jens Harbott (frickler)279a7582018-04-16 12:08:30 +000094 sudo -H -E python${PYTHON3_VERSION} $LOCAL_PIP
95 if ! python3_enabled; then
96 sudo -H -E python $LOCAL_PIP
Doug Hellmannddc38392015-05-07 21:06:24 +000097 fi
Dean Troyer62d1d692013-08-01 17:40:40 -050098}
99
Dean Troyer62d1d692013-08-01 17:40:40 -0500100
Franck Yelles683ff422014-06-19 02:14:42 -0700101function configure_pypi_alternative_url {
102 PIP_ROOT_FOLDER="$HOME/.pip"
103 PIP_CONFIG_FILE="$PIP_ROOT_FOLDER/pip.conf"
104 if [[ ! -d $PIP_ROOT_FOLDER ]]; then
105 echo "Creating $PIP_ROOT_FOLDER"
106 mkdir $PIP_ROOT_FOLDER
107 fi
108 if [[ ! -f $PIP_CONFIG_FILE ]]; then
109 echo "Creating $PIP_CONFIG_FILE"
110 touch $PIP_CONFIG_FILE
111 fi
112 if ! ini_has_option "$PIP_CONFIG_FILE" "global" "index-url"; then
Dean Troyerdc97cb72015-03-28 08:20:50 -0500113 # It means that the index-url does not exist
Franck Yelles683ff422014-06-19 02:14:42 -0700114 iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE"
115 fi
116
117}
118
Jeremy Stanley35b52832014-12-18 17:27:22 +0000119# Setuptools 8 implements PEP 440, and 8.0.4 adds a warning triggered any time
120# pkg_resources inspects the list of installed Python packages if there are
121# non-compliant version numbers in the egg-info (for example, from distro
122# system packaged Python libraries). This is off by default after 8.2 but can
123# be enabled by uncommenting the lines below.
124#PYTHONWARNINGS=$PYTHONWARNINGS,always::RuntimeWarning:pkg_resources
125#export PYTHONWARNINGS
Franck Yelles683ff422014-06-19 02:14:42 -0700126
Dean Troyer62d1d692013-08-01 17:40:40 -0500127# Show starting versions
128get_versions
129
Jens Harbottf1ed7c72020-06-11 05:51:26 +0000130if [[ -n $PYPI_ALTERNATIVE_URL ]]; then
131 configure_pypi_alternative_url
132fi
133
134# Just use system pkgs on Focal
135if [[ "$DISTRO" == focal ]]; then
136 exit 0
137fi
Dean Troyer62d1d692013-08-01 17:40:40 -0500138
Monty Taylordace92f2013-08-10 23:49:47 -0300139# Eradicate any and all system packages
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100140
Dirk Mueller4404f682018-03-02 00:37:58 +0100141# Python in fedora/suse depends on the python-pip package so removing it
Steve Bakerbd4048a2015-11-18 10:55:22 +1300142# results in a nonfunctional system. pip on fedora installs to /usr so pip
143# can safely override the system pip for all versions of fedora
Dirk Mueller4404f682018-03-02 00:37:58 +0100144if ! is_fedora && ! is_suse; then
Colleen Murphy782efb02020-05-11 18:28:32 -0700145 if is_package_installed python-pip ; then
146 uninstall_package python-pip
147 fi
148 if is_package_installed python3-pip ; then
149 uninstall_package python3-pip
150 fi
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100151fi
Dean Troyer62d1d692013-08-01 17:40:40 -0500152
Sean Dague7b63c5e2014-06-04 16:25:52 -0400153install_get_pip
Monty Taylordace92f2013-08-10 23:49:47 -0300154
Doug Hellmannddc38392015-05-07 21:06:24 +0000155set -x
Ian Wienand1e7f7382017-02-27 11:19:40 +1100156
157# Note setuptools is part of requirements.txt and we want to make sure
158# we obey any versioning as described there.
159pip_install_gr setuptools
Mathieu Gagné76ed4272014-06-05 16:50:40 -0400160
Monty Taylordace92f2013-08-10 23:49:47 -0300161get_versions