| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# |
|
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Rex::Transaction - Transaction support |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
With this module you can define transactions and rollback scenarios on failure. |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Rex::Transaction; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
task 'do-something', 'server01', sub { |
|
18
|
|
|
|
|
|
|
transaction { |
|
19
|
|
|
|
|
|
|
on_rollback { |
|
20
|
|
|
|
|
|
|
rmdir '/tmp/mydata'; |
|
21
|
|
|
|
|
|
|
}; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
mkdir '/tmp/mydata'; |
|
24
|
|
|
|
|
|
|
upload 'files/myapp.tar.gz', '/tmp/mydata'; |
|
25
|
|
|
|
|
|
|
run 'tar xzf myapp.tar.gz -C /tmp/mydata'; |
|
26
|
|
|
|
|
|
|
if ( $? != 0 ) { die('Error extracting myapp.tar.gz'); } |
|
27
|
|
|
|
|
|
|
}; |
|
28
|
|
|
|
|
|
|
}; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 EXPORTED FUNCTIONS |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
package Rex::Transaction; |
|
35
|
|
|
|
|
|
|
|
|
36
|
22
|
|
|
22
|
|
320
|
use v5.12.5; |
|
|
22
|
|
|
|
|
76
|
|
|
37
|
22
|
|
|
22
|
|
131
|
use warnings; |
|
|
22
|
|
|
|
|
48
|
|
|
|
22
|
|
|
|
|
1090
|
|
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our $VERSION = '1.14.2.2'; # TRIAL VERSION |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
require Exporter; |
|
42
|
|
|
|
|
|
|
|
|
43
|
22
|
|
|
22
|
|
111
|
use vars qw(@EXPORT @ROLLBACKS); |
|
|
22
|
|
|
|
|
45
|
|
|
|
22
|
|
|
|
|
1005
|
|
|
44
|
22
|
|
|
22
|
|
287
|
use base qw(Exporter); |
|
|
22
|
|
|
|
|
74
|
|
|
|
22
|
|
|
|
|
1817
|
|
|
45
|
|
|
|
|
|
|
|
|
46
|
22
|
|
|
22
|
|
157
|
use Rex::Logger; |
|
|
22
|
|
|
|
|
244
|
|
|
|
22
|
|
|
|
|
163
|
|
|
47
|
22
|
|
|
22
|
|
811
|
use Rex::TaskList; |
|
|
22
|
|
|
|
|
82
|
|
|
|
22
|
|
|
|
|
195
|
|
|
48
|
22
|
|
|
22
|
|
451
|
use Data::Dumper; |
|
|
22
|
|
|
|
|
199
|
|
|
|
22
|
|
|
|
|
7964
|
|
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
@EXPORT = qw(transaction on_rollback); |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 transaction($codeRef) |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Start a transaction for C<$codeRef>. If C<$codeRef> dies, Rex will run the L code to roll back the transaction. |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
task 'deploy', group => 'frontend', sub { |
|
57
|
|
|
|
|
|
|
on_rollback { |
|
58
|
|
|
|
|
|
|
rmdir '...'; |
|
59
|
|
|
|
|
|
|
}; |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
deploy 'myapp.tar.gz'; |
|
62
|
|
|
|
|
|
|
}; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
task 'restart_server', group => 'frontend', sub { |
|
65
|
|
|
|
|
|
|
service apache2 => 'restart'; |
|
66
|
|
|
|
|
|
|
}; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
task 'all', group => 'frontend', sub { |
|
69
|
|
|
|
|
|
|
transaction { |
|
70
|
|
|
|
|
|
|
do_task [qw/deploy restart_server/]; |
|
71
|
|
|
|
|
|
|
}; |
|
72
|
|
|
|
|
|
|
}; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub transaction(&) { |
|
77
|
2
|
|
|
2
|
1
|
108
|
my ($code) = @_; |
|
78
|
2
|
|
|
|
|
6
|
my $ret = 1; |
|
79
|
|
|
|
|
|
|
|
|
80
|
2
|
|
|
|
|
8
|
Rex::Logger::debug("Cleaning ROLLBACKS array"); |
|
81
|
2
|
|
|
|
|
9
|
@ROLLBACKS = (); |
|
82
|
|
|
|
|
|
|
|
|
83
|
2
|
|
|
|
|
73
|
Rex::TaskList->create()->set_in_transaction(1); |
|
84
|
|
|
|
|
|
|
|
|
85
|
2
|
|
|
|
|
5
|
eval { &$code(); }; |
|
|
2
|
|
|
|
|
22
|
|
|
86
|
|
|
|
|
|
|
|
|
87
|
2
|
50
|
|
|
|
17
|
if ($@) { |
|
88
|
2
|
|
|
|
|
24
|
my $err = $@; |
|
89
|
2
|
|
|
|
|
13
|
Rex::Logger::info("Transaction failed. Rolling back."); |
|
90
|
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
24
|
$ret = 0; |
|
92
|
2
|
|
|
|
|
50
|
for my $rollback_code ( reverse @ROLLBACKS ) { |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# push the connection of the task back |
|
95
|
0
|
|
|
|
|
0
|
Rex::push_connection( $rollback_code->{"connection"} ); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# run the rollback code |
|
98
|
0
|
|
|
|
|
0
|
&{ $rollback_code->{"code"} }($err); |
|
|
0
|
|
|
|
|
0
|
|
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# and pop it away |
|
101
|
0
|
|
|
|
|
0
|
Rex::pop_connection(); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
2
|
|
|
|
|
56
|
Rex::TaskList->create()->set_in_transaction(0); |
|
105
|
|
|
|
|
|
|
|
|
106
|
2
|
|
|
|
|
76
|
die("Transaction failed. Rollback done."); |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
Rex::TaskList->create()->set_in_transaction(0); |
|
110
|
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
return $ret; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 on_rollback($codeRef) |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This will execute C<$codeRef> if a step in the L fails. |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub on_rollback(&) { |
|
122
|
0
|
|
|
0
|
1
|
|
my ($code) = @_; |
|
123
|
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
push( |
|
125
|
|
|
|
|
|
|
@ROLLBACKS, |
|
126
|
|
|
|
|
|
|
{ |
|
127
|
|
|
|
|
|
|
code => $code, |
|
128
|
|
|
|
|
|
|
connection => Rex::get_current_connection() |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
); |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |