Question

Here is my content of fabfile.py

from fabric.api import run, local, abort, env, put, task
from fabric.contrib.files import exists
from fabric.context_managers import cd, lcd, settings, hide
from fabric.operations import require


PROD_SERVER='user@user.webfactional.com'

# Host and login username:
def prod():
    env.hosts=[PROD_SERVER]
    env.remote_app_dir='~/webapps/django/myapp/'
    env.remote_apache_dir='~/webapps/django/apache2/'

def commit():
    message=raw_input("Enter a git commit message: ")
    local("git add -A && git commit -m '%s'" %message)
    local("git push webfactioncarmarket")
    print "changes pushed to remote repository...."


def test():
    local("python2.7 manage.py test")

def install_dependencies():
    with cd(env.remote_app_dir):
        run("pip2.7 install -r requirements.txt")

def testing_fabric():
    prod()
    print env.hosts
    print env.remote_app_dir
    print env.remote_apache_dir

def collectstatic():
    prod()
    print env.hosts
    require('hosts',provided_by=[prod])
    run ("cd $(remote_app_dir);python2.7 manage.py collectstatic --noinput

Running :

fab testing_fabric #works fine
['user@user.webfactional.com']
~/webapps/django/myapp/
~/webapps/django/myapp/

Done.

Running:

fab collectstatic
['user@user.webfactional.com']
No hosts found. Please specify (single) host string for connection:

why do I get this prompt: No hosts found. I print env.hosts and it prints the host? how do I fix this?

EDIT: I tried the chaining: I get this error:

run: cd $(remote_app_dir);python2.7 manage.py collectstatic --noinput
out: /bin/bash: remote_app_dir: command not found

why can I not pass the value of remote_app_dir does not get passed?

Was it helpful?

Solution

There are two ways to do this:

  • Set env.hosts at the module level, so it gets set when the fabfile is imported
  • Chain modules to set the env variable.

In your case, the second one can be accomplished by removing the line prod() from collectstatic and instead invoking fabric like this:

fab prod collecstatic

This sort of decoupling can be immensely powerful. See here: http://fabric.readthedocs.org/en/latest/usage/execution.html#globally-via-env

To run from a specific directory, try using with cd:

def collectstatic():
    with cd(env.remote_app_dir):
        run("python2.7 manage.py collectstatic --noinput")

See here: https://fabric.readthedocs.org/en/latest/api/core/context_managers.html

OTHER TIPS

I write answer, becaue I don't have enough points to make a comment:

run ("cd $(remote_app_dir);python2.7 manage.py collectstatic --noinput

You should not use remote_app_dir variable (wrapping in it $() ) as you would do using bash/shell. First of all, this variable is not available in shell.

env.remote_app_dir='~/webapps/django/myapp/'

cause that python object env gets new attribute remote_app_dir

please change your code and use your variable in pythonic way:

run ("cd %spython2.7 manage.py collectstatic --noinput" % env.remote_app_dir)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top