lib/Rex/Pkg/Base.pm

filter: Statementstmt 38.5% 59 / 153 Branchbran 15.3% 8 / 52 Conditioncond 30.7% 4 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 47.0% 8 / 17 Podpod 0.0% 0 / 12 totaltotal 30.9% 79 / 255 CC 36 SCAR 32.9
CC
36
Cov
33%
update_system
90.0
is_installed
42.0
remove
20.0
CRAP 26.9
« lib/Rex/TaskList/Base.pm ↑ Summary lib/Rex/Commands/Cloud.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Pkg::Base;
6  
7 2 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Net::OpenSSH::ShellQuoter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '1.16.1'; # VERSION
14  
15 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
16 2 my $that = shift;
17 2 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
18 2 my $self = {@_};
19  
20 2 bless( $self, $proto );
21  
22 2 return $self;
23 }
24  
25 0 ▶sub is_installed {
Subroutine is_installed: not called CC=6 SCAR=37.4
Pod undocumented
26  
27 0 my ( $self, $pkg, $option ) = @_;
28 0 my $version = $option->{version};
29  
30 0 ▶ Rex::Logger::debug(
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 0 0 0
31 "Checking if $pkg" . ( $version ? "-$version" : "" ) . " is installed" );
32  
33 0 my @pkg_info = grep { $_->{name} eq $pkg } $self->get_installed();
34 0 ▶ @pkg_info = grep { $_->{version} eq $version } @pkg_info if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 0 0
35  
36 0 ▶ unless (@pkg_info) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@pkg_info) 0 0 0
37 0 ▶ Rex::Logger::debug(
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 0 0 0
38 "$pkg" . ( $version ? "-$version" : "" ) . " is NOT installed." );
39 0 return 0;
40 }
41  
42 Rex::Logger::debug(
43 0 ▶ "$pkg" . ( $version ? "-$version" : "" ) . " is installed." );
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 0 0 0
44 0 return 1;
45  
46 }
47  
48 0 ▶sub install {
Subroutine install: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my ( $self, $pkg, $option ) = @_;
50  
51 0 ▶ if ( $self->is_installed( $pkg, $option ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_installed($pkg, $option)) 0 0 0
52 0 Rex::Logger::info("$pkg is already installed");
53 0 return 1;
54 }
55  
56 0 $self->update( $pkg, $option );
57  
58 0 return 1;
59 }
60  
61 1 ▶sub update {
Subroutine update: called CC=8 SCAR=26.5
Pod undocumented
62 1 my ( $self, $pkg, $option ) = @_;
63  
64 1 ▶ my $version = $option->{'version'} || '';
Condition50%1/2
$option->{'version'} || ''
l!l
01
MC/DC0%0/1
$option->{'version'} || ''
$option->{'version'}
Truth table50%1/2
A
A= $option->{'version'}
Aresult
11
00
65 1 ▶ my $env = $option->{'env'} || {};
Condition50%1/2
$option->{'env'} || {}
l!l
01
MC/DC0%0/1
$option->{'env'} || {}
$option->{'env'}
Truth table50%1/2
A
A= $option->{'env'}
Aresult
11
00
66  
67 1 ▶ Rex::Logger::debug( "Installing $pkg" . ( $version ? "-$version" : "" ) );
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 0 1 1
68 1 my $cmd;
69 1 ▶ if ( ( $pkg =~ /\*/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ /\*/u and defined $self->{'commands'}{'install_glob'}) { } 0 1 1
Condition33%1/3
$pkg =~ /\*/u and defined $self->{"commands"}{"install_glob"}
!ll&&!rl&&r
100
MC/DC0%0/2
$pkg =~ /\*/u and defined $self->{"commands"}{"install_glob"}
$pkg =~ /\*/u defined $self->{"commands"}{"install_glob"}
Truth table33%1/3
A and B
A= $pkg =~ /\*/uB= defined $self->{"commands"}{"install_glob"}
ABresult
0X0
100
111
70 && defined( $self->{commands}->{install_glob} ) )
71 {
72  
73 # quote the pkg name so it won't error when ran
74 0 my $exec = Rex::Interface::Exec->create;
75 0 my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
76 0 $pkg = $quoter->quote($pkg);
77 0 $cmd = sprintf $self->{commands}->{install_glob}, $pkg;
78 }
79 else {
80 1 $cmd = sprintf $self->{commands}->{install}, $pkg;
81  
82 # not compatible with globs, so skip over this
83 1 ▶ if ( exists $option->{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"version"}) 0 1 1
84 $cmd = sprintf $self->{commands}->{install_version}, $pkg,
85 0 $option->{version};
86 }
87 }
88  
89 1 my $f = i_run $cmd, fail_ok => 1, env => $env;
90  
91 1 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 1 1
92 0 Rex::Logger::info( "Error installing $pkg.", "warn" );
93 0 Rex::Logger::debug($f);
94 0 die("Error installing $pkg");
95 }
96  
97 1 Rex::Logger::debug("$pkg successfully installed.");
98  
99 1 return 1;
100 }
101  
102 0 ▶sub update_system {
Subroutine update_system: not called CC=9 SCAR=45.0
Pod undocumented
103 0 my ( $self, %option ) = @_;
104  
105 # default is to update packages
106 0 ▶ $option{update_packages} = 1 if ( !exists $option{update_packages} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{"update_packages"} 0 0 0
107  
108 0 ▶ if ( !exists $self->{commands}->{update_system} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"commands"}{"update_system"}) 0 0 0
109 0 Rex::Logger::debug("Not supported under this OS");
110 0 return;
111 }
112  
113 0 ▶ if ( $option{update_metadata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"update_metadata"}) 0 0 0
114 0 $self->update_pkg_db(%option);
115 }
116  
117 0 ▶ if ( $option{update_packages} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"update_packages"}) 0 0 0
118 0 my $cmd = $self->{commands}->{update_system};
119 0 my $f = i_run $cmd, fail_ok => 1;
120  
121 0 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 0 0
122 0 Rex::Logger::debug($f);
123 0 die("Error updating system");
124 }
125 }
126  
127 0 ▶ if ( $option{dist_upgrade} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"dist_upgrade"}) 0 0 0
128 0 ▶ if ( !exists $self->{commands}->{dist_update_system} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'commands'}{'dist_update_system'}) { } 0 0 0
129 0 Rex::Logger::debug("dist upgrades not supported under this OS");
130 }
131 else {
132 0 my $cmd = $self->{commands}->{dist_update_system};
133 0 my $f = i_run $cmd, fail_ok => 1;
134  
135 0 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 0 0
136 0 Rex::Logger::debug($f);
137 0 die("Error dist-updating system");
138 }
139 }
140 }
141  
142 0 Rex::Logger::debug("System successfully updated.");
143  
144 0 return 1;
145 }
146  
147 0 ▶sub remove {
Subroutine remove: not called CC=4 SCAR=30.0
Pod undocumented
148 0 my ( $self, $pkg ) = @_;
149  
150 0 Rex::Logger::debug("Removing $pkg");
151 0 my $cmd;
152 0 ▶ if ( ( $pkg =~ /\*/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ /\*/u and defined $self->{'commands'}{'remove_glob'}) { } 0 0 0
Condition0%0/3
$pkg =~ /\*/u and defined $self->{"commands"}{"remove_glob"}
!ll&&!rl&&r
000
MC/DC0%0/2
$pkg =~ /\*/u and defined $self->{"commands"}{"remove_glob"}
$pkg =~ /\*/u defined $self->{"commands"}{"remove_glob"}
Truth table0%0/3
A and B
A= $pkg =~ /\*/uB= defined $self->{"commands"}{"remove_glob"}
ABresult
0X0
100
111
153 && defined( $self->{commands}->{remove_glob} ) )
154 {
155  
156 # quote the pkg name so it won't error when ran
157 0 my $exec = Rex::Interface::Exec->create;
158 0 my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
159 0 $pkg = $quoter->quote($pkg);
160 0 $cmd = sprintf $self->{commands}->{remove_glob}, $pkg;
161 }
162 else {
163 0 $cmd = sprintf $self->{commands}->{remove}, $pkg;
164 }
165  
166 0 my $f = i_run $cmd, fail_ok => 1;
167  
168 0 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 0 0
169 0 Rex::Logger::info( "Error removing $pkg.", "warn" );
170 0 Rex::Logger::debug($f);
171 0 die("Error removing $pkg");
172 }
173  
174 0 Rex::Logger::debug("$pkg successfully removed.");
175  
176 0 return 1;
177 }
178  
179 0 ▶sub purge {
Subroutine purge: not called CC=3 SCAR=24.8
Pod undocumented
180 0 my ( $self, $pkg ) = @_;
181 0 ▶ return 1 if ( !exists $self->{commands}->{purge} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"commands"}{"purge"} 0 0 0
182 0 Rex::Logger::debug("Purging $pkg");
183 0 my $cmd = sprintf $self->{commands}->{purge}, $pkg;
184  
185 0 my $f = i_run $cmd, fail_ok => 1;
186  
187 0 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 0 0
188 0 Rex::Logger::info( "Error purging $pkg.", "warn" );
189 0 Rex::Logger::debug($f);
190 0 die("Error purging $pkg");
191 }
192  
193 0 Rex::Logger::debug("$pkg successfully purged.");
194  
195 0 return 1;
196 }
197  
198 0 ▶sub update_pkg_db {
Subroutine update_pkg_db: not called CC=3 SCAR=24.8
Pod undocumented
199 0 my ( $self, %option ) = @_;
200  
201 0 ▶ if ( !exists $self->{commands}->{update_package_db} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"commands"}{"update_package_db"}) 0 0 0
202 0 Rex::Logger::debug("Not supported under this OS");
203 0 return;
204 }
205  
206 0 my $cmd = $self->{commands}->{update_package_db};
207 0 i_run $cmd, fail_ok => 1;
208 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
209 0 die("Error updating package database");
210 }
211 }
212  
213 0 ▶sub bulk_install {
Subroutine bulk_install: not called CC=2 SCAR=17.9
Pod undocumented
214 0 Rex::Logger::info(
215 "Installing bulk packages not supported on this platform. Falling back to one by one method",
216 "warn"
217 );
218  
219 0 my ( $self, $packages_aref, $option ) = @_;
220 0 for my $pkg_to_install ( @{$packages_aref} ) {
221 0 $self->install( $pkg_to_install, $option );
222 }
223  
224 0 return 1;
225 }
226  
227 0 ▶sub add_repository {
Subroutine add_repository: not called CC=1 SCAR=6.9
Pod undocumented
228 0 my ( $self, %data ) = @_;
229 0 Rex::Logger::debug("Not supported under this OS");
230 }
231  
232 0 ▶sub rm_repository {
Subroutine rm_repository: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my ( $self, $name ) = @_;
234 0 Rex::Logger::debug("Not supported under this OS");
235 }
236  
237 1 ▶sub diff_package_list {
Subroutine diff_package_list: called CC=6 SCAR=17.9
Pod undocumented
238 1 my ( $self, $list1, $list2 ) = @_;
239  
240 1 my @old_installed = @{$list1};
241 1 my @new_installed = @{$list2};
242  
243 1 my @modifications;
244  
245 # getting modifications of old packages
246 OLD_PKG:
247 1 for my $old_pkg (@old_installed) {
248 NEW_PKG:
249 3 for my $new_pkg (@new_installed) {
250 6 ▶ if ( $old_pkg->{name} eq $new_pkg->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_pkg->{"name"} eq $new_pkg->{"name"}) 2 4 6
251  
252 # flag the package as found in new package list,
253 # to find removed and new ones.
254 2 $old_pkg->{found} = 1;
255 2 $new_pkg->{found} = 1;
256  
257 2 ▶ if ( $old_pkg->{version} ne $new_pkg->{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_pkg->{"version"} ne $new_pkg->{"version"}) 1 1 2
258 1 push @modifications, { %{$new_pkg}, action => 'updated' };
259 }
260 2 next OLD_PKG;
261 }
262 }
263 }
264  
265 # getting removed old packages
266 1 push @modifications, map { $_->{action} = 'removed'; $_ }
267 1 grep { !exists $_->{found} } @old_installed;
268  
269 # getting new packages
270 1 push @modifications, map { $_->{action} = 'installed'; $_ }
271 1 grep { !exists $_->{found} } @new_installed;
272  
273 1 for (@modifications) {
274 3 delete $_->{found};
275 }
276  
277 1 return @modifications;
278 }
279  
280 2 1;
« lib/Rex/TaskList/Base.pm ↑ Summary lib/Rex/Commands/Cloud.pm »