lib/Giovanni/Stages.pm

filter: Statementstmt 7.6% 10 / 130 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 20.0% 3 / 15 Podpod 0.0% 0 / 12 totaltotal 6.6% 13 / 195 SCAR 58.6
CC
20
Cov
6%
cleanup_timestamped
72.0
rollback_timestamped
30.0
post_rollout
20.0
CRAP 351.6
↑ Summary blib/lib/Giovanni/Stages.pm »
1 package Giovanni::Stages;
2  
3 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Expect;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ my ($self, $ssh) = @_;
Subroutine update_cache: not called CC=1 SCAR=6.9
Pod undocumented
15 0 $self->log($ssh, "running update_cache task ...");
16 0 return;
17 }
18  
19 sub rollout {
20 0 ▶ my ($self, $ssh) = @_;
Subroutine rollout: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($self, $ssh) = @_;
Subroutine rollout_timestamped: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($self, $ssh, $offset) = @_;
Subroutine rollback_timestamped: not called CC=5 SCAR=34.0
Pod undocumented
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 ▶ next unless $rel =~ m{^\w};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel =~ /^\w/ 0 0 0
65 0 ▶ if ($rel == $current_rel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel == $current_rel) 0 0 0
66 0 push(@future, $rel);
67 0 next;
68 }
69 0 ▶ if (@future) {
Branch: 1 branch
BranchTrueFalseTotal
if (@future) { } 0 0 0
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 ▶ my ($self, $ssh, $offset) = @_;
Subroutine rollback_scm: not called CC=2 SCAR=17.9
Pod undocumented
85  
86 # load SCM plugin
87 0 $self->load_plugin($self->scm);
88 0 my $tag = $self->get_last_tag($offset);
89 0 ▶ $self->log($ssh, "Rolling back to tag: $tag") if $self->is_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_debug 0 0 0
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 ▶ my ($self, $ssh) = @_;
Subroutine restart: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($self, $ssh) = @_;
Subroutine checkout: not called CC=1 SCAR=6.9
Pod undocumented
108 0 $self->log($ssh, "running checkout task ...");
109 0 return;
110 }
111  
112 sub cleanup_timestamped {
113 0 ▶ my ($self, $ssh, $offset) = @_;
Subroutine cleanup_timestamped: not called CC=8 SCAR=42.8
Pod undocumented
114  
115 0 $self->log($ssh, "running cleanup task ...");
116  
117 0 ▶ if ($self->config->{root} =~ m{^.*/\d+$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"root"} =~ m[^.*/\d+$]) 0 0 0
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 ▶ next unless $rel =~ m{^\w};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel =~ /^\w/ 0 0 0
136 0 ▶ if ($rel == $current_rel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel == $current_rel) 0 0 0
137 0 push(@future, $rel);
138 0 next;
139 }
140 0 ▶ if (@future) {
Branch: 1 branch
BranchTrueFalseTotal
if (@future) { } 0 0 0
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 ▶ my $num = $self->config->{keep_versions} || 5;
Condition0%0/2
$self->config->{'keep_versions'} || 5
l!l
00
MC/DC0%0/1
$self->config->{'keep_versions'} || 5
$self->config->{'keep_versions'}
Truth table0%0/2
A
A= $self->config->{'keep_versions'}
Aresult
11
00
149 0 my $log;
150 0 ▶ while ($#past > ($num)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#past > $num) 0 0 0
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 ▶ my ($self, $ssh) = @_;
Subroutine restart_phased: not called CC=3 SCAR=24.8
Pod undocumented
162  
163 0 $self->log($ssh, "running restart_phased task ...");
164 0 ▶ unless ($ssh->test("sudo " . $self->config->{init} . " restart")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh->test('sudo ' . $self->config->{"init"} . ' restart')) 0 0 0
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 ▶ $ssh->test("sudo " . $self->config->{init} . " start")
Branch: 1 branch
BranchTrueFalseTotal
$ssh->test('sudo ' . $self->config->{"init"} . ' start') or $self->error('restart failed: ' . $ssh->error) 0 0 0
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 ▶ my ($self, $ssh) = @_;
Subroutine reload_phased: not called CC=3 SCAR=24.8
Pod undocumented
185  
186 0 $self->log($ssh, "running reload_phased task ...");
187 0 ▶ unless ($ssh->test("sudo " . $self->config->{reload})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh->test('sudo ' . $self->config->{"reload"})) 0 0 0
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 ▶ $ssh->test("sudo " . $self->config->{init} . " start")
Branch: 1 branch
BranchTrueFalseTotal
$ssh->test('sudo ' . $self->config->{"init"} . ' start') or $self->error('restart failed: ' . $ssh->error) 0 0 0
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 ▶ my ($self, $ssh) = @_;
Subroutine send_notify: not called CC=1 SCAR=6.9
Pod undocumented
208 0 $self->log($ssh, "running send_notify task ...");
209 0 return;
210 }
211  
212 sub post_rollout {
213 0 ▶ my ($self, $ssh) = @_;
Subroutine post_rollout: not called CC=4 SCAR=30.0
Pod undocumented
214  
215 return
216 unless (exists $self->config->{post_rollout}
217 0 ▶ and defined $self->config->{post_rollout});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->config->{"post_rollout"} and defined $self->config->{"post_rollout"} 0 0 0
Condition0%0/3
exists $self->config->{"post_rollout"} and defined $self->config->{"post_rollout"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->config->{"post_rollout"} and defined $self->config->{"post_rollout"}
exists $self->config->{"post_rollout"} defined $self->config->{"post_rollout"}
Truth table0%0/3
A and B
A= exists $self->config->{"post_rollout"}B= defined $self->config->{"post_rollout"}
ABresult
0X0
100
111
218  
219 $self->log($ssh,
220 'running post_rollout script "'
221 . $self->config->{post_rollout}
222 0 . '" ...');
223 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh->test('cd ' . $self->config->{"deploy_dir"} . ' && ' . $self->config->{"post_rollout"})) 0 0 0
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 1 __PACKAGE__->meta->make_immutable;
235  
236 __END__
↑ Summary blib/lib/Giovanni/Stages.pm »