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')
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?
# # # response # # #
Thanks bitprophet, that led down the right track. Using fabric 1.0 we ended up going with the following fabfile recipe which includes a call to pip requirements file:
def production():
env.hosts = ['servername']
env.directory = '/path/to/virtualenvs/project'
env.activate = 'source /home/deploy/.virtualenvs/project/bin/activate'
env.deploy_user = 'deploy'
def virtualenv(command):
with cd(env.directory):
sudo(env.activate + '&&' + command, user=env.deploy_user)
def git_pull():
'Updates the repository.'
with cd(env.directory):
sudo('git pull', user=env.deploy_user)
def pip_install_req():
virtualenv('pip install -U -r ../../../requirements/external_apps.txt')
def reload():
sudo('/etc/init.d/apache2 reload')
def deploy_pip():
local('git push')
git_pull()
pip_install_req()
reload()