line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Giovanni; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
14379
|
use 5.010; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
426
|
use Mouse; |
|
1
|
|
|
|
|
20295
|
|
|
1
|
|
|
|
|
4
|
|
5
|
1
|
|
|
1
|
|
255
|
use Mouse::Util; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
956
|
use Net::OpenSSH; |
|
1
|
|
|
|
|
27697
|
|
|
1
|
|
|
|
|
42
|
|
7
|
1
|
|
|
1
|
|
585
|
use Sys::Hostname; |
|
1
|
|
|
|
|
857
|
|
|
1
|
|
|
|
|
52
|
|
8
|
1
|
|
|
1
|
|
6
|
use Cwd; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
48
|
|
9
|
1
|
|
|
1
|
|
279
|
use Giovanni::Stages; |
|
1
|
|
|
|
|
109
|
|
|
1
|
|
|
|
|
900
|
|
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
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# load SCM plugin |
82
|
0
|
|
|
|
|
|
$self->load_plugin($self->scm); |
83
|
0
|
|
|
|
|
|
my $tag = $self->tag(); |
84
|
0
|
|
|
|
|
|
my $ssh = $self->_get_ssh_conn; |
85
|
0
|
|
|
|
|
|
$self->process_stages($ssh, 'deploy'); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub rollback { |
89
|
0
|
|
|
0
|
0
|
|
my ($self, $offset) = @_; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $ssh = $self->_get_ssh_conn; |
92
|
0
|
|
|
|
|
|
$self->process_stages($ssh, 'rollback'); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub process_stages { |
96
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh, $mode) = @_; |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
my $conf_key = ($mode eq 'restart' ? 'deploy' : $mode); |
99
|
0
|
|
|
|
|
|
my @stages = split(/\s*,\s*/, $self->config->{$conf_key}); |
100
|
0
|
|
|
|
|
|
foreach my $stage (@stages) { |
101
|
0
|
0
|
|
|
|
|
if($mode eq 'restart'){ |
102
|
0
|
0
|
|
|
|
|
next unless $stage =~ m/restart/; |
103
|
0
|
|
|
|
|
|
$self->process_hosts($ssh, $stage, $mode); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} else { |
106
|
0
|
|
|
|
|
|
$self->process_hosts($ssh, $stage, $mode); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# if one host produced an error while restarting, rollback all |
109
|
0
|
0
|
0
|
|
|
|
if ($self->error and ($stage =~ m/^restart/i) and ($mode eq 'deploy')) { |
|
|
|
0
|
|
|
|
|
110
|
0
|
|
|
|
|
|
$self->log('ERROR', $self->error); |
111
|
0
|
|
|
|
|
|
$self->process_stages($ssh, 'rollback'); |
112
|
0
|
|
|
|
|
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub process_hosts { |
119
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh, $stage, $mode) = @_; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
my @hosts = split(/\s*,\s*/, $self->config->{hosts}); |
122
|
0
|
|
|
|
|
|
foreach my $host (@hosts) { |
123
|
0
|
|
|
|
|
|
$self->log($ssh->{$host}, "running $stage"); |
124
|
0
|
|
|
|
|
|
$self->$stage($ssh->{$host}); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub _get_ssh_conn { |
129
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
my @hosts = split(/\s*,\s*/, $self->config->{hosts}); |
132
|
0
|
|
|
|
|
|
my $ssh; |
133
|
0
|
|
|
|
|
|
foreach my $host (@hosts) { |
134
|
0
|
|
|
|
|
|
my $conn = $host; |
135
|
0
|
|
0
|
|
|
|
$conn = ($self->config->{user} || $self->user) . '@' . $host; |
136
|
0
|
|
|
|
|
|
$ssh->{$host} = Net::OpenSSH->new($conn, async => 1); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# trigger noop command to check for connection |
140
|
0
|
|
|
|
|
|
foreach my $host (@hosts) { |
141
|
0
|
0
|
|
|
|
|
$ssh->{$host}->test('echo') |
142
|
|
|
|
|
|
|
or confess "could not connect to $host: " . $ssh->{$host}->error; |
143
|
0
|
|
|
|
|
|
$self->log($host, 'connected'); |
144
|
|
|
|
|
|
|
} |
145
|
0
|
|
|
|
|
|
return $ssh; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub restart { |
149
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $ssh = $self->_get_ssh_conn; |
152
|
0
|
|
|
|
|
|
$self->process_stages($ssh, 'restart'); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub load_plugin { |
156
|
0
|
|
|
0
|
0
|
|
my ($self, $plugin) = @_; |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $plug = 'Giovanni::Plugins::' . ucfirst(lc($plugin)); |
159
|
0
|
0
|
|
|
|
|
unless (Mouse::Util::is_class_loaded($plug)) { |
160
|
0
|
0
|
|
|
|
|
print STDERR "Loading $plugin Plugin\n" if $self->is_debug; |
161
|
0
|
|
|
|
|
|
with($plug); # or confess "Could not load Plugin: '$plugin'\n"; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
return; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub log { |
168
|
0
|
|
|
0
|
0
|
|
my ($self, $host, $log) = @_; |
169
|
|
|
|
|
|
|
|
170
|
0
|
0
|
|
|
|
|
return unless $log; |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my $name; |
173
|
0
|
|
|
|
|
|
given ($host) { |
174
|
0
|
|
|
|
|
|
when (ref $host eq 'Net::OpenSSH') { $name = $host->get_host; } |
|
0
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
default { $name = $host; } |
|
0
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
177
|
0
|
|
|
|
|
|
chomp($log); |
178
|
0
|
|
|
|
|
|
print STDERR "[" . $name . "] " . $log . $/; |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
return; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub notify { |
184
|
0
|
|
|
0
|
0
|
|
my ($self, $ssh, $conf) = @_; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# load notify plugin |
187
|
0
|
|
|
|
|
|
$self->load_plugin($self->notifyer); |
188
|
0
|
|
|
|
|
|
$self->send_notify($ssh, $conf); |
189
|
0
|
|
|
|
|
|
return; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
__END__ |