MCP/Net/Reset/OSRC.pm

filter: Statementstmt 36.0% 22 / 61 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 37.0% 30 / 81 CC 8 SCAR 29.8
CC
8
Cov
31%
reset_host
42.0
ssh_reboot
12.0
BEGIN
1.0
CRAP 19.6
↑ Summary MCP/Child.pm »
1 package Tapper::MCP::Net::Reset::OSRC;
2 2 our $AUTHORITY = 'cpan:TAPPER';
3 2 $Tapper::MCP::Net::Reset::OSRC::VERSION = '5.0.9';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use File::Temp qw"tempfile tempdir";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Net::OpenSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 extends 'Tapper::Base';
13  
14  
15  
16 sub ssh_reboot
17 0 ▶{
Subroutine ssh_reboot: not called CC=3 SCAR=24.8
Pod documented
18 0 my ($self, $host, $options) = @_;
19 0 $self->log->info("Try reboot '$host' via ssh");
20 my $ssh = Net::OpenSSH->new(
21 host=>$host,
22 user=>'root',
23 password=>$options->{testmachine_password},
24 0 timeout=> '10',
25 kill_ssh_on_timeout => 1,
26 master_opts => [ -o => 'StrictHostKeyChecking=no',
27 -o => 'UserKnownHostsFile=/dev/null' ]);
28 0 ▶ if ($ssh->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssh->error) 0 0 0
29 0 $self->log->debug("Could not establish SSH connection to '$host': ". $ssh->error);
30 0 return;
31 }
32  
33 0 my $output;
34 0 $output = $ssh->capture("reboot");
35  
36 0 ▶ if ($ssh->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssh->error) { } 0 0 0
37 0 $self->log->debug("Can not reboot '$host' with SSH: $output");
38 0 return;
39 } else {
40 0 return 1;
41 }
42 }
43  
44  
45 sub reset_host
46 0 ▶{
Subroutine reset_host: not called CC=6 SCAR=37.4
Pod documented
47 0 my ($self, $host, $options) = @_;
48  
49 0 my ($error, $retval);
50 0 my $cmd = "/public/bin/osrc_rst_no_menu -f $host";
51  
52 # several possible TFTP daemons and logs, choose the one with latest write access
53 0 my $log = `ls -1rt /opt/opentftp/log/opentftp*.log* /var/log/atftpd.log* | tail -1`; chomp $log;
54 0 ▶ if (-z $log) {
Branch: 1 branch
BranchTrueFalseTotal
if (-z $log) 0 0 0
55 0 $self->log->warn("TFTP log '$log' is zero size!");
56 }
57  
58 0 my $tmpdir = tempdir( CLEANUP => 1 );
59 0 my $tmplog = File::Temp->new(TEMPLATE => "osrcreset-tftplog-before-XXXXXX", DIR => $tmpdir, UNLINK => 1);
60 0 my $logbefore = $tmplog->filename;
61  
62 # store tftp log before reboot
63 0 $self->log_and_exec("cp $log $logbefore");
64 0 ▶ $self->ssh_reboot( $host, $options ) or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->ssh_reboot($host, $options) or do { $self->log->info("Try reboot '${host}' via reset switch"); () = $self->log_and_exec($cmd) } 0 0 0
65 0 $self->log->info("Try reboot '$host' via reset switch");
66 0 ($error, $retval) = $self->log_and_exec($cmd);
67 };
68  
69 TRY:
70 0 for my $try (1..3)
71 {
72 # watch tftp log for $host entries which signal successful reset
73 0 for my $check (1..36) # 36 * 10sec sleep == 360sec (6min) per try
74 {
75 # check every 10 seconds to early catch success
76 0 sleep 10;
77 0 $self->log->debug("(try $try: $host, check $check)");
78 0 ▶ if (system("diff -u $logbefore $log | grep -q '+.*$host'") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system("diff -u $logbefore $log | grep -q '+.*${host}'") == 0) 0 0 0
79 0 $self->log->info("(try $try: $host) reset succeeded");
80 0 last TRY;
81 }
82 }
83 0 $self->log->info("Try reboot '$host' via reset switch");
84 # store tftp log before reset
85 0 $self->log_and_exec("cp $log $logbefore");
86  
87 0 ($error, $retval) = $self->log_and_exec($cmd);
88 }
89 0 undef $tmplog;
90 0 rmdir $tmpdir;
91 0 return ($error, $retval);
92 }
93  
94 2 1;
95  
96 __END__
↑ Summary MCP/Child.pm »