RemoteCommand/SSH.pm

filter: Statementstmt 28.2% 33 / 117 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 55.0% 11 / 20 Podpod 0.0% 0 / 8 totaltotal 17.2% 44 / 255 CC 41 SCAR 59.9
CC
41
Cov
16%
one_tick
812.0
cancel
110.0
error
12.0
CRAP 398.6
↑ Summary RemoteCommand.pm »
1 package App::RemoteCommand::SSH;
2 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use experimental qw(lexical_subs signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use App::RemoteCommand::Util qw(DEBUG logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::OpenSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶sub _ssh ($self, %args) {
Subroutine _ssh: not called CC=2 SCAR=17.9
11 0 my $loglevel = DEBUG ? "error" : "quiet";
12 Net::OpenSSH->new($args{host},
13 async => 1,
14 strict_mode => 0,
15 timeout => 6,
16 kill_ssh_on_timeout => 1,
17 master_setpgrp => 1,
18 master_opts => [
19 -o => "ConnectTimeout=5",
20 -o => "StrictHostKeyChecking=no",
21 -o => "UserKnownHostsFile=/dev/null",
22 -o => "LogLevel=$loglevel",
23 0 ▶ ($args{configfile} ? (-F => $args{configfile}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$args{'configfile'} ? : 0 0 0
24 ],
25 );
26 }
27  
28 1 ▶use constant STATE_TODO => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant STATE_CONNECTING => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant STATE_CONNECTED => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant STATE_DISCONNECTING => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant STATE_DONE => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 0 ▶sub new ($class, %args) {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
35 0 bless {
36 %args,
37 ssh => undef,
38 cmd => [],
39 at_exit => undef,
40 state => STATE_TODO,
41 exit => -1,
42 current => undef,
43 sudo => undef,
44 }, $class;
45 }
46  
47 0 ▶sub add ($self, $cmd) {
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
48 0 push $self->{cmd}->@*, $cmd;
49 }
50  
51 0 ▶sub at_exit ($self, @args) {
Subroutine at_exit: not called CC=2 SCAR=17.9
Pod undocumented
52 0 ▶ @args ? $self->{at_exit} = shift @args : $self->{at_exit};
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
53 }
54  
55 0 ▶sub cancel ($self, $signal) {
Subroutine cancel: not called CC=10 SCAR=47.0
Pod undocumented
56  
57 0 DEBUG and logger " CANCEL %s, state %s", $self->host, $self->{state};
58  
59 0 ▶ if ($self->{state} == STATE_TODO) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->{'state'} == 1) { } 0 0 0
elsif ($self->{'state'} == 2) { } 0 0 0
elsif ($self->{'state'} == 3) { } 0 0 0
elsif ($self->{'state'} == 4) { } 0 0 0
elsif ($self->{'state'} == 5) { } 0 0 0
60 0 $self->{state} == STATE_DONE;
61 } elsif ($self->{state} == STATE_CONNECTING) {
62 0 $self->{cmd}->@* = ();
63 0 undef $self->{at_exit};
64 0 my $master_pid = $self->{ssh}->get_master_pid;
65 0 DEBUG and logger " SEND SIG$signal %s, process group for master pid %d", $self->host, $master_pid;
66 0 kill $signal => -$master_pid;
67 } elsif ($self->{state} == STATE_CONNECTED) {
68 0 $self->{cmd}->@* = ();
69 0 ▶ if ($signal
Branch: 1 branch
BranchTrueFalseTotal
if ($signal and $self->{"current"} and $self->{"current"}{"type"} eq "cmd" and my $pid = $self->{"current"}{"pid"}) 0 0 0
Condition0%0/9
$signal and $self->{"current"} and $self->{"current"}{"type"} eq "cmd" and my $pid = $self->{"current"}{"pid"}
!ll&&!rl&&r
000
$signal and $self->{"current"} and $self->{"current"}{"type"} eq "cmd"
!ll&&!rl&&r
000
$signal and $self->{"current"}
!ll&&!rl&&r
000
MC/DC0%0/4
$signal and $self->{"current"} and $self->{"current"}{"type"} eq "cmd" and my $pid = $self->{"current"}{"pid"}
$signal $self->{"current"} $self->{"current"}{"type"} eq "cmd" my $pid = $self->{"current"}{"pid"}
Truth table0%0/5
A and B and C and D
A= $signalB= $self->{"current"}C= $self->{"current"}{"type"} eq "cmd"D= my $pid = $self->{"current"}{"pid"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
70 and $self->{current}
71 and $self->{current}{type} eq "cmd"
72 and my $pid = $self->{current}{pid}
73 ) {
74 0 DEBUG and logger " SEND SIG$signal %s, pid %s", $self->host, $pid;
75 0 kill $signal => $pid;
76 }
77 } elsif ($self->{state} == STATE_DISCONNECTING) {
78 # nop
79 } elsif ($self->{state} == STATE_DONE) {
80 # nop
81 } else {
82 0 die;
83 }
84 }
85  
86 0 ▶sub error ($self) {
Subroutine error: not called CC=3 SCAR=24.8
Pod undocumented
87 0 ▶ ($self->{ssh} && $self->{ssh}->error) || $self->{_error};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ssh"} and $self->{"ssh"}->error or $self->{"_error"} 0 0 0
Condition0%0/3
$self->{"ssh"} and $self->{"ssh"}->error
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"ssh"} and $self->{"ssh"}->error
$self->{"ssh"} $self->{"ssh"}->error
Truth table0%0/3
A and B
A= $self->{"ssh"}B= $self->{"ssh"}->error
ABresult
0X0
100
111
88 }
89  
90 0 ▶sub host ($self) {
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
91 0 $self->{host};
92 }
93  
94 0 ▶sub exit ($self) {
Subroutine exit: not called CC=1 SCAR=6.9
Pod undocumented
95 0 $self->{exit};
96 }
97  
98 0 ▶sub one_tick ($self, %args) {
Subroutine one_tick: not called CC=28 SCAR=67.0
Pod undocumented
99  
100 0 my $exit_pid = $args{pid};
101 0 my $exit_code = $args{exit};
102 0 my $select = $args{select};
103  
104 0 my $ssh = $self->{ssh};
105  
106 0 ▶ if ($ssh and $exit_pid and $ssh->get_master_pid and $exit_pid == $ssh->get_master_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssh and $exit_pid and $ssh->get_master_pid and $exit_pid == $ssh->get_master_pid) 0 0 0
Condition0%0/9
$ssh and $exit_pid and $ssh->get_master_pid and $exit_pid == $ssh->get_master_pid
!ll&&!rl&&r
000
$ssh and $exit_pid and $ssh->get_master_pid
!ll&&!rl&&r
000
$ssh and $exit_pid
!ll&&!rl&&r
000
MC/DC0%0/4
$ssh and $exit_pid and $ssh->get_master_pid and $exit_pid == $ssh->get_master_pid
$ssh $exit_pid $ssh->get_master_pid $exit_pid == $ssh->get_master_pid
Truth table0%0/5
A and B and C and D
A= $sshB= $exit_pidC= $ssh->get_master_pidD= $exit_pid == $ssh->get_master_pid
ABCDresult
0XXX0
10XX0
110X0
11100
11111
107 0 DEBUG and logger " FAIL %s, master process exited unexpectedly", $self->host;
108 0 $ssh->master_exited;
109 0 ▶ $self->{exit} = $exit_code || -1;
Condition0%0/2
$exit_code || -1
l!l
00
MC/DC0%0/1
$exit_code || -1
$exit_code
Truth table0%0/2
A
A= $exit_code
Aresult
11
00
110 0 ▶ $self->{_error} = $self->{ssh}->error || "master process exited unexpectedly";
Condition0%0/2
$self->{'ssh'}->error || 'master process exited unexpectedly'
l!l
00
MC/DC0%0/1
$self->{'ssh'}->error || 'master process exited unexpectedly'
$self->{'ssh'}->error
Truth table0%0/2
A
A= $self->{'ssh'}->error
Aresult
11
00
111 0 $self->{state} = STATE_DONE;
112 0 undef $self->{ssh};
113 }
114  
115 0 ▶ if ($self->{state} == STATE_TODO) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == 1) 0 0 0
116 0 DEBUG and logger " CONNECT %s", $self->host;
117 0 $ssh = $self->{ssh} = $self->_ssh(host => $self->{host}, configfile => $self->{configfile});
118 0 $self->{state} = STATE_CONNECTING;
119 }
120  
121 0 ▶ if ($self->{state} == STATE_CONNECTING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == 2) 0 0 0
122 0 my $master_state = $ssh->wait_for_master(1);
123 0 ▶ if ($master_state) {
Branch: 2 branches
BranchTrueFalseTotal
if ($master_state) { } 0 0 0
elsif (not defined $master_state) { } 0 0 0
124 0 DEBUG and logger " CONNECTED %s", $self->host;
125 0 $self->{state} = STATE_CONNECTED;
126 } elsif (!defined $master_state) {
127 # still connecting...
128 0 return 1;
129 } else {
130 0 DEBUG and logger " FAIL TO CONNECT %s", $self->host;
131 0 $self->{exit} = -1;
132 0 ▶ $self->{_error} = $self->{ssh}->error || "master process exited unexpectedly";
Condition0%0/2
$self->{'ssh'}->error || 'master process exited unexpectedly'
l!l
00
MC/DC0%0/1
$self->{'ssh'}->error || 'master process exited unexpectedly'
$self->{'ssh'}->error
Truth table0%0/2
A
A= $self->{'ssh'}->error
Aresult
11
00
133 0 $self->{state} = STATE_DONE;
134 0 undef $self->{ssh};
135 }
136 }
137  
138 0 ▶ if ($self->{state} == STATE_CONNECTED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == 3) 0 0 0
139 0 ▶ if (!$self->{current} or $exit_pid && $self->{current} && $self->{current}{pid} == $exit_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'current'} or $exit_pid and $self->{'current'} and $self->{'current'}{'pid'} == $exit_pid) { } 0 0 0
Condition0%0/9
not $self->{"current"} or $exit_pid and $self->{"current"} and $self->{"current"}{"pid"} == $exit_pid
l!l&&r!l&&!r
000
$exit_pid and $self->{"current"} and $self->{"current"}{"pid"} == $exit_pid
!ll&&!rl&&r
000
$exit_pid and $self->{"current"}
!ll&&!rl&&r
000
MC/DC0%0/4
not $self->{"current"} or $exit_pid and $self->{"current"} and $self->{"current"}{"pid"} == $exit_pid
not $self->{"current"} $exit_pid $self->{"current"} $self->{"current"}{"pid"} == $exit_pid
Truth table0%0/5
not(A) or B and C and D
A= not $self->{"current"}B= $exit_pidC= $self->{"current"}D= $self->{"current"}{"pid"} == $exit_pid
ABCDresult
1XXX1
01111
00XX0
010X0
01100
140  
141 0 ▶ if ($self->{current}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current"}) 0 0 0
142 DEBUG and logger " FINISH %s, pid %d, type %s, exit %d",
143 0 $self->host, $exit_pid, $self->{current}{type}, $exit_code;
144 0 ▶ if ($self->{current}{type} eq "cmd") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current"}{"type"} eq "cmd") 0 0 0
145 0 $self->{exit} = $exit_code;
146 }
147 }
148  
149 0 my ($cmd, $type);
150 0 ▶ if ($self->{cmd}->@*) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{'cmd'}}) { } 0 0 0
elsif ($self->{'at_exit'}) { } 0 0 0
151 0 $cmd = shift $self->{cmd}->@*;
152 0 $type = "cmd";
153 } elsif ($self->{at_exit}) {
154 0 $cmd = delete $self->{at_exit};
155 0 $type = "at_exit";
156 }
157  
158 0 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) 0 0 0
159 0 my $ssh = $self->{ssh};
160 0 my ($pid, $fh) = $cmd->($ssh);
161 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
162 0 DEBUG and logger " START %s, pid %d, type %s", $self->host, $pid, $type;
163 0 $self->{current} = {pid => $pid, type => $type};
164 0 ▶ $select->add(pid => $pid, fh => $fh, host => $self->host) if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 0 0
165 0 return 1;
166 }
167 # save error
168 0 $self->{_error} = $self->{ssh}->error;
169 }
170  
171 0 undef $self->{current};
172 0 DEBUG and logger " DISCONNECTING %s", $self->host;
173 0 $ssh->disconnect(0); # XXX block disconnect
174 0 $self->{state} = STATE_DISCONNECTING;
175 } else {
176 0 return 1;
177 }
178 }
179  
180 0 ▶ if ($self->{state} == STATE_DISCONNECTING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == 4) 0 0 0
181 0 my $master_state = $ssh->wait_for_master(1);
182 0 ▶ if (defined $master_state && !$master_state) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $master_state and not $master_state) { } 0 0 0
Condition0%0/3
defined $master_state and not $master_state
!ll&&!rl&&r
000
MC/DC0%0/2
defined $master_state and not $master_state
defined $master_state not $master_state
Truth table0%0/3
A and not(B)
A= defined $master_stateB= not $master_state
ABresult
0X0
100
111
183 0 DEBUG and logger " DISCONNECTED %s", $self->host;
184 0 $self->{state} = STATE_DONE;
185 0 undef $self->{ssh};
186 } else {
187 0 return 1;
188 }
189 }
190  
191 0 ▶ if ($self->{state} == STATE_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == 5) 0 0 0
192 0 DEBUG and logger " DONE %s", $self->host;
193 0 return;
194 }
195  
196 0 die;
197 }
198  
199 1 1;
↑ Summary RemoteCommand.pm »