blob: 87660a677c1394640ecc2ccc02055b240123b315 [file] [log] [blame]
Dean Troyer62d1d692013-08-01 17:40:40 -05001#!/usr/bin/env bash
2
3# **install_pip.sh**
4
Monty Taylordace92f2013-08-10 23:49:47 -03005# install_pip.sh [--pip-version <version>] [--use-get-pip] [--force]
Dean Troyer62d1d692013-08-01 17:40:40 -05006#
7# Update pip and friends to a known common version
8
9# Assumptions:
Dean Troyer62d1d692013-08-01 17:40:40 -050010# - update pip to $INSTALL_PIP_VERSION
Doug Hellmannddc38392015-05-07 21:06:24 +000011# - if USE_PYTHON3=True, PYTHON3_VERSION refers to a version already installed
Dean Troyer62d1d692013-08-01 17:40:40 -050012
Adam Spiersc85ade72013-10-01 00:35:16 +010013set -o errexit
Adam Spiersc85ade72013-10-01 00:35:16 +010014
Dean Troyer62d1d692013-08-01 17:40:40 -050015# Keep track of the current directory
16TOOLS_DIR=$(cd $(dirname "$0") && pwd)
17TOP_DIR=`cd $TOOLS_DIR/..; pwd`
18
Dean Troyerdc97cb72015-03-28 08:20:50 -050019# Change dir to top of DevStack
Dean Troyer62d1d692013-08-01 17:40:40 -050020cd $TOP_DIR
21
22# Import common functions
Clark Boylan05aa3842015-08-03 11:14:13 -070023source $TOP_DIR/stackrc
Dean Troyer62d1d692013-08-01 17:40:40 -050024
Sean Dague646085d2016-03-21 17:00:51 -040025# don't start tracing until after we've sourced the world
26set -o xtrace
27
Dean Troyer62d1d692013-08-01 17:40:40 -050028FILES=$TOP_DIR/files
29
Sean Dague7b63c5e2014-06-04 16:25:52 -040030PIP_GET_PIP_URL=https://bootstrap.pypa.io/get-pip.py
Sean Daguef625ffe2014-06-05 07:04:41 -040031LOCAL_PIP="$FILES/$(basename $PIP_GET_PIP_URL)"
Dean Troyer62d1d692013-08-01 17:40:40 -050032
33GetDistro
34echo "Distro: $DISTRO"
35
Ian Wienandaee18c72014-02-21 15:35:08 +110036function get_versions {
Doug Hellmannddc38392015-05-07 21:06:24 +000037 # FIXME(dhellmann): Deal with multiple python versions here? This
38 # is just used for reporting, so maybe not?
Attila Fazekas46ea7232013-10-07 07:29:27 +020039 PIP=$(which pip 2>/dev/null || which pip-python 2>/dev/null || true)
Dean Troyer62d1d692013-08-01 17:40:40 -050040 if [[ -n $PIP ]]; then
Dean Troyer62d1d692013-08-01 17:40:40 -050041 PIP_VERSION=$($PIP --version | awk '{ print $2}')
Monty Taylordace92f2013-08-10 23:49:47 -030042 echo "pip: $PIP_VERSION"
Attila Fazekas46ea7232013-10-07 07:29:27 +020043 else
44 echo "pip: Not Installed"
Dean Troyer62d1d692013-08-01 17:40:40 -050045 fi
46}
47
Dean Troyer62d1d692013-08-01 17:40:40 -050048
Ian Wienandaee18c72014-02-21 15:35:08 +110049function install_get_pip {
Sean Dague0280f6f2015-10-07 09:19:53 -040050 # If get-pip.py isn't python, delete it. This was probably an
51 # outage on the server.
52 if [[ -r $LOCAL_PIP ]]; then
53 if ! head -1 $LOCAL_PIP | grep -q '#!/usr/bin/env python'; then
54 echo "WARNING: Corrupt $LOCAL_PIP found removing"
55 rm $LOCAL_PIP
56 fi
57 fi
58
Dean Troyerdc97cb72015-03-28 08:20:50 -050059 # The OpenStack gate and others put a cached version of get-pip.py
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110060 # for this to find, explicitly to avoid download issues.
61 #
Dean Troyerdc97cb72015-03-28 08:20:50 -050062 # However, if DevStack *did* download the file, we want to check
63 # for updates; people can leave their stacks around for a long
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110064 # time and in the mean-time pip might get upgraded.
65 #
66 # Thus we use curl's "-z" feature to always check the modified
67 # since and only download if a new version is out -- but only if
68 # it seems we downloaded the file originally.
69 if [[ ! -r $LOCAL_PIP || -r $LOCAL_PIP.downloaded ]]; then
Sean Daguea0cc2912015-10-07 09:06:42 -040070 # only test freshness if LOCAL_PIP is actually there,
71 # otherwise we generate a scary warning.
72 local timecond=""
73 if [[ -r $LOCAL_PIP ]]; then
74 timecond="-z $LOCAL_PIP"
75 fi
76
Sean Daguefa41b5b2015-10-08 06:05:20 -040077 curl -f --retry 6 --retry-delay 5 \
Sean Daguea0cc2912015-10-07 09:06:42 -040078 $timecond -o $LOCAL_PIP $PIP_GET_PIP_URL || \
Sean Daguef625ffe2014-06-05 07:04:41 -040079 die $LINENO "Download of get-pip.py failed"
Ian Wienand7c4ce9e2015-03-10 11:32:26 +110080 touch $LOCAL_PIP.downloaded
Dean Troyer62d1d692013-08-01 17:40:40 -050081 fi
Matthew Treinish1d271552016-01-19 20:29:46 -050082 sudo -H -E python $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt
Doug Hellmannddc38392015-05-07 21:06:24 +000083 if python3_enabled; then
Matthew Treinish1d271552016-01-19 20:29:46 -050084 sudo -H -E python${PYTHON3_VERSION} $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt
Doug Hellmannddc38392015-05-07 21:06:24 +000085 fi
Dean Troyer62d1d692013-08-01 17:40:40 -050086}
87
Dean Troyer62d1d692013-08-01 17:40:40 -050088
Franck Yelles683ff422014-06-19 02:14:42 -070089function configure_pypi_alternative_url {
90 PIP_ROOT_FOLDER="$HOME/.pip"
91 PIP_CONFIG_FILE="$PIP_ROOT_FOLDER/pip.conf"
92 if [[ ! -d $PIP_ROOT_FOLDER ]]; then
93 echo "Creating $PIP_ROOT_FOLDER"
94 mkdir $PIP_ROOT_FOLDER
95 fi
96 if [[ ! -f $PIP_CONFIG_FILE ]]; then
97 echo "Creating $PIP_CONFIG_FILE"
98 touch $PIP_CONFIG_FILE
99 fi
100 if ! ini_has_option "$PIP_CONFIG_FILE" "global" "index-url"; then
Dean Troyerdc97cb72015-03-28 08:20:50 -0500101 # It means that the index-url does not exist
Franck Yelles683ff422014-06-19 02:14:42 -0700102 iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE"
103 fi
104
105}
106
Jeremy Stanley35b52832014-12-18 17:27:22 +0000107# Setuptools 8 implements PEP 440, and 8.0.4 adds a warning triggered any time
108# pkg_resources inspects the list of installed Python packages if there are
109# non-compliant version numbers in the egg-info (for example, from distro
110# system packaged Python libraries). This is off by default after 8.2 but can
111# be enabled by uncommenting the lines below.
112#PYTHONWARNINGS=$PYTHONWARNINGS,always::RuntimeWarning:pkg_resources
113#export PYTHONWARNINGS
Franck Yelles683ff422014-06-19 02:14:42 -0700114
Dean Troyer62d1d692013-08-01 17:40:40 -0500115# Show starting versions
116get_versions
117
Dean Troyer62d1d692013-08-01 17:40:40 -0500118# Do pip
Dean Troyer62d1d692013-08-01 17:40:40 -0500119
Monty Taylordace92f2013-08-10 23:49:47 -0300120# Eradicate any and all system packages
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100121
Steve Bakerbd4048a2015-11-18 10:55:22 +1300122# Python in f23 and f22 depends on the python-pip package so removing it
123# results in a nonfunctional system. pip on fedora installs to /usr so pip
124# can safely override the system pip for all versions of fedora
125if ! is_fedora ; then
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100126 uninstall_package python-pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000127 uninstall_package python3-pip
Attila Fazekas9127c1a2015-11-05 10:09:02 +0100128fi
Dean Troyer62d1d692013-08-01 17:40:40 -0500129
Sean Dague7b63c5e2014-06-04 16:25:52 -0400130install_get_pip
Monty Taylordace92f2013-08-10 23:49:47 -0300131
Franck Yelles683ff422014-06-19 02:14:42 -0700132if [[ -n $PYPI_ALTERNATIVE_URL ]]; then
133 configure_pypi_alternative_url
134fi
135
Doug Hellmannddc38392015-05-07 21:06:24 +0000136set -x
Jeremy Stanleyb7ebc472014-12-17 03:32:42 +0000137pip_install -U setuptools
Mathieu Gagné76ed4272014-06-05 16:50:40 -0400138
Monty Taylordace92f2013-08-10 23:49:47 -0300139get_versions