Skip to content

Instantly share code, notes, and snippets.

@dcramer
Created April 4, 2013 00:19
Show Gist options
  • Select an option

  • Save dcramer/5306655 to your computer and use it in GitHub Desktop.

Select an option

Save dcramer/5306655 to your computer and use it in GitHub Desktop.
def in_role(host, role):
return host in env.roledefs.get(role, '')
def only_roles(*role_names):
"""
Guarantee a task is only run on the host if its a member of one of the
given roles.
"""
def decorator(func):
@wraps(func)
def wrapped(*args, **kwargs):
host_string = env.host_string
if any(in_role(host_string, k) for k in role_names):
return func(*args, **kwargs)
puts('{} not a member of {}, skipping {}'.format(
host_string, roles, func.__name__))
for attr in dir(func):
if attr.startswith(('func_', '_')):
continue
setattr(wrapped, attr, getattr(func, attr))
return roles(role_names)(wrapped)
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment