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