lib/Rex/Commands/Gather.pm

filter: Statementstmt 50.0% 63 / 126 Branchbran 25.0% 14 / 56 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 55.2% 21 / 38 Podpod 100% 27 / 27 totaltotal 47.9% 131 / 273 CC 36 SCAR 17.0
CC
36
Cov
42%
is_fedora
12.0
is_suse
12.0
is_mageia
12.0
CRAP 5.5
« lib/Rex/Virtualization.pm ↑ Summary lib/Rex/Interface/Exec/Base.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Gather - Hardware and Information gathering
8  
9 =head1 DESCRIPTION
10  
11 With this module you can gather hardware and software information.
12  
13 All these functions will not be reported. These functions don't modify anything.
14  
15 =head1 SYNOPSIS
16  
17 operating_system_is("SuSE");
18  
19  
20 =head1 EXPORTED FUNCTIONS
21  
22 =cut
23  
24 package Rex::Commands::Gather;
25  
26 36 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 36 our $VERSION = '1.16.1'; # VERSION
30  
31 36 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 36 ▶use Rex::Hardware;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 36 ▶use Rex::Hardware::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 36 ▶use Rex::Hardware::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 36 ▶use Rex::Hardware::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 36 ▶use Rex::Helper::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 36 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 36 require Rex::Exporter;
40 36 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 36 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 36 @EXPORT = qw(operating_system_is network_interfaces memory
45 get_operating_system operating_system operating_system_version operating_system_release
46 is_freebsd is_netbsd is_openbsd is_redhat is_linux is_bsd is_solaris is_suse is_debian is_mageia is_windows is_alt is_openwrt is_gentoo is_fedora is_arch is_void
47 get_system_information dump_system_information kernelname);
48  
49 =head2 get_operating_system
50  
51 Will return the current operating system name.
52  
53 task "get-os", "server01", sub {
54 say get_operating_system();
55 };
56  
57 Aliased by operating_system().
58  
59 =cut
60  
61 19 ▶sub get_operating_system {
Subroutine get_operating_system: called CC=2 SCAR=7.2
Pod documented
62  
63 19 my $operatingsystem = Rex::Hardware::Host->get_operating_system();
64  
65 19 ▶ return $operatingsystem || "unknown";
Condition50%1/2
$operatingsystem || 'unknown'
l!l
190
MC/DC0%0/1
$operatingsystem || 'unknown'
$operatingsystem
Truth table50%1/2
A
A= $operatingsystem
Aresult
11
00
66  
67 }
68  
69 =head2 operating_system
70  
71 Alias for get_operating_system()
72  
73 =cut
74  
75 0 ▶sub operating_system {
Subroutine operating_system: not called CC=1 SCAR=6.9
Pod documented
76 0 return get_operating_system();
77 }
78  
79 =head2 get_system_information
80  
81 Will return a hash of all system information. These Information will be also used by the template function.
82  
83 =cut
84  
85 0 ▶sub get_system_information {
Subroutine get_system_information: not called CC=1 SCAR=6.9
Pod documented
86 0 return Rex::Helper::System::info();
87 }
88  
89 =head2 kernelname
90  
91 Will return the kernel name of the operating system. For example on a linux system it will return I<Linux>.
92  
93 =cut
94  
95 0 ▶sub kernelname {
Subroutine kernelname: not called CC=1 SCAR=6.9
Pod documented
96 0 my $host = Rex::Hardware::Host->get();
97 0 return $host->{kernelname};
98 }
99  
100 =head2 dump_system_information
101  
102 This function dumps all known system information on stdout.
103  
104 =cut
105  
106 0 ▶sub dump_system_information {
Subroutine dump_system_information: not called CC=1 SCAR=6.9
Pod documented
107 0 my ($info) = @_;
108 0 my %sys_info = get_system_information();
109  
110 0 inspect( \%sys_info,
111 { prepend_key => '$', key_value_sep => " = ", no_root => 1 } );
112 }
113  
114 =head2 operating_system_is($string)
115  
116 Will return 1 if the operating system is $string.
117  
118 task "is_it_suse", "server01", sub {
119 if( operating_system_is("SuSE") ) {
120 say "This is a SuSE system.";
121 }
122 };
123  
124 =cut
125  
126 4 ▶sub operating_system_is {
Subroutine operating_system_is: called CC=3 SCAR=11.0
Pod documented
127  
128 4 my (@oses) = @_;
129  
130 4 my $operatingsystem = Rex::Hardware::Host->get_operating_system();
131  
132 4 for my $os (@oses) {
133 4 ▶ if ( $operatingsystem eq $os ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operatingsystem eq $os) 2 2 4
134 2 return 1;
135 }
136 }
137  
138 2 return 0;
139  
140 }
141  
142 =head2 operating_system_version()
143  
144 Will return the os release number as an integer. For example, it will convert 5.10 to 510, 10.04 to 1004 or 6.0.3 to 603.
145  
146 task "prepare", "server01", sub {
147 if( operating_system_version() >= 510 ) {
148 say "OS Release is higher or equal to 510";
149 }
150 };
151  
152 =cut
153  
154 0 ▶sub operating_system_version {
Subroutine operating_system_version: not called CC=1 SCAR=6.9
Pod documented
155  
156 0 my ($os) = @_;
157  
158 0 my $operatingsystemrelease = operating_system_release();
159  
160 0 $operatingsystemrelease =~ s/[\.,]//g;
161  
162 0 return $operatingsystemrelease;
163  
164 }
165  
166 =head2 operating_system_release()
167  
168 Will return the os release number as is.
169  
170 =cut
171  
172 0 ▶sub operating_system_release {
Subroutine operating_system_release: not called CC=1 SCAR=6.9
Pod documented
173 0 my ($os) = @_;
174 0 return Rex::Hardware::Host->get_operating_system_version();
175 }
176  
177 =head2 network_interfaces
178  
179 Return an HashRef of all the networkinterfaces and their configuration.
180  
181 task "get_network_information", "server01", sub {
182 my $net_info = network_interfaces();
183 };
184  
185 You can iterate over the devices as follow
186  
187 my $net_info = network_interfaces();
188 for my $dev ( keys %{ $net_info } ) {
189 say "$dev has the ip: " . $net_info->{$dev}->{"ip"} . " and the netmask: " . $net_info->{$dev}->{"netmask"};
190 }
191  
192 =cut
193  
194 0 ▶sub network_interfaces {
Subroutine network_interfaces: not called CC=1 SCAR=6.9
Pod documented
195  
196 0 my $net = Rex::Hardware::Network->get();
197  
198 0 return $net->{"networkconfiguration"};
199  
200 }
201  
202 =head2 memory
203  
204 Return an HashRef of all memory information.
205  
206 task "get_memory_information", "server01", sub {
207 my $memory = memory();
208  
209 say "Total: " . $memory->{"total"};
210 say "Free: " . $memory->{"free"};
211 say "Used: " . $memory->{"used"};
212 say "Cached: " . $memory->{"cached"};
213 say "Buffers: " . $memory->{"buffers"};
214 };
215  
216 =cut
217  
218 0 ▶sub memory {
Subroutine memory: not called CC=1 SCAR=6.9
Pod documented
219  
220 0 my $mem = Rex::Hardware::Memory->get();
221  
222 0 return $mem;
223  
224 }
225  
226 =head2 is_freebsd
227  
228 Returns true if the target system is a FreeBSD.
229  
230 task "foo", "server1", "server2", sub {
231 if(is_freebsd) {
232 say "This is a freebsd system...";
233 }
234 else {
235 say "This is not a freebsd system...";
236 }
237 };
238  
239 =cut
240  
241 2 ▶sub is_freebsd {
Subroutine is_freebsd: called CC=3 SCAR=13.1
Pod documented
242 2 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 2 2
243 2 ▶ if ( $os =~ m/FreeBSD/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /FreeBSD/iu) 0 2 2
244 0 return 1;
245 }
246 }
247  
248 =head2 is_redhat
249  
250 task "foo", "server1", sub {
251 if(is_redhat) {
252 # do something on a redhat system (like RHEL, Fedora, CentOS, Scientific Linux
253 }
254 };
255  
256 =cut
257  
258 2 ▶sub is_redhat {
Subroutine is_redhat: called CC=3 SCAR=12.0
Pod documented
259 2 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 2 2
260  
261 2 my @redhat_clones = (
262 "Fedora", "Redhat",
263 "CentOS", "Scientific",
264 "RedHatEnterpriseServer", "RedHatEnterpriseES",
265 "RedHatEnterpriseWorkstation", "RedHatEnterpriseWS",
266 "Amazon", "ROSAEnterpriseServer",
267 "CloudLinuxServer", "XenServer",
268 "OracleServer", "Virtuozzo",
269 );
270  
271 2 ▶ if ( grep { /$os/i } @redhat_clones ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/$os/iu;} @redhat_clones) 0 2 2
272 0 return 1;
273 }
274 }
275  
276 =head2 is_fedora
277  
278 task "foo", "server1", sub {
279 if(is_fedora) {
280 # do something on a fedora system
281 }
282 };
283  
284 =cut
285  
286 0 ▶sub is_fedora {
Subroutine is_fedora: not called CC=3 SCAR=24.8
Pod documented
287 0 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
288  
289 0 my @fedora_clones = ("Fedora");
290  
291 0 ▶ if ( grep { /$os/i } @fedora_clones ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/$os/iu;} @fedora_clones) 0 0 0
292 0 return 1;
293 }
294 }
295  
296 =head2 is_suse
297  
298 task "foo", "server1", sub {
299 if(is_suse) {
300 # do something on a suse system
301 }
302 };
303  
304 =cut
305  
306 0 ▶sub is_suse {
Subroutine is_suse: not called CC=3 SCAR=24.8
Pod documented
307 0 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
308  
309 0 my @suse_clones = ( "OpenSuSE", "SuSE", "openSUSE project" );
310  
311 0 ▶ if ( grep { /$os/i } @suse_clones ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/$os/iu;} @suse_clones) 0 0 0
312 0 return 1;
313 }
314 }
315  
316 =head2 is_mageia
317  
318 task "foo", "server1", sub {
319 if(is_mageia) {
320 # do something on a mageia system (or other Mandriva followers)
321 }
322 };
323  
324 =cut
325  
326 0 ▶sub is_mageia {
Subroutine is_mageia: not called CC=3 SCAR=24.8
Pod documented
327 0 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
328  
329 0 my @mdv_clones = ( "Mageia", "ROSADesktopFresh" );
330  
331 0 ▶ if ( grep { /$os/i } @mdv_clones ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/$os/iu;} @mdv_clones) 0 0 0
332 0 return 1;
333 }
334 }
335  
336 =head2 is_debian
337  
338 task "foo", "server1", sub {
339 if(is_debian) {
340 # do something on a debian system
341 }
342 };
343  
344 =cut
345  
346 0 ▶sub is_debian {
Subroutine is_debian: not called CC=3 SCAR=24.8
Pod documented
347 0 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
348  
349 0 my @debian_clones = ( "Debian", "Ubuntu", "LinuxMint", "Raspbian", "Devuan" );
350  
351 0 ▶ if ( grep { /$os/i } @debian_clones ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/$os/iu;} @debian_clones) 0 0 0
352 0 return 1;
353 }
354 }
355  
356 =head2 is_alt
357  
358 task "foo", "server1", sub {
359 if(is_alt) {
360 # do something on a ALT Linux system
361 }
362 };
363  
364 =cut
365  
366 0 ▶sub is_alt {
Subroutine is_alt: not called CC=3 SCAR=24.8
Pod documented
367 0 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
368  
369 0 my @alt_clones = ("ALT");
370  
371 0 ▶ if ( grep { /$os/i } @alt_clones ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/$os/iu;} @alt_clones) 0 0 0
372 0 return 1;
373 }
374 }
375  
376 =head2 is_netbsd
377  
378 Returns true if the target system is a NetBSD.
379  
380 task "foo", "server1", "server2", sub {
381 if(is_netbsd) {
382 say "This is a netbsd system...";
383 }
384 else {
385 say "This is not a netbsd system...";
386 }
387 };
388  
389 =cut
390  
391 2 ▶sub is_netbsd {
Subroutine is_netbsd: called CC=3 SCAR=13.1
Pod documented
392 2 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 2 2
393 2 ▶ if ( $os =~ m/NetBSD/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /NetBSD/iu) 0 2 2
394 0 return 1;
395 }
396 }
397  
398 =head2 is_openbsd
399  
400 Returns true if the target system is an OpenBSD.
401  
402 task "foo", "server1", "server2", sub {
403 if(is_openbsd) {
404 say "This is an openbsd system...";
405 }
406 else {
407 say "This is not an openbsd system...";
408 }
409 };
410  
411 =cut
412  
413 2 ▶sub is_openbsd {
Subroutine is_openbsd: called CC=3 SCAR=13.1
Pod documented
414 2 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 2 2
415 2 ▶ if ( $os =~ m/OpenBSD/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /OpenBSD/iu) 0 2 2
416 0 return 1;
417 }
418 }
419  
420 =head2 is_linux
421  
422 Returns true if the target system is a Linux System.
423  
424 task "prepare", "server1", "server2", sub {
425 if(is_linux) {
426 say "This is a linux system...";
427 }
428 else {
429 say "This is not a linux system...";
430 }
431 };
432  
433 =cut
434  
435 9 ▶sub is_linux {
Subroutine is_linux: called CC=4 SCAR=17.1
Pod documented
436  
437 9 my $host = Rex::Hardware::Host->get();
438 9 ▶ if ( exists $host->{kernelname}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $host->{"kernelname"} and $host->{"kernelname"} and $host->{"kernelname"} =~ /Linux/u) 9 0 9
Condition33%2/6
exists $host->{"kernelname"} and $host->{"kernelname"} and $host->{"kernelname"} =~ /Linux/u
!ll&&!rl&&r
009
exists $host->{"kernelname"} and $host->{"kernelname"}
!ll&&!rl&&r
009
MC/DC0%0/3
exists $host->{"kernelname"} and $host->{"kernelname"} and $host->{"kernelname"} =~ /Linux/u
exists $host->{"kernelname"} $host->{"kernelname"} $host->{"kernelname"} =~ /Linux/u
Truth table25%1/4
A and B and C
A= exists $host->{"kernelname"}B= $host->{"kernelname"}C= $host->{"kernelname"} =~ /Linux/u
ABCresult
0XX0
10X0
1100
1111
439 && $host->{kernelname}
440 && $host->{kernelname} =~ m/Linux/ )
441 {
442 9 return 1;
443 }
444 }
445  
446 =head2 is_bsd
447  
448 Returns true if the target system is a BSD System.
449  
450 task "prepare", "server1", "server2", sub {
451 if(is_bsd) {
452 say "This is a BSD system...";
453 }
454 else {
455 say "This is not a BSD system...";
456 }
457 };
458  
459 =cut
460  
461 0 ▶sub is_bsd {
Subroutine is_bsd: not called CC=2 SCAR=17.9
Pod documented
462  
463 0 my $host = Rex::Hardware::Host->get();
464 0 ▶ if ( $host->{"kernelname"} =~ m/BSD/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host->{"kernelname"} =~ /BSD/u) 0 0 0
465 0 return 1;
466 }
467 }
468  
469 =head2 is_solaris
470  
471 Returns true if the target system is a Solaris System.
472  
473 task "prepare", "server1", "server2", sub {
474 if(is_solaris) {
475 say "This is a Solaris system...";
476 }
477 else {
478 say "This is not a Solaris system...";
479 }
480 };
481  
482 =cut
483  
484 9 ▶sub is_solaris {
Subroutine is_solaris: called CC=4 SCAR=18.9
Pod documented
485  
486 9 my $host = Rex::Hardware::Host->get();
487 9 ▶ if ( exists $host->{kernelname}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $host->{"kernelname"} and $host->{"kernelname"} and $host->{"kernelname"} =~ /SunOS/u) 0 9 9
Condition33%2/6
exists $host->{"kernelname"} and $host->{"kernelname"} and $host->{"kernelname"} =~ /SunOS/u
!ll&&!rl&&r
090
exists $host->{"kernelname"} and $host->{"kernelname"}
!ll&&!rl&&r
009
MC/DC0%0/3
exists $host->{"kernelname"} and $host->{"kernelname"} and $host->{"kernelname"} =~ /SunOS/u
exists $host->{"kernelname"} $host->{"kernelname"} $host->{"kernelname"} =~ /SunOS/u
Truth table25%1/4
A and B and C
A= exists $host->{"kernelname"}B= $host->{"kernelname"}C= $host->{"kernelname"} =~ /SunOS/u
ABCresult
0XX0
10X0
1100
1111
488 && $host->{kernelname}
489 && $host->{"kernelname"} =~ m/SunOS/ )
490 {
491 0 return 1;
492 }
493 }
494  
495 =head2 is_windows
496  
497 Returns true if the target system is a Windows System.
498  
499 =cut
500  
501 1 ▶sub is_windows {
Subroutine is_windows: called CC=3 SCAR=14.2
Pod documented
502  
503 1 my $host = Rex::Hardware::Host->get();
504 1 ▶ if ( $host->{"operatingsystem"} =~ m/^MSWin/
Branch: 1 branch
BranchTrueFalseTotal
if ($host->{"operatingsystem"} =~ /^MSWin/u or $host->{"operatingsystem"} eq "Windows") 0 1 1
Condition33%1/3
$host->{"operatingsystem"} =~ /^MSWin/u or $host->{"operatingsystem"} eq "Windows"
l!l&&r!l&&!r
001
MC/DC0%0/2
$host->{"operatingsystem"} =~ /^MSWin/u or $host->{"operatingsystem"} eq "Windows"
$host->{"operatingsystem"} =~ /^MSWin/u $host->{"operatingsystem"} eq "Windows"
Truth table33%1/3
A or B
A= $host->{"operatingsystem"} =~ /^MSWin/uB= $host->{"operatingsystem"} eq "Windows"
ABresult
1X1
011
000
505 || $host->{operatingsystem} eq "Windows" )
506 {
507 0 return 1;
508 }
509  
510 }
511  
512 =head2 is_openwrt
513  
514 Returns true if the target system is an OpenWrt System.
515  
516 =cut
517  
518 2 ▶sub is_openwrt {
Subroutine is_openwrt: called CC=2 SCAR=8.1
Pod documented
519 2 my $os = get_operating_system();
520 2 ▶ if ( $os =~ m/OpenWrt/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /OpenWrt/iu) 0 2 2
521 0 return 1;
522 }
523  
524 }
525  
526 =head2 is_gentoo
527  
528 Returns true if the target system is a Gentoo System.
529  
530 =cut
531  
532 0 ▶sub is_gentoo {
Subroutine is_gentoo: not called CC=2 SCAR=17.9
Pod documented
533 0 my $os = get_operating_system();
534 0 ▶ if ( $os =~ m/Gentoo/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /Gentoo/iu) 0 0 0
535 0 return 1;
536 }
537  
538 }
539  
540 =head2 is_arch
541  
542 task "foo", "server1", sub {
543 if(is_arch) {
544 # do something on a arch system
545 }
546 };
547  
548 =cut
549  
550 0 ▶sub is_arch {
Subroutine is_arch: not called CC=3 SCAR=24.8
Pod documented
551 0 ▶ my $os = @_ ? shift : get_operating_system();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
552  
553 0 my @arch_clones = ( "Arch", "Manjaro", "Antergos" );
554  
555 0 ▶ if ( grep { /$os/i } @arch_clones ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/$os/iu;} @arch_clones) 0 0 0
556 0 return 1;
557 }
558 }
559  
560 =head2 is_void
561  
562 Returns true if the target system is a Void Linux system.
563  
564 =cut
565  
566 0 ▶sub is_void {
Subroutine is_void: not called CC=2 SCAR=17.9
Pod documented
567 0 my $os = get_operating_system();
568 0 ▶ if ( $os =~ m/VoidLinux/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os =~ /VoidLinux/iu) 0 0 0
569 0 return 1;
570 }
571  
572 }
573  
574 36 1;
« lib/Rex/Virtualization.pm ↑ Summary lib/Rex/Interface/Exec/Base.pm »