如何通过RPM分发Python / Django App

时间:2021-11-15 17:08:15

I've read over this question and I think that is a great start, but I'm looking for more insight.

我已经阅读了这个问题,我认为这是一个很好的开始,但我正在寻找更多的见解。

I have written an application using Python 2.6 and Django 1.2.1. I would like to deploy this application on a system that does not have Python 2.6 or Django installed. The system itself doesn't have many things installed - sqlite3, pysqlite2, gcc, etc. The system does have Python 2.4.3, however.

我使用Python 2.6和Django 1.2.1编写了一个应用程序。我想在没有安装Python 2.6或Django的系统上部署此应用程序。系统本身并没有安装很多东西--sqlite3,pysqlite2,gcc等。但系统确实有Python 2.4.3。

What would be the most sane way to create RPMs of Python 2.6, Django, and my application itself (a standard Django app) so that each can be installed separately to allow for upgrades of different components?

什么是创建Python 2.6,Django和我的应用程序本身(一个标准的Django应用程序)的RPM最理智的方式,以便每个可以单独安装,以允许升级不同的组件?

I don't know much about creating source RPMs but I suspect that is information readily available. If there are any especially good resources for creating source RPMs, I would appreciate them too.

我不太了解创建源RPM,但我怀疑这是可用的信息。如果有任何特别好的资源来创建源RPM,我也会很感激。

The system is ESX 4 Update 1, if it matters.

如果重要,系统是ESX 4 Update 1。

Thanks!

谢谢!

UPDATE: It also never occurred to me that distributing Python eggs or even using setup.py is a viable solution to content distribution. I would be interested in hearing about that too.

更新:我从来没有想过,分发Python鸡蛋甚至使用setup.py是内容分发的可行解决方案。我也有兴趣听到这个。

5 个解决方案

#1


3  

Python packages are normally distributed with setuptools/distribute. Generally you can install every python package (and it's dependencies) with easy_install <package_name> (optionally with a specific version number). I would recommend you to use setuptools as our primary distribution method, because:

Python包通常与setuptools / distribute一起分发。通常,您可以使用easy_install (可选择使用特定版本号)安装每个python包(以及它的依赖项)。我建议你使用setuptools作为我们的主要分发方法,因为:

  • it works on all platforms
  • 它适用于所有平台
  • is the preferred way for Python packages in general
  • 一般来说,它是Python包的首选方式
  • .rpm or .deb install scripts can reuse parts of it too
  • .rpm或.deb安装脚本也可以重用它的一部分
  • you can install this packages inside a virtualenv without the need of root privileges, conflict-free of the rest of the installed web applications on the server
  • 您可以在virtualenv中安装此软件包,而无需root权限,服务器上已安装的其他Web应用程序无冲突

A good resource for you to read is Deploying with Distribute. It focuses on Flask (another web framework), but simply replacing "Flask" with "Django" will do the trick :)

您可以阅读的一个很好的资源是Deploying with Distribute。它专注于Flask(另一个Web框架),但只需将“Flask”替换为“Django”就可以了

Another python tool which is great for distributing and deploying things is fabric, which you can use in addition to distribute (or without it, however you want). With fabric you can for example, bootstrap and install your application (inclusive dependencies) on N servers at once, manage several development, testing and production servers and update them all concurrently with a single command.

另一个非常适合分发和部署东西的python工具是fabric,你可以使用它除了分发(或者没有它,你想要的)。使用结构,您可以同时在N个服务器上引导和安装应用程序(包含依赖项),管理多个开发,测试和生产服务器,并使用单个命令同时更新它们。

This article might help you to get started with Fabric: Deploying with Fabric

本文可能会帮助您开始使用Fabric:使用Fabric进行部署

And additionally I would also recommend you to publish your repository (or mirror an existing one) on github or bitbucket for people who want to get the latest sources manually and install your application with python setup.py install afterwards. The setup.py install script is part of distribute which I've mentioned before.

此外,我还建议您在github或bitbucket上发布您的存储库(或镜像现有存储库),以便那些想要手动获取最新资源并随后使用python setup.py install安装应用程序的人员。 setup.py安装脚本是我之前提到的分发的一部分。

