line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Giovanni::Stages; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
9
|
use Mouse; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
14
|
|
4
|
1
|
|
|
1
|
|
2919
|
use Expect; |
|
1
|
|
|
|
|
33416
|
|
|
1
|
|
|
|
|
101
|
|
5
|
1
|
|
|
1
|
|
1108
|
use Data::Dumper; |
|
1
|
|
|
|
|
10617
|
|
|
1
|
|
|
|
|
1684
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# Stages are defined here and expected to be overridden with plugins. |
8
|
|
|
|
|
|
|
# the idea is to have different plugins that can extend the existing |
9
|
|
|
|
|
|
|
# stages real easy. If this stages approach turns out to be too limited |
10
|
|
|
|
|
|
|
# (ie 1000s of stages in one file, not a good look) we may need to |
11
|
|
|
|
|
|
|
# rethink this approach. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub update_cache { |
14
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
15
|
0
|
|
|
|
|
|
$self->log($ssh, "running update_cache task ..."); |
16
|
0
|
|
|
|
|
|
return; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub rollout { |
20
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
$self->log($ssh, "running rollout task ..."); |
23
|
0
|
|
|
|
|
|
$self->config->{deploy_dir} = $self->config->{root}; |
24
|
0
|
|
|
|
|
|
my $log = $ssh->capture("mkdir -p " . $self->config->{deploy_dir}); |
25
|
0
|
|
|
|
|
|
$self->log($ssh, $log); |
26
|
0
|
|
|
|
|
|
$self->checkout($ssh); |
27
|
0
|
|
|
|
|
|
$self->post_rollout($ssh); |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
return; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub rollout_timestamped { |
33
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$self->log($ssh, "running rollout_timestamped task ..."); |
36
|
0
|
|
|
|
|
|
my $deploy_dir = join('/', $self->config->{root}, 'releases', time); |
37
|
0
|
|
|
|
|
|
my $current = join('/', $self->config->{root}, 'current'); |
38
|
0
|
|
|
|
|
|
my $log = $ssh->capture("mkdir -p " . $deploy_dir); |
39
|
0
|
|
|
|
|
|
$self->config->{deploy_dir} = $deploy_dir; |
40
|
0
|
|
|
|
|
|
$self->checkout($ssh); |
41
|
0
|
|
|
|
|
|
$log .= $ssh->capture( |
42
|
|
|
|
|
|
|
"unlink " . $current . "; ln -s " . $deploy_dir . " " . $current); |
43
|
0
|
|
|
|
|
|
$self->log($ssh, $log); |
44
|
0
|
|
|
|
|
|
$self->post_rollout($ssh); |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub rollback_timestamped { |
50
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh, $offset) = @_; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$self->log($ssh, "running rollback task ..."); |
53
|
0
|
|
|
|
|
|
my $deploy_dir = join('/', $self->config->{root}, 'releases'); |
54
|
0
|
|
|
|
|
|
my $current = join('/', $self->config->{root}, 'current'); |
55
|
0
|
|
|
|
|
|
my @rels = $ssh->capture("ls -1 " . $deploy_dir); |
56
|
0
|
|
|
|
|
|
@rels = sort(@rels); |
57
|
0
|
|
|
|
|
|
my $link = $ssh->capture("ls -l " . $current . " | sed 's/^.*->\\s*//'"); |
58
|
0
|
|
|
|
|
|
my @path = split(/\//, $link); |
59
|
0
|
|
|
|
|
|
my $current_rel = pop(@path); |
60
|
0
|
|
|
|
|
|
my (@past, @future); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
foreach my $rel (@rels) { |
63
|
0
|
|
|
|
|
|
chomp($rel); |
64
|
0
|
0
|
|
|
|
|
next unless $rel =~ m{^\w}; |
65
|
0
|
0
|
|
|
|
|
if ($rel == $current_rel) { |
66
|
0
|
|
|
|
|
|
push(@future, $rel); |
67
|
0
|
|
|
|
|
|
next; |
68
|
|
|
|
|
|
|
} |
69
|
0
|
0
|
|
|
|
|
if (@future) { |
70
|
0
|
|
|
|
|
|
push(@future, $rel); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
0
|
|
|
|
|
|
push(@past, $rel); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
0
|
|
|
|
|
|
$deploy_dir = join('/', $self->config->{root}, 'releases', pop(@past)); |
77
|
0
|
|
|
|
|
|
my $log = $ssh->capture( |
78
|
|
|
|
|
|
|
"unlink " . $current . "; ln -s " . $deploy_dir . " " . $current); |
79
|
0
|
|
|
|
|
|
$self->log($ssh, $log); |
80
|
0
|
|
|
|
|
|
return; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub rollback_scm { |
84
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh, $offset) = @_; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# load SCM plugin |
87
|
0
|
|
|
|
|
|
$self->load_plugin($self->scm); |
88
|
0
|
|
|
|
|
|
my $tag = $self->get_last_tag($offset); |
89
|
0
|
0
|
|
|
|
|
$self->log($ssh, "Rolling back to tag: $tag") if $self->is_debug; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# TODO change checkout to accept an optional tag so we can reuse it |
92
|
|
|
|
|
|
|
# here to check out an old version. |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub restart { |
98
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
$self->log("running restart task ..."); |
101
|
0
|
|
|
|
|
|
my $log = $ssh->capture($self->config->{init}); |
102
|
0
|
|
|
|
|
|
$self->log($ssh, $log); |
103
|
0
|
|
|
|
|
|
return; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub checkout { |
107
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
108
|
0
|
|
|
|
|
|
$self->log($ssh, "running checkout task ..."); |
109
|
0
|
|
|
|
|
|
return; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub cleanup_timestamped { |
113
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh, $offset) = @_; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
$self->log($ssh, "running cleanup task ..."); |
116
|
|
|
|
|
|
|
|
117
|
0
|
0
|
|
|
|
|
if ($self->config->{root} =~ m{^.*/\d+$}) { |
118
|
0
|
|
|
|
|
|
my @path = split(/\//, $self->config->{root}); |
119
|
0
|
|
|
|
|
|
pop(@path); |
120
|
0
|
|
|
|
|
|
pop(@path); |
121
|
0
|
|
|
|
|
|
$self->config->{root} = join('/', @path); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
my $deploy_dir = join('/', $self->config->{root}, 'releases'); |
125
|
0
|
|
|
|
|
|
my $current = join('/', $self->config->{root}, 'current'); |
126
|
0
|
|
|
|
|
|
my @rels = $ssh->capture("ls -1 " . $deploy_dir); |
127
|
0
|
|
|
|
|
|
@rels = sort(@rels); |
128
|
0
|
|
|
|
|
|
my $link = $ssh->capture("ls -l " . $current . " | sed 's/^.*->\\s*//'"); |
129
|
0
|
|
|
|
|
|
my @path = split(/\//, $link); |
130
|
0
|
|
|
|
|
|
my $current_rel = pop(@path); |
131
|
0
|
|
|
|
|
|
my (@past, @future); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
foreach my $rel (@rels) { |
134
|
0
|
|
|
|
|
|
chomp($rel); |
135
|
0
|
0
|
|
|
|
|
next unless $rel =~ m{^\w}; |
136
|
0
|
0
|
|
|
|
|
if ($rel == $current_rel) { |
137
|
0
|
|
|
|
|
|
push(@future, $rel); |
138
|
0
|
|
|
|
|
|
next; |
139
|
|
|
|
|
|
|
} |
140
|
0
|
0
|
|
|
|
|
if (@future) { |
141
|
0
|
|
|
|
|
|
push(@future, $rel); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
else { |
144
|
0
|
|
|
|
|
|
push(@past, $rel); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
0
|
|
|
|
|
|
$deploy_dir = join('/', $self->config->{root}, pop(@past)); |
148
|
0
|
|
0
|
|
|
|
my $num = $self->config->{keep_versions} || 5; |
149
|
0
|
|
|
|
|
|
my $log; |
150
|
0
|
|
|
|
|
|
while ($#past > ($num)) { |
151
|
0
|
|
|
|
|
|
my $to_del = join('/', $self->config->{root}, 'releases', shift(@past)); |
152
|
0
|
|
|
|
|
|
$self->log($ssh, "deleting $to_del"); |
153
|
0
|
|
|
|
|
|
$log = $ssh->capture("rm -rf " . $to_del); |
154
|
|
|
|
|
|
|
} |
155
|
0
|
|
|
|
|
|
$self->log($ssh, $log); |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
return; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub restart_phased { |
161
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
$self->log($ssh, "running restart_phased task ..."); |
164
|
0
|
0
|
|
|
|
|
unless ($ssh->test("sudo " . $self->config->{init} . " restart")) { |
165
|
0
|
|
|
|
|
|
$self->log( |
166
|
|
|
|
|
|
|
'restart failed: ' . $ssh->error . ' trying stop -> start instead'); |
167
|
0
|
|
|
|
|
|
$ssh->test("sudo " . $self->config->{init} . " stop"); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# give time to exit |
170
|
0
|
|
|
|
|
|
sleep 2; |
171
|
0
|
0
|
|
|
|
|
$ssh->test("sudo " . $self->config->{init} . " start") |
172
|
|
|
|
|
|
|
or $self->error('restart failed: ' . $ssh->error); |
173
|
0
|
|
|
|
|
|
return; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# my $exp = Expect->init($pty); |
177
|
|
|
|
|
|
|
# $exp->interact(); |
178
|
0
|
|
|
|
|
|
$self->log($ssh, 'restarted ' . $self->config->{init}); |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
return; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub reload_phased { |
184
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
$self->log($ssh, "running reload_phased task ..."); |
187
|
0
|
0
|
|
|
|
|
unless ($ssh->test("sudo " . $self->config->{reload})) { |
188
|
0
|
|
|
|
|
|
$self->log( |
189
|
|
|
|
|
|
|
'reload failed: ' . $ssh->error . ' trying stop -> start instead'); |
190
|
0
|
|
|
|
|
|
$ssh->test("sudo " . $self->config->{init} . " stop"); |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# give time to exit |
193
|
0
|
|
|
|
|
|
sleep 2; |
194
|
0
|
0
|
|
|
|
|
$ssh->test("sudo " . $self->config->{init} . " start") |
195
|
|
|
|
|
|
|
or $self->error('restart failed: ' . $ssh->error); |
196
|
0
|
|
|
|
|
|
return; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# my $exp = Expect->init($pty); |
200
|
|
|
|
|
|
|
# $exp->interact(); |
201
|
0
|
|
|
|
|
|
$self->log($ssh, 'reloaded ' . $self->config->{reload}); |
202
|
|
|
|
|
|
|
|
203
|
0
|
|
|
|
|
|
return; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub send_notify { |
207
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
208
|
0
|
|
|
|
|
|
$self->log($ssh, "running send_notify task ..."); |
209
|
0
|
|
|
|
|
|
return; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub post_rollout { |
213
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh) = @_; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
return |
216
|
|
|
|
|
|
|
unless (exists $self->config->{post_rollout} |
217
|
0
|
0
|
0
|
|
|
|
and defined $self->config->{post_rollout}); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
$self->log($ssh, |
220
|
|
|
|
|
|
|
'running post_rollout script "' |
221
|
|
|
|
|
|
|
. $self->config->{post_rollout} |
222
|
0
|
|
|
|
|
|
. '" ...'); |
223
|
0
|
0
|
|
|
|
|
unless ( |
224
|
|
|
|
|
|
|
$ssh->test( |
225
|
|
|
|
|
|
|
'cd ' |
226
|
|
|
|
|
|
|
. $self->config->{deploy_dir} . ' && ' |
227
|
|
|
|
|
|
|
. $self->config->{post_rollout})) |
228
|
|
|
|
|
|
|
{ |
229
|
0
|
|
|
|
|
|
$self->error("post_rollout script failed: " . $ssh->error); |
230
|
|
|
|
|
|
|
} |
231
|
0
|
|
|
|
|
|
return; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
__END__ |