Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running fabric script locally

Tags:

django

fabric

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.

like image 365
alexarsh Avatar asked Jul 17 '11 16:07

alexarsh


People also ask

What is the Fabric command to execute a shell command locally?

run (fabric. Fabric's run procedure is used for executing a shell command on one or more remote hosts. The output results of run can be captured using a variable.

What is Fabric Python?

Python. Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric is very simple and powerful and can help to automate repetitive command-line tasks. This approach can save time by automating your entire workflow.

What is Fabric command?

More specifically, Fabric is: A tool that lets you execute arbitrary Python functions via the command line; A library of subroutines (built on top of a lower-level library) to make executing shell commands over SSH easy and Pythonic.


2 Answers

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 
like image 57
Varun Katta Avatar answered Oct 07 '22 18:10

Varun Katta


I am using another trick for executing remote task locally:

from fabric.api import run, sudo, local from contextlib import contextmanager  @contextmanager def locally():     global run     global sudo     global local     _run, _sudo = run, sudo     run = sudo = local     yield     run, sudo = _run, _sudo  def local_task():     with locally():         run("ls -la") 
like image 31
xuhcc Avatar answered Oct 07 '22 18:10

xuhcc