running fabric script locally

alexarsh picture alexarsh · Jul 17, 2011 · Viewed 16.7k times · Source

I have a django app and I wrote a fabric script that installs my app on deployment server (Cent OS 5).

Now I want to run the same fabric script locally on the deployment server.

Is there a way to do it without supplying ssh user and password?

I mean just with "-H localhost"?

Thanks, Alex A.

Answer

Varun Katta picture Varun Katta · Jul 20, 2011

Yes, you can run fab locally by using method local instead of run. What I do typically is have methods for setting up the environment, and call these methods first before calling the actual task. Let me illustrate this with an example for your specific question

fabfile.py

    from fabric.operations import local as lrun, run
    from fabric.api import task
    from fabric.state import env

    @task
    def localhost():
        env.run = lrun
        env.hosts = ['localhost']

    @task
    def remote():
        env.run = run
        env.hosts = ['some.remote.host']

    @task
    def install():
        env.run('deploymentcmd')

And based on the environment, you can do the following

Install on localhost:

    fab localhost install

Install on remote machine:

    fab remote install