python 你如何使用 pip、virtualenv 和 Fabric 来处理部署?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2441704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you use pip, virtualenv and Fabric to handle deployment?
提问by e-satis
What are your settings, your tricks, and above all, your workflow?
你的设置,你的技巧,最重要的是你的工作流程是什么?
These tools are great but there are still no best practices attached to their usage, so I don't know what is the most efficient way to use them.
这些工具很棒,但仍然没有与它们的使用相关的最佳实践,所以我不知道使用它们的最有效方法是什么。
- Do you use pipbundles or always download?
- Do you set up Apache/Cherokee/MySQL by hand or do you have a script for that?
- Do you put everything in virtualenvand use
--no-site-packages
? - Do you use one virtualenv for several projects?
- What do you use Fabricfor (which part of your deployment do you script)?
- Do you put your Fabric scripts on the client or the server?
- How do you handle database and media file migration?
- Do you ever need a build tool such as SCons?
- What are the steps of your deployment? How often do you perform each of them?
- etc.
- 你使用pip包还是总是下载?
- 您是手动设置 Apache/Cherokee/MySQL 还是您有相应的脚本?
- 您是否将所有内容都放在virtualenv 中并使用
--no-site-packages
? - 您是否为多个项目使用一个 virtualenv?
- 您将Fabric用于什么(您编写脚本部署的哪个部分)?
- 您将 Fabric 脚本放在客户端还是服务器上?
- 您如何处理数据库和媒体文件迁移?
- 您是否需要像SCons这样的构建工具?
- 您的部署步骤是什么?你每一次表演的频率是多少?
- 等等。
回答by Carl Meyer
"Best practices" are very context-dependent, so I won't claim my practices are best, just that they work for me. I work on mostly small sites, so no multiple-server deployments, CDNs etc. I do need to support Webfaction shared hosting deployment, as some clients need the cheapest hosting they can find. I do often have to deploy sites multiple times in different environments, so repeatable scripted deploys are critical.
“最佳实践”非常依赖于上下文,所以我不会声称我的实践是最好的,只是它们对我有用。我主要在小型站点上工作,所以没有多服务器部署、CDN 等。我确实需要支持 Webfaction 共享托管部署,因为有些客户需要他们能找到的最便宜的托管。我确实经常需要在不同的环境中多次部署站点,因此可重复的脚本部署至关重要。
- I don't use pip bundles, I install from a requirements.txt. I do run my own chishopserver with sdists of everything I need, so there aren't multiple single points of failure in the build process. I also use PIP_DOWNLOAD_CACHE on my development machines to speed up bootstrapping project environments, since most of my projects' requirements overlap quite a bit.
- I have Fabricscripts that can automatically set up and configure nginx + Apache/mod_wsgi on an Ubuntu VPS, or configure the equivalent on Webfactionshared hosting, and then deploy the project.
- I do not use --no-site-packages with virtualenv, because I prefer having slow-moving compiled packages (Python Imaging Library, psycopg2) installed at the system level; too slow and troublesome to do inside every virtualenv. I have not had trouble with polluted system site-packages, because I generally don't pollute it. And in any case, you can install a different version of something in the virtualenv and it will take precedence.
- Each project has its own virtualenv. I have some bash scripts (not virtualenvwrapper, though a lot of people use that and love it) that automate deploying the virtualenv for a given project to a known location and installing that project's requirements into it.
- The entire deployment process, from a bare Ubuntu server VPS or Webfaction shared hosting account to a running website, is scripted using Fabric.
- Fabric scripts are part of the project source tree, and I run them from a local development checkout.
- I have no need for SCons (that I am aware of).
- 我不使用 pip 包,我从 requirements.txt 安装。我确实使用我需要的所有东西的sdists运行我自己的chishop服务器,因此在构建过程中没有多个单点故障。我还在我的开发机器上使用 PIP_DOWNLOAD_CACHE 来加速引导项目环境,因为我的大多数项目的需求有很多重叠。
- 我有Fabric脚本,可以在 Ubuntu VPS 上自动设置和配置 nginx + Apache/mod_wsgi,或者在Webfaction共享主机上配置等效项,然后部署项目。
- 我不将 --no-site-packages 与 virtualenv 一起使用,因为我更喜欢在系统级别安装缓慢移动的编译包(Python Imaging Library,psycopg2);在每个 virtualenv 中都做起来太慢也太麻烦了。我没有遇到受污染的系统站点包的问题,因为我通常不会污染它。无论如何,你可以在 virtualenv 中安装不同版本的东西,它会优先。
- 每个项目都有自己的 virtualenv。我有一些 bash 脚本(不是virtualenvwrapper,尽管很多人使用它并喜欢它),它们可以自动将给定项目的 virtualenv 部署到已知位置并将该项目的要求安装到其中。
- 整个部署过程,从裸 Ubuntu 服务器 VPS 或 Webfaction 共享托管帐户到正在运行的网站,都是使用 Fabric 编写的。
- Fabric 脚本是项目源代码树的一部分,我从本地开发结帐处运行它们。
- 我不需要 SCons(我知道)。
Deployment
部署
At the moment a fresh deployment is split into these steps:
目前,新部署分为以下步骤:
fab staging bootstrap
(server setup and initial code deploy)fab staging enable
(enable the Apache/nginx config for this site)fab staging reload_server
(reload Apache/nginx config).
fab staging bootstrap
(服务器设置和初始代码部署)fab staging enable
(为此站点启用 Apache/nginx 配置)fab staging reload_server
(重新加载 Apache/nginx 配置)。
Those can of course be combined into a single command line fab staging bootstrap enable reload_server
.
这些当然可以组合成一个单一的命令行fab staging bootstrap enable reload_server
。
Once these steps are done, updating the deployment with new code is just fab staging deploy
.
完成这些步骤后,使用新代码更新部署只需fab staging deploy
.
If I need to roll back an update, fab staging rollback
. Nothing particularly magical in the rollback; it just rolls back the code to the last-deployed version and migrates the database to the previous state (this does require recording some metadata about the migration state of the DB post-deploy, I just do that in a text file).
如果我需要回滚更新,fab staging rollback
. 回滚没有什么特别神奇的;它只是将代码回滚到上次部署的版本并将数据库迁移到以前的状态(这确实需要记录一些关于部署后数据库迁移状态的元数据,我只是在文本文件中这样做)。
Examples
例子
I haven't used the Fabric scripts described in this answer for a few years, so they aren't maintained at all and I disclaim responsibility for their quality :-) But you can see them at https://bitbucket.org/carljm/django-project-template- in fabfile.py
in the repo root, and in the deploy/
subdirectory.
我已经有几年没有使用过这个答案中描述的 Fabric 脚本了,所以它们根本没有得到维护,我对它们的质量不承担任何责任 :-) 但是你可以在https://bitbucket.org/carljm 上看到它们/django-project-template- 在fabfile.py
repo 根目录和deploy/
子目录中。
回答by Rodrigue
I use fabric to build and deploy my code and assume a system already set up for that. I think that a tool like puppetis more appropriate to automate the installation of things like apache and mysql, though I have yet to really include it in my workflow.
我使用结构来构建和部署我的代码,并假设一个系统已经为此设置好了。我认为像puppet这样的工具更适合自动安装诸如 apache 和 mysql 之类的东西,尽管我还没有真正将它包含在我的工作流程中。
Also, I usually have a different virtualenv per project. They are created from a 'base' install of python where - as Carl pointed out - you can leave some global python libraries.
另外,我通常每个项目都有一个不同的 virtualenv。它们是从 python 的“基本”安装创建的,其中 - 正如卡尔指出的那样 - 您可以保留一些全局 python 库。
So in terms of workflow that would be:
因此,就工作流程而言,这将是:
- puppet to install required services (web server, database, ssh server, ...)
- puppet to set up required users and base folders
- fabric to create virtualenv for the application
- fabric to pip install from requirements.txt
- fabric to deploy your app
- fabric to deploy configuration files (web server, ...)
- puppet 安装所需的服务(Web 服务器、数据库、ssh 服务器等)
- puppet 设置所需的用户和基本文件夹
- 为应用程序创建 virtualenv 的结构
- 从requirements.txt到pip安装的fabric
- 用于部署您的应用程序的结构
- 用于部署配置文件的结构(Web 服务器,...)