blib/lib/Giovanni.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 144 SCAR 42.8
CC
8
Cov
0%
process_stages
30.0
load_plugin
12.0
log
6.0
CRAP 72.0
1 package Giovanni;
2  
3 use 5.010;
4 use Mouse;
5 use Mouse::Util;
6 use Net::OpenSSH;
7 use Sys::Hostname;
8 use Cwd;
9 use Giovanni::Stages;
10  
11 extends 'Giovanni::Stages';
12  
13  
14 our $VERSION = '1.9';
15  
16 has 'debug' => (
17 is => 'rw',
18 isa => 'Bool',
19 required => 1,
20 default => 0,
21 predicate => 'is_debug',
22 );
23  
24 has 'hostname' => (
25 is => 'rw',
26 isa => 'Str',
27 default => hostname(),
28 );
29  
30 has 'repo' => (
31 is => 'rw',
32 isa => 'Str',
33 required => 1,
34 default => cwd(),
35 );
36  
37 has 'scm' => (
38 is => 'rw',
39 isa => 'Str',
40 default => 'git',
41 );
42  
43 has 'deploy_to' => (
44 is => 'rw',
45 isa => 'Str',
46 default => '/var/www',
47 );
48  
49 has 'user' => (
50 is => 'rw',
51 isa => 'Str',
52 default => 'deploy',
53 );
54  
55 has 'version' => (
56 is => 'rw',
57 isa => 'Str',
58 default => 'v1',
59 );
60  
61 has 'error' => (
62 is => 'rw',
63 isa => 'Str',
64 );
65  
66 has 'config' => (
67 is => 'rw',
68 required => 1,
69 );
70  
71 has 'notifyer' => (
72 is => 'rw',
73 isa => 'Str',
74 default => 'jabber',
75 );
76  
77  
78 sub deploy {
79 my ($self) = @_;
80  
81 # load SCM plugin
82 $self->load_plugin($self->scm);
83 my $tag = $self->tag();
84 my $ssh = $self->_get_ssh_conn;
85 $self->process_stages($ssh, 'deploy');
86 }
87  
88 sub rollback {
89 my ($self, $offset) = @_;
90  
91 my $ssh = $self->_get_ssh_conn;
92 $self->process_stages($ssh, 'rollback');
93 }
94  
95 sub process_stages {
96 my ($self, $ssh, $mode) = @_;
97  
98 my $conf_key = ($mode eq 'restart' ? 'deploy' : $mode);
99 my @stages = split(/\s*,\s*/, $self->config->{$conf_key});
100 foreach my $stage (@stages) {
101 if($mode eq 'restart'){
102 next unless $stage =~ m/restart/;
103 $self->process_hosts($ssh, $stage, $mode);
104  
105 } else {
106 $self->process_hosts($ssh, $stage, $mode);
107  
108 # if one host produced an error while restarting, rollback all
109 if ($self->error and ($stage =~ m/^restart/i) and ($mode eq 'deploy')) {
110 $self->log('ERROR', $self->error);
111 $self->process_stages($ssh, 'rollback');
112 return;
113 }
114 }
115 }
116 }
117  
118 sub process_hosts {
119 my ($self, $ssh, $stage, $mode) = @_;
120  
121 my @hosts = split(/\s*,\s*/, $self->config->{hosts});
122 foreach my $host (@hosts) {
123 $self->log($ssh->{$host}, "running $stage");
124 $self->$stage($ssh->{$host});
125 }
126 }
127  
128 sub _get_ssh_conn {
129 my ($self) = @_;
130  
131 my @hosts = split(/\s*,\s*/, $self->config->{hosts});
132 my $ssh;
133 foreach my $host (@hosts) {
134 my $conn = $host;
135 $conn = ($self->config->{user} || $self->user) . '@' . $host;
136 $ssh->{$host} = Net::OpenSSH->new($conn, async => 1);
137 }
138  
139 # trigger noop command to check for connection
140 foreach my $host (@hosts) {
141 $ssh->{$host}->test('echo')
142 or confess "could not connect to $host: " . $ssh->{$host}->error;
143 $self->log($host, 'connected');
144 }
145 return $ssh;
146 }
147  
148 sub restart {
149 my ($self) = @_;
150  
151 my $ssh = $self->_get_ssh_conn;
152 $self->process_stages($ssh, 'restart');
153 }
154  
155 sub load_plugin {
156 my ($self, $plugin) = @_;
157  
158 my $plug = 'Giovanni::Plugins::' . ucfirst(lc($plugin));
159 unless (Mouse::Util::is_class_loaded($plug)) {
160 print STDERR "Loading $plugin Plugin\n" if $self->is_debug;
161 with($plug); # or confess "Could not load Plugin: '$plugin'\n";
162 }
163  
164 return;
165 }
166  
167 sub log {
168 my ($self, $host, $log) = @_;
169  
170 return unless $log;
171  
172 my $name;
173 given ($host) {
174 when (ref $host eq 'Net::OpenSSH') { $name = $host->get_host; }
175 default { $name = $host; }
176 }
177 chomp($log);
178 print STDERR "[" . $name . "] " . $log . $/;
179  
180 return;
181 }
182  
183 sub notify {
184 my ($self, $ssh, $conf) = @_;
185  
186 # load notify plugin
187 $self->load_plugin($self->notifyer);
188 $self->send_notify($ssh, $conf);
189 return;
190 }
191  
192  
193 1;
194  
195 __END__