def test_decorator_incompatibility_on_task(): from fabric.decorators import task, hosts, runs_once, roles def foo(): return "foo" foo = task(foo) # since we aren't setting foo to be the newly decorated thing, its cool hosts('me@localhost')(foo) runs_once(foo) roles('www')(foo)
def hosts(appname, vms=None): """hosts(appname, vms=None) Run a task on *vms* in application *appname*. This function should be used as a decorator on a Fabric task. For example:: @ravello.hosts('production', 'web') def deploy(): # deploy to VM 'web' here Which is identical to:: @hosts(ravello.lookup('production', 'web')) def deploy(): # deploy to VM 'web' here Note that this decorator will do the lookup at the time the decorator is called. This lookup requires a Ravello API connection. You must either have ``env.ravello_api_user`` and ``env.ravello_api_password`` set, or you will be prompted for the API username and password. In case you want to do the lookup later, use :func:`lookup` directly and assign the return value to ``env.hosts``. """ hosts = lookup(apppname, vms) return fab.hosts(*hosts)
def selectively_attach(func): if not env.roles and not env.hosts: return roles(*role_list)(func) else: if env.hosts: func = hosts(*env.hosts)(func) if env.roles: func = roles(*env.roles)(func) return func
def test_decorator_closure_hiding(): """ @task should not accidentally destroy decorated attributes from @hosts/etc """ from fabric.decorators import task, hosts def foo(): print(env.host_string) foo = task(hosts("me@localhost")(foo)) eq_(["me@localhost"], foo.hosts)
def test_decorator_closure_hiding(): from fabric.decorators import task, hosts def foo(): print env.host_string foo = hosts("me@localhost")(foo) foo = task(foo) # this broke in the old way, due to closure stuff hiding in the # function, but task making an object eq_(["me@localhost"], foo.hosts)
def test_decorator_closure_hiding(): """ @task should not accidentally destroy decorated attributes from @hosts/etc """ from fabric.decorators import task, hosts def foo(): print((env.host_string)) foo = task(hosts("me@localhost")(foo)) eq_(["me@localhost"], foo.hosts)
def selectively_attach(func): """Only decorate if nothing specified on command line""" # pylint: disable=W0142 if not env.roles and not env.hosts: return roles(*role_list)(func) else: if env.hosts: func = hosts(*env.hosts)(func) if env.roles: func = roles(*env.roles)(func) return func
def setup_default_hosts(self): default_prod_host = env['prod']['hosts'][0] default_local_host = env['local']['hosts'][0] self.insert_db = hosts(self.insert_db, default_local_host) self.dump_fetch = hosts(self.dump_fetch, default_prod_host) self.dump_fetch = hosts(self.dump_fetch, default_prod_host) self.fetch = hosts(self.fetch, default_prod_host) self.dump = hosts(self.dump, default_prod_host) self.migrate = hosts(self.migrate, default_local_host) pass
def __init__(self, hosts=(), roles=(), runs_once=False, **kw): super(ManagedTask, self).__init__() if 'name' in kw: self.name = kw['name'] else: if hasattr(self.__class__, 'name'): self.name = getattr(self.__class__, 'name') if self.name == 'undefined': self.name = self.__class__.__name__.lower() self._decorator = lambda x: x if roles: self._decorator = lambda x: fab_api.roles(roles)(x) if hosts: self._decorator = lambda x: fab_api.hosts(hosts)(x) if runs_once: self._decorator = lambda x: fab_api.runs_once()(x) for _, f in self.getCommands().items(): setattr(self, f, self._decorator(getattr(self, f))) self.__createTasks()
def classes(): """List available classes""" for name in sorted(env.roledefs.classes): hosts = env.roledefs['class-%s' % name] print("%-30.30s %s" % (name, len(hosts())))
""" using fabric as a library without having to call the """ from fabric.api import run, execute, hosts @hosts('[email protected]:22') def return_user(): """ Runs whoami on remote host """ run('whoami') if __name__ == '__main__': execute(return_user) # Can also execute arbitrary commands and save the output out = execute( hosts('*****@*****.**')(lambda: run('uname -a'))) print(out)
from fabric.api import hosts from fabfile import EyeCandy from fabfile.FabHacks import maintask setenv = maintask(hosts('localhost')(EyeCandy.setenv))
""" using fabric as a library without having to call the """ from fabric.api import run,execute,hosts @hosts('[email protected]:22') def return_user() : """ Runs whoami on remote host """ run('whoami') if __name__ == '__main__' : execute(return_user) # Can also execute arbitrary commands and save the output out = execute(hosts('*****@*****.**')(lambda : run('uname -a'))) print(out)
def vdcs(): """List available virtual datacentres""" for name in sorted(env.roledefs.vdcs): hosts = env.roledefs['vdc-%s' % name] print("%-30.30s %s" % (name, len(hosts())))