通过Fabric以部署用户身份激活virtualenv [英] Activate a virtualenv via fabric as deploy user

查看:90
本文介绍了通过Fabric以部署用户身份激活virtualenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在本地运行我的结构脚本,这将依次登录到我的服务器,切换用户以进行部署,激活项目.virtualenv,这将把dir更改为项目并发出git pull.

I want to run my fabric script locally, which will in turn, log into my server, switch user to deploy, activate the projects .virtualenv, which will change dir to the project and issue a git pull.

def git_pull():
    sudo('su deploy')
    # here i need to switch to the virtualenv
    run('git pull')

我通常使用virtualenvwrapper的workon命令,该命令提供激活文件,后激活文件会将我放在项目文件夹中.在这种情况下,似乎是因为fabric在shell中运行,所以控制权移交给了fabric,所以我无法使用bash内置的源代码到'$ source〜/.virtualenv/myvenv/bin/activate'

I typically use the workon command from virtualenvwrapper which sources the activate file and the postactivate file will put me in the project folder. In this case, it seems that because fabric runs from within shell, control is give over to fabric, so I can't use bash's source built-in to '$source ~/.virtualenv/myvenv/bin/activate'

每个人都有一个示例并说明如何执行此操作吗?

Anybody have an example and explanation of how they have done this?

推荐答案

现在,您可以做我想做的事,虽然很笨拙,但效果很好*(此用法假设您使用的是virtualenvwrapper-应该这样做-但您可以轻松地替换为您提到的较长的源"调用,如果没有的话):

Right now, you can do what I do, which is kludgy but works perfectly well* (this usage assumes you're using virtualenvwrapper -- which you should be -- but you can easily substitute in the rather longer 'source' call you mentioned, if not):

def task():
    workon = 'workon myvenv && '
    run(workon + 'git pull')
    run(workon + 'do other stuff, etc')

从1.0版开始,Fabric具有

Since version 1.0, Fabric has a prefix context manager which uses this technique so you can for example:

def task():
    with prefix('workon myvenv'):
        run('git pull')
        run('do other stuff, etc')


*在某些情况下,使用command1 && command2方法可能会给您带来麻烦,例如command1失败(command2永远不会运行)或command1无法正确转义并包含特殊的外壳字符,等等.


* There are bound to be cases where using the command1 && command2 approach may blow up on you, such as when command1 fails (command2 will never run) or if command1 isn't properly escaped and contains special shell characters, and so forth.

这篇关于通过Fabric以部署用户身份激活virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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