-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooks.py
84 lines (65 loc) · 2.54 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# http://www.saltycrane.com/blog/2010/11/fabric-post-run-processing-python-decorator/
import traceback
import logging
from functools import wraps
from fabric.api import env
# global variable for add_hooks()
parent_task_name = ''
LOG = logging.getLogger("hooks")
def add_post_run_hook(hook, *args, **kwargs):
'''Run hook after Fabric tasks have completed on all hosts
Example usage:
@add_post_run_hook(postrunfunc, 'arg1', 'arg2')
def mytask():
# ...
'''
def true_decorator(f):
return add_hooks(post=hook, post_args=args, post_kwargs=kwargs)(f)
return true_decorator
def add_hooks(pre=None, pre_args=(), pre_kwargs={},
post=None, post_args=(), post_kwargs={}):
'''
Function decorator to be used with Fabric tasks. Adds pre-run
and/or post-run hooks to a Fabric task. Uses env.all_hosts to
determine when to run the post hook. Uses the global variable,
parent_task_name, to check if the task is a subtask (i.e. a
decorated task called by another decorated task). If it is a
subtask, do not perform pre or post processing.
pre: callable to be run before starting Fabric tasks
pre_args: a tuple of arguments to be passed to "pre"
pre_kwargs: a dict of keyword arguments to be passed to "pre"
post: callable to be run after Fabric tasks have completed on all hosts
post_args: a tuple of arguments to be passed to "post"
post_kwargs: a dict of keyword arguments to be passed to "post"
'''
# create a namespace to save state across hosts and tasks
class NS(object):
run_counter = 0
def true_decorator(f):
@wraps(f)
def f_wrapper(*args, **kwargs):
# set state variables
global parent_task_name
if not parent_task_name:
parent_task_name = f.__name__
NS.run_counter += 1
# pre-run processing
if f.__name__ == parent_task_name and NS.run_counter == 1:
if pre:
pre(*pre_args, **pre_kwargs)
# run the task
r = None
try:
r = f(*args, **kwargs)
except SystemExit:
pass
except:
print traceback.format_exc()
# post-run processing
if (f.__name__ == parent_task_name and
NS.run_counter >= len(env.all_hosts)):
if post:
post(*post_args, **post_kwargs)
return r
return f_wrapper
return true_decorator