lib/Rex/Virtualization/LibVirt/guestinfo.pm

filter: Statementstmt 43.6% 38 / 87 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 1 totaltotal 37.5% 50 / 133 CC 15 SCAR 49.0
CC
15
Cov
33%
execute
240.0
BEGIN
1.0
BEGIN
1.0
CRAP 133.8
« lib/Rex/Interface/Exec/Sudo.pm ↑ Summary lib/Rex/Commands/File.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Virtualization::LibVirt::guestinfo;
6  
7 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.16.1'; # VERSION
11  
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Rex::Virtualization::LibVirt::iflist;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Rex::Virtualization::LibVirt::info;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Rex::Interface::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 require Rex::Commands::Run;
23  
24 0 ▶sub execute {
Subroutine execute: not called CC=15 SCAR=54.8
Pod undocumented
25 0 my ( $class, $vmname ) = @_;
26  
27 0 ▶ unless ($vmname) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vmname) 0 0 0
28 0 die("You have to define the vm name!");
29 }
30  
31 0 Rex::Logger::debug("Getting info of guest: $vmname");
32  
33 0 my $info = Rex::Virtualization::LibVirt::info->execute($vmname);
34 0 ▶ if ( $info->{State} eq "shut off" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"State"} eq "shut off") 0 0 0
35 0 return {};
36 }
37  
38 0 my @ifaces;
39  
40 0 ▶ if ( Rex::Config::get_use_rex_kvm_agent
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::Config::get_use_rex_kvm_agent() and exists $info->{"has_kvm_agent_on_port"} and $info->{"has_kvm_agent_on_port"}) 0 0 0
Condition0%0/6
Rex::Config::get_use_rex_kvm_agent() and exists $info->{"has_kvm_agent_on_port"} and $info->{"has_kvm_agent_on_port"}
!ll&&!rl&&r
000
Rex::Config::get_use_rex_kvm_agent() and exists $info->{"has_kvm_agent_on_port"}
!ll&&!rl&&r
000
MC/DC0%0/3
Rex::Config::get_use_rex_kvm_agent() and exists $info->{"has_kvm_agent_on_port"} and $info->{"has_kvm_agent_on_port"}
Rex::Config::get_use_rex_kvm_agent() exists $info->{"has_kvm_agent_on_port"} $info->{"has_kvm_agent_on_port"}
Truth table0%0/4
A and B and C
A= Rex::Config::get_use_rex_kvm_agent()B= exists $info->{"has_kvm_agent_on_port"}C= $info->{"has_kvm_agent_on_port"}
ABCresult
0XX0
10X0
1100
1111
41 && exists $info->{has_kvm_agent_on_port}
42 && $info->{has_kvm_agent_on_port} )
43 {
44  
45 0 my $got_ip = 0;
46  
47 0 ▶ while ( $got_ip == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_ip == 0) 0 0 0
48  
49 0 my $fh = Rex::Interface::File->create();
50 0 my $rnd_file = get_tmp_file;
51  
52 0 my $content = q|
53 use strict;
54 use warnings;
55  
56 unlink $0;
57  
58 use IO::Socket::INET;
59  
60 my $sock;
61  
62 while(! $sock) {
63 $sock = IO::Socket::INET->new(PeerHost => '127.0.0.1', PeerPort => $ARGV[0], Proto => 'tcp');
64 sleep 1;
65 }
66  
67 my $got_info = 0;
68 my $try = 0;
69 while($got_info == 0) {
70 eval {
71 local $SIG{ALRM} = sub { die; };
72 alarm 3;
73 if($try >= 10) {
74 exit 1;
75 }
76  
77 $try++;
78 print $sock "GET /network/devices\n";
79  
80 my $line = <$sock>;
81 $line = <$sock>;
82 $line =~ s/[\r\n]//gms;
83 if($line =~ m/^\{"networkdevices/) {
84 print "$line\n";
85 $got_info++;
86 }
87 alarm 0;
88 };
89 }
90 |;
91  
92 0 $fh->open( ">", $rnd_file );
93 0 $fh->write($content);
94 0 $fh->close;
95  
96 0 my $exec = Rex::Interface::Exec->create();
97 0 my ($data) = $exec->exec("perl $rnd_file $info->{has_kvm_agent_on_port}");
98  
99 0 ▶ if ( $? == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 1) 0 0 0
100  
101 # can't connect to agent
102 0 $info->{has_kvm_agent_on_port} = 0;
103 0 last;
104 }
105  
106 0 my $ref = decode_json($data);
107 0 delete $ref->{networkconfiguration}->{lo};
108  
109 0 for my $net ( keys %{ $ref->{networkconfiguration} } ) {
110 push @ifaces,
111 {
112 device => $net,
113 0 %{ $ref->{networkconfiguration}->{$net} },
114 };
115 }
116  
117 0 ▶ if ( $ifaces[0]->{ip} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ifaces[0]{'ip'}) { } 0 0 0
118 0 $got_ip++;
119 }
120 else {
121 0 @ifaces = ();
122 0 sleep 1;
123 }
124 }
125  
126 }
127  
128 0 ▶ if ( !$info->{has_kvm_agent_on_port} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"has_kvm_agent_on_port"}) 0 0 0
129  
130 0 my $ifs = Rex::Virtualization::LibVirt::iflist->execute($vmname);
131  
132 0 my $got_ip = 0;
133  
134 # search arp in different locations.
135 # sometimes there is not PATH for normal users to /sbin commands.
136 # and also arp can be in different locations.
137 0 my $command =
138 Rex::Commands::Run::can_run( "/sbin/arp", "/usr/sbin/arp", "arp" );
139  
140 0 ▶ while ( $got_ip < scalar( keys %{$ifs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_ip < scalar keys %{$ifs;}) 0 0 0
141 my %arp =
142 map {
143 0 my @x = ( $_ =~ m/\(([^\)]+)\) at ([^\s]+)\s/ );
144 0 ( $x[1], $x[0] )
145 } i_run "$command -an", fail_ok => 1;
146  
147 0 for my $if ( keys %{$ifs} ) {
148 0 ▶ if ( exists $arp{ $ifs->{$if}->{mac} } && $arp{ $ifs->{$if}->{mac} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arp{$ifs->{$if}{"mac"}} and $arp{$ifs->{$if}{"mac"}}) 0 0 0
Condition0%0/3
exists $arp{$ifs->{$if}{"mac"}} and $arp{$ifs->{$if}{"mac"}}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $arp{$ifs->{$if}{"mac"}} and $arp{$ifs->{$if}{"mac"}}
exists $arp{$ifs->{$if}{"mac"}} $arp{$ifs->{$if}{"mac"}}
Truth table0%0/3
A and B
A= exists $arp{$ifs->{$if}{"mac"}}B= $arp{$ifs->{$if}{"mac"}}
ABresult
0X0
100
111
149 0 $got_ip++;
150 push @ifaces,
151 {
152 device => $if,
153 ip => $arp{ $ifs->{$if}->{mac} },
154 0 %{ $ifs->{$if} }
155 };
156 }
157 }
158  
159 0 sleep 1;
160 }
161  
162 }
163  
164 0 return { network => \@ifaces, };
165 }
166  
167 1 1;
« lib/Rex/Interface/Exec/Sudo.pm ↑ Summary lib/Rex/Commands/File.pm »