lib/Rex/Inventory/Hal.pm

filter: Statementstmt 21.9% 25 / 114 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 47.0% 8 / 17 Podpod 0.0% 0 / 7 totaltotal 15.9% 33 / 207 CC 28 SCAR 41.4
CC
28
Cov
15%
_read_lshal
182.0
get_storage_devices
30.0
get_storage_volumes
20.0
CRAP 62.7
« lib/Rex/Hardware/Network/FreeBSD.pm ↑ Summary lib/Rex/Inventory.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Inventory::Hal;
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 ▶use Rex::Inventory::Hal::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '1.16.1'; # VERSION
17  
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
21 0 my $that = shift;
22 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
23 0 my $self = {@_};
24  
25 0 bless( $self, $proto );
26  
27 0 $self->_read_lshal();
28  
29 0 return $self;
30 }
31  
32 # get devices of $category
33 # like net or storage
34 0 ▶sub get_devices_of {
Subroutine get_devices_of: not called CC=2 SCAR=17.9
Pod undocumented
35  
36 0 my ( $self, $cat, $rex_class ) = @_;
37 0 my @ret;
38  
39 0 for my $dev ( keys %{ $self->{'__hal'}->{$cat} } ) {
40 0 push( @ret, $self->get_object_by_cat_and_udi( $cat, $dev, $rex_class ) );
41 }
42  
43 0 return @ret;
44 }
45  
46 # get network devices
47 0 ▶sub get_network_devices {
Subroutine get_network_devices: not called CC=1 SCAR=6.9
Pod undocumented
48  
49 0 my ($self) = @_;
50 0 return $self->get_devices_of('net');
51  
52 }
53  
54 # get storage devices
55 0 ▶sub get_storage_devices {
Subroutine get_storage_devices: not called CC=5 SCAR=34.0
Pod undocumented
56  
57 0 my ($self) = @_;
58 0 my $os = get_operating_system();
59  
60 0 ▶ if ( $os =~ m/BSD/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /BSD/u) { } 0 0 0
61 return
62 0 ▶ grep { !$_->is_cdrom && !$_->is_volume && !$_->is_floppy }
Condition0%0/6
$_->is_cdrom || $_->is_volume || $_->is_floppy
l!l&&r!l&&!r
000
$_->is_cdrom or $_->is_volume
l!l&&r!l&&!r
000
MC/DC0%0/3
$_->is_cdrom || $_->is_volume || $_->is_floppy
$_->is_cdrom $_->is_volume $_->is_floppy
Truth table0%0/4
A or B || C
A= $_->is_cdromB= $_->is_volumeC= $_->is_floppy
ABCresult
1XX1
01X1
0011
0000
63 $self->get_devices_of( 'block', 'storage' );
64 }
65 else {
66 # default linux
67 return
68 0 ▶ grep { !$_->is_cdrom && !$_->is_floppy } $self->get_devices_of('storage');
Condition0%0/3
$_->is_cdrom || $_->is_floppy
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->is_cdrom || $_->is_floppy
$_->is_cdrom $_->is_floppy
Truth table0%0/3
A || B
A= $_->is_cdromB= $_->is_floppy
ABresult
1X1
011
000
69 }
70  
71 }
72  
73 # get storage volumes
74 0 ▶sub get_storage_volumes {
Subroutine get_storage_volumes: not called CC=4 SCAR=30.0
Pod undocumented
75  
76 0 my ($self) = @_;
77  
78 0 my $os = get_operating_system();
79  
80 0 ▶ if ( $os =~ m/BSD/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /BSD/u) { } 0 0 0
81 return
82 0 ▶ grep { !$_->is_cdrom && $_->is_volume && !$_->is_floppy }
Condition0%0/6
!$_->is_cdrom && $_->is_volume && !$_->is_floppy
!ll&&!rl&&r
000
not $_->is_cdrom and $_->is_volume
!ll&&!rl&&r
000
MC/DC0%0/3
!$_->is_cdrom && $_->is_volume && !$_->is_floppy
not $_->is_cdrom $_->is_volume !$_->is_floppy
Truth table0%0/4
not(A) and B && not(C)
A= not $_->is_cdromB= $_->is_volumeC= !$_->is_floppy
ABCresult
0XX0
10X0
1100
1111
83 $self->get_devices_of( 'block', 'volume' );
84 }
85 else {
86 # default linux
87 0 return $self->get_devices_of('volume');
88 }
89  
90 }
91  
92 # get a hal object from category and udi
93 0 ▶sub get_object_by_cat_and_udi {
Subroutine get_object_by_cat_and_udi: not called CC=3 SCAR=24.8
Pod undocumented
94 0 my ( $self, $cat, $udi, $rex_class ) = @_;
95  
96 0 ▶ $rex_class ||= $cat;
Condition0%0/3
$rex_class ||= $cat
l!l&&r!l&&!r
000
MC/DC0%0/2
$rex_class ||= $cat
$rex_class $cat
Truth table0%0/3
A ||= B
A= $rex_classB= $cat
ABresult
1X1
011
000
97  
98 0 my $class_name = "Rex::Inventory::Hal::Object::\u$rex_class";
99 0 eval "use $class_name";
100 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
101 0 Rex::Logger::debug(
102 "This Hal Object isn't supported yet. Falling back to Base Object.");
103 0 $class_name = "Rex::Inventory::Hal::Object";
104 }
105  
106 0 return $class_name->new( %{ $self->{'__hal'}->{$cat}->{$udi} },
107 hal => $self );
108 }
109  
110 # get object by udi
111 0 ▶sub get_object_by_udi {
Subroutine get_object_by_udi: not called CC=4 SCAR=30.0
Pod undocumented
112 0 my ( $self, $udi ) = @_;
113  
114 0 for my $cat ( keys %{ $self->{'__hal'} } ) {
115 0 for my $dev ( keys %{ $self->{'__hal'}->{$cat} } ) {
116 0 ▶ if ( $dev eq $udi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dev eq $udi) 0 0 0
117 0 return $self->get_object_by_cat_and_udi( $cat, $dev );
118 }
119 }
120 }
121 }
122  
123 # private method to read lshal output
124 # you don't see that...
125 0 ▶sub _read_lshal {
Subroutine _read_lshal: not called CC=13 SCAR=52.0
126  
127 0 my ($self) = @_;
128  
129 0 ▶ unless ( can_run "lshal" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_run("lshal")) 0 0 0
130 0 Rex::Logger::info("No lshal available");
131 0 die;
132 }
133  
134 0 my @lines = i_run "lshal", fail_ok => 1;
135 0 my %devices;
136 my %tmp_devices;
137  
138 0 my $in_dev = 0;
139 0 my %data;
140 my $dev_name;
141  
142 0 for my $l (@lines) {
143 0 chomp $l;
144  
145 0 ▶ if ( $l =~ m/^udi = '(.*?)'/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^udi = '(.*?)'/u) 0 0 0
146 0 $in_dev = 1;
147 0 $dev_name = $1;
148 }
149  
150 0 ▶ if ( $l =~ m/^$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^$/u) 0 0 0
151 0 $in_dev = 0;
152 0 ▶ unless ($dev_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dev_name) 0 0 0
153 0 %data = ();
154 0 next;
155 }
156 0 $tmp_devices{$dev_name} = {%data};
157 0 %data = ();
158 }
159  
160 0 ▶ if ($in_dev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dev) 0 0 0
161 0 my ( $key, $val ) = split( / = /, $l, 2 );
162 0 $key =~ s/^\s+//;
163 0 $key =~ s/^'|'$//g;
164 0 $val =~ s/\(.*?\)$//;
165 0 $val =~ s/^\s+//;
166 0 $val =~ s/\s+$//;
167 0 $val =~ s/^'|'$//g;
168 0 $data{$key} = $self->_parse_hal_string($val);
169 }
170  
171 }
172  
173 0 for my $dev ( keys %tmp_devices ) {
174  
175 my $s_key = $tmp_devices{$dev}->{"info.subsystem"}
176 0 ▶ || $tmp_devices{$dev}->{"linux.subsystem"};
Condition0%0/3
$tmp_devices{$dev}{'info.subsystem'} || $tmp_devices{$dev}{'linux.subsystem'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmp_devices{$dev}{'info.subsystem'} || $tmp_devices{$dev}{'linux.subsystem'}
$tmp_devices{$dev}{'info.subsystem'} $tmp_devices{$dev}{'linux.subsystem'}
Truth table0%0/3
A || B
A= $tmp_devices{$dev}{'info.subsystem'}B= $tmp_devices{$dev}{'linux.subsystem'}
ABresult
1X1
011
000
177 0 ▶ $s_key ||= $tmp_devices{$dev}->{"info.category"};
Condition0%0/3
$s_key ||= $tmp_devices{$dev}{"info.category"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$s_key ||= $tmp_devices{$dev}{"info.category"}
$s_key $tmp_devices{$dev}{"info.category"}
Truth table0%0/3
A ||= B
A= $s_keyB= $tmp_devices{$dev}{"info.category"}
ABresult
1X1
011
000
178  
179 0 ▶ if ( !$s_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s_key) 0 0 0
180  
181 #print Dumper($tmp_devices{$dev});
182 0 next;
183 }
184  
185 0 ▶ if ( $s_key =~ m/\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_key =~ /\./u) 0 0 0
186 0 ($s_key) = split( /\./, $s_key );
187 }
188  
189 0 ▶ if ( !exists $devices{$s_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $devices{$s_key}) 0 0 0
190 0 $devices{$s_key} = {};
191 }
192  
193 0 $devices{$s_key}->{$dev} = $tmp_devices{$dev};
194  
195 }
196  
197 0 $self->{'__hal'} = \%devices;
198  
199 }
200  
201 0 ▶sub _parse_hal_string {
Subroutine _parse_hal_string: not called CC=2 SCAR=17.9
202  
203 0 my ( $self, $line ) = @_;
204  
205 0 ▶ if ( $line =~ m/^\{.*\}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\{.*\}$/u) 0 0 0
206 0 $line =~ s/^\{/[/;
207 0 $line =~ s/\}$/]/;
208  
209 0 return eval $line;
210 }
211  
212 0 return $line;
213  
214 }
215  
216 1 1;
« lib/Rex/Hardware/Network/FreeBSD.pm ↑ Summary lib/Rex/Inventory.pm »