在rpm安装过程中运行Python脚本
在从rpm安装中运行Python脚本时,有什么需要注意的地方吗?
问题的核心是这样的:我们为部署我们的Django应用程序创建了一个自定义的RPM安装程序。在安装过程中,我想运行一个Django管理命令,这个命令会收集所有静态文件并将它们复制到一个预定义的位置。手动从命令行运行时,效果是这样的:
$ python2.6 manage.py collectstatic --noinput
/usr/lib/python2.6/site-packages/reversion/__init__.py:31: UserWarning: django-reversion 1.5 is intended for use with django 1.3.0. You are running django 1.3.1, so some features, such as admin integration, may not work. Please see https://github.com/etianen/django-reversion/wiki/Compatible-Django-Versions
"django_version": format_version(django.VERSION[:3]),
Copying '/usr/lib/python2.6/site-packages/django/contrib/admin/media/img/gis/move_vertex_on.png'
Copying '/usr/lib/python2.6/site-packages/django/contrib/admin/media/img/gis/move_vertex_off.png'
Copying '/usr/lib/python2.6/site-packages/django/contrib/admin/media/img/admin/icon_clock.gif'
Copying '/usr/lib/python2.6/site-packages/django/contrib/admin/media/img/admin/arrow-down.gif'
Copying '/usr/lib/python2.6/site-packages/django/contrib/admin/media/img/admin/inline-restore.png'
...
为了在RPM安装的一部分中运行这个命令,我在规格文件中添加了以下内容:
%post
# collect static files
pushd .
cd %{installpath}/src/app/
%{__python} manage.py collectstatic --noinput --settings=settings_prod
popd
问题是,当我运行这个时,我可以看到任务开始执行:
sudo rpm -U app-0.2.8.18889M-1.x86_64.rpm -vv
...
+ pushd .
/ /
+ cd /opt/qpsi/app/src/app/
+ /usr/bin/python2.6 manage.py collectstatic --noinput --settings=settings_prod
/usr/lib/python2.6/site-packages/reversion/__init__.py:31: UserWarning: django-reversion 1.5 is intended for use with django 1.3.0. You are running django 1.3.1, so some features, such as admin integration, may not work. Please see https://github.com/etianen/django-reversion/wiki/Compatible-Django-Versions
"django_version": format_version(django.VERSION[:3]),
There is no South database module 'south.db.oracle' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS.
+ popd
但是在输出中没有看到预期的文件列表被复制,静态目录实际上也没有被填充。
所以问题是:
- 在从RPM运行Python脚本时,有什么特别需要注意的地方吗?
- 我使用了rpm命令的-vv选项来获取安装过程的详细输出,但有没有办法进一步调试在Python脚本启动后,rpm内部发生了什么?
谢谢,D。
3 个回答
0
你应该看看virtualenv(http://pypi.python.org/pypi/virtualenv),然后让你的rpm打包整个虚拟环境,以及site-packages目录,这样你需要的库(也叫“egg”)就能在部署的系统上使用,而且这些库的版本也会符合你的脚本要求。(在上面的例子中,听起来south的版本可能和你预期的不一样)。
接着在%post部分,调用你虚拟环境里的python,而不是系统自带的python。
缺少库的问题比较容易发现,但由于库版本不同而引发的问题可能就不那么明显了。
0
那我们可以试试用符号链接来解决这个问题...
%post
#Configure django admin media if it hasn't already been:
[ -d /path/to/new/media ] || ln -s /usr/lib/python2.6/site-packages/django/contrib/admin/media/ /path/to/new/media
0
我最后把脚本从 %post 移到了 %build。这解决了问题。