#2


4  

On top of all the normal issues encountered when packaging Python code, Django projects have a few other issues that need to be handled:

除了打包Python代码时遇到的所有常见问题,Django项目还有一些其他问题需要处理:

  • Playing nice with system paths
  • 与系统路径玩得很好
  • Playing nice with system facilities
  • 与系统设施配合使用
  • Handling translations
  • 处理翻译
  • Randomizing the secret key
  • 随机化密钥

I had to solve these issues for packaging Transifex, and I recommend you investigate the various bits that have put in place in order to make it work:

我必须解决这些包装Transifex的问题,我建议你研究一下为了使它工作的各种各样的问题:

  • Install under /usr/share/<projectname>
  • 在/ usr / share / 下安装
  • Use execfile() to load settings from /etc/<projectname>
  • 使用execfile()从/ etc / 加载设置
  • The huge translation-handling runaround
  • 巨大的翻译处理周期
  • Replacement of the secret key in the post-install script
  • 替换安装后脚本中的密钥
  • All the other bloody changes and fudges that were required
  • 所有其他所需的血腥变化和软糖

#3


3  

Refer to chapter 5 of the Python documentation. It will show how to deal with the bdist Python utility. With it, you'll be able to generate packages (or, how it says, "built distribution") of several types, includding RPM.

请参阅Python文档的第5章。它将展示如何处理bdist Python实用程序。有了它,您将能够生成几种类型的包(或者,如何说,“构建分发”),包括RPM。

#4


2  

There are already RPM's of python 2.6 and django available - so those at least are (relatively) easy to grab and install via RPM. FOr creating an RPM for your project, there's a write up on some of the details using Distutils, that goes into the details of what you need in a spec file and what you invoke to make it happen.

已有RPM的python 2.6和django可用 - 所以至少(相对)容易通过RPM获取和安装。为项目创建一个RPM,可以使用Distutils编写一些详细信息,详细说明spec文件中需要的内容以及调用它来实现的内容。

There's also a note about creating RPM's from python on * at Creating Python RPM that has a touch of detail as well.

还有一个关于在*上创建RPM的说明,创建Python RPM,还有一些细节。

From experience, packaging up python itself as an RPM is the trickiest of the critters, so taking advantage of what's already there is something I'd definitely recommend. For a few programs where I've seen this done, we have a Makefile (yeah, old school) run the setup.py bdist_rpm command on a continuous integration server, making the RPM available as the result of that build and immediately available on the CI server.

根据经验,将python本身打包为RPM是最狡猾的小动物,因此利用已有的东西我肯定会推荐。对于我已经看过这个的一些程序,我们有一个Makefile(是的,旧学校)在持续集成服务器上运行setup.py bdist_rpm命令,使得RPM可用作该构建的结果并立即可用于CI服务器。

#5


2  

I've written up a blog post on how to use setuptools and it's bdist_rpm command to create the rpm for you. So indeed, use setuptools, but indeed, distribute your app as an rpm. There's a few catches and weird under documented things in setuptools you can use to eventually end up in a nice workflow.

我写了一篇关于如何使用setuptools的博客文章,它是bdist_rpm命令为你创建rpm。确实,使用setuptools,但实际上,将您的应用程序分配为rpm。在setuptools中记录的东西有一些捕获和奇怪,你可以用来最终在一个很好的工作流程。

The key is you need a post install script that runs your migrations and collects your static files, that gets run after the rpm is installed

关键是您需要一个安装后脚本来运行迁移并收集静态文件,这些文件在安装rpm后运行

export DJANGO_SETTINGS_MODULE=accountpage.settings
mkdir -p /var/www/django/static && chown apache:apache /var/www/django/static
runuser -m apache -c 'echo "yes" | django-admin collectstatic'
django-admin migrate
service httpd restart

and you can tell setuptools to add this to the spec file before it creates the rpm by giving this option in the setup call:

你可以通过在setup调用中给出这个选项,告诉setuptools在创建rpm之前将它添加到spec文件中:

options = {'bdist_rpm':{'post_install' : 'bdist_rpm_post_install.sh'}},

