Skip to content

Instantly share code, notes, and snippets.

@bmannix
Created April 9, 2012 17:52
Show Gist options
  • Save bmannix/2345006 to your computer and use it in GitHub Desktop.
Save bmannix/2345006 to your computer and use it in GitHub Desktop.
pyfab provides a simpler fabric interface for python
import fabfile as fab
class Pyfab:
def __init__(self, role, user):
self.role = role
self.user = user
def fab_exec(self, command, *args, **kwargs):
'''
this runs fabric commands defined in the fabfile. By default it
will execute commands based on the roles passed from the fabric config
stored in a dictionary called 'roledefs'.
By default it will execute commands as the user passed during init.
You may override the user or define a single host e.g.:
fab_exec("<fab file cmd>", args, kwargs, host='xenon.rdc.lctl.gov', user='brma')
'''
user = self.user
host = None
fabkwargs = {}
hosts = fab.env["roledefs"][self.role]
for key in kwargs:
if key == 'user':
user = kwargs[key]
elif key == 'host':
host = [ kwargs[key] ]
else:
fabkwargs[key] = kwargs[key]
fab.env.user = user
if host: hosts = host
for host_str in hosts:
fab.env.host_string = host_str
getattr(fab, command)(*args, **fabkwargs)
@bmannix
Copy link
Author

bmannix commented Jun 13, 2013

leaving this here for historical reasons, but fabric included support for this right around the same time I wrote it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment