我想在本地运行fabric脚本,它将依次登录到我的服务器,切换用户到deploy,激活项目.virtualenv,这将更改项目的目录并发出git拉。
def git_pull():
sudo('su deploy')
# here i need to switch to the virtualenv
run('git pull')
我通常使用来自virtualenvwrapper的workon命令,它提供了激活文件,而postactivate文件将把我放到项目文件夹中。在本例中,似乎因为fabric是从shell内部运行的,所以控制权交给了fabric,所以我不能使用bash的内置源代码'$source ~/.virtualenv/myvenv/bin/activate‘。
谁有一个例子和解释他们是如何做到这一点的?
发布于 2011-03-19 12:06:47
作为bitprophet预测的更新:使用Fabric 1.0,您可以使用prefix()和您自己的上下文管理器。
from __future__ import with_statement
from fabric.api import *
from contextlib import contextmanager as _contextmanager
env.hosts = ['servername']
env.user = 'deploy'
env.keyfile = ['$HOME/.ssh/deploy_rsa']
env.directory = '/path/to/virtualenvs/project'
env.activate = 'source /path/to/virtualenvs/project/bin/activate'
@_contextmanager
def virtualenv():
with cd(env.directory):
with prefix(env.activate):
yield
def deploy():
with virtualenv():
run('pip freeze')
发布于 2010-08-04 15:48:49
我只是使用了一个简单的包装器函数virtualenv(),可以调用它而不是run()。它不使用cd上下文管理器,因此可以使用相对路径。
def virtualenv(command):
"""
Run a command in the virtualenv. This prefixes the command with the source
command.
Usage:
virtualenv('pip install django')
"""
source = 'source %(project_directory)s/bin/activate && ' % env
run(source + command)
发布于 2013-09-10 19:51:45
这是我在本地部署中使用virtualenv
的方法。
使用fabric的path()上下文管理器,您可以使用来自virtualenv的二进制文件运行pip
或python
。
from fabric.api import lcd, local, path
project_dir = '/www/my_project/sms/'
env_bin_dir = project_dir + '../env/bin/'
def deploy():
with lcd(project_dir):
local('git pull origin')
local('git checkout -f')
with path(env_bin_dir, behavior='prepend'):
local('pip freeze')
local('pip install -r requirements/staging.txt')
local('./manage.py migrate') # Django related
# Note: previous line is the same as:
local('python manage.py migrate')
# Using next line, you can make sure that python
# from virtualenv directory is used:
local('which python')
https://stackoverflow.com/questions/1180411
复制相似问题