@@ -35,12 +35,13 @@ class CommandsBase:
35
35
so that it can be passed through Pool.map().
36
36
"""
37
37
38
- def __init__ (self , name , commands ):
38
+ def __init__ (self , name , commands , cleanup = None ):
39
39
self .name = name
40
40
self .commands = commands
41
41
self .failed = False
42
42
self .retcodes = {}
43
43
self .outputs = {}
44
+ self .cleanup = cleanup
44
45
45
46
def __str__ (self ):
46
47
return str (self .name )
@@ -61,7 +62,7 @@ def fill(self, retcodes, outputs, failed):
61
62
62
63
class Commands (CommandsBase ):
63
64
def __init__ (self , base ):
64
- super ().__init__ (base .name , base .commands )
65
+ super ().__init__ (base .name , base .commands , base . cleanup )
65
66
66
67
self .logger = logging .getLogger (__name__ )
67
68
logging .basicConfig ()
@@ -88,12 +89,29 @@ def run(self):
88
89
if retcode == 2 :
89
90
self .logger .info ("command '{}' requested break" .
90
91
format (cmd ))
92
+ self .run_cleanup ()
91
93
else :
92
94
self .logger .info ("command '{}' failed with code {}, "
93
95
"breaking" .format (cmd , retcode ))
94
96
self .failed = True
97
+ self .run_cleanup ()
95
98
break
96
99
100
+ def run_cleanup (self ):
101
+ """
102
+ Call cleanup in case the sequence failed or termination was requested.
103
+ """
104
+ if self .cleanup :
105
+ self .logger .debug ("Running cleanup command '{}'" .
106
+ format (self .cleanup ))
107
+ cmd = Command (self .cleanup ,
108
+ args_subst = {"ARG" : self .name },
109
+ args_append = [self .name ], excl_subst = True )
110
+ cmd .execute ()
111
+ if cmd .getretcode () != 0 :
112
+ self .logger .info ("cleanup command '{}' failed with code {}" .
113
+ format (self .cleanup , cmd .getretcode ()))
114
+
97
115
def check (self , ignore_errors ):
98
116
"""
99
117
Check the output of the commands and perform logging.
0 commit comments