Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python Fabric without the command-line tool (fab)

Tags:

python

fabric

Altough Fabric documentations refers to a way of using the library for SSH access without requiring the fab command-line tool and/or tasks, I can't seem to manage a way to do it.

I want to run this file (example.py) by only executing 'python example.py':

env.hosts = [ "example.com" ]
def ps():
    run("ps")
ps()

Thanks.

like image 475
fabiopedrosa Avatar asked Jul 19 '11 02:07

fabiopedrosa


2 Answers

I ended up doing this:

from fabric.api import env
from fabric.api import run

class FabricSupport:
    def __init__ (self):
        pass

    def run(self, host, port, command):
        env.host_string = "%s:%s" % (host, port)
        run(command)

myfab = FabricSupport()

myfab.run('example.com', 22, 'uname')

Which produces:

[example.com:22] run: uname
[example.com:22] out: Linux
like image 133
blueFast Avatar answered Oct 19 '22 04:10

blueFast


#!/usr/bin/env python
from fabric.api import hosts, run, task
from fabric.tasks import execute

@task
@hosts(['user@host:port'])
def test():
    run('hostname -f')

if __name__ == '__main__':
   execute(test)

More information: http://docs.fabfile.org/en/latest/usage/library.html

like image 44
semente Avatar answered Oct 19 '22 04:10

semente