Helm.pm

filter: Statementstmt 24.5% 89 / 363 Branchbran 0.0% 0 / 142 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 39.1% 18 / 46 Podpod 0.0% 0 / 11 totaltotal 16.5% 107 / 646 CC 95 SCAR 56.6
CC
95
Cov
16%
steer
812.0
BUILD
600.0
__ANON__
156.0
CRAP 287.5
↑ Summary Helm/Log/Channel/irc.pm »
1 package Helm;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose::Util::TypeConstraints qw(enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec::Functions qw(catdir catfile tmpdir devnull);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::OpenSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Helm::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Helm::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Parallel::ForkManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = 0.4;
22 1 our $DEBUG = 0;
23 1 our $DEBUG_LOG;
24 1 our $DEBUG_LOG_PID;
25  
26 1 enum LOG_LEVEL => qw(debug info warn error);
27 1 enum LOCK_TYPE => qw(none local remote both);
28  
29 1 has task => (is => 'ro', writer => '_task', required => 1);
30 1 has user => (is => 'ro', writer => '_user', isa => 'Maybe[Str]');
31 1 has config_uri => (is => 'ro', writer => '_config_uri', isa => 'Maybe[Str]');
32 1 has config => (is => 'ro', writer => '_config', isa => 'Helm::Conf');
33 1 has lock_type => (is => 'ro', writer => '_lock_type', isa => 'LOCK_TYPE');
34 1 has sleep => (is => 'ro', writer => '_sleep', isa => 'Maybe[Num]');
35 1 has current_server => (is => 'ro', writer => '_current_server', isa => 'Helm::Server');
36 1 has current_ssh => (is => 'ro', writer => '_current_ssh', isa => 'Net::OpenSSH');
37 1 has log => (is => 'ro', writer => '_log', isa => 'Helm::Log');
38 1 has default_port => (is => 'ro', writer => '_port', isa => 'Maybe[Int]');
39 1 has timeout => (is => 'ro', writer => '_timeout', isa => 'Maybe[Int]');
40 1 has sudo => (is => 'rw', isa => 'Maybe[Str]', default => '');
41 0 ▶has extra_options => (is => 'ro', isa => 'Maybe[HashRef]', default => sub { {} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 0 ▶has extra_args => (is => 'ro', isa => 'Maybe[ArrayRef]', default => sub { [] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
43 1 has parallel => (is => 'ro', isa => 'Maybe[Bool]', default => 0);
44 1 has parallel_max => (is => 'ro', isa => 'Maybe[Int]', default => 100);
45 1 has continue_with_errors => (is => 'ro', isa => 'Maybe[Bool]', default => 0);
46 1 has all_configured_servers => (
47 is => 'ro',
48 writer => '_all_configured_servers',
49 isa => 'Maybe[Bool]',
50 default => 0,
51 );
52 1 has local_lock_handle => (
53 is => 'ro',
54 writer => '_local_lock_handle',
55 isa => 'Maybe[FileHandle]',
56 );
57 has servers => (
58 is => 'ro',
59 writer => '_servers',
60 isa => 'Maybe[ArrayRef]',
61 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 1 );
63 has roles => (
64 is => 'ro',
65 writer => '_roles',
66 isa => 'ArrayRef[Str]',
67 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 1 );
69 has exclude_servers => (
70 is => 'ro',
71 writer => '_exclude_servers',
72 isa => 'Maybe[ArrayRef]',
73 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 1 );
75 has exclude_roles => (
76 is => 'ro',
77 writer => '_exclude_roles',
78 isa => 'ArrayRef[Str]',
79 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 1 );
81 1 has log_level => (
82 is => 'ro',
83 writer => '_log_level',
84 isa => 'LOG_LEVEL',
85 default => 'info',
86 );
87 1 has _dont_exit => (
88 is => 'rw',
89 isa => 'Maybe[Bool]',
90 default => 0
91 );
92  
93 1 my %REGISTERED_MODULES = (
94 task => {
95 get => 'Helm::Task::get',
96 patch => 'Helm::Task::patch',
97 put => 'Helm::Task::put',
98 rsync_put => 'Helm::Task::rsync_put',
99 run => 'Helm::Task::run',
100 unlock => 'Helm::Task::unlock',
101 },
102 log => {
103 console => 'Helm::Log::Channel::console',
104 file => 'Helm::Log::Channel::file',
105 mailto => 'Helm::Log::Channel::email',
106 irc => 'Helm::Log::Channel::irc',
107 },
108 configuration => {helm => 'Helm::Conf::Loader::helm'},
109 );
110  
111 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
112 0 my $orig = shift;
113 0 my $class = shift;
114 0 ▶ my %args = (@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/6
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/4
A and B and C
A= @_ == 1B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
115  
116 # allow "log" list of URIs to be passed into new() and then convert them into
117 # a Helm::Log object with various Helm::Log::Channel objects
118 0 ▶ if (my $log_uris = delete $args{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_uris = delete $args{"log"}) 0 0 0
119 my $log =
120 0 ▶ Helm::Log->new($args{log_level} ? (log_level => $args{log_level}) : ());
Branch: 1 branch
BranchTrueFalseTotal
$args{'log_level'} ? : 0 0 0
121 0 foreach my $uri (@$log_uris) {
122 # console is a special case
123 0 ▶ $uri = 'console://blah' if $uri eq 'console';
Branch: 1 branch
BranchTrueFalseTotal
if $uri eq "console" 0 0 0
124 0 ▶ $uri = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 URI->new($uri);
126 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 0 CORE::die("Invalid log URI $uri");
128 0 };
129 0 my $scheme = $uri->scheme;
130 0 ▶ CORE::die("Unknown log type for $uri") unless $scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme 0 0 0
131 0 my $log_class = $REGISTERED_MODULES{log}->{$scheme};
132 0 ▶ CORE::die("Unknown log type for $uri") unless $log_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $log_class 0 0 0
133 0 eval "require $log_class";
134  
135 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
136 0 my $log_class_file = $log_class;
137 0 $log_class_file =~ s/::/\//g;
138 0 ▶ if( $@ =~ /Can't locate \Q$log_class_file\E\.pm/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Can't locate \Q$log_class_file\E\.pm/) { } 0 0 0
139 0 CORE::die("Can not find module $log_class for log type $scheme");
140 } else {
141 0 CORE::die("Could not load module $log_class for log type $scheme: $@");
142 }
143 }
144 0 Helm->debug("Adding new logging channel for URI $uri using class $log_class");
145 0 $log->add_channel($log_class->new(uri => $uri, task => $args{task}));
146 }
147 0 $args{log} = $log;
148 }
149  
150 0 return $class->$orig(%args);
151 1 };
152  
153 0 ▶sub BUILD {
Subroutine BUILD: not called CC=24 SCAR=64.0
Pod undocumented
154 0 my $self = shift;
155  
156 0 $self->log->initialize($self);
157  
158 # create a config object from the config URI string (if it's not already a config object)
159 0 ▶ if ($self->config_uri && !$self->config ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config_uri and not $self->config) 0 0 0
Condition0%0/3
$self->config_uri and not $self->config
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config_uri and not $self->config
$self->config_uri not $self->config
Truth table0%0/3
A and not(B)
A= $self->config_uriB= not $self->config
ABresult
0X0
100
111
160 0 Helm->debug("Loading configuration for URI " . $self->config_uri);
161 0 $self->_config($self->load_configuration($self->config_uri));
162 }
163  
164 # do we have any servers we're excluding?
165 0 my %excludes;
166 0 ▶ if( my @excludes = @{$self->exclude_servers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@excludes) = @{$self->exclude_servers;}) 0 0 0
167 0 foreach my $server_name (Helm::Server->expand_server_names(@excludes)) {
168 0 ▶ if( my $config = $self->config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config = $self->config) 0 0 0
169 0 ▶ if( my $server = $config->get_server_by_abbrev($server_name, $self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $server = $config->get_server_by_abbrev($server_name, $self)) 0 0 0
170 0 $server_name = $server->name;
171 }
172 }
173 0 $excludes{$server_name} = 1;
174 }
175 }
176  
177 # if we have servers let's turn them into Helm::Server objects, let's fully expand their names in case we're using abbreviations
178 0 my @server_names = @{$self->servers};
179 0 ▶ if(@server_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@server_names) 0 0 0
180 0 my @server_objs;
181 0 foreach my $server_name (Helm::Server->expand_server_names(@server_names)) {
182 0 ▶ if( $excludes{$server_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excludes{$server_name}) 0 0 0
183 0 Helm->debug("Excluding server $server_name");
184 0 next;
185 }
186 # if it's already a Helm::Server just keep it
187 0 ▶ if( ref $server_name && blessed($server_name) && $server_name->isa('Helm::Server') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $server_name and builtin::blessed($server_name) and $server_name->isa('Helm::Server')) { } 0 0 0
elsif (my $config = $self->config) { } 0 0 0
Condition0%0/6
ref $server_name and builtin::blessed($server_name) and $server_name->isa("Helm::Server")
!ll&&!rl&&r
000
ref $server_name and builtin::blessed($server_name)
!ll&&!rl&&r
000
MC/DC0%0/3
ref $server_name and builtin::blessed($server_name) and $server_name->isa("Helm::Server")
ref $server_name builtin::blessed($server_name) $server_name->isa("Helm::Server")
Truth table0%0/4
A and B and C
A= ref $server_nameB= builtin::blessed($server_name)C= $server_name->isa("Helm::Server")
ABCresult
0XX0
10X0
1100
1111
188 0 push(@server_objs, $server_name);
189 } elsif( my $config = $self->config ) {
190 # with a config file we can find out more about these servers
191 0 ▶ my $server = $config->get_server_by_abbrev($server_name, $self)
Condition0%0/3
$config->get_server_by_abbrev($server_name, $self) || 'Helm::Server'->new('name', $server_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->get_server_by_abbrev($server_name, $self) || 'Helm::Server'->new('name', $server_name)
$config->get_server_by_abbrev($server_name, $self) 'Helm::Server'->new('name', $server_name)
Truth table0%0/3
A || B
A= $config->get_server_by_abbrev($server_name, $self)B= 'Helm::Server'->new('name', $server_name)
ABresult
1X1
011
000
192 || Helm::Server->new(name => $server_name);
193 0 push(@server_objs, $server);
194 } else {
195 0 push(@server_objs, Helm::Server->new(name => $server_name));
196 }
197 }
198 0 $self->_servers(\@server_objs);
199 }
200  
201 # if we have any roles, then get the servers with (or without) those roles
202 0 my @roles = @{$self->roles};
203 0 my @exclude_roles = @{$self->exclude_roles};
204 0 ▶ if( @roles ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@roles) 0 0 0
205 0 ▶ $self->die("Can't specify roles without a config") if !$self->config;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config 0 0 0
206 0 my @servers = @{$self->servers};
207 0 push(@servers, grep { !$excludes{$_->name} } $self->config->get_servers_by_roles(\@roles, \@exclude_roles));
208 0 ▶ if(!@servers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@servers) 0 0 0
209 0 ▶ if( @exclude_roles ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@exclude_roles) { } 0 0 0
210 0 $self->die("No servers with roles ("
211 . join(', ', @roles)
212 . ") when roles ("
213 . join(', ', @exclude_roles)
214 . ") are excluded");
215 } else {
216 0 $self->die("No servers with roles: " . join(', ', @roles));
217 }
218 }
219 0 $self->_servers(\@servers);
220 }
221
222 # if we still don't have any servers, then use 'em all
223 0 my @servers = @{$self->servers};
224 0 ▶ if(!@servers) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @servers) 0 0 0
225 0 ▶ $self->die("You must specify servers if you don't have a config") if !$self->config;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config 0 0 0
226
227 # exclude any servers we don't want
228 @servers =
229 0 ▶ grep { !@exclude_roles || !$_->has_role(@exclude_roles) }
Condition0%0/3
@exclude_roles && $_->has_role(@exclude_roles)
!ll&&!rl&&r
000
MC/DC0%0/2
@exclude_roles && $_->has_role(@exclude_roles)
@exclude_roles $_->has_role(@exclude_roles)
Truth table0%0/3
A && B
A= @exclude_rolesB= $_->has_role(@exclude_roles)
ABresult
0X0
100
111
230 0 grep { !$excludes{$_->name} } @{$self->config->servers};
231  
232 # are we operating on all the servers?
233 0 ▶ if(!@exclude_roles && !%excludes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@exclude_roles or %excludes) 0 0 0
Condition0%0/3
@exclude_roles or %excludes
l!l&&r!l&&!r
000
MC/DC0%0/2
@exclude_roles or %excludes
@exclude_roles %excludes
Truth table0%0/3
A or B
A= @exclude_rolesB= %excludes
ABresult
1X1
011
000
234 0 $self->_all_configured_servers(1);
235 }
236
237 0 $self->_servers(\@servers);
238 }
239 }
240  
241 0 ▶sub steer {
Subroutine steer: not called CC=28 SCAR=67.0
Pod undocumented
242 0 my $self = shift;
243 0 my $task = $self->task;
244  
245 # make sure it's a task we know about and can load
246 0 my $task_class = $REGISTERED_MODULES{task}->{$task};
247 0 ▶ $self->die("Unknown task $task") unless $task_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $task_class 0 0 0
248 0 eval "require $task_class";
249  
250 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
251 0 my $task_class_file = $task_class;
252 0 $task_class_file =~ s/::/\//g;
253 0 ▶ if( $@ =~ /Can't locate \Q$task_class_file\E\.pm/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Can't locate \Q$task_class_file\E\.pm/) { } 0 0 0
254 0 $self->die("Can not find module $task_class for task $task");
255 } else {
256 0 $self->die("Could not load module $task_class for task $task");
257 }
258 }
259  
260 0 my $task_obj = $task_class->new(helm => $self);
261 0 $task_obj->validate();
262  
263 # make sure have a local lock if we need it
264 0 ▶ if ($self->lock_type eq 'local' || $self->lock_type eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lock_type eq "local" or $self->lock_type eq "both") 0 0 0
Condition0%0/3
$self->lock_type eq "local" or $self->lock_type eq "both"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lock_type eq "local" or $self->lock_type eq "both"
$self->lock_type eq "local" $self->lock_type eq "both"
Truth table0%0/3
A or B
A= $self->lock_type eq "local"B= $self->lock_type eq "both"
ABresult
1X1
011
000
265 0 Helm->debug("Trying to optain local helm lock");
266 0 ▶ $self->die("Cannot obtain a local helm lock. Is another helm process running?",
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_local_lock 0 0 0
267 no_release_locks => 1)
268 unless $self->_get_local_lock;
269 }
270  
271 0 my @servers = @{$self->servers};
272 0 $self->log->info("Helm execution started by " . getlogin);
273 0 ▶ if( @servers > 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@servers > 20) { } 0 0 0
274 0 $self->log->info(qq("Running task "$task" on ) . scalar(@servers) . " servers");
275 } else {
276 0 $self->log->info(qq(Running task "$task" on servers: ) . join(', ', @servers));
277 }
278  
279 0 $self->log->debug("Running task setup");
280 0 $task_obj->setup();
281  
282 0 my $forker;
283 0 ▶ if( $self->parallel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parallel) 0 0 0
284 0 Helm->debug("Setting up fork manager");
285 0 $forker = Parallel::ForkManager->new($self->parallel_max);
286 0 Helm->debug("Letting loggers know we're going to parallelize things");
287 0 $self->log->parallelize($self);
288 }
289  
290 # execute the task for each server
291 0 ▶ $self->_dont_exit(1) if $self->continue_with_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->continue_with_errors 0 0 0
292 0 foreach my $server (@servers) {
293 0 $self->log->start_server($server);
294 0 $self->_current_server($server);
295  
296 0 ▶ my $port = $server->port || $self->default_port;
Condition0%0/3
$server->port || $self->default_port
l!l&&r!l&&!r
000
MC/DC0%0/2
$server->port || $self->default_port
$server->port $self->default_port
Truth table0%0/3
A || B
A= $server->portB= $self->default_port
ABresult
1X1
011
000
297 0 my %ssh_args = (
298 ctl_dir => catdir(File::HomeDir->my_home, '.helm'),
299 strict_mode => 0,
300 );
301 0 ▶ $ssh_args{port} = $port if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 0 0 0
302 0 ▶ $ssh_args{timeout} = $self->timeout if $self->timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $self->timeout 0 0 0
303 0 ▶ $self->log->debug("Setting up SSH connection to $server" . ($port ? ":$port" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$port ? : 0 0 0
304  
305 # in parallel mode, send all stdout/stderr from each connection to a file
306 0 ▶ if( $self->parallel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parallel) 0 0 0
307 0 my $log_file = catfile(tmpdir(), "helm-$server.log");
308 0 ▶ open(my $log_fh, '>', $log_file) or die "Could not open file $log_file for logging: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $log_fh, ">", $log_file or CORE::die "Could not open file $log_file for logging: $!" 0 0 0
309 0 ▶ open(my $devnull, '<', devnull) or die "Could not open /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $devnull, "<", "/dev/null" or CORE::die "Could not open /dev/null: $!" 0 0 0
310 0 $ssh_args{default_stdout_fh} = $log_fh;
311 0 $ssh_args{default_stderr_fh} = $log_fh;
312 0 $ssh_args{default_stdin_fh} = $devnull;
313 0 $self->log->info("Logging output for $server to $log_file");
314  
315 0 my $pid = $forker->start;
316 0 Helm->debug("Letting the loggers know we've actually forked off a child task worker");
317 0 ▶ if( $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
318 # let the loggers know we're now forked;
319 0 $self->log->forked('parent');
320 0 next;
321 } else {
322 0 $self->log->forked('child');
323 }
324 }
325  
326 0 my $connection_name = $server->name;
327 0 my $user = $self->user;
328 0 ▶ $connection_name = $user . '@' . $connection_name if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
329  
330 0 my $ssh = Net::OpenSSH->new($connection_name, %ssh_args);
331 0 ▶ $ssh->error
Branch: 2 branches
BranchTrueFalseTotal
$ssh->error and $self->die("Can't ssh to $server" . ($user ? " as user $user" : "") . ': ' . $ssh->error) 0 0 0
$user ? : 0 0 0
332 && $self->die("Can't ssh to $server" . ($user ? " as user $user" : '') . ": " . $ssh->error);
333 0 $self->_current_ssh($ssh);
334  
335 # get a lock on the server if we need to
336 0 ▶ if ($self->lock_type eq 'remote' || $self->lock_type eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lock_type eq "remote" or $self->lock_type eq "both") 0 0 0
Condition0%0/3
$self->lock_type eq "remote" or $self->lock_type eq "both"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lock_type eq "remote" or $self->lock_type eq "both"
$self->lock_type eq "remote" $self->lock_type eq "both"
Truth table0%0/3
A or B
A= $self->lock_type eq "remote"B= $self->lock_type eq "both"
ABresult
1X1
011
000
337 0 Helm->debug("Trying to obtain remote lock on $server");
338 0 ▶ $self->die("Cannot obtain remote lock on $server. Is another helm process working there?",
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_remote_lock($ssh) 0 0 0
339 no_release_locks => 1) unless $self->_get_remote_lock($ssh);
340 }
341  
342 0 Helm->debug(qq(Excuting task "$task" on server "$server"));
343 0 $task_obj->execute(
344 ssh => $ssh,
345 server => $server,
346 );
347  
348 0 $self->log->end_server($server);
349 0 $self->_release_remote_lock($ssh);
350 0 ▶ if( my $secs = $self->sleep ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $secs = $self->sleep) 0 0 0
351 0 Helm->debug("Sleeping for $secs seconds between servers");
352 0 sleep($secs);
353 }
354 0 ▶ if($self->parallel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parallel) 0 0 0
355 0 Helm->debug("Finished work in child task process");
356 0 $forker->finish;
357 }
358 }
359  
360 0 ▶ if( $self->parallel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parallel) 0 0 0
361 0 Helm->debug("Waiting on all child task processes to finish");
362 0 $forker->wait_all_children;
363 }
364  
365 0 $self->log->debug("Running task teardown");
366 0 $task_obj->teardown();
367  
368 # release the local lock
369 0 $self->_release_local_lock();
370 0 Helm->debug("Finalizing loggers");
371 0 $self->log->finalize($self);
372 0 Helm->debug("Finished with all tasks on all servers");
373 }
374  
375 0 ▶sub load_configuration {
Subroutine load_configuration: not called CC=5 SCAR=34.0
Pod undocumented
376 0 my ($self, $uri) = @_;
377 0 ▶ $uri = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
378 0 URI->new($uri)
379 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 0 $self->die("Invalid configuration URI $uri");
381 0 };
382  
383 # try to load the right config module
384 0 my $scheme = $uri->scheme;
385 0 ▶ $self->die("Unknown config type for $uri") unless $scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme 0 0 0
386 0 my $loader_class = $REGISTERED_MODULES{configuration}->{$scheme};
387 0 ▶ $self->die("Unknown config type for $uri") unless $loader_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $loader_class 0 0 0
388 0 eval "require $loader_class";
389  
390 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
391 0 my $loader_class_file = $loader_class;
392 0 $loader_class_file =~ s/::/\//g;
393 0 ▶ if( $@ =~ /Can't locate \Q$loader_class_file\E\.pm/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Can't locate \Q$loader_class_file\E\.pm/) { } 0 0 0
394 0 $self->die("Can not find module $loader_class for configuration type $scheme");
395 } else {
396 0 $self->die("Could not load module $loader_class for configuration type $scheme: $@");
397 }
398 }
399  
400 0 $self->log->debug("Loading configuration for $uri from $loader_class");
401 0 return $loader_class->load(uri => $uri, helm => $self);
402 }
403  
404 0 ▶sub task_help {
Subroutine task_help: not called CC=3 SCAR=24.8
Pod undocumented
405 0 my ($class, $task) = @_;
406 # make sure it's a task we know about and can load
407 0 my $task_class = $REGISTERED_MODULES{task}->{$task};
408 0 ▶ CORE::die(qq(Unknown task "$task")) unless $task_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $task_class 0 0 0
409 0 eval "require $task_class";
410 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
411  
412 0 return $task_class->help($task);
413 }
414  
415 0 ▶sub known_tasks {
Subroutine known_tasks: not called CC=1 SCAR=6.9
Pod undocumented
416 0 my $class = shift;
417 0 return sort keys %{$REGISTERED_MODULES{task}};
418 }
419  
420 0 ▶sub _get_local_lock {
Subroutine _get_local_lock: not called CC=3 SCAR=24.8
421 0 my $self = shift;
422 0 $self->log->debug("Trying to acquire global local helm lock");
423 # lock the file so nothing else can run at the same time
424 0 my $lock_handle;
425 0 my $lock_file = $self->_local_lock_file();
426 0 ▶ open($lock_handle, '>', $lock_file) or $self->die("Can't open $lock_file for locking: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $lock_handle, ">", $lock_file or $self->die("Can't open $lock_file for locking: $!") 0 0 0
427 0 ▶ if (flock($lock_handle, LOCK_EX | LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $lock_handle, 6) { } 0 0 0
428 0 $self->_local_lock_handle($lock_handle);
429 0 $self->log->debug("Local helm lock obtained");
430 0 return 1;
431 } else {
432 0 return 0;
433 }
434 }
435  
436 0 ▶sub _release_local_lock {
Subroutine _release_local_lock: not called CC=2 SCAR=17.9
437 0 my $self = shift;
438 0 ▶ if($self->local_lock_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->local_lock_handle) 0 0 0
439 0 $self->log->debug("Releasing global local helm lock");
440 0 close($self->local_lock_handle)
441 }
442 }
443  
444 0 ▶sub _local_lock_file {
Subroutine _local_lock_file: not called CC=1 SCAR=6.9
445 0 my $self = shift;
446 0 return catfile(tmpdir(), 'helm.lock');
447 }
448  
449 0 ▶sub _get_remote_lock {
Subroutine _get_remote_lock: not called CC=2 SCAR=17.9
450 0 my ($self, $ssh) = @_;
451 0 my $server = $self->current_server;
452 0 $self->log->debug("Trying to obtain remote server lock for $server");
453  
454 # make sure the lock file on the server doesn't exist
455 0 my $lock_file = $self->_remote_lock_file();
456 0 my $output = $self->run_remote_command(
457 ssh => $ssh,
458 command => qq(if [ -e "/tmp/helm.remote.lock" ]; then echo "lock found"; else echo "no lock found"; fi),
459 ssh_method => 'capture',
460 );
461 0 chomp($output);
462 0 ▶ if( $output eq 'lock found') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output eq 'lock found') { } 0 0 0
463 0 return 0;
464 } else {
465 # XXX - there's a race condition here, not sure what the right fix is though
466 0 $self->run_remote_command(ssh => $ssh, command => "touch $lock_file");
467 0 $self->log->debug("Remote server lock for $server obtained");
468 0 return 1;
469 }
470 }
471  
472 0 ▶sub _release_remote_lock {
Subroutine _release_remote_lock: not called CC=3 SCAR=24.8
473 0 my ($self, $ssh) = @_;
474 0 ▶ if( $self->lock_type eq 'remote' || $self->lock_type eq 'both' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lock_type eq "remote" or $self->lock_type eq "both") 0 0 0
Condition0%0/3
$self->lock_type eq "remote" or $self->lock_type eq "both"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lock_type eq "remote" or $self->lock_type eq "both"
$self->lock_type eq "remote" $self->lock_type eq "both"
Truth table0%0/3
A or B
A= $self->lock_type eq "remote"B= $self->lock_type eq "both"
ABresult
1X1
011
000
475 0 $self->log->debug("Releasing remote server lock for " . $self->current_server);
476 0 my $lock_file = $self->_remote_lock_file();
477 0 $self->run_remote_command(ssh => $ssh, command => "rm -f $lock_file");
478 }
479 }
480  
481 0 ▶sub _remote_lock_file {
Subroutine _remote_lock_file: not called CC=1 SCAR=6.9
482 0 my $self = shift;
483 0 return catfile(tmpdir(), 'helm.remote.lock');
484 }
485  
486 0 ▶sub run_remote_command {
Subroutine run_remote_command: not called CC=7 SCAR=40.3
Pod undocumented
487 0 my ($self, %args) = @_;
488 0 my $ssh = $args{ssh};
489 0 ▶ my $ssh_options = $args{ssh_options} || {};
Condition0%0/2
$args{'ssh_options'} || {}
l!l
00
MC/DC0%0/1
$args{'ssh_options'} || {}
$args{'ssh_options'}
Truth table0%0/2
A
A= $args{'ssh_options'}
Aresult
11
00
490 0 my $cmd = $args{command};
491 0 ▶ my $ssh_method = $args{ssh_method} || 'system';
Condition0%0/2
$args{'ssh_method'} || 'system'
l!l
00
MC/DC0%0/1
$args{'ssh_method'} || 'system'
$args{'ssh_method'}
Truth table0%0/2
A
A= $args{'ssh_method'}
Aresult
11
00
492 0 ▶ my $server = $args{server} || $self->current_server;
Condition0%0/3
$args{'server'} || $self->current_server
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'server'} || $self->current_server
$args{'server'} $self->current_server
Truth table0%0/3
A || B
A= $args{'server'}B= $self->current_server
ABresult
1X1
011
000
493 0 my $sudo = $self->sudo;
494  
495 0 ▶ if( $sudo && !$args{no_sudo}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sudo and not $args{"no_sudo"}) 0 0 0
Condition0%0/3
$sudo and not $args{"no_sudo"}
!ll&&!rl&&r
000
MC/DC0%0/2
$sudo and not $args{"no_sudo"}
$sudo not $args{"no_sudo"}
Truth table0%0/3
A and not(B)
A= $sudoB= not $args{"no_sudo"}
ABresult
0X0
100
111
496 0 $cmd = "sudo -u $sudo $cmd";
497 0 $ssh_options->{tty} = 1;
498 }
499  
500 0 $self->log->debug("Running remote command ($cmd) on server $server");
501 0 ▶ $ssh->$ssh_method($ssh_options, $cmd)
Branch: 1 branch
BranchTrueFalseTotal
$ssh->$ssh_method($ssh_options, $cmd) or $self->die("Can't execute command ($cmd) on server ${server}: " . $ssh->error) 0 0 0
502 or $self->die("Can't execute command ($cmd) on server $server: " . $ssh->error);
503 }
504  
505 0 ▶sub run_local_command {
Subroutine run_local_command: not called CC=4 SCAR=30.0
Pod undocumented
506 0 my $self = shift;
507 0 ▶ my %args = @_ > 1 ? @_ : (command => $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
508 0 ▶ my @cmd = ref $args{command} ? @{$args{command}} : ($args{command});
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'command'} ? : 0 0 0
509  
510 0 my $return = system(@cmd);
511 0 ▶ if( system(@cmd) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (system(@cmd) != 0) 0 0 0
512 0 $self->die("Can't execute local command (" . join(' ', @cmd) . ": " . $!);
513 }
514 }
515  
516 0 ▶sub ssh_connection {
Subroutine ssh_connection: not called CC=5 SCAR=34.0
Pod undocumented
517 0 my ($self, %args) = @_;
518 0 my $server = $args{server};
519 0 my %ssh_args = (
520 ctl_dir => catdir(File::HomeDir->my_home, '.helm'),
521 strict_mode => 0,
522 );
523 0 ▶ my $port = $server->port || $self->default_port;
Condition0%0/3
$server->port || $self->default_port
l!l&&r!l&&!r
000
MC/DC0%0/2
$server->port || $self->default_port
$server->port $self->default_port
Truth table0%0/3
A || B
A= $server->portB= $self->default_port
ABresult
1X1
011
000
524 0 ▶ $ssh_args{port} = $port if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 0 0 0
525 0 ▶ $ssh_args{timeout} = $self->timeout if $self->timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $self->timeout 0 0 0
526 0 ▶ $self->log->debug("Setting up SSH connection to $server" . ($port ? ":$port" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$port ? : 0 0 0
527 0 return Net::OpenSSH->new($server->name, %ssh_args);
528 }
529  
530 0 ▶sub die {
Subroutine die: not called CC=3 SCAR=24.8
Pod undocumented
531 0 my ($self, $msg, %options) = @_;
532 0 $self->log->error($msg);
533 0 ▶ unless($options{no_release_locks}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"no_release_locks"}) 0 0 0
534 0 $self->_release_remote_lock($self->current_ssh);
535 0 $self->_release_local_lock();
536 }
537  
538 0 ▶ exit(1) unless $self->_dont_exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_dont_exit 0 0 0
539 }
540  
541 0 ▶sub register_module {
Subroutine register_module: not called CC=2 SCAR=17.9
Pod undocumented
542 0 my ($class, $type, $key, $module) = @_;
543 0 ▶ CORE::die("Unknown Helm module type '$type'!") unless exists $REGISTERED_MODULES{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $REGISTERED_MODULES{$type} 0 0 0
544 0 Helm->debug("Loading module $module for $type plugins with key $key");
545 0 $REGISTERED_MODULES{$type}->{$key} = $module;
546 }
547  
548 # this is a class method so that it can be called even before any objects
549 # have been fully initialized.
550 0 ▶sub debug {
Subroutine debug: not called CC=6 SCAR=37.4
Pod undocumented
551 0 ▶ return unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $Helm::DEBUG 0 0 0
552 0 my ($self, @msgs) = @_;
553  
554 # open the debug log handle if we haven't opened it yet
555 # or re-open if we're already opened it in another process
556 0 ▶ if(!$DEBUG_LOG || $DEBUG_LOG_PID != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Helm::DEBUG_LOG or $Helm::DEBUG_LOG_PID != $$) 0 0 0
Condition0%0/3
not $Helm::DEBUG_LOG or $Helm::DEBUG_LOG_PID != $$
l!l&&r!l&&!r
000
MC/DC0%0/2
not $Helm::DEBUG_LOG or $Helm::DEBUG_LOG_PID != $$
not $Helm::DEBUG_LOG $Helm::DEBUG_LOG_PID != $$
Truth table0%0/3
not(A) or B
A= not $Helm::DEBUG_LOGB= $Helm::DEBUG_LOG_PID != $$
ABresult
1X1
011
000
557 0 ▶ $DEBUG_LOG = IO::File->new('>> debug.log')
Branch: 1 branch
BranchTrueFalseTotal
$Helm::DEBUG_LOG = "IO::File"->new(">> debug.log") or $self->die("Could not open helm.debug for appending: $!") 0 0 0
558 or $self->die("Could not open helm.debug for appending: $!");
559 0 $DEBUG_LOG->autoflush(1);
560 0 $DEBUG_LOG_PID = $$;
561 }
562  
563 0 my $ts = DateTime->now->strftime('%a %b %d %H:%M:%S %Y');
564 0 my ($calling_class) = caller();
565 0 foreach my $msg (@msgs) {
566 0 $msg =~ s/\s+$//;
567 0 $DEBUG_LOG->print("[$ts] [$$] [$calling_class] $msg\n");
568 }
569 }
570  
571 1 __PACKAGE__->meta->make_immutable;
572  
573 1 1;
↑ Summary Helm/Log/Channel/irc.pm »