options = {'bdist_rpm':{'post_install':'bdist_rpm_post_install.sh'}},

See the link above for a full working setup.py script, post install hook, and a testsuite that works with python setup.py test that will install all your dependencies locally, and run the django tests.

请参阅上面的链接,了解完整的setup.py脚本,安装后挂钩以及与python setup.py测试一起使用的测试套件,它将在本地安装所有依赖项,并运行django测试。

To have the rpm also automatically install it's dependencies, create a setup.cfg file which tells bdist_rpm about it's dependencies

要让rpm也自动安装它的依赖项,请创建一个setup.cfg文件,告诉bdist_rpm它的依赖项

[bdist_rpm]
requires = python >= 2.7
           django >= 1.8
           httpd
           Django-Select2 >= 4.3.1
           django-easy-select2
           django-model-utils
           pytz
           django-oauth-toolkit

#1


3  

Python packages are normally distributed with setuptools/distribute. Generally you can install every python package (and it's dependencies) with easy_install <package_name> (optionally with a specific version number). I would recommend you to use setuptools as our primary distribution method, because:

Python包通常与setuptools / distribute一起分发。通常,您可以使用easy_install (可选择使用特定版本号)安装每个python包(以及它的依赖项)。我建议你使用setuptools作为我们的主要分发方法,因为:

  • it works on all platforms
  • 它适用于所有平台
  • is the preferred way for Python packages in general
  • 一般来说,它是Python包的首选方式
  • .rpm or .deb install scripts can reuse parts of it too
  • .rpm或.deb安装脚本也可以重用它的一部分
  • you can install this packages inside a virtualenv without the need of root privileges, conflict-free of the rest of the installed web applications on the server
  • 您可以在virtualenv中安装此软件包,而无需root权限,服务器上已安装的其他Web应用程序无冲突

A good resource for you to read is Deploying with Distribute. It focuses on Flask (another web framework), but simply replacing "Flask" with "Django" will do the trick :)

您可以阅读的一个很好的资源是Deploying with Distribute。它专注于Flask(另一个Web框架),但只需将“Flask”替换为“Django”就可以了

Another python tool which is great for distributing and deploying things is fabric, which you can use in addition to distribute (or without it, however you want). With fabric you can for example, bootstrap and install your application (inclusive dependencies) on N servers at once, manage several development, testing and production servers and update them all concurrently with a single command.

另一个非常适合分发和部署东西的python工具是fabric,你可以使用它除了分发(或者没有它,你想要的)。使用结构,您可以同时在N个服务器上引导和安装应用程序(包含依赖项),管理多个开发,测试和生产服务器,并使用单个命令同时更新它们。

This article might help you to get started with Fabric: Deploying with Fabric

本文可能会帮助您开始使用Fabric:使用Fabric进行部署

And additionally I would also recommend you to publish your repository (or mirror an existing one) on github or bitbucket for people who want to get the latest sources manually and install your application with python setup.py install afterwards. The setup.py install script is part of distribute which I've mentioned before.

此外,我还建议您在github或bitbucket上发布您的存储库(或镜像现有存储库),以便那些想要手动获取最新资源并随后使用python setup.py install安装应用程序的人员。 setup.py安装脚本是我之前提到的分发的一部分。

#2


4  

On top of all the normal issues encountered when packaging Python code, Django projects have a few other issues that need to be handled:

除了打包Python代码时遇到的所有常见问题,Django项目还有一些其他问题需要处理:

  • Playing nice with system paths
  • 与系统路径玩得很好
  • Playing nice with system facilities
  • 与系统设施配合使用
  • Handling translations
  • 处理翻译
  • Randomizing the secret key
  • 随机化密钥

I had to solve these issues for packaging Transifex, and I recommend you investigate the various bits that have put in place in order to make it work:

我必须解决这些包装Transifex的问题,我建议你研究一下为了使它工作的各种各样的问题:

  • Install under /usr/share/<projectname>
  • 在/ usr / share / 下安装
  • Use execfile() to load settings from /etc/<projectname>
  • 使用execfile()从/ etc / 加载设置
  • The huge translation-handling runaround
  • 巨大的翻译处理周期
  • Replacement of the secret key in the post-install script
  • 替换安装后脚本中的密钥
  • All the other bloody changes and fudges that were required
  • 所有其他所需的血腥变化和软糖

