开发OpenStack,搭建环境是免不了使用pip安装,下面介绍使用pip搭建内部pip源操作步骤。

搭建

[root@xiexianbin_cn ~]# cd /var/local/
[root@xiexianbin_cn local]# mkdir pypi
[root@xiexianbin_cn local]# cd pypi/
[root@xiexianbin_cn pypi]# pip install pip2pi
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting pip2pi
  Downloading pip2pi-0.6.8-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): pip>=1.1 in /usr/lib/python2.7/site-packages (from pip2pi)
Installing collected packages: pip2pi
Successfully installed pip2pi-0.6.8
[root@xiexianbin_cn pypi]# 

同步tar.gz

[root@xiexianbin_cn pypi]# pip2tgz /var/local/pypi/ -r /var/local/pypi_requirements/requirements.txt 
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting python-keystoneclient (from -r requirements.txt (line 1))
  Downloading python_keystoneclient-3.7.0-py2.py3-none-any.whl (368kB)
    21% |######                          | 77kB 81kB/s eta 0:00:04

漫长的同步过程。。。

requirements.txt 是OpenStack组件的依赖,

requirements.txt内容

[root@xiexianbin_cn pypi_requirements]# cat requirements.txt 
python-keystoneclient
[root@xiexianbin_cn pypi]# 

建立索引

[root@xiexianbin_cn pypi]# dir2pi /var/local/pypi/
[root@xiexianbin_cn pypi]# 

创建好的结构

[root@xiexianbin_cn pypi]# ll
total 3140
-rw-r--r--  1 root root   20131 Nov 26 14:55 debtcollector-1.9.0-py2.py3-none-any.whl
-rw-r--r--  1 root root  235399 Nov 26 14:54 keystoneauth1-2.15.0-py2.py3-none-any.whl
-rw-r--r--  1 root root 1533436 Nov 26 14:59 netaddr-0.7.18-py2.py3-none-any.whl
-rw-r--r--  1 root root   99848 Nov 26 14:53 oslo.config-3.19.0-py2.py3-none-any.whl
-rw-r--r--  1 root root   41824 Nov 26 14:53 oslo.i18n-3.10.0-py2.py3-none-any.whl
-rw-r--r--  1 root root   21073 Nov 26 14:53 oslo.serialization-2.14.0-py2.py3-none-any.whl
-rw-r--r--  1 root root   96492 Nov 26 14:54 oslo.utils-3.18.0-py2.py3-none-any.whl
-rw-r--r--  1 root root   96932 Nov 26 14:54 pbr-1.10.0-py2.py3-none-any.whl
-rw-r--r--  1 root root  368527 Nov 26 14:53 python_keystoneclient-3.7.0-py2.py3-none-any.whl
-rw-r--r--  1 root root  574526 Nov 26 14:54 requests-2.12.1-py2.py3-none-any.whl
-rw-r--r--  1 root root   21814 Nov 26 14:55 rfc3986-0.4.1-py2.py3-none-any.whl
drwxr-xr-x 15 root root    4096 Nov 26 15:02 simple
-rw-r--r--  1 root root   10341 Nov 26 14:55 six-1.10.0-py2.py3-none-any.whl
-rw-r--r--  1 root root   32860 Nov 26 14:54 stevedore-1.18.0-py2.py3-none-any.whl
[root@xiexianbin_cn pypi]# cd simple/
[root@xiexianbin_cn simple]# ll
total 56
drwxr-xr-x 2 root root 4096 Nov 26 15:02 debtcollector
-rw-r--r-- 1 root root  668 Nov 26 15:02 index.html
drwxr-xr-x 2 root root 4096 Nov 26 15:02 keystoneauth1
drwxr-xr-x 2 root root 4096 Nov 26 15:02 netaddr
drwxr-xr-x 2 root root 4096 Nov 26 15:02 oslo.config
drwxr-xr-x 2 root root 4096 Nov 26 15:02 oslo.i18n
drwxr-xr-x 2 root root 4096 Nov 26 15:02 oslo.serialization
drwxr-xr-x 2 root root 4096 Nov 26 15:02 oslo.utils
drwxr-xr-x 2 root root 4096 Nov 26 15:02 pbr
drwxr-xr-x 2 root root 4096 Nov 26 15:02 python-keystoneclient
drwxr-xr-x 2 root root 4096 Nov 26 15:02 requests
drwxr-xr-x 2 root root 4096 Nov 26 15:02 rfc3986
drwxr-xr-x 2 root root 4096 Nov 26 15:02 six
drwxr-xr-x 2 root root 4096 Nov 26 15:02 stevedore
[root@xiexianbin_cn simple]# cd six/
[root@xiexianbin_cn six]# ll
total 4
-rw-r--r-- 1 root root 84 Nov 26 15:02 index.html
lrwxrwxrwx 1 root root 37 Nov 26 15:02 six-1.10.0-py2.py3-none-any.whl -> ../../six-1.10.0-py2.py3-none-any.whl
[root@xiexianbin_cn six]# cat index.html 
<a href='six-1.10.0-py2.py3-none-any.whl'>six-1.10.0-py2.py3-none-any.whl</a><br />
[root@xiexianbin_cn six]# 

启动HTTP服务,本文为简单期间,采用SimpleHTTPServer启动http服务

[root@xiexianbin_cn pypi]# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

添加iptables

[root@xiexianbin_cn pypi]# iptables -I INPUT -p tcp -m tcp --dport 8000 -j ACCEPT
[root@xiexianbin_cn pypi]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@xiexianbin_cn pypi]# 

客户端使用

[root@xiexianbin_cn ~]# cat .pip/.pip.conf
[global]
index-url = http://host_ip_or_domain/simple
[root@xiexianbin_cn ~]# 

大功告成!

参考

https://xiexianbin.cn/python/2016/09/18/pip-cloud-not-use-pypi.python.org-bug-for-China

完毕。