| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
|  | 2 |  | 
|  | 3 | # **install_pip.sh** | 
|  | 4 |  | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 5 | # Update pip and friends to a known common version | 
|  | 6 |  | 
|  | 7 | # Assumptions: | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 8 | # - if USE_PYTHON3=True, PYTHON3_VERSION refers to a version already installed | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 9 |  | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 10 | set -o errexit | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 11 |  | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 12 | # Keep track of the current directory | 
|  | 13 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) | 
|  | 14 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` | 
|  | 15 |  | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 16 | # Change dir to top of DevStack | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 17 | cd $TOP_DIR | 
|  | 18 |  | 
|  | 19 | # Import common functions | 
| Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 20 | source $TOP_DIR/stackrc | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 21 |  | 
| Sean Dague | 646085d | 2016-03-21 17:00:51 -0400 | [diff] [blame] | 22 | # don't start tracing until after we've sourced the world | 
|  | 23 | set -o xtrace | 
|  | 24 |  | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 25 | FILES=$TOP_DIR/files | 
|  | 26 |  | 
| Andreas Scheuring | 0063495 | 2016-08-26 10:29:20 +0200 | [diff] [blame] | 27 | # 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 |  | 
|  | 40 | PIP_GET_PIP_URL=${PIP_GET_PIP_URL:-"https://bootstrap.pypa.io/get-pip.py"} | 
| Sean Dague | f625ffe | 2014-06-05 07:04:41 -0400 | [diff] [blame] | 41 | LOCAL_PIP="$FILES/$(basename $PIP_GET_PIP_URL)" | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 42 |  | 
|  | 43 | GetDistro | 
|  | 44 | echo "Distro: $DISTRO" | 
|  | 45 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 46 | function get_versions { | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 47 | # FIXME(dhellmann): Deal with multiple python versions here? This | 
|  | 48 | # is just used for reporting, so maybe not? | 
| Attila Fazekas | 46ea723 | 2013-10-07 07:29:27 +0200 | [diff] [blame] | 49 | PIP=$(which pip 2>/dev/null || which pip-python 2>/dev/null || true) | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 50 | if [[ -n $PIP ]]; then | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 51 | PIP_VERSION=$($PIP --version | awk '{ print $2}') | 
| Monty Taylor | dace92f | 2013-08-10 23:49:47 -0300 | [diff] [blame] | 52 | echo "pip: $PIP_VERSION" | 
| Attila Fazekas | 46ea723 | 2013-10-07 07:29:27 +0200 | [diff] [blame] | 53 | else | 
|  | 54 | echo "pip: Not Installed" | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 55 | fi | 
|  | 56 | } | 
|  | 57 |  | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 58 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 59 | function install_get_pip { | 
| Sean Dague | 0280f6f | 2015-10-07 09:19:53 -0400 | [diff] [blame] | 60 | # 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 Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 69 | # The OpenStack gate and others put a cached version of get-pip.py | 
| Ian Wienand | 7c4ce9e | 2015-03-10 11:32:26 +1100 | [diff] [blame] | 70 | # for this to find, explicitly to avoid download issues. | 
|  | 71 | # | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 72 | # However, if DevStack *did* download the file, we want to check | 
|  | 73 | # for updates; people can leave their stacks around for a long | 
| Ian Wienand | 7c4ce9e | 2015-03-10 11:32:26 +1100 | [diff] [blame] | 74 | # 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 Dague | a0cc291 | 2015-10-07 09:06:42 -0400 | [diff] [blame] | 80 | # 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 Dague | fa41b5b | 2015-10-08 06:05:20 -0400 | [diff] [blame] | 87 | curl -f --retry 6 --retry-delay 5 \ | 
| Sean Dague | a0cc291 | 2015-10-07 09:06:42 -0400 | [diff] [blame] | 88 | $timecond -o $LOCAL_PIP $PIP_GET_PIP_URL || \ | 
| Sean Dague | f625ffe | 2014-06-05 07:04:41 -0400 | [diff] [blame] | 89 | die $LINENO "Download of get-pip.py failed" | 
| Ian Wienand | 7c4ce9e | 2015-03-10 11:32:26 +1100 | [diff] [blame] | 90 | touch $LOCAL_PIP.downloaded | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 91 | fi | 
| Matthew Treinish | 1d27155 | 2016-01-19 20:29:46 -0500 | [diff] [blame] | 92 | sudo -H -E python $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 93 | if python3_enabled; then | 
| Matthew Treinish | 1d27155 | 2016-01-19 20:29:46 -0500 | [diff] [blame] | 94 | sudo -H -E python${PYTHON3_VERSION} $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 95 | fi | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 98 |  | 
| Franck Yelles | 683ff42 | 2014-06-19 02:14:42 -0700 | [diff] [blame] | 99 | function 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 Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 111 | # It means that the index-url does not exist | 
| Franck Yelles | 683ff42 | 2014-06-19 02:14:42 -0700 | [diff] [blame] | 112 | iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE" | 
|  | 113 | fi | 
|  | 114 |  | 
|  | 115 | } | 
|  | 116 |  | 
| Jeremy Stanley | 35b5283 | 2014-12-18 17:27:22 +0000 | [diff] [blame] | 117 | # 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 Yelles | 683ff42 | 2014-06-19 02:14:42 -0700 | [diff] [blame] | 124 |  | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 125 | # Show starting versions | 
|  | 126 | get_versions | 
|  | 127 |  | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 128 | # Do pip | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 129 |  | 
| Monty Taylor | dace92f | 2013-08-10 23:49:47 -0300 | [diff] [blame] | 130 | # Eradicate any and all system packages | 
| Attila Fazekas | 9127c1a | 2015-11-05 10:09:02 +0100 | [diff] [blame] | 131 |  | 
| Attila Fazekas | 72b233c | 2016-06-01 16:43:07 +0200 | [diff] [blame] | 132 | # Python in fedora depends on the python-pip package so removing it | 
| Steve Baker | bd4048a | 2015-11-18 10:55:22 +1300 | [diff] [blame] | 133 | # 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 | 
|  | 135 | if ! is_fedora ; then | 
| Attila Fazekas | 9127c1a | 2015-11-05 10:09:02 +0100 | [diff] [blame] | 136 | uninstall_package python-pip | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 137 | uninstall_package python3-pip | 
| Attila Fazekas | 9127c1a | 2015-11-05 10:09:02 +0100 | [diff] [blame] | 138 | fi | 
| Dean Troyer | 62d1d69 | 2013-08-01 17:40:40 -0500 | [diff] [blame] | 139 |  | 
| Sean Dague | 7b63c5e | 2014-06-04 16:25:52 -0400 | [diff] [blame] | 140 | install_get_pip | 
| Monty Taylor | dace92f | 2013-08-10 23:49:47 -0300 | [diff] [blame] | 141 |  | 
| Franck Yelles | 683ff42 | 2014-06-19 02:14:42 -0700 | [diff] [blame] | 142 | if [[ -n $PYPI_ALTERNATIVE_URL ]]; then | 
|  | 143 | configure_pypi_alternative_url | 
|  | 144 | fi | 
|  | 145 |  | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 146 | set -x | 
| Ian Wienand | 1e7f738 | 2017-02-27 11:19:40 +1100 | [diff] [blame] | 147 |  | 
|  | 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 | 
| Mathieu Gagné | 76ed427 | 2014-06-05 16:50:40 -0400 | [diff] [blame] | 151 |  | 
| Monty Taylor | dace92f | 2013-08-10 23:49:47 -0300 | [diff] [blame] | 152 | get_versions |