File Coverage

blib/lib/Ixchel/functions/perl_module_via_pkg.pm
Criterion Covered Total %
statement 23 52 44.2
branch 0 14 0.0
condition 0 18 0.0
subroutine 8 9 88.8
pod 1 1 100.0
total 32 94 34.0


line stmt bran cond sub pod time code
1             package Ixchel::functions::perl_module_via_pkg;
2              
3 5     5   135614 use 5.006;
  5         23  
4 5     5   37 use strict;
  5         16  
  5         175  
5 5     5   46 use warnings;
  5         10  
  5         355  
6 5     5   31 use Exporter 'import';
  5         16  
  5         376  
7             our @EXPORT = qw(perl_module_via_pkg);
8 5     5   1694 use Rex -feature => [qw/1.4/];
  5         231237  
  5         67  
9 5     5   1128516 use Rex::Commands::Gather;
  5         10  
  5         43  
10 5     5   5670 use Rex::Commands::Pkg;
  5         20  
  5         46  
11 5     5   2430 use Ixchel::functions::status;
  5         20  
  5         3401  
12              
13             # prevents Rex from printing out rex is exiting after the script ends
14             $::QUIET = 2;
15              
16             =head1 NAME
17              
18             Ixchel::functions::perl_module_via_pkg - Tries to install a Perl module via the package manager.
19              
20             =head1 VERSION
21              
22             Version 0.0.1
23              
24             =cut
25              
26             our $VERSION = '0.0.1';
27              
28             =head1 SYNOPSIS
29              
30             use Ixchel::functions::perl_module_via_pkg;
31             use Data::Dumper;
32              
33             my $returned;
34             eval{
35             $returned=perl_module_via_pkg(module=>'Monitoring::Sneck');
36             };
37              
38             print Dumper($returned);
39              
40             Supported OS families are...
41              
42             Alt Linux
43             Arch Linux
44             Debian Linux
45             FreeBSD
46             Mageia Linux
47             NetBSD
48             OpenBSD
49             Redhat Linux
50             Suse Linux
51              
52             =head1 Functions
53              
54             =head2 perl_module_via_pkg
55              
56             The function that makes it so.
57              
58             - module :: The name of name of the module to install.
59              
60             =cut
61              
62             sub perl_module_via_pkg {
63 0     0 1   my (%opts) = @_;
64              
65 0 0         if ( !defined( $opts{module} ) ) {
66 0           die('Nothing specified for a module to install');
67             }
68              
69             # LWP is a bit name unfriendly for this given it is on cpan as libwww
70 0           my $lwp_modules = {
71             'LWP' => 1,
72             'LWP::ConnCache' => 1,
73             'LWP::Debug' => 1,
74             'LWP::MemberMixin' => 1,
75             'LWP::Protocol' => 1,
76             'LWP::RobotUA' => 1,
77             'LWP::Simple' => 1,
78             'LWP::UserAgent' => 1,
79             'LWP::Authen::Basic' => 1,
80             'LWP::Authen::Digest' => 1,
81             'LWP::Debug::TraceHTTP' => 1,
82             'LWP::DebugFile' => 1,
83             'LWP::Protocol::cpan' => 1,
84             'LWP::Protocol::data' => 1,
85             'LWP::Protocol::file' => 1,
86             'LWP::Protocol::ftp' => 1,
87             'LWP::Protocol::gopher' => 1,
88             'LWP::Protocol::http' => 1,
89             'LWP::Protocol::loopback' => 1,
90             'LWP::Protocol::mailto' => 1,
91             'LWP::Protocol::nntp' => 1,
92             'LWP::Protocol::nogo' => 1,
93             };
94              
95 0           my $pkg = $opts{module};
96 0           my @pkg_alts;
97              
98 0           my $type = 'perl_module_via_pkg';
99              
100             my $status = status(
101             type => $type,
102             error => 0,
103             status => 'Trying to install Perl module ' . $opts{module}
104 0           );
105              
106 0 0 0       if ( is_freebsd || is_netbsd || is_freebsd ) {
    0 0        
    0 0        
      0        
      0        
      0        
107 0           $status = $status
108             . status( type => $type, error => 0, status => 'OS Family FreeBSD, NetBSD, or OpenBSD detectected' );
109 0 0         if ($lwp_modules->{$pkg}) {
110 0           $pkg='p5-libwww';
111             }else {
112 0           $pkg =~ s/^/p5\-/;
113 0           $pkg =~ s/\:\:/\-/g;
114             }
115             } elsif (is_debian) {
116 0           $status = $status . status( type => $type, error => 0, status => 'OS Family Debian detectected' );
117 0 0         if ($lwp_modules->{$pkg}) {
118 0           $pkg='libwww-perl';
119             }else {
120 0           $pkg =~ s/\:\:/\-/g;
121 0           $pkg = 'lib' . lc($pkg) . '-perl';
122             }
123             } elsif ( is_redhat || is_arch || is_suse || is_alt || is_mageia ) {
124 0           $status = $status
125             . status( type => $type, error => 0, status => 'OS Family Redhat, Arch, Suse, Alt, or Mageia detectected' );
126 0           $pkg =~ s/\:\:/\-/g;
127 0           $pkg = 'perl-' . $pkg;
128             }
129              
130 0           $status = $status . status( type => $type, error => 0, status => 'Ensuring package "' . $pkg . '" is present' );
131              
132 0           eval { pkg( $pkg, ensure => 'present' ); };
  0            
133 0 0         if ($@) {
134 0           die( $status . "\n"
135             . status( type => $type, error => 1, status => 'Installing package "' . $pkg . '" failed ... ' . $@ ) );
136             }
137              
138 0           $status = $status . status( type => $type, error => 0, status => 'Package "' . $pkg . '" is present' );
139              
140 0           return 1;
141             } ## end sub perl_module_via_pkg
142              
143             1;