你如何使用 pip、virtualenv 和 Fabric 来处理部署? [英] How do you use pip, virtualenv and Fabric to handle deployment?

查看:17
本文介绍了你如何使用 pip、virtualenv 和 Fabric 来处理部署?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您的设置、技巧以及最重要的工作流程是什么?

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

  • 你使用 pip 包还是一直使用下载?
  • 您是手动设置 Apache/Cherokee/MySQL 还是手动设置你有一个脚本吗?
  • 您是否将所有内容都放在 virtualenv 中并使用 --no-site-packages?
  • 您是否为多个项目使用一个 virtualenv?
  • 你用 Fabric 做什么(哪个部分您的部署是否编写脚本)?
  • 您是将 Fabric 脚本放在客户端还是服务器上?
  • 您如何处理数据库和媒体文件迁移?
  • 您是否需要诸如 SCons 之类的构建工具?
  • 您的部署步骤是什么?您多久执行一次?

解决方案

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

  • 我不使用 pip 包,我从 requirements.txt 安装.我确实运行自己的 chishop 服务器,其中包含我需要的所有内容的 sdists,因此没有多个单点在构建过程中失败.我还在我的开发机器上使用 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(我知道).

部署

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

  • fab staging bootstrap(服务器设置和初始代码部署)
  • fab staging enable(为此站点启用 Apache/nginx 配置)
  • fab staging reload_server(重新加载 Apache/nginx 配置).

这些当然可以组合成一个命令行fab staging bootstrap enable reload_server.

完成这些步骤后,使用新代码更新部署只是fab staging deploy.

如果我需要回滚更新,fab staging rollback.回滚没有什么特别神奇的;它只是将代码回滚到上次部署的版本并将数据库迁移到以前的状态(这确实需要记录一些关于部署后数据库迁移状态的元数据,我只是在文本文件中这样做).

示例

我已经几年没有使用这个答案中描述的 Fabric 脚本了,所以它们根本没有得到维护,我不对其质量负责:-) 但是你可以在 https://bitbucket.org/carljm/django-project-template - 在 fabfile.pycode> 在 repo 根目录和 deploy/ 子目录中.

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.

解决方案

"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.

这篇关于你如何使用 pip、virtualenv 和 Fabric 来处理部署?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