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

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

问题描述

您的设置是什么,您的技巧,尤其是您的工作流程?



这些工具很棒,但仍然没有附加到其使用的最佳做法,所以我不知道使用它们最有效的方法是什么。




  • 您是否使用 pip 捆绑或总是
    下载?

  • 您是否手动设置了Apache / Cherokee / MySQL或
    您有脚本?

  • virtualenv 并使用 - 无站点包

  • 您是否在多个项目中使用一个virtualenv?

  • 您使用织物(您的部署的
    你脚本的哪一部分)?

  • 您是否将Fabric脚本放在客户端或服务器上?

  • 如何处理数据库和媒体文件迁移?

  • 您是否需要一个构建工具,例如 SCons

  • 您的部署步骤如何?你多久执行一次?

  • 等。


解决方案

最佳做法是非常依赖于上下文,所以我不会声称自己的做法是最好的,只是他们为我工作。我在大多数小型网站上工作,所以没有多服务器部署,CDN等。我需要支持Webfaction共享托管部署,因为一些客户端需要可以找到的最便宜的托管。我经常需要在不同的环境中多次部署站点,所以可重复的脚本部署至关重要。




  • 我不使用点捆绑,我从一个require.txt安装。我自己运行我自己的 chishop 服务器,具有我需要的所有东西,所以没有多个单点的构建过程中的失败。我也在我的开发机器上使用PIP_DOWNLOAD_CACHE来加快引导项目环境,因为我的大部分项目的需求重叠了很多。

  • 我有一个 Fabric 脚本,可以在Ubuntu VPS上自动设置和配置nginx + Apache / mod_wsgi,或者在 Webfaction 共享托管,然后部署项目。

  • 我不使用--no-site-packages与virtualenv,因为我更喜欢在系统级别安装了缓慢移动的编译包(Python Imaging Library,psycopg2)在每个虚拟环境中做得太慢和麻烦。我没有遇到污染的系统站点包的麻烦,因为我一般不会污染它。在任何情况下,您可以在virtualenv中安装不同版本的东西,并将优先。

  • 每个项目都有自己的virtualenv。我有一些bash脚本(不是 virtualenvwrapper ,虽然很多人使用它,并爱上它)将自动将给定项目的virtualenv部署到已知位置并将该项目的要求安装到其中。

  • 整个部署过程从裸机Ubuntu服务器VPS或Webfaction共享托管帐户一个正在运行的网站,使用Fabric编写脚本。

  • Fabric脚本是项目源代码树的一部分,我从本地开发结帐运行。

  • 我不需要SCons(我知道的)。



部署



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




  • fab分段引导(服务器设置和初始代码部署)

  • fab staging enable (启用此站点的Apache / nginx配置)

  • fab staging reload_server (重新加载Apache / nginx配置)。



当然可以将它们组合成一个命令行制作阶段启动启动启用重新加载_server



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



如果我需要回滚更新, / code>。在回滚中没什么特别神奇的;它只是将代码回滚到最后部署的版本,并将数据库迁移到以前的状态(这需要记录一些关于数据库后部署的迁移状态的元数据,我只是在文本文件中进行)。 p>

示例



我没有使用这个答案描述的Fabric脚本几年,所以他们是'我保留所有权利,我不承担其质量责任:-)但您可以在 https: //bitbucket.org/carljm/django-project-template - 在repo根中的 fabfile.py 部署/ 子目录。


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天全站免登陆