Created
April 4, 2013 00:19
-
-
Save dcramer/5306655 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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