#3


3  

Refer to chapter 5 of the Python documentation. It will show how to deal with the bdist Python utility. With it, you'll be able to generate packages (or, how it says, "built distribution") of several types, includding RPM.

请参阅Python文档的第5章。它将展示如何处理bdist Python实用程序。有了它,您将能够生成几种类型的包(或者,如何说,“构建分发”),包括RPM。

#4


2  

There are already RPM's of python 2.6 and django available - so those at least are (relatively) easy to grab and install via RPM. FOr creating an RPM for your project, there's a write up on some of the details using Distutils, that goes into the details of what you need in a spec file and what you invoke to make it happen.

已有RPM的python 2.6和django可用 - 所以至少(相对)容易通过RPM获取和安装。为项目创建一个RPM,可以使用Distutils编写一些详细信息,详细说明spec文件中需要的内容以及调用它来实现的内容。

There's also a note about creating RPM's from python on * at Creating Python RPM that has a touch of detail as well.

还有一个关于在*上创建RPM的说明,创建Python RPM,还有一些细节。

From experience, packaging up python itself as an RPM is the trickiest of the critters, so taking advantage of what's already there is something I'd definitely recommend. For a few programs where I've seen this done, we have a Makefile (yeah, old school) run the setup.py bdist_rpm command on a continuous integration server, making the RPM available as the result of that build and immediately available on the CI server.

根据经验,将python本身打包为RPM是最狡猾的小动物,因此利用已有的东西我肯定会推荐。对于我已经看过这个的一些程序,我们有一个Makefile(是的,旧学校)在持续集成服务器上运行setup.py bdist_rpm命令,使得RPM可用作该构建的结果并立即可用于CI服务器。

#5


2  

I've written up a blog post on how to use setuptools and it's bdist_rpm command to create the rpm for you. So indeed, use setuptools, but indeed, distribute your app as an rpm. There's a few catches and weird under documented things in setuptools you can use to eventually end up in a nice workflow.

我写了一篇关于如何使用setuptools的博客文章,它是bdist_rpm命令为你创建rpm。确实,使用setuptools,但实际上,将您的应用程序分配为rpm。在setuptools中记录的东西有一些捕获和奇怪,你可以用来最终在一个很好的工作流程。

The key is you need a post install script that runs your migrations and collects your static files, that gets run after the rpm is installed

关键是您需要一个安装后脚本来运行迁移并收集静态文件,这些文件在安装rpm后运行

export DJANGO_SETTINGS_MODULE=accountpage.settings
mkdir -p /var/www/django/static && chown apache:apache /var/www/django/static
runuser -m apache -c 'echo "yes" | django-admin collectstatic'
django-admin migrate
service httpd restart

and you can tell setuptools to add this to the spec file before it creates the rpm by giving this option in the setup call:

你可以通过在setup调用中给出这个选项,告诉setuptools在创建rpm之前将它添加到spec文件中:

options = {'bdist_rpm':{'post_install' : 'bdist_rpm_post_install.sh'}},

options = {'bdist_rpm':{'post_install':'bdist_rpm_post_install.sh'}},

See the link above for a full working setup.py script, post install hook, and a testsuite that works with python setup.py test that will install all your dependencies locally, and run the django tests.

请参阅上面的链接,了解完整的setup.py脚本,安装后挂钩以及与python setup.py测试一起使用的测试套件,它将在本地安装所有依赖项,并运行django测试。

To have the rpm also automatically install it's dependencies, create a setup.cfg file which tells bdist_rpm about it's dependencies

要让rpm也自动安装它的依赖项,请创建一个setup.cfg文件,告诉bdist_rpm它的依赖项

[bdist_rpm]
requires = python >= 2.7
           django >= 1.8
           httpd
           Django-Select2 >= 4.3.1
           django-easy-select2
           django-model-utils
           pytz
           django-oauth-toolkit