blob: dbe52782a479a616f021fee74909de85ae5857ee [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:
Doug Hellmannddc38392015-05-07 21:06:24 +00008# - if USE_PYTHON3=True, 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.
38# [1] http://git.openstack.org/cgit/openstack-infra/project-config/tree/nodepool/elements/cache-devstack/source-repository-pip
39
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
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
Matthew Treinish1d271552016-01-19 20:29:46 -050092 sudo -H -E python $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt
Doug Hellmannddc38392015-05-07 21:06:24 +000093 if python3_enabled; then
Matthew Treinish1d271552016-01-19 20:29:46 -050094 sudo -H -E python${PYTHON3_VERSION} $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt
Doug Hellmannddc38392015-05-07 21:06:24 +000095 fi
Dean Troyer62d1d692013-08-01 17:40:40 -050096}
97
Dean Troyer62d1d692013-08-01 17:40:40 -050098
Franck Yelles683ff422014-06-19 02:14:42 -070099function configure_pypi_alternative_url {
100 PIP_ROOT_FOLDER="$HOME/.pip"
101 PIP_CONFIG_FILE="$PIP_ROOT_FOLDER/pip.conf"
102 if [[ ! -d $PIP_ROOT_FOLDER ]]; then
103 echo "Creating $PIP_ROOT_FOLDER"
104 mkdir $PIP_ROOT_FOLDER
105 fi
106 if [[ ! -f $PIP_CONFIG_FILE ]]; then
107 echo "Creating $PIP_CONFIG_FILE"
108 touch $PIP_CONFIG_FILE
109 fi
110 if ! ini_has_option "$PIP_CONFIG_FILE" "global" "index-url"; then
Dean Troyerdc97cb72015-03-28 08:20:50 -0500111 # It means that the index-url does not exist
Franck Yelles683ff422014-06-19 02:14:42 -0700112 iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE"
113 fi
114
115}
116
Jeremy Stanley35b52832014-12-18 17:27:22 +0000117# Setuptools 8 implements PEP 440, and 8.0.4 adds a warning triggered any time
118# pkg_resources inspects the list of installed Python packages if there are
119# non-compliant version numbers in the egg-info (for example, from distro
120# system packaged Python libraries). This is off by default after 8.2 but can
121# be enabled by uncommenting the lines below.
122#PYTHONWARNINGS=$PYTHONWARNINGS,always::RuntimeWarning:pkg_resources
123#export PYTHONWARNINGS
Franck Yelles683ff422014-06-19 02:14:42 -0700124
Dean Troyer62d1d692013-08-01 17:40:40 -0500125# Show starting versions
126get_versions
127
Dean Troyer62d1d692013-08-01 17:40:40 -0500128# Do pip
Dean Troyer62d1d692013-08-01 17:40:40 -0500129
Monty Taylordace92f2013-08-10 23:49:47 -0300130# Eradicate any and all system packages
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100131
Attila Fazekas72b233c2016-06-01 16:43:07 +0200132# Python in fedora depends on the python-pip package so removing it
Steve Bakerbd4048a2015-11-18 10:55:22 +1300133# results in a nonfunctional system. pip on fedora installs to /usr so pip
134# can safely override the system pip for all versions of fedora
135if ! is_fedora ; then
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100136 uninstall_package python-pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000137 uninstall_package python3-pip
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100138fi
Dean Troyer62d1d692013-08-01 17:40:40 -0500139
Sean Dague7b63c5e2014-06-04 16:25:52 -0400140install_get_pip
Monty Taylordace92f2013-08-10 23:49:47 -0300141
Franck Yelles683ff422014-06-19 02:14:42 -0700142if [[ -n $PYPI_ALTERNATIVE_URL ]]; then
143 configure_pypi_alternative_url
144fi
145
Doug Hellmannddc38392015-05-07 21:06:24 +0000146set -x
Ian Wienand1e7f7382017-02-27 11:19:40 +1100147
148# Note setuptools is part of requirements.txt and we want to make sure
149# we obey any versioning as described there.
150pip_install_gr setuptools
Mathieu Gagné76ed4272014-06-05 16:50:40 -0400151
Monty Taylordace92f2013-08-10 23:49:47 -0300152get_versions