-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pkg.pm
278 lines (210 loc) · 6.82 KB
/
Pkg.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
package Csistck::Test::Pkg;
use 5.010;
use strict;
use warnings;
use base 'Csistck::Test';
use Csistck::Oper qw/debug/;
use Csistck::Config qw/option/;
our @EXPORT_OK = qw/pkg/;
use Digest::MD5;
use File::Basename;
# Conditionally use linux-only modules
BEGIN {
if ("$^O" eq "linux") {
require Linux::Distribution;
}
}
our $Cmds = {
dpkg => {
check => 'dpkg -L "%s"',
diff => 'apt-get -s install "%s"',
install => 'apt-get -qq -y install "%s"'
},
rpm => {
check => 'rpm -q "%s"',
install => 'yum -q -y --noplugins install "%s"'
},
emerge => {
check => 'equery -qC list "%s"',
diff => 'emerge --color n -pq "%s"',
install => 'emerge --color n -q "%s"'
},
pacman => {
check => 'pacman -Qe "%s"',
install => 'pacman -Sq --noconfirm "%s"'
},
pkg_info => {
check => 'pkg_info -Qq "%s>0"'
}
};
=head1 NAME
Csistck::Test::Pkg - Csistck package check
=head1 METHODS
=head2 pkg($package, [$type])
Test for existing package using forks to system package managers. Package can be
specified as a string, or as a hashref:
pkg({
dpkg => 'test-server',
emerge => 'net-test',
default => 'test-server'
});
The package manager will be automatically detected if none is explicitly
specified, and the hashref key matching the package manager decides the package
name to check. If a default key is provided, that package is used by default.
In repair mode, install the package quietly, unless package manager doesn't
handle automating install.
Supported package managers:
=over
=item dpkg
Debian package management utility
=item pacman
Arch linux package management utility
=item More planned..
=back
=cut
sub pkg {
my ($pkg, $args) = @_;
# TODO detect assoc array
# If args is a hashref, deref and turn into an array, Csistck::Test
# processes arguments as named arguments -- an assoc array.
my $t_args = (ref($args) eq "HASH") ? [%{$args}] : [ 'type', $args ];
Csistck::Test::Pkg->new($pkg, @{$t_args});
}
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
bless($self, $class);
# We'll fix package name here, instead of redoing each call
# Priority: 'type' argument, 'pkg_type' option, detect_pkg_manager.
my $type = $self->{type};
if (! $type) {
$type = option('pkg_type') // detect_pkg_manager();
}
die("Unsupported package manager or OS: manager=<none>")
if (! $type);
die("Package manager not supported: type=<$type>")
if (! $Cmds->{$type});
$self->{type} = $type;
return $self;
}
sub desc {
return sprintf("Package test for %s, using %s",
$_[0]->pkg_name, $_[0]->pkg_type);
}
sub check {
my $self = shift;
my $pkg = $self->pkg_name;
my $type = $self->pkg_type;
my $cmd = sprintf($Cmds->{$type}->{check}, $pkg) or
die("Package check command missing: type=<$type>");
debug("Searching for package via command: cmd=<$cmd>");
my $ret = system("$cmd 1>/dev/null 2>/dev/null");
die("Package missing")
unless($ret == 0);
}
sub repair {
my $self = shift;
my $pkg = $self->pkg_name;
my $type = $self->pkg_type;
my $cmd;
if (defined $Cmds->{$type}->{install}) {
$cmd = sprintf($Cmds->{$type}->{install}, $pkg);
}
else {
die("Package install command missing: type=<$type>");
}
$ENV{DEBIAN_FRONTEND} = "noninteractive";
debug("Installing package via command: cmd=<$cmd>");
my $ret = system("$cmd 1>/dev/null 2>/dev/null");
die("Package installation failed")
unless ($ret == 0);
}
# Package diff
sub diff {
my $self = shift;
my $pkg = $self->pkg_name;
my $type = $self->pkg_type;
my $cmd;
if (defined $Cmds->{$type}->{diff}) {
$cmd = sprintf($Cmds->{$type}->{diff}, $pkg);
}
else {
die("Package diff command missing: type=<$type>");
}
$ENV{DEBIAN_FRONTEND} = "noninteractive";
debug("Showing package differences via command: cmd=<$cmd>");
my $ret = system("$cmd 2>/dev/null");
die("Package differences query failed")
unless ($ret == 0);
}
=head2 detect_pkg_manager()
Detect package manager based on system OS and Linux distribution if
applicable. Return package manager as string. This is not exported, it is
used for the package test.
=cut
sub detect_pkg_manager {
my $self = shift;
given ("$^O") {
when (/^freebsd$/) { return 'pkg_info'; }
when (/^netbsd$/) { return 'pkg_info'; }
when (/^linux$/) {
given (Linux::Distribution::distribution_name()) {
when (/^(?:debian|ubuntu)$/) { return 'dpkg'; }
when (/^(?:fedora|redhat|centos)$/) { return 'rpm'; }
when (/^gentoo$/) { return 'emerge'; }
when (/^arch$/) { return 'pacman'; }
default { return undef; }
}
}
when (/^darwin$/) { return undef; }
default { return undef; }
}
}
=head2 pkg_name($package, $type)
Based on input package, return package name. With OS and distribution detection,
$package can be passed as a string or a hashref.
See pkg() for information on passing hashrefs as package name
=cut
sub pkg_name {
my $self = shift;
my $pkg = $self->{target};
my $type = $self->{type};
given (ref $pkg) {
when ('') {
return $pkg if ($pkg =~ m/^[A-Za-z0-9\-\_\.\/]+$/);
}
when ('HASH') {
my $pkg_name = $pkg->{$type} // $pkg->{default};
return $pkg_name if ($pkg_name =~ m/^[A-Za-z0-9\-\_\.\/]+$/);
}
}
die('Invalid package');
}
=head2 pkg_type()
Return fixed package type
=cut
sub pkg_type { $_[0]->{type}; }
1;
__END__
=head1 OPTIONS
=over
=item pkg_type [string]
Set the default package type
=back
=head1 AUTHOR
Anthony Johnson, C<< <aj@ohess.org> >>
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2012 Anthony Johnson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,