Image building tools for OpenStack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

04-install-pip 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
  3. set -x
  4. fi
  5. set -eu
  6. set -o pipefail
  7. if [[ $DISTRO_NAME =~ (opensuse|fedora|centos|centos7|rhel|rhel7) ]]; then
  8. # Default packages
  9. _do_py3=0
  10. _extra_repo=''
  11. # see notes below on this var...
  12. _clear_old_files=0
  13. case "$DISTRO_NAME" in
  14. centos*|rhel7)
  15. # note python2-pip in epel
  16. _extra_repo="--enablerepo=epel"
  17. packages="python-virtualenv python2-pip"
  18. if [[ "$(rpm -q --qf '[%{obsoletes}\n]' python2-setuptools)" == "python-setuptools" ]]; then
  19. # If OpenStack release is installed, then python-setuptools is
  20. # obsoleted by python2-setuptools
  21. packages+=" python2-setuptools"
  22. else
  23. packages+=" python-setuptools"
  24. fi
  25. # see notes below
  26. _clear_old_files=1
  27. ;;
  28. fedora)
  29. _do_py3=1
  30. packages="python2-virtualenv python2-pip python2-setuptools"
  31. packages+=" python3-virtualenv python3-pip python3-setuptools"
  32. ;;
  33. rhel)
  34. case "$DIB_RELEASE" in
  35. 8)
  36. _do_py3=1
  37. _clear_old_files=0
  38. packages=" python3-virtualenv python3-pip python3-setuptools"
  39. ;;
  40. 7)
  41. # note python2-pip in epel
  42. _extra_repo="--enablerepo=epel"
  43. _clear_old_files=1
  44. packages="python-virtualenv python2-pip"
  45. if [[ "$(rpm -q --qf '[%{obsoletes}\n]' python2-setuptools)" == "python-setuptools" ]]; then
  46. packages+=" python2-setuptools"
  47. else
  48. packages+=" python-setuptools"
  49. fi
  50. ;;
  51. esac
  52. ;;
  53. opensuse)
  54. case "$DIB_RELEASE" in
  55. 42*)
  56. packages="python-virtualenv python-pip python-setuptools"
  57. _clear_old_files=1
  58. ;;
  59. tumbleweed|15*)
  60. # XXX: python3?
  61. packages="python2-virtualenv python2-pip python2-setuptools"
  62. ;;
  63. esac
  64. ;;
  65. esac
  66. # force things to happen so our assumptions hold
  67. pip_args="-U --force-reinstall"
  68. # GENERAL WARNING : mixing packaged python libraries with
  69. # pip-installed versions always creates issues. Upstream
  70. # openstack-infra uses this a lot (especially devstack) but be
  71. # warned: here be dragons :)
  72. # Firstly we want to install the system packages. Otherwise later
  73. # on somebody does a "yum install python-virtualenv" and goes and
  74. # overwrites the pip installed version with the packaged version,
  75. # leading to all sorts of weird version issues.
  76. if [[ $DISTRO_NAME = opensuse ]]; then
  77. zypper -n install $packages
  78. else
  79. ${YUM:-yum} ${_extra_repo} install -y $packages
  80. fi
  81. # pip10 onwards (unlike earlier versions) will not uninstall
  82. # packages installed by distutils (note this is only a subset of
  83. # packages that don't use setuptools for various reasons; the
  84. # problem is essentially they do not include a manifest of files
  85. # in the package to delete, so pip was just guessing). We give it
  86. # a little help by clearing out the files from the packages we are
  87. # about to re-install so pip doesn't think anything is installed.
  88. # See: https://github.com/pypa/pip/issues/4805
  89. #
  90. # This is only necessary on old CentOS and suse -- for complicated
  91. # reasons of course. On Fedora, the Python2 virtualenv packages
  92. # are *not* distutils based and pip overwrites them correctly.
  93. # For python3, pip has changed to not overwrite system packages (a
  94. # long standing difference between Debuntu and Fedora), but a
  95. # number of tools run with "python3 -Es" to isolate themselves to
  96. # the package installed versions. So we definitely don't want to
  97. # clear the packaged versions out in that case.
  98. if [[ ${_clear_old_files} == 1 ]]; then
  99. for pkg in $packages; do
  100. rpm -ql $pkg | xargs rm -rf
  101. done
  102. fi
  103. # install the latest python2 pip; this overwrites packaged pip
  104. python /tmp/get-pip.py ${pip_args}
  105. # Install latest setuptools; there is a slight chicken-egg issue in
  106. # that pip requires setuptools for some operations like building a
  107. # wheel. But this simple install should be fine.
  108. pip install ${pip_args} setuptools
  109. if [[ $_do_py3 -eq 1 ]]; then
  110. # Repeat above for python3
  111. # python2 on fedora always installs into /usr/bin. Move pip2
  112. # binary out, as we want "pip" in the final image to be
  113. # python2 for historical reasons.
  114. mv /usr/bin/pip /usr/bin/pip2
  115. # You would think that installing python3 bits first, then
  116. # python2 would work -- alas get-pip.py doesn't seem to leave
  117. # python3 alone:
  118. # https://github.com/pypa/pip/issues/4435
  119. python3 /tmp/get-pip.py ${pip_args}
  120. pip3 install ${pip_args} setuptools
  121. # on < 27, this installed pip3 to /usr/bin/pip. On >=27 it's
  122. # /usr/local/bin/pip. reclaim /usr/bin/pip back to pip2 and
  123. # remove the /usr/local/bin/pip (i.e. python3 version) if it
  124. # exists, so that "pip" calls pip2 always. if we want pip3 we
  125. # call it explicitly.
  126. ln -sf /usr/bin/pip2 /usr/bin/pip
  127. rm -f /usr/local/bin/pip
  128. # So on Fedora, there are now supposed to be two versions of
  129. # python3 setuptools installed; the one installed by pip in
  130. # /usr/local and the one installed by the system
  131. # python3-setuptools rpm package in /usr/local. The idea is
  132. # that packaged python tools use the "system" python (with -Es
  133. # flag) and are isolated from pip installs ... except there is
  134. # an issue where pip clears out the RPM version files before
  135. # installing it's isolated version:
  136. # https://bugzilla.redhat.com/show_bug.cgi?id=1550368
  137. #
  138. # Thus we need to *reinstall* the RPM version now, so those
  139. # files come back and system tools continue to work
  140. dnf reinstall -y python3-setuptools
  141. fi
  142. # now install latest virtualenv. it vendors stuff it needs so
  143. # doesn't have issues with other system packages.
  144. # python[2|3]-virtualenv package has installed versioned scripts
  145. # (/usr/bin/virtualenv-[2|3]) but upstream does not! (see [2]).
  146. # For consistency, reinstall so we're just left with python2's
  147. # version. Note this is a rather moot point, the usual way we get
  148. # a python3 environment is to call "virtualenv -p python3 foo" and
  149. # that works to create a python3 virtualenv, even if using
  150. # python2's version. Thus we probably don't *really* need to
  151. # "pip3 install virtualenv". What we don't want is "virtualenv
  152. # foo" creating a python3 virtualenv by default, because that
  153. # confuses a lot of legacy code.
  154. #
  155. #[2] http://pkgs.fedoraproject.org/cgit/rpms/python-virtualenv.git/tree/python-virtualenv.spec#n116)
  156. pip install ${pip_args} virtualenv
  157. mv /usr/bin/virtualenv /usr/bin/virtualenv2
  158. if [[ $_do_py3 -eq 1 ]]; then
  159. pip3 install ${pip_args} virtualenv
  160. fi
  161. # Reclaim virtualenv to virtualenv2; similar to above, on fedora
  162. # >27 the pip3 version has gone into /usr/local/bin; remove it so
  163. # only /usr/bin/virtualenv exists
  164. ln -sf /usr/bin/virtualenv2 /usr/bin/virtualenv
  165. rm -f /usr/local/bin/virtualenv
  166. # at this point, we should have the latest
  167. # pip/setuptools/virtualenv packages for python2 & 3, and
  168. # "/usr/bin/pip" and "/usr/bin/virtualenv" should be python2
  169. # versions.
  170. if [[ $DISTRO_NAME = opensuse ]]; then
  171. for pkg in virtualenv pip setuptools; do
  172. cat - >> /etc/zypp/locks <<EOF
  173. type: package
  174. match_type: glob
  175. case_sensitive: on
  176. solvable_name: python-$pkg
  177. EOF
  178. done
  179. else
  180. # Add this to exclude so that we don't install a later package
  181. # over it if it updates. Note that fedora-minimal, bootstrapped
  182. # via yum, can have an old yum.conf around, so look for dnf first.
  183. if [[ -f /etc/dnf/dnf.conf ]]; then
  184. conf=/etc/dnf/dnf.conf
  185. elif [[ -f /etc/yum.conf ]]; then
  186. conf=/etc/yum.conf
  187. else
  188. die "No conf to modify?"
  189. fi
  190. echo "exclude=$packages" >> ${conf}
  191. fi
  192. elif [[ $DISTRO_NAME = gentoo ]]; then
  193. packages="dev-python/pip dev-python/virtualenv"
  194. emerge -U $packages
  195. else
  196. # pre-install packages so dependencies are there. We will
  197. # overwrite with latest below.
  198. packages="python-pip python3-pip python-virtualenv"
  199. # Unfortunately older ubuntu (trusty) doesn't have a
  200. # python3-virtualenv package -- it seems it wasn't ready at the
  201. # time and you had to use "python -m venv". Since then virtualenv
  202. # has gained 3.4 support so the pip install below will work
  203. if [[ ${DIB_PYTHON_VERSION} == 3 ]]; then
  204. packages+=" python3-virtualenv"
  205. fi
  206. apt-get -y install $packages
  207. # force things to happen so our assumptions hold
  208. pip_args="-U --force-reinstall"
  209. # These install into /usr/local/bin so override any packages, even
  210. # if installed later.
  211. python3 /tmp/get-pip.py $pip_args
  212. python2 /tmp/get-pip.py $pip_args
  213. pip3 install $pip_args virtualenv
  214. pip install $pip_args virtualenv
  215. fi