如何使用 pip、virtualenv 和 Fabric 来处理部署?

发布于 2024-08-25 05:40:52 字数 724 浏览 6 评论 0原文

你的设置、你的技巧是什么,最重要的是你的工作流程是什么?

这些工具很棒,但仍然没有关于它们的使用的最佳实践,所以我不知道使用它们的最有效的方法是什么。

  • 您是否使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2024-09-01 05:40:52

“最佳实践”非常依赖于上下文,因此我不会声称我的实践是最好的,只是它们对我有用。我主要在小型网站上工作,因此没有多服务器部署、CDN 等。我确实需要支持 Webfaction 共享托管部署,因为某些客户需要他们能找到的最便宜的托管。我经常需要在不同的环境中多次部署站点,因此可重复的脚本化部署至关重要。

  • 我不使用 pip 捆绑包,我从requirements.txt 安装。我确实运行自己的 chishop 服务器,其中包含我需要的所有内容,因此没有多个单点构建过程中失败。我还在我的开发机器上使用 PIP_DOWNLOAD_CACHE 来加速引导项目环境,因为我的大多数项目的需求有很多重叠。
  • 我有 Fabric 脚本,可以在 Ubuntu VPS 上自动设置和配置 nginx + Apache/mod_wsgi,或者在 < a href="http://webfaction.com" rel="noreferrer">Webfaction 共享托管,然后部署项目。
  • 我不将 --no-site-packages 与 virtualenv 一起使用,因为我更喜欢在系统级别安装缓慢移动的编译包(Python 成像库、psycopg2);在每个 virtualenv 中执行起来太慢且麻烦。我没有遇到污染系统站点包的问题,​​因为我通常不会污染它。无论如何,您可以在 virtualenv 中安装不同版本的东西,它将优先。
  • 每个项目都有自己的 virtualenv。我有一些 bash 脚本(不是 virtualenvwrapper,尽管很多人使用它并且喜欢它)自动将给定项目的 virtualenv 部署到已知位置并将该项目的需求安装到其中。
  • 从裸 Ubuntu 服务器 VPS 或 Webfaction 共享托管帐户到正在运行的网站的整个部署过程都是使用 Fabric 编写的。
  • Fabric 脚本是项目源代码树的一部分,我从本地开发结帐运行它们。
  • 我不需要 SCons(据我所知)。

部署

目前,全新部署分为以下步骤:

  • fab staging bootstrap(服务器设置和初始代码部署)
  • fab staging enable(为此启用 Apache/nginx 配置) site)
  • 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.

  • I don't use pip bundles, I install from a requirements.txt. I do run my own chishop server 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 Fabric scripts that can automatically set up and configure nginx + Apache/mod_wsgi on an Ubuntu VPS, or configure the equivalent on Webfaction shared 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).

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 the deploy/ subdirectory.

叫嚣ゝ 2024-09-01 05:40:52

我使用 Fabric 来构建和部署我的代码,并假设已经为此设置了系统。我认为像 puppet 这样的工具更适合自动安装 apache 和 mysql 之类的东西,尽管我尚未真正将其纳入我的工作流程中。

另外,我通常每个项目都有不同的 virtualenv。它们是从 python 的“基本”安装创建的,正如 Carl 指出的那样,您可以保留一些全局 python 库。

因此,就工作流程而言,将是:

  1. puppet 安装所需的服务(Web 服务器、数据库、ssh 服务器等)
  2. puppet 设置所需的用户和基础文件夹
  3. Fabric 为应用程序 Fabric 创建 virtualenv,
  4. 以便根据需求进行 pip 安装。 txt
  5. Fabric 来部署您的应用程序
  6. Fabric 来部署配置文件(Web 服务器,...)

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:

  1. puppet to install required services (web server, database, ssh server, ...)
  2. puppet to set up required users and base folders
  3. fabric to create virtualenv for the application
  4. fabric to pip install from requirements.txt
  5. fabric to deploy your app
  6. fabric to deploy configuration files (web server, ...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文