Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fabric API direct call in python

Tags:

python

fabric

i remember fabric API can be called directly in py script but forgot where to start anybody give a clue?

like image 751
whi Avatar asked Aug 08 '12 09:08

whi


2 Answers

Yes, you can call it, for example:

from fabric.api import run
from fabric.tasks import execute


def do_something():
    run("echo $RANDOM")

if __name__ == "__main__":
    execute(do_something, hosts=["username@host"])
like image 167
Fedor Gogolev Avatar answered Oct 18 '22 18:10

Fedor Gogolev


There is a whole section on using fabric as a library in the docs. I'd refer to that and how best to approach fabric's use in this way.

Specifically, you'll want to address the problem of performing task X on a list of hosts Y by using the execute function as execute( X, hosts=Y ). When you are done, you should disconnect from all of the connected hosts. Normally the fab tool does this for you, but you must do this manually when using fabric as a library. Fabric 0.9.4 and later have a disconnect_all() function. Ideally, this should be in the finally clause of a try...finally statement.

like image 6
Morgan Avatar answered Oct 18 '22 17:10

Morgan