2

I have being using fabric to deploy an app with virtualenv. I was using fabric 1.4 and upgraded to 1.5.1 last week. My script stopped working.

It can't install the requirements. It seems it's not activating the virtualenv. In my code, I have:

with cd('%(path)s' % env):
        with prefix('source bin/activate'):
            run('pip install -U distribute')

I'm getting a permission denied error: error: could not delete '/usr/local/lib/python2.7/dist-packages/pkg_resources.py': Permission denied

The command being executed is:

Executed: /bin/bash -l -c "cd /var/www/myproject && source bin/activate && export PATH=\"\\$PATH:\\"/var/www/myproject\\" \" && pip install -U distribute"

If I ssh to the remote machine and run cd /var/www/myproject && source bin/activate && pip install -U distribute, it works just fine.

Why is my fabric script not working?

Thanks in advance

duduklein
  • 10,014
  • 11
  • 44
  • 55
  • I have a slight suspicion that having fabric instantiate a virtualenv on a target host might have unwanted results.. – favoretti Dec 27 '12 at 19:09
  • According to this link, it's possible, but I don't get what I'm doing wrong. http://stackoverflow.com/questions/1180411/activate-a-virtualenv-via-fabric-as-deploy-user – duduklein Dec 27 '12 at 19:13
  • Well, the only difference is, you're using `run()`, rather than `sudo()`, which might be a permission issue. Try using `sudo()`? – favoretti Dec 27 '12 at 19:29
  • it's strange, because as I'm in a virtualenv, it should not require sudo – duduklein Dec 27 '12 at 19:32
  • It depends who created the virtualenv and whether you can do it as your own user. Other point is, fabric really executes command with that `/bin/bash -l -c "..."` prefix, if you do that as the user fabric uses on the machine, does it work as well? – favoretti Dec 27 '12 at 19:33
  • using sudo, the packages are not installed in the virtualenv. I'm using the same user as fabric – duduklein Dec 27 '12 at 19:36
  • Aha, so basically it means sourcing the venv fails. What if you do `source /full/path/to/bin/activate` instead of relative path? Despite the `with cd()`... – favoretti Dec 27 '12 at 19:40

2 Answers2

6

Instead of the serial approach with..

source bin/activate
pip install -U distribute

..directly use the pip executable of the virtualenv:

myenv/bin/pip install -U distribute
Jasper van den Bosch
  • 3,169
  • 4
  • 32
  • 55
3

Although not exactly a solution, fabtools has a number of functions related to virtualenvs that are very handy. They do (almost) all of the hard work for you, and are probably worth using to check it isn't something else going wrong.

# Cut (and modified) from the fabtools documentation
from fabric.api import *
from fabtools import require
import fabtools

@task
def setup():
    # Require a Python package
    with fabtools.python.virtualenv('/home/myuser/env'):
        require.python.package('pyramid')
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84