blib/lib/Giovanni/Stages.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 192 SCAR 52.0
CC
13
Cov
0%
cleanup_timestamped
30.0
rollback_timestamped
20.0
post_rollout
12.0
CRAP 182.0
1 package Giovanni::Stages;
2  
3 use Mouse;
4 use Expect;
5 use Data::Dumper;
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 my ($self, $ssh) = @_;
15 $self->log($ssh, "running update_cache task ...");
16 return;
17 }
18  
19 sub rollout {
20 my ($self, $ssh) = @_;
21  
22 $self->log($ssh, "running rollout task ...");
23 $self->config->{deploy_dir} = $self->config->{root};
24 my $log = $ssh->capture("mkdir -p " . $self->config->{deploy_dir});
25 $self->log($ssh, $log);
26 $self->checkout($ssh);
27 $self->post_rollout($ssh);
28  
29 return;
30 }
31  
32 sub rollout_timestamped {
33 my ($self, $ssh) = @_;
34  
35 $self->log($ssh, "running rollout_timestamped task ...");
36 my $deploy_dir = join('/', $self->config->{root}, 'releases', time);
37 my $current = join('/', $self->config->{root}, 'current');
38 my $log = $ssh->capture("mkdir -p " . $deploy_dir);
39 $self->config->{deploy_dir} = $deploy_dir;
40 $self->checkout($ssh);
41 $log .= $ssh->capture(
42 "unlink " . $current . "; ln -s " . $deploy_dir . " " . $current);
43 $self->log($ssh, $log);
44 $self->post_rollout($ssh);
45  
46 return;
47 }
48  
49 sub rollback_timestamped {
50 my ($self, $ssh, $offset) = @_;
51  
52 $self->log($ssh, "running rollback task ...");
53 my $deploy_dir = join('/', $self->config->{root}, 'releases');
54 my $current = join('/', $self->config->{root}, 'current');
55 my @rels = $ssh->capture("ls -1 " . $deploy_dir);
56 @rels = sort(@rels);
57 my $link = $ssh->capture("ls -l " . $current . " | sed 's/^.*->\\s*//'");
58 my @path = split(/\//, $link);
59 my $current_rel = pop(@path);
60 my (@past, @future);
61  
62 foreach my $rel (@rels) {
63 chomp($rel);
64 next unless $rel =~ m{^\w};
65 if ($rel == $current_rel) {
66 push(@future, $rel);
67 next;
68 }
69 if (@future) {
70 push(@future, $rel);
71 }
72 else {
73 push(@past, $rel);
74 }
75 }
76 $deploy_dir = join('/', $self->config->{root}, 'releases', pop(@past));
77 my $log = $ssh->capture(
78 "unlink " . $current . "; ln -s " . $deploy_dir . " " . $current);
79 $self->log($ssh, $log);
80 return;
81 }
82  
83 sub rollback_scm {
84 my ($self, $ssh, $offset) = @_;
85  
86 # load SCM plugin
87 $self->load_plugin($self->scm);
88 my $tag = $self->get_last_tag($offset);
89 $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 return;
95 }
96  
97 sub restart {
98 my ($self, $ssh) = @_;
99  
100 $self->log("running restart task ...");
101 my $log = $ssh->capture($self->config->{init});
102 $self->log($ssh, $log);
103 return;
104 }
105  
106 sub checkout {
107 my ($self, $ssh) = @_;
108 $self->log($ssh, "running checkout task ...");
109 return;
110 }
111  
112 sub cleanup_timestamped {
113 my ($self, $ssh, $offset) = @_;
114  
115 $self->log($ssh, "running cleanup task ...");
116  
117 if ($self->config->{root} =~ m{^.*/\d+$}) {
118 my @path = split(/\//, $self->config->{root});
119 pop(@path);
120 pop(@path);
121 $self->config->{root} = join('/', @path);
122 }
123  
124 my $deploy_dir = join('/', $self->config->{root}, 'releases');
125 my $current = join('/', $self->config->{root}, 'current');
126 my @rels = $ssh->capture("ls -1 " . $deploy_dir);
127 @rels = sort(@rels);
128 my $link = $ssh->capture("ls -l " . $current . " | sed 's/^.*->\\s*//'");
129 my @path = split(/\//, $link);
130 my $current_rel = pop(@path);
131 my (@past, @future);
132  
133 foreach my $rel (@rels) {
134 chomp($rel);
135 next unless $rel =~ m{^\w};
136 if ($rel == $current_rel) {
137 push(@future, $rel);
138 next;
139 }
140 if (@future) {
141 push(@future, $rel);
142 }
143 else {
144 push(@past, $rel);
145 }
146 }
147 $deploy_dir = join('/', $self->config->{root}, pop(@past));
148 my $num = $self->config->{keep_versions} || 5;
149 my $log;
150 while ($#past > ($num)) {
151 my $to_del = join('/', $self->config->{root}, 'releases', shift(@past));
152 $self->log($ssh, "deleting $to_del");
153 $log = $ssh->capture("rm -rf " . $to_del);
154 }
155 $self->log($ssh, $log);
156  
157 return;
158 }
159  
160 sub restart_phased {
161 my ($self, $ssh) = @_;
162  
163 $self->log($ssh, "running restart_phased task ...");
164 unless ($ssh->test("sudo " . $self->config->{init} . " restart")) {
165 $self->log(
166 'restart failed: ' . $ssh->error . ' trying stop -> start instead');
167 $ssh->test("sudo " . $self->config->{init} . " stop");
168  
169 # give time to exit
170 sleep 2;
171 $ssh->test("sudo " . $self->config->{init} . " start")
172 or $self->error('restart failed: ' . $ssh->error);
173 return;
174 }
175  
176 # my $exp = Expect->init($pty);
177 # $exp->interact();
178 $self->log($ssh, 'restarted ' . $self->config->{init});
179  
180 return;
181 }
182  
183 sub reload_phased {
184 my ($self, $ssh) = @_;
185  
186 $self->log($ssh, "running reload_phased task ...");
187 unless ($ssh->test("sudo " . $self->config->{reload})) {
188 $self->log(
189 'reload failed: ' . $ssh->error . ' trying stop -> start instead');
190 $ssh->test("sudo " . $self->config->{init} . " stop");
191  
192 # give time to exit
193 sleep 2;
194 $ssh->test("sudo " . $self->config->{init} . " start")
195 or $self->error('restart failed: ' . $ssh->error);
196 return;
197 }
198  
199 # my $exp = Expect->init($pty);
200 # $exp->interact();
201 $self->log($ssh, 'reloaded ' . $self->config->{reload});
202  
203 return;
204 }
205  
206 sub send_notify {
207 my ($self, $ssh) = @_;
208 $self->log($ssh, "running send_notify task ...");
209 return;
210 }
211  
212 sub post_rollout {
213 my ($self, $ssh) = @_;
214  
215 return
216 unless (exists $self->config->{post_rollout}
217 and defined $self->config->{post_rollout});
218  
219 $self->log($ssh,
220 'running post_rollout script "'
221 . $self->config->{post_rollout}
222 . '" ...');
223 unless (
224 $ssh->test(
225 'cd '
226 . $self->config->{deploy_dir} . ' && '
227 . $self->config->{post_rollout}))
228 {
229 $self->error("post_rollout script failed: " . $ssh->error);
230 }
231 return;
232 }
233  
234 __PACKAGE__->meta->make_immutable;
235  
236 __END__