如何使用 pip、virtualenv 和 Fabric 来处理部署?
你的设置、你的技巧是什么,最重要的是你的工作流程是什么?
这些工具很棒,但仍然没有关于它们的使用的最佳实践,所以我不知道使用它们的最有效的方法是什么。
- 您是否使用 pip 捆绑包还是始终使用 下载?
- 您是手动设置 Apache/Cherokee/MySQL 还是这样做 你有一个脚本吗?
- 您是否将所有内容放入 virtualenv 并使用
--no-site-packages
? - 您是否将一个 virtualenv 用于多个项目?
- 您使用 Fabric 来做什么(哪一部分) 您的部署是由您编写的吗)?
- 您将 Fabric 脚本放在客户端还是服务器上?
- 您如何处理数据库和媒体文件迁移?
- 您是否需要诸如 SCons 之类的构建工具?
- 你们的部署步骤是什么?您多久执行一次这些操作?
- ETC。
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 pip bundles 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 virtualenv and use
--no-site-packages
? - Do you use one virtualenv for several projects?
- What do you use Fabric for (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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“最佳实践”非常依赖于上下文,因此我不会声称我的实践是最好的,只是它们对我有用。我主要在小型网站上工作,因此没有多服务器部署、CDN 等。我确实需要支持 Webfaction 共享托管部署,因为某些客户需要他们能找到的最便宜的托管。我经常需要在不同的环境中多次部署站点,因此可重复的脚本化部署至关重要。
部署
目前,全新部署分为以下步骤:
fab staging reload_server
(重新加载 Apache/nginx 配置)。当然,这些可以组合成一个命令行
fab staging bootstrap enable reload_server
。完成这些步骤后,使用新代码更新部署只是 fab staging 部署。
如果我需要回滚更新,
fab staging rollback
。回滚并没有什么特别神奇的地方;它只是将代码回滚到最后部署的版本并将数据库迁移到以前的状态(这确实需要记录一些有关部署后数据库迁移状态的元数据,我只是在文本文件中执行此操作)。示例
我已经有几年没有使用此答案中描述的 Fabric 脚本了,因此它们根本没有得到维护,并且我不对其质量负责:-) 但您可以在 https://bitbucket.org/carljm/django-project-template - 在
fabfile.py
中在存储库根目录和deploy/
子目录中。"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.
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).Those can of course be combined into a single command line
fab staging bootstrap enable reload_server
.Once these steps are done, updating the deployment with new code is just
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).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 thedeploy/
subdirectory.我使用 Fabric 来构建和部署我的代码,并假设已经为此设置了系统。我认为像 puppet 这样的工具更适合自动安装 apache 和 mysql 之类的东西,尽管我尚未真正将其纳入我的工作流程中。
另外,我通常每个项目都有不同的 virtualenv。它们是从 python 的“基本”安装创建的,正如 Carl 指出的那样,您可以保留一些全局 python 库。
因此,就工作流程而言,将是:
I use fabric to build and deploy my code and assume a system already set up for that. I think that a tool like puppet is more appropriate to automate the installation of things like apache and mysql, though I have yet to really include it in my workflow.
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.
So in terms of workflow that would be: