diff --git a/.perlcriticrc b/.perlcriticrc index cfcd32fd4..b5b176fe3 100644 --- a/.perlcriticrc +++ b/.perlcriticrc @@ -12,8 +12,14 @@ private_name_regex = _(?!_)\w+ [Subroutines::ProtectPrivateSubs] private_name_regex = _(?!_)\w+ -[TooMuchCode::ProhibitDuplicateLiteral] -allowlist = 'MSWin32' +#pgp this is one of the stupid things in perlcritic :-) +#[TooMuchCode::ProhibitDuplicateLiteral] +#allowlist = 'MSWin32' +[-TooMuchCode::ProhibitDuplicateLiteral] +#pgp another stupid one +[-ValuesAndExpressions::ProhibitEmptyQuotes] +#pgp would make sense if perlcritic has been used from start :-) +[-RegularExpressions::ProhibitEscapedMetacharacters] [-ValuesAndExpressions::ProhibitConstantPragma] @@ -21,3 +27,4 @@ allowlist = 'MSWin32' [Variables::ProhibitPackageVars] add_packages = Rex::Logger + diff --git a/lib/Rex.pm b/lib/Rex.pm index 9e37e6725..16bbd8519 100644 --- a/lib/Rex.pm +++ b/lib/Rex.pm @@ -48,6 +48,7 @@ package Rex; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION diff --git a/lib/Rex/Commands/Gather.pm b/lib/Rex/Commands/Gather.pm index cdff9bc1f..07a1477e0 100644 --- a/lib/Rex/Commands/Gather.pm +++ b/lib/Rex/Commands/Gather.pm @@ -2,6 +2,8 @@ # (c) Jan Gehring # +## no critic (Documentation) + =head1 NAME Rex::Commands::Gather - Hardware and Information gathering @@ -14,7 +16,7 @@ All these functions will not be reported. These functions don't modify anything. =head1 SYNOPSIS - operating_system_is("SuSE"); + operating_system_is('SuSE'); =head1 EXPORTED FUNCTIONS @@ -25,13 +27,15 @@ package Rex::Commands::Gather; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION use Data::Dumper; use Rex::Hardware; use Rex::Hardware::Host; -use Rex::Hardware::Network; + +#use Rex::Hardware::Network; use Rex::Hardware::Memory; use Rex::Helper::System; use Rex::Commands; @@ -44,13 +48,13 @@ use vars qw(@EXPORT); @EXPORT = qw(operating_system_is network_interfaces memory get_operating_system operating_system operating_system_version operating_system_release 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 - get_system_information dump_system_information kernelname); + get_system_information dump_system_information kernelname is_alpine); =head2 get_operating_system Will return the current operating system name. - task "get-os", "server01", sub { + task 'get-os', 'server01', sub { say get_operating_system(); }; @@ -62,7 +66,7 @@ sub get_operating_system { my $operatingsystem = Rex::Hardware::Host->get_operating_system(); - return $operatingsystem || "unknown"; + return $operatingsystem || 'unknown'; } @@ -107,17 +111,21 @@ sub dump_system_information { my ($info) = @_; my %sys_info = get_system_information(); - inspect( \%sys_info, - { prepend_key => '$', key_value_sep => " = ", no_root => 1 } ); + ## no critic (ProhibitNoisyQuotes) + + return inspect( \%sys_info, + { prepend_key => '$', key_value_sep => ' = ', no_root => 1 } ); + + ## use critic } =head2 operating_system_is($string) Will return 1 if the operating system is $string. - task "is_it_suse", "server01", sub { - if( operating_system_is("SuSE") ) { - say "This is a SuSE system."; + task 'is_it_suse', 'server01', sub { + if( operating_system_is('SuSE') ) { + say 'This is a SuSE system.'; } }; @@ -143,9 +151,9 @@ sub operating_system_is { 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. - task "prepare", "server01", sub { + task 'prepare', 'server01', sub { if( operating_system_version() >= 510 ) { - say "OS Release is higher or equal to 510"; + say 'OS Release is higher or equal to 510'; } }; @@ -178,7 +186,7 @@ sub operating_system_release { Return an HashRef of all the networkinterfaces and their configuration. - task "get_network_information", "server01", sub { + task 'get_network_information', 'server01', sub { my $net_info = network_interfaces(); }; @@ -186,7 +194,7 @@ You can iterate over the devices as follow my $net_info = network_interfaces(); for my $dev ( keys %{ $net_info } ) { - say "$dev has the ip: " . $net_info->{$dev}->{"ip"} . " and the netmask: " . $net_info->{$dev}->{"netmask"}; + say '$dev has the ip: ' . $net_info->{$dev}->{ip} . ' and the netmask: ' . $net_info->{$dev}->{netmask}; } =cut @@ -195,7 +203,7 @@ sub network_interfaces { my $net = Rex::Hardware::Network->get(); - return $net->{"networkconfiguration"}; + return $net->{networkconfiguration}; } @@ -203,14 +211,14 @@ sub network_interfaces { Return an HashRef of all memory information. - task "get_memory_information", "server01", sub { + task 'get_memory_information', 'server01', sub { my $memory = memory(); - say "Total: " . $memory->{"total"}; - say "Free: " . $memory->{"free"}; - say "Used: " . $memory->{"used"}; - say "Cached: " . $memory->{"cached"}; - say "Buffers: " . $memory->{"buffers"}; + say 'Total: ' . $memory->{total}; + say 'Free: ' . $memory->{free}; + say 'Used: ' . $memory->{used}; + say 'Cached: ' . $memory->{cached}; + say 'Buffers: ' . $memory->{buffers}; }; =cut @@ -227,19 +235,19 @@ sub memory { Returns true if the target system is a FreeBSD. - task "foo", "server1", "server2", sub { + task 'foo', 'server1', 'server2', sub { if(is_freebsd) { - say "This is a freebsd system..."; + say 'This is a freebsd system...'; } else { - say "This is not a freebsd system..."; + say 'This is not a freebsd system...'; } }; =cut sub is_freebsd { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); if ( $os =~ m/FreeBSD/i ) { return 1; } @@ -247,7 +255,7 @@ sub is_freebsd { =head2 is_redhat - task "foo", "server1", sub { + task 'foo', 'server1', sub { if(is_redhat) { # do something on a redhat system (like RHEL, Fedora, CentOS, Scientific Linux } @@ -256,26 +264,26 @@ sub is_freebsd { =cut sub is_redhat { - my $os = @_ ? shift : get_operating_system(); - - my @redhat_clones = ( - "Fedora", "Redhat", - "CentOS", "Scientific", - "RedHatEnterpriseServer", "RedHatEnterpriseES", - "RedHatEnterpriseWorkstation", "RedHatEnterpriseWS", - "Amazon", "ROSAEnterpriseServer", - "CloudLinuxServer", "XenServer", - "OracleServer", "Virtuozzo", + my $os = shift || get_operating_system(); + + my @redhat_clones = qw( + Fedora Redhat + CentOS Scientific + RedHatEnterpriseServer RedHatEnterpriseES + RedHatEnterpriseWorkstation RedHatEnterpriseWS + Amazon ROSAEnterpriseServer + CloudLinuxServer XenServer + OracleServer Virtuozzo ); - if ( grep { /$os/i } @redhat_clones ) { + if ( scalar grep { /$os/i } @redhat_clones ) { return 1; } } =head2 is_fedora - task "foo", "server1", sub { + task 'foo', 'server1', sub { if(is_fedora) { # do something on a fedora system } @@ -284,18 +292,18 @@ sub is_redhat { =cut sub is_fedora { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); - my @fedora_clones = ("Fedora"); + my @fedora_clones = ('Fedora'); - if ( grep { /$os/i } @fedora_clones ) { + if ( scalar grep { /$os/i } @fedora_clones ) { return 1; } } =head2 is_suse - task "foo", "server1", sub { + task 'foo', 'server1', sub { if(is_suse) { # do something on a suse system } @@ -304,18 +312,18 @@ sub is_fedora { =cut sub is_suse { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); - my @suse_clones = ( "OpenSuSE", "SuSE", "openSUSE project" ); + my @suse_clones = ( 'OpenSuSE', 'SuSE', 'openSUSE project' ); - if ( grep { /$os/i } @suse_clones ) { + if ( scalar grep { /$os/i } @suse_clones ) { return 1; } } =head2 is_mageia - task "foo", "server1", sub { + task 'foo', 'server1', sub { if(is_mageia) { # do something on a mageia system (or other Mandriva followers) } @@ -324,18 +332,18 @@ sub is_suse { =cut sub is_mageia { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); - my @mdv_clones = ( "Mageia", "ROSADesktopFresh" ); + my @mdv_clones = qw( Mageia ROSADesktopFresh ); - if ( grep { /$os/i } @mdv_clones ) { + if ( scalar grep { /$os/i } @mdv_clones ) { return 1; } } =head2 is_debian - task "foo", "server1", sub { + task 'foo', 'server1', sub { if(is_debian) { # do something on a debian system } @@ -344,18 +352,18 @@ sub is_mageia { =cut sub is_debian { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); - my @debian_clones = ( "Debian", "Ubuntu", "LinuxMint", "Raspbian", "Devuan" ); + my @debian_clones = qw( Debian Ubuntu LinuxMint Raspbian Devuan ); - if ( grep { /$os/i } @debian_clones ) { + if ( scalar grep { /$os/i } @debian_clones ) { return 1; } } =head2 is_alt - task "foo", "server1", sub { + task 'foo', 'server1', sub { if(is_alt) { # do something on a ALT Linux system } @@ -364,11 +372,11 @@ sub is_debian { =cut sub is_alt { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); - my @alt_clones = ("ALT"); + my @alt_clones = ('ALT'); - if ( grep { /$os/i } @alt_clones ) { + if ( scalar grep { /$os/i } @alt_clones ) { return 1; } } @@ -377,19 +385,19 @@ sub is_alt { Returns true if the target system is a NetBSD. - task "foo", "server1", "server2", sub { + task 'foo', 'server1', 'server2', sub { if(is_netbsd) { - say "This is a netbsd system..."; + say 'This is a netbsd system...'; } else { - say "This is not a netbsd system..."; + say 'This is not a netbsd system...'; } }; =cut sub is_netbsd { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); if ( $os =~ m/NetBSD/i ) { return 1; } @@ -399,19 +407,19 @@ sub is_netbsd { Returns true if the target system is an OpenBSD. - task "foo", "server1", "server2", sub { + task 'foo', 'server1', 'server2', sub { if(is_openbsd) { - say "This is an openbsd system..."; + say 'This is an openbsd system...'; } else { - say "This is not an openbsd system..."; + say 'This is not an openbsd system...'; } }; =cut sub is_openbsd { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); if ( $os =~ m/OpenBSD/i ) { return 1; } @@ -421,12 +429,12 @@ sub is_openbsd { Returns true if the target system is a Linux System. - task "prepare", "server1", "server2", sub { + task 'prepare', 'server1', 'server2', sub { if(is_linux) { - say "This is a linux system..."; + say 'This is a linux system...'; } else { - say "This is not a linux system..."; + say 'This is not a linux system...'; } }; @@ -447,12 +455,12 @@ sub is_linux { Returns true if the target system is a BSD System. - task "prepare", "server1", "server2", sub { + task 'prepare', 'server1', 'server2', sub { if(is_bsd) { - say "This is a BSD system..."; + say 'This is a BSD system...'; } else { - say "This is not a BSD system..."; + say 'This is not a BSD system...'; } }; @@ -461,7 +469,7 @@ Returns true if the target system is a BSD System. sub is_bsd { my $host = Rex::Hardware::Host->get(); - if ( $host->{"kernelname"} =~ m/BSD/ ) { + if ( $host->{kernelname} =~ m/BSD/ ) { return 1; } } @@ -470,12 +478,12 @@ sub is_bsd { Returns true if the target system is a Solaris System. - task "prepare", "server1", "server2", sub { + task 'prepare', 'server1', 'server2', sub { if(is_solaris) { - say "This is a Solaris system..."; + say 'This is a Solaris system...'; } else { - say "This is not a Solaris system..."; + say 'This is not a Solaris system...'; } }; @@ -486,7 +494,7 @@ sub is_solaris { my $host = Rex::Hardware::Host->get(); if ( exists $host->{kernelname} && $host->{kernelname} - && $host->{"kernelname"} =~ m/SunOS/ ) + && $host->{kernelname} =~ m/SunOS/ ) { return 1; } @@ -501,8 +509,8 @@ Returns true if the target system is a Windows System. sub is_windows { my $host = Rex::Hardware::Host->get(); - if ( $host->{"operatingsystem"} =~ m/^MSWin/ - || $host->{operatingsystem} eq "Windows" ) + if ( $host->{operatingsystem} =~ m/^MSWin/ + || $host->{operatingsystem} eq 'Windows' ) { return 1; } @@ -539,7 +547,7 @@ sub is_gentoo { =head2 is_arch - task "foo", "server1", sub { + task 'foo', 'server1', sub { if(is_arch) { # do something on a arch system } @@ -548,11 +556,11 @@ sub is_gentoo { =cut sub is_arch { - my $os = @_ ? shift : get_operating_system(); + my $os = shift || get_operating_system(); - my @arch_clones = ( "Arch", "Manjaro", "Antergos" ); + my @arch_clones = qw( Arch Manjaro Antergos ); - if ( grep { /$os/i } @arch_clones ) { + if ( scalar grep { /$os/i } @arch_clones ) { return 1; } } @@ -571,4 +579,11 @@ sub is_void { } +sub is_alpine { + my $os = get_operating_system(); + if ( $os =~ m/Alpine/i ) { + return 1; + } +} + 1; diff --git a/lib/Rex/Commands/Pkg.pm b/lib/Rex/Commands/Pkg.pm index 1118828d8..7ec0c16ac 100644 --- a/lib/Rex/Commands/Pkg.pm +++ b/lib/Rex/Commands/Pkg.pm @@ -31,6 +31,7 @@ package Rex::Commands::Pkg; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION diff --git a/lib/Rex/Commands/Process.pm b/lib/Rex/Commands/Process.pm index 57aeaab4e..eaf7c29b2 100644 --- a/lib/Rex/Commands/Process.pm +++ b/lib/Rex/Commands/Process.pm @@ -28,6 +28,7 @@ package Rex::Commands::Process; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION @@ -118,7 +119,7 @@ sub ps { my (@custom) = @_; my @list; - if (is_openwrt) { + if ( is_openwrt() ) { # openwrt doesn't have ps aux @list = i_run( "ps", fail_ok => 1 ); diff --git a/lib/Rex/Commands/User.pm b/lib/Rex/Commands/User.pm index 7b57bda7d..b2c9842ea 100644 --- a/lib/Rex/Commands/User.pm +++ b/lib/Rex/Commands/User.pm @@ -36,6 +36,7 @@ package Rex::Commands::User; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION diff --git a/lib/Rex/Hardware/Host.pm b/lib/Rex/Hardware/Host.pm index a044144ce..76d61b30f 100644 --- a/lib/Rex/Hardware/Host.pm +++ b/lib/Rex/Hardware/Host.pm @@ -21,10 +21,12 @@ use Rex::Inventory::Bios; require Rex::Hardware; +## no critic (ProhibitExcessComplexity, ProhibitCascadingIfElse) + sub get { my $cache = Rex::get_cache(); - my $cache_key_name = $cache->gen_key_name("hardware.host"); + my $cache_key_name = $cache->gen_key_name('hardware.host'); if ( $cache->valid($cache_key_name) ) { return $cache->get($cache_key_name); @@ -35,37 +37,37 @@ sub get { my $os = get_operating_system(); my ( $domain, $hostname ); - if ( $os eq "Windows" ) { + if ( $os eq 'Windows' ) { my @env = i_run('set'); ($hostname) = map { /^COMPUTERNAME=(.*)$/ } @env; ($domain) = map { /^USERDOMAIN=(.*)$/ } @env; } - elsif ( $os eq "NetBSD" || $os eq "OpenBSD" || $os eq 'FreeBSD' ) { + elsif ( $os eq 'NetBSD' || $os eq 'OpenBSD' || $os eq 'FreeBSD' ) { ( $hostname, $domain ) = - split( /\./, ( eval { i_run("hostname") } || "unknown.nodomain" ), 2 ); + split /\./, ( eval { i_run('hostname') } || 'unknown.nodomain' ), 2; } - elsif ( $os eq "SunOS" ) { + elsif ( $os eq 'SunOS' ) { ($hostname) = - map { /^([^\.]+)$/ } ( eval { i_run("hostname"); } || "unknown" ); - ($domain) = eval { i_run("domainname"); } || ("nodomain"); + map { /^([^\.]+)$/ } ( eval { i_run('hostname'); } || 'unknown' ); + ($domain) = eval { i_run('domainname'); } || ('nodomain'); } - elsif ( $os eq "OpenWrt" ) { - ($hostname) = eval { i_run("uname -n"); } || ("unknown"); + elsif ( $os eq 'OpenWrt' ) { + ($hostname) = eval { i_run('uname -n'); } || ('unknown'); ($domain) = - eval { i_run("cat /proc/sys/kernel/domainname"); } || ("unknown"); + eval { i_run('cat /proc/sys/kernel/domainname'); } || ('unknown'); } else { - my @out = i_run "hostname -f 2>/dev/null", fail_ok => 1; + my @out = i_run 'hostname -f 2>/dev/null', fail_ok => 1; - if ( $? == 0 && defined $out[0] ) { - ( $hostname, $domain ) = split( /\./, $out[0], 2 ); + if ( scalar @out && defined $out[0] ) { + ( $hostname, $domain ) = split /\./, $out[0], 2; } else { Rex::Logger::debug( - "Error getting hostname and domainname. There is something wrong with your /etc/hosts file." + 'Error getting hostname and domainname. There is something wrong with your /etc/hosts file.' ); - ($hostname) = eval { i_run("hostname -s"); } || ("unknown"); - ($domain) = eval { i_run("hostname -d"); } || ("nodomain"); + ($hostname) = eval { i_run('hostname -s'); } || ('unknown'); + ($domain) = eval { i_run('hostname -d'); } || ('nodomain'); } } @@ -79,11 +81,11 @@ sub get { my $data = { - manufacturer => $bios->get_system_information()->get_manufacturer() || "", - hostname => $hostname || "", - domain => $domain || "", - operatingsystem => $os || "", - operating_system => $os || "", + manufacturer => $bios->get_system_information()->get_manufacturer() || '', + hostname => $hostname || '', + domain => $domain || '', + operatingsystem => $os || '', + operating_system => $os || '', operatingsystemrelease => $operating_system_version, operating_system_release => $operating_system_version, kernelname => $kernelname, @@ -99,95 +101,99 @@ sub get { sub get_operating_system { my $cache = Rex::get_cache(); - if ( $cache->valid("hardware.host") ) { - my $host_cache = $cache->get("hardware.host"); + if ( $cache->valid('hardware.host') ) { + my $host_cache = $cache->get('hardware.host'); if ( exists $host_cache->{operatingsystem} ) { return $host_cache->{operatingsystem}; } } # use lsb_release if available - my $is_lsb = can_run("lsb_release"); + my $is_lsb = can_run('lsb_release'); if ($is_lsb) { - if ( my $ret = i_run "lsb_release -s -i" ) { + if ( my $ret = i_run 'lsb_release -s -i' ) { if ( $ret =~ m/SUSE/i ) { - $ret = "SuSE"; + $ret = 'SuSE'; } - elsif ( $ret eq "ManjaroLinux" ) { - $ret = "Manjaro"; + elsif ( $ret eq 'ManjaroLinux' ) { + $ret = 'Manjaro'; } return $ret; } } - if ( is_dir("c:/") ) { + if ( is_dir('c:/') ) { # windows - return "Windows"; + return 'Windows'; } - if ( is_file("/etc/system-release") ) { - my $content = cat "/etc/system-release"; + if ( is_file('/etc/system-release') ) { + my $content = cat '/etc/system-release'; if ( $content =~ m/Amazon/sm ) { - return "Amazon"; + return 'Amazon'; } } - if ( is_file("/etc/debian_version") ) { - return "Debian"; + if ( is_file('/etc/debian_version') ) { + return 'Debian'; } - if ( is_file("/etc/SuSE-release") or is_file("/etc/SUSE-brand") ) { - return "SuSE"; + if ( is_file('/etc/SuSE-release') or is_file('/etc/SUSE-brand') ) { + return 'SuSE'; } - if ( is_file("/etc/mageia-release") ) { - return "Mageia"; + if ( is_file('/etc/mageia-release') ) { + return 'Mageia'; } - if ( is_file("/etc/fedora-release") ) { - return "Fedora"; + if ( is_file('/etc/fedora-release') ) { + return 'Fedora'; } - if ( is_file("/etc/gentoo-release") ) { - return "Gentoo"; + if ( is_file('/etc/gentoo-release') ) { + return 'Gentoo'; } - if ( is_file("/etc/altlinux-release") ) { - return "ALT"; + if ( is_file('/etc/altlinux-release') ) { + return 'ALT'; } - if ( is_file("/etc/redhat-release") ) { - my $fh = file_read("/etc/redhat-release"); + if ( is_file('/etc/redhat-release') ) { + my $fh = file_read('/etc/redhat-release'); my $content = $fh->read_all; $fh->close; chomp $content; if ( $content =~ m/CentOS/ ) { - return "CentOS"; + return 'CentOS'; } elsif ( $content =~ m/Scientific/ ) { - return "Scientific"; + return 'Scientific'; } else { - return "Redhat"; + return 'Redhat'; } } - if ( is_file("/etc/openwrt_release") ) { - return "OpenWrt"; + if ( is_file('/etc/openwrt_release') ) { + return 'OpenWrt'; + } + + if ( is_file('/etc/arch-release') ) { + return 'Arch'; } - if ( is_file("/etc/arch-release") ) { - return "Arch"; + if ( is_file('/etc/manjaro-release') ) { + return 'Manjaro'; } - if ( is_file("/etc/manjaro-release") ) { - return "Manjaro"; + if ( is_file('/etc/alpine-release') ) { + return 'Alpine'; } - my $os_string = i_run("uname -s"); + my $os_string = i_run('uname -s'); return $os_string; # return the plain os } @@ -195,8 +201,8 @@ sub get_operating_system { sub get_operating_system_version { my $cache = Rex::get_cache(); - if ( $cache->valid("hardware.host") ) { - my $host_cache = $cache->get("hardware.host"); + if ( $cache->valid('hardware.host') ) { + my $host_cache = $cache->get('hardware.host'); if ( exists $host_cache->{operatingsystemrelease} ) { return $host_cache->{operatingsystemrelease}; } @@ -204,21 +210,21 @@ sub get_operating_system_version { my $op = get_operating_system(); - my $is_lsb = can_run("lsb_release"); + my $is_lsb = can_run('lsb_release'); # use lsb_release if available if ($is_lsb) { - if ( my $ret = i_run "lsb_release -r -s" ) { - my $os_check = i_run "lsb_release -d"; - unless ( $os_check =~ m/SUSE\sLinux\sEnterprise/ ) { + if ( my $ret = i_run 'lsb_release -r -s' ) { + my $os_check = i_run 'lsb_release -d'; + if ( !( $os_check =~ m/SUSE\sLinux\sEnterprise/ ) ) { return $ret; } } } - if ( $op eq "Debian" ) { + if ( $op eq 'Debian' ) { - my $fh = file_read("/etc/debian_version"); + my $fh = file_read('/etc/debian_version'); my $content = $fh->read_all; $fh->close; @@ -227,59 +233,66 @@ sub get_operating_system_version { return $content; } - elsif ( $op eq "Ubuntu" ) { - my @l = i_run "lsb_release -r -s", fail_ok => 1; + elsif ( $op eq 'Ubuntu' ) { + my @l = i_run 'lsb_release -r -s', fail_ok => 1; return $l[0]; } - elsif ( lc($op) eq "redhat" - or lc($op) eq "centos" - or lc($op) eq "scientific" - or lc($op) eq "fedora" ) + elsif ( lc($op) eq 'redhat' + or lc($op) eq 'centos' + or lc($op) eq 'scientific' + or lc($op) eq 'fedora' ) { - my $fh = file_read("/etc/redhat-release"); + my $fh = file_read('/etc/redhat-release'); my $content = $fh->read_all; $fh->close; chomp $content; - $content =~ m/(\d+(\.\d+)?)/; - - return $1; + if ( $content =~ m/(\d+(\.\d+)?)/ ) { + return $1; + } + else { + return; + } } - elsif ( $op eq "Mageia" ) { - my $fh = file_read("/etc/mageia-release"); + elsif ( $op eq 'Mageia' ) { + my $fh = file_read('/etc/mageia-release'); my $content = $fh->read_all; $fh->close; chomp $content; - $content =~ m/(\d+)/; - - return $1; + if ( $content =~ m/(\d+)/ ) { + return $1; + } + else { + return; + } } - elsif ( $op eq "Gentoo" ) { - my $fh = file_read("/etc/gentoo-release"); + elsif ( $op eq 'Gentoo' ) { + my $fh = file_read('/etc/gentoo-release'); my $content = $fh->read_all; $fh->close; chomp $content; - return [ split( /\s+/, $content ) ]->[-1]; + return [ split /\s+/, $content ]->[-1]; } - elsif ( $op eq "SuSE" ) { + elsif ( $op eq 'SuSE' ) { - my ( $version, $release ); + #pgp my ( $version, $release ); + my $version; my $release_file; - if ( is_file("/etc/os-release") ) { - $release_file = "/etc/os-release"; + if ( is_file('/etc/os-release') ) { + $release_file = '/etc/os-release'; } else { - $release_file = "/etc/SuSE-release"; + $release_file = '/etc/SuSE-release'; } my $fh = file_read($release_file); @@ -298,24 +311,26 @@ sub get_operating_system_version { return $version; } - elsif ( $op eq "ALT" ) { - my $fh = file_read("/etc/altlinux-release"); + elsif ( $op eq 'ALT' ) { + my $fh = file_read('/etc/altlinux-release'); my $content = $fh->read_all; $fh->close; chomp $content; - $content =~ m/(\d+(\.\d+)*)/; - - return $1; - + if ( $content =~ m/(\d+(\.\d+)*)/ ) { + return $1; + } + else { + return; + } } elsif ( $op =~ /BSD/ ) { - my ($version) = map { /(\d+\.\d+)/ } i_run "uname -r"; + my ($version) = map { /(\d+\.\d+)/ } i_run 'uname -r'; return $version; } - elsif ( $op eq "OpenWrt" ) { - my $fh = file_read("/etc/openwrt_version"); + elsif ( $op eq 'OpenWrt' ) { + my $fh = file_read('/etc/openwrt_version'); my $content = $fh->read_all; $fh->close; @@ -323,13 +338,13 @@ sub get_operating_system_version { return $content; } - elsif ( $op eq "Arch" ) { - my $available_updates = i_run "checkupdates", fail_ok => 1; - if ( $available_updates eq "" ) { - return "latest"; + elsif ( $op eq 'Arch' ) { + my $available_updates = i_run 'checkupdates', fail_ok => 1; + if ( $available_updates eq '' ) { + return 'latest'; } else { - return "outdated"; + return 'outdated'; } } elsif ( $op eq 'Windows' ) { @@ -339,9 +354,22 @@ sub get_operating_system_version { return $version; } } + elsif ( $op eq 'Alpine' ) { - return [ i_run("uname -r") ]->[0]; + my $fh = file_read('/etc/alpine-release'); + my $content = $fh->read_all; + $fh->close; + + chomp $content; + + return $content; + + } + + return [ i_run('uname -r') ]->[0]; } +## use critic + 1; diff --git a/lib/Rex/Helper/INI.pm b/lib/Rex/Helper/INI.pm index b7a554ce3..3c6b47afe 100644 --- a/lib/Rex/Helper/INI.pm +++ b/lib/Rex/Helper/INI.pm @@ -6,10 +6,13 @@ package Rex::Helper::INI; use v5.14.4; use warnings; +use strict; + +use String::Escape 'string2hash'; our $VERSION = '9999.99.99_99'; # VERSION -BEGIN { String::Escape->use('string2hash'); } +#BEGIN { String::Escape->use('string2hash'); } sub parse { my (@lines) = @_; diff --git a/lib/Rex/Output.pm b/lib/Rex/Output.pm index 469f9f758..64db34dc3 100644 --- a/lib/Rex/Output.pm +++ b/lib/Rex/Output.pm @@ -6,12 +6,14 @@ package Rex::Output; use v5.14.4; use warnings; +use strict; +use IPC::Shareable; my $handle; use vars qw($output_object); -BEGIN { IPC::Shareable->use; } -END { IPC::Shareable->clean_up_all; } +#BEGIN { IPC::Shareable->use; } +END { IPC::Shareable->clean_up_all; } use base 'Rex::Output::Base'; diff --git a/lib/Rex/Pkg.pm b/lib/Rex/Pkg.pm index c7e2336d6..54b9227dc 100644 --- a/lib/Rex/Pkg.pm +++ b/lib/Rex/Pkg.pm @@ -6,6 +6,7 @@ package Rex::Pkg; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION @@ -15,6 +16,8 @@ use Rex::Hardware; use Rex::Hardware::Host; use Rex::Logger; +use Class::Load; +use Carp; use Data::Dumper; my %PKG_PROVIDER; @@ -32,24 +35,24 @@ sub get { my %_host = %{ Rex::Hardware::Host->get() }; my $host = {%_host}; - my $pkg_provider_for = Rex::Config->get("package_provider") || {}; + my $pkg_provider_for = Rex::Config->get('package_provider') || {}; #if(lc($host->{"operatingsystem"}) eq "centos" || lc($host->{"operatingsystem"}) eq "redhat") { if ( is_redhat() ) { - $host->{"operatingsystem"} = "Redhat"; + $host->{operatingsystem} = 'Redhat'; } if ( is_debian() ) { - $host->{"operatingsystem"} = "Debian"; + $host->{operatingsystem} = 'Debian'; } - my $class = "Rex::Pkg::" . $host->{"operatingsystem"}; + my $class = 'Rex::Pkg::' . $host->{operatingsystem}; my $provider; if ( ref($pkg_provider_for) - && exists $pkg_provider_for->{ $host->{"operatingsystem"} } ) + && exists $pkg_provider_for->{ $host->{operatingsystem} } ) { - $provider = $pkg_provider_for->{ $host->{"operatingsystem"} }; + $provider = $pkg_provider_for->{ $host->{operatingsystem} }; $class .= "::$provider"; } elsif ( exists $PKG_PROVIDER{$pkg_provider_for} ) { @@ -57,19 +60,25 @@ sub get { } Rex::Logger::debug("Using $class for package management"); - eval "use $class"; - - if ($@) { - - if ($provider) { - Rex::Logger::info( "Provider not supported (" . $provider . ")" ); - } - else { - Rex::Logger::info( - "OS not supported (" . $host->{"operatingsystem"} . ")" ); - } - die("OS/Provider not supported"); + #pgp this code does nothing - eval 'use class' will ALWAYS return undef + #eval "use $class"; + + #if ($@) { + # + # if ($provider) { + # Rex::Logger::info( 'Provider not supported (' . $provider . ')' ); + # } + # else { + # Rex::Logger::info( + # 'OS not supported (' . $host->{operatingsystem} . ') ' ); + # } + # croak('OS/Provider not supported'); + # + #} + if ( !Class::Load::is_class_loaded($class) ) { + Rex::Logger::info("OS ($host->{operatingsystem}) not supported"); + exit 1; } return $class->new; diff --git a/lib/Rex/Pkg/Alpine.pm b/lib/Rex/Pkg/Alpine.pm new file mode 100644 index 000000000..b26681584 --- /dev/null +++ b/lib/Rex/Pkg/Alpine.pm @@ -0,0 +1,72 @@ +# +# (c) Pieter le Roux +# + +package Rex::Pkg::Alpine; + +use v5.14.4; +use warnings; + +our $VERSION = '9999.99.99_99'; # VERSION + +use Rex::Helper::Run; +use Rex::Commands::File; +use Rex::Commands::Fs; + +use Rex::Pkg::Base; +use base qw(Rex::Pkg::Base); + +sub new { + my $that = shift; + my $proto = ref($that) || $that; + my $self = $proto->SUPER::new(@_); + + bless $self, $proto; + + $self->{commands} = { + install => '/sbin/apk -q add %s', + update_system => '/sbin/apk -q upgrade', + remove => '/sbin/apk -q del %s', + update_package_db => '/sbin/apk -q update', + install_version => '/sbin/apk -q add %s=%s', + purge => '/sbin/apk -q --purge %s', + }; + + return $self; +} + +sub bulk_install { + my ( $self, $packages_aref, $option ) = @_; + + $self->update("@{$packages_aref}"); + + return 1; +} + +sub get_installed { + my ( $self, $pkg ) = @_; + my @pkgs; + my $cmd = 'apk -I list'; + if ($pkg) { + $cmd .= " $pkg"; + } + + my @lines = i_run $cmd; + + foreach my $line (@lines) { + my ( $xname, $arch ) = split /\s/sxm, $line; + if ( $xname =~ /^(.+)-(\d.+)/sxm ) { + push @pkgs, + { + name => $1, + version => $2, + architecture => $arch, + }; + } + } + + return @pkgs; +} + +1; + diff --git a/lib/Rex/Resource/firewall.pm b/lib/Rex/Resource/firewall.pm index 524e9e769..87cd04c98 100644 --- a/lib/Rex/Resource/firewall.pm +++ b/lib/Rex/Resource/firewall.pm @@ -103,7 +103,7 @@ resource "firewall", { export => 1 }, sub { }; my $provider = - param_lookup( "provider", case ( lc(operating_system), $__provider ) ); + param_lookup( "provider", case ( lc( operating_system() ), $__provider ) ); if ( $provider !~ m/::/ ) { $provider = "Rex::Resource::firewall::Provider::$provider"; diff --git a/lib/Rex/Service.pm b/lib/Rex/Service.pm index 9d85c91e5..251a611fc 100644 --- a/lib/Rex/Service.pm +++ b/lib/Rex/Service.pm @@ -6,6 +6,7 @@ package Rex::Service; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION @@ -16,6 +17,8 @@ use Rex::Hardware::Host; use Rex::Helper::Run; use Rex::Logger; +use Class::Load; + my %SERVICE_PROVIDER; sub register_service_provider { @@ -24,60 +27,68 @@ sub register_service_provider { return 1; } +## no critic (ProhibitExcessComplexity, ProhibitCascadingIfElse) + sub get { my $operatingsystem = Rex::Hardware::Host->get_operating_system(); - i_run "systemctl --no-pager > /dev/null", fail_ok => 1; - my $can_run_systemctl = $? == 0 ? 1 : 0; + my $r = i_run 'systemctl --no-pager > /dev/null', fail_ok => 1; + my $can_run_systemctl = $r == 0 ? 1 : 0; - i_run "initctl version | grep upstart", fail_ok => 1; - my $running_upstart = $? == 0 ? 1 : 0; + $r = i_run 'initctl version | grep upstart', fail_ok => 1; + my $running_upstart = $r == 0 ? 1 : 0; my $class; - $class = "Rex::Service::" . $operatingsystem; + $class = 'Rex::Service::' . $operatingsystem; if ( is_redhat($operatingsystem) && $can_run_systemctl ) { - $class = "Rex::Service::Redhat::systemd"; + $class = 'Rex::Service::Redhat::systemd'; } elsif ( is_redhat($operatingsystem) ) { # this also counts for fedora, centos, ... - $class = "Rex::Service::Redhat"; + $class = 'Rex::Service::Redhat'; } elsif ( is_suse($operatingsystem) && $can_run_systemctl ) { - $class = "Rex::Service::SuSE::systemd"; + $class = 'Rex::Service::SuSE::systemd'; } elsif ( is_alt($operatingsystem) && $can_run_systemctl ) { - $class = "Rex::Service::ALT::systemd"; + $class = 'Rex::Service::ALT::systemd'; } elsif ( is_gentoo($operatingsystem) && $can_run_systemctl ) { - $class = "Rex::Service::Gentoo::systemd"; + $class = 'Rex::Service::Gentoo::systemd'; } elsif ( is_gentoo($operatingsystem) ) { - $class = "Rex::Service::Gentoo"; + $class = 'Rex::Service::Gentoo'; } elsif ( is_mageia($operatingsystem) && $can_run_systemctl ) { - $class = "Rex::Service::Mageia::systemd"; + $class = 'Rex::Service::Mageia::systemd'; } elsif ( is_debian($operatingsystem) && $can_run_systemctl ) { # this also counts for Ubuntu and LinuxMint - $class = "Rex::Service::Debian::systemd"; + $class = 'Rex::Service::Debian::systemd'; } elsif ( is_debian($operatingsystem) && $running_upstart ) { # this is mainly Ubuntu with upstart - $class = "Rex::Service::Ubuntu"; + $class = 'Rex::Service::Ubuntu'; } elsif ( is_debian($operatingsystem) ) { - $class = "Rex::Service::Debian"; + $class = 'Rex::Service::Debian'; } elsif ( is_arch($operatingsystem) && $can_run_systemctl ) { - $class = "Rex::Service::Arch::systemd"; + $class = 'Rex::Service::Arch::systemd'; + } + elsif ( is_alpine($operatingsystem) ) { + $class = 'Rex::Service::Alpine'; + } + elsif ( is_voidlinux($operatingsystem) ) { + $class = 'Rex::Service::VoidLinux'; } - my $provider_for = Rex::Config->get("service_provider") || {}; + my $provider_for = Rex::Config->get('service_provider') || {}; my $provider; if ( ref($provider_for) && exists $provider_for->{$operatingsystem} ) { @@ -89,17 +100,18 @@ sub get { } Rex::Logger::debug("service using class: $class"); - eval "use $class"; - if ($@) { + # eval "use $class" = will no work! + if ( !Class::Load::is_class_loaded($class) ) { Rex::Logger::info("OS ($operatingsystem) not supported"); exit 1; - } return $class->new; } +## use critic + 1; diff --git a/lib/Rex/Service/Alpine.pm b/lib/Rex/Service/Alpine.pm new file mode 100644 index 000000000..1d4da5b77 --- /dev/null +++ b/lib/Rex/Service/Alpine.pm @@ -0,0 +1,37 @@ +# +# (c) Pieter le Roux +# + +package Rex::Service::Alpine; + +use v5.14.4; +use warnings; + +our $VERSION = '9999.99.99_99'; # VERSION + +use base qw(Rex::Service::Base); + +sub new { + my $that = shift; + my $proto = ref($that) || $that; + my $self = $proto->SUPER::new(@_); + + bless $self, $proto; + + #stop | start | restart | status | describe | zap $self->{commands} = { + $self->{commands} = { + start => '/etc/init.d/%s start', + restart => '/etc/init.d/%s restart', + stop => '/etc/init.d/%s stop', + reload => '/etc/init.d/%s stop && /etc/init.d/%s start', + status => '/etc/init.d/%s status', + ensure_stop => 'rc-update del %s', + ensure_start => 'rc-update add %s default', + action => '/etc/init.d/%s %s', + service_exists => 'rc-service --list | grep -i %s', + }; + + return $self; +} + +1; diff --git a/lib/Rex/User.pm b/lib/Rex/User.pm index 4098fe2e9..6ba56df0e 100644 --- a/lib/Rex/User.pm +++ b/lib/Rex/User.pm @@ -6,6 +6,7 @@ package Rex::User; use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION @@ -15,19 +16,19 @@ use Rex::Logger; sub get { my $user_o = "Linux"; - if (is_freebsd) { + if ( is_freebsd() ) { $user_o = "FreeBSD"; } - elsif (is_netbsd) { + elsif ( is_netbsd() ) { $user_o = "NetBSD"; } - elsif (is_openbsd) { + elsif ( is_openbsd() ) { $user_o = "OpenBSD"; } elsif ( operating_system_is("SunOS") ) { $user_o = "SunOS"; } - elsif (is_openwrt) { + elsif ( is_openwrt() ) { $user_o = "OpenWrt"; } diff --git a/t/file.t b/t/file.t index 9d3199d8e..bd5dd6b5d 100755 --- a/t/file.t +++ b/t/file.t @@ -2,6 +2,7 @@ use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION diff --git a/t/path.t b/t/path.t index 204915813..cd43629e3 100755 --- a/t/path.t +++ b/t/path.t @@ -2,12 +2,14 @@ use v5.14.4; use warnings; +use strict; our $VERSION = '9999.99.99_99'; # VERSION use Test::More tests => 4; use Test::Warnings; +use Rex::Commands::Gather; use Rex::Helper::Path; my $path = Rex::Helper::Path::resolv_path( "/home/foo/bar/baz", 1 );