lib/Giovanni/Stages.pm

filter: Statementstmt 7.4% 10 / 135 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 18.7% 3 / 16 Podpod 0.0% 0 / 12 totaltotal 6.2% 13 / 207 CC 23 SCAR 32.8
CC
23
Cov
6%
cleanup_timestamped
72.0
rollback_timestamped
30.0
restart_phased
20.0
CRAP 26.4
↑ Summary lib/Giovanni.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 0 ▶sub update_cache {
Subroutine update_cache: not called CC=1 SCAR=6.9
Pod undocumented
14 0 my ($self, $ssh) = @_;
15 0 $self->log($ssh, "running update_cache task ...");
16 0 return;
17 }
18  
19 0 ▶sub rollout {
Subroutine rollout: not called CC=1 SCAR=6.9
Pod undocumented
20 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 0 ▶sub rollout_timestamped {
Subroutine rollout_timestamped: not called CC=1 SCAR=6.9
Pod undocumented
33 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 0 ▶sub rollback_timestamped {
Subroutine rollback_timestamped: not called CC=5 SCAR=34.0
Pod undocumented
50 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 ▶ 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 0 ▶sub rollback_scm {
Subroutine rollback_scm: not called CC=2 SCAR=17.9
Pod undocumented
84 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 ▶ $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 0 ▶sub restart {
Subroutine restart: not called CC=1 SCAR=6.9
Pod undocumented
98 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 0 ▶sub checkout {
Subroutine checkout: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my ($self, $ssh) = @_;
108 0 $self->log($ssh, "running checkout task ...");
109 0 return;
110 }
111  
112 0 ▶sub cleanup_timestamped {
Subroutine cleanup_timestamped: not called CC=8 SCAR=42.8
Pod undocumented
113 0 my ($self, $ssh, $offset) = @_;
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 0 ▶sub restart_phased {
Subroutine restart_phased: not called CC=4 SCAR=30.0
Pod undocumented
161 0 my ($self, $ssh) = @_;
162  
163 0 $self->log($ssh, "running restart_phased task ...");
164 0 ▶ unless ($ssh->test($self->_init_command('restart'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh->test($self->_init_command("restart"))) 0 0 0
165 0 $self->log(
166 'restart failed: ' . $ssh->error . ' trying stop -> start instead');
167 0 $ssh->test($self->_init_command('stop'));
168  
169 # give time to exit
170 0 sleep 2;
171 0 ▶ $ssh->test($self->_init_command('start'))
Branch: 1 branch
BranchTrueFalseTotal
$ssh->test($self->_init_command("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 $self->log(
179 $ssh,
180 'restarted '
181 . (
182 $self->config->{systemd}
183 ? $self->config->{systemd}
184 0 ▶ : $self->config->{init}));
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{'systemd'} ? : 0 0 0
185  
186 0 return;
187 }
188  
189 0 ▶sub reload_phased {
Subroutine reload_phased: not called CC=4 SCAR=30.0
Pod undocumented
190 0 my ($self, $ssh) = @_;
191  
192 0 $self->log($ssh, "running reload_phased task ...");
193 0 ▶ unless ($ssh->test($self->_init_command('restart'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh->test($self->_init_command("restart"))) 0 0 0
194 0 $self->log(
195 'reload failed: ' . $ssh->error . ' trying stop -> start instead');
196 0 $ssh->test($self->_init_command("stop"));
197  
198 # give time to exit
199 0 sleep 2;
200 0 ▶ $ssh->test($self->_init_command("start"))
Branch: 1 branch
BranchTrueFalseTotal
$ssh->test($self->_init_command("start")) or $self->error('restart failed: ' . $ssh->error) 0 0 0
201 or $self->error('restart failed: ' . $ssh->error);
202 0 return;
203 }
204  
205 # my $exp = Expect->init($pty);
206 # $exp->interact();
207 $self->log(
208 $ssh,
209 'restarted '
210 . (
211 $self->config->{systemd}
212 ? $self->config->{systemd}
213 0 ▶ : $self->config->{init}));
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{'systemd'} ? : 0 0 0
214  
215 0 return;
216 }
217  
218 0 ▶sub send_notify {
Subroutine send_notify: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my ($self, $ssh) = @_;
220 0 $self->log($ssh, "running send_notify task ...");
221 0 return;
222 }
223  
224 0 ▶sub post_rollout {
Subroutine post_rollout: not called CC=4 SCAR=30.0
Pod undocumented
225 0 my ($self, $ssh) = @_;
226  
227 return
228 unless (exists $self->config->{post_rollout}
229 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
230  
231 $self->log($ssh,
232 'running post_rollout script "'
233 . $self->config->{post_rollout}
234 0 . '" ...');
235 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh->test('cd ' . $self->config->{"deploy_dir"} . ' && ' . $self->config->{"post_rollout"})) 0 0 0
236 $ssh->test(
237 'cd '
238 . $self->config->{deploy_dir} . ' && '
239 . $self->config->{post_rollout}))
240 {
241 0 $self->error("post_rollout script failed: " . $ssh->error);
242 }
243 0 return;
244 }
245  
246 0 ▶sub _init_command {
Subroutine _init_command: not called CC=2 SCAR=17.9
247 0 my ($self, $command) = @_;
248  
249 0 ▶ if (defined $self->config->{systemd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->config->{'systemd'}) { } 0 0 0
250 0 return "sudo systemctl $command " . $self->config->{systemd} . ".service";
251 }
252 else {
253 0 return "sudo " . $self->config->{init} . " $command";
254 }
255 0 return;
256 }
257  
258 1 __PACKAGE__->meta->make_immutable;
259  
260 __END__
↑ Summary lib/Giovanni.pm »