]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/APT.pm
implement pmgversion
[pmg-api.git] / PMG / API2 / APT.pm
1 package PMG::API2::APT;
2
3 use strict;
4 use warnings;
5
6 use POSIX;
7 use File::stat ();
8 use IO::File;
9 use File::Basename;
10 use JSON;
11 use LWP::UserAgent;
12
13 use PVE::Tools qw(extract_param);
14 use PVE::SafeSyslog;
15 use PVE::INotify;
16 use PVE::Exception;
17 use PVE::RESTHandler;
18 use PVE::JSONSchema qw(get_standard_option);
19
20 use PMG::RESTEnvironment;
21 use PMG::pmgcfg;
22 use PMG::Config;
23
24 use AptPkg::Cache;
25 use AptPkg::Version;
26 use AptPkg::PkgRecords;
27
28 my $get_apt_cache = sub {
29
30 my $apt_cache = AptPkg::Cache->new() || die "unable to initialize AptPkg::Cache\n";
31
32 return $apt_cache;
33 };
34
35 use base qw(PVE::RESTHandler);
36
37 __PACKAGE__->register_method({
38 name => 'index',
39 path => '',
40 method => 'GET',
41 description => "Directory index for apt (Advanced Package Tool).",
42 permissions => {
43 user => 'all',
44 },
45 parameters => {
46 additionalProperties => 0,
47 properties => {
48 node => get_standard_option('pve-node'),
49 },
50 },
51 returns => {
52 type => "array",
53 items => {
54 type => "object",
55 properties => {
56 id => { type => 'string' },
57 },
58 },
59 links => [ { rel => 'child', href => "{id}" } ],
60 },
61 code => sub {
62 my ($param) = @_;
63
64 my $res = [
65 { id => 'changelog' },
66 { id => 'update' },
67 { id => 'versions' },
68 ];
69
70 return $res;
71 }});
72
73 my $get_pkgfile = sub {
74 my ($veriter) = @_;
75
76 foreach my $verfile (@{$veriter->{FileList}}) {
77 my $pkgfile = $verfile->{File};
78 next if !$pkgfile->{Origin};
79 return $pkgfile;
80 }
81
82 return undef;
83 };
84
85 my $get_changelog_url =sub {
86 my ($pkgname, $info, $pkgver, $origin, $component) = @_;
87
88 my $changelog_url;
89 my $base = dirname($info->{FileName});
90 if ($origin && $base) {
91 $pkgver =~ s/^\d+://; # strip epoch
92 my $srcpkg = $info->{SourcePkg} || $pkgname;
93 if ($origin eq 'Debian') {
94 $base =~ s!pool/updates/!pool/!; # for security channel
95 $changelog_url = "http://packages.debian.org/changelogs/$base/" .
96 "${srcpkg}_${pkgver}/changelog";
97 } elsif ($origin eq 'Proxmox') {
98 if ($component eq 'pve-enterprise') {
99 $changelog_url = "https://enterprise.proxmox.com/debian/$base/" .
100 "${pkgname}_${pkgver}.changelog";
101 } else {
102 $changelog_url = "http://download.proxmox.com/debian/$base/" .
103 "${pkgname}_${pkgver}.changelog";
104 }
105 }
106 }
107
108 return $changelog_url;
109 };
110
111 my $assemble_pkginfo = sub {
112 my ($pkgname, $info, $current_ver, $candidate_ver) = @_;
113
114 my $data = {
115 Package => $info->{Name},
116 Title => $info->{ShortDesc},
117 Origin => 'unknown',
118 };
119
120 if (my $pkgfile = &$get_pkgfile($candidate_ver)) {
121 $data->{Origin} = $pkgfile->{Origin};
122 if (my $changelog_url = &$get_changelog_url($pkgname, $info, $candidate_ver->{VerStr},
123 $pkgfile->{Origin}, $pkgfile->{Component})) {
124 $data->{ChangeLogUrl} = $changelog_url;
125 }
126 }
127
128 if (my $desc = $info->{LongDesc}) {
129 $desc =~ s/^.*\n\s?//; # remove first line
130 $desc =~ s/\n / /g;
131 $data->{Description} = $desc;
132 }
133
134 foreach my $k (qw(Section Arch Priority)) {
135 $data->{$k} = $candidate_ver->{$k};
136 }
137
138 $data->{Version} = $candidate_ver->{VerStr};
139 $data->{OldVersion} = $current_ver->{VerStr} if $current_ver;
140
141 return $data;
142 };
143
144 # we try to cache results
145 my $pmg_pkgstatus_fn = "/var/lib/pmg/pkgupdates";
146
147 my $read_cached_pkgstatus = sub {
148 my $data = [];
149 eval {
150 my $jsonstr = PVE::Tools::file_get_contents($pmg_pkgstatus_fn, 5*1024*1024);
151 $data = decode_json($jsonstr);
152 };
153 if (my $err = $@) {
154 warn "error reading cached package status in $pmg_pkgstatus_fn\n";
155 }
156 return $data;
157 };
158
159 my $update_pmg_pkgstatus = sub {
160
161 syslog('info', "update new package list: $pmg_pkgstatus_fn");
162
163 my $notify_status = {};
164 my $oldpkglist = &$read_cached_pkgstatus();
165 foreach my $pi (@$oldpkglist) {
166 $notify_status->{$pi->{Package}} = $pi->{NotifyStatus};
167 }
168
169 my $pkglist = [];
170
171 my $cache = &$get_apt_cache();
172 my $policy = $cache->policy;
173 my $pkgrecords = $cache->packages();
174
175 foreach my $pkgname (keys %$cache) {
176 my $p = $cache->{$pkgname};
177 next if !$p->{SelectedState} || ($p->{SelectedState} ne 'Install');
178 my $current_ver = $p->{CurrentVer} || next;
179 my $candidate_ver = $policy->candidate($p) || next;
180
181 if ($current_ver->{VerStr} ne $candidate_ver->{VerStr}) {
182 my $info = $pkgrecords->lookup($pkgname);
183 my $res = &$assemble_pkginfo($pkgname, $info, $current_ver, $candidate_ver);
184 push @$pkglist, $res;
185
186 # also check if we need any new package
187 # Note: this is just a quick hack (not recursive as it should be), because
188 # I found no way to get that info from AptPkg
189 if (my $deps = $candidate_ver->{DependsList}) {
190 my $found;
191 my $req;
192 for my $d (@$deps) {
193 if ($d->{DepType} eq 'Depends') {
194 $found = $d->{TargetPkg}->{SelectedState} eq 'Install' if !$found;
195 $req = $d->{TargetPkg} if !$req;
196
197 if (!($d->{CompType} & AptPkg::Dep::Or)) {
198 if (!$found && $req) { # New required Package
199 my $tpname = $req->{Name};
200 my $tpinfo = $pkgrecords->lookup($tpname);
201 my $tpcv = $policy->candidate($req);
202 if ($tpinfo && $tpcv) {
203 my $res = &$assemble_pkginfo($tpname, $tpinfo, undef, $tpcv);
204 push @$pkglist, $res;
205 }
206 }
207 undef $found;
208 undef $req;
209 }
210 }
211 }
212 }
213 }
214 }
215
216 # keep notification status (avoid sending mails abou new packages more than once)
217 foreach my $pi (@$pkglist) {
218 if (my $ns = $notify_status->{$pi->{Package}}) {
219 $pi->{NotifyStatus} = $ns if $ns eq $pi->{Version};
220 }
221 }
222
223 PVE::Tools::file_set_contents($pmg_pkgstatus_fn, encode_json($pkglist));
224
225 return $pkglist;
226 };
227
228 __PACKAGE__->register_method({
229 name => 'list_updates',
230 path => 'update',
231 method => 'GET',
232 description => "List available updates.",
233 protected => 1,
234 proxyto => 'node',
235 parameters => {
236 additionalProperties => 0,
237 properties => {
238 node => get_standard_option('pve-node'),
239 },
240 },
241 returns => {
242 type => "array",
243 items => {
244 type => "object",
245 properties => {},
246 },
247 },
248 code => sub {
249 my ($param) = @_;
250
251 if (my $st1 = File::stat::stat($pmg_pkgstatus_fn)) {
252 my $st2 = File::stat::stat("/var/cache/apt/pkgcache.bin");
253 my $st3 = File::stat::stat("/var/lib/dpkg/status");
254
255 if ($st2 && $st3 && $st2->mtime <= $st1->mtime && $st3->mtime <= $st1->mtime) {
256 if (my $data = &$read_cached_pkgstatus()) {
257 return $data;
258 }
259 }
260 }
261
262 my $pkglist = &$update_pmg_pkgstatus();
263
264 return $pkglist;
265 }});
266
267 __PACKAGE__->register_method({
268 name => 'update_database',
269 path => 'update',
270 method => 'POST',
271 description => "This is used to resynchronize the package index files from their sources (apt-get update).",
272 protected => 1,
273 proxyto => 'node',
274 parameters => {
275 additionalProperties => 0,
276 properties => {
277 node => get_standard_option('pve-node'),
278 notify => {
279 type => 'boolean',
280 description => "Send notification mail about new packages (to email address specified for user 'root\@pam').",
281 optional => 1,
282 default => 0,
283 },
284 quiet => {
285 type => 'boolean',
286 description => "Only produces output suitable for logging, omitting progress indicators.",
287 optional => 1,
288 default => 0,
289 },
290 },
291 },
292 returns => {
293 type => 'string',
294 },
295 code => sub {
296 my ($param) = @_;
297
298 my $rpcenv = PMG::RESTEnvironment->get();
299
300 my $authuser = $rpcenv->get_user();
301
302 my $realcmd = sub {
303 my $upid = shift;
304
305 my $pmg_cfg = PMG::Config->new();
306
307 my $http_proxy = $pmg_cfg->get('admin', 'http_proxy');
308 my $aptconf = "// no proxy configured\n";
309 if ($http_proxy) {
310 $aptconf = "Acquire::http::Proxy \"${http_proxy}\";\n";
311 }
312 my $aptcfn = "/etc/apt/apt.conf.d/76pmgproxy";
313 PVE::Tools::file_set_contents($aptcfn, $aptconf);
314
315 my $cmd = ['apt-get', 'update'];
316
317 print "starting apt-get update\n" if !$param->{quiet};
318
319 if ($param->{quiet}) {
320 PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {});
321 } else {
322 PVE::Tools::run_command($cmd);
323 }
324
325 my $pkglist = &$update_pmg_pkgstatus();
326
327 if ($param->{notify} && scalar(@$pkglist)) {
328
329 my $mailfrom = "root";
330
331 if (my $mailto = $pmg_cfg->get('admin', 'email', 1)) {
332
333 my $text .= "The following updates are available:\n\n";
334
335 my $count = 0;
336 foreach my $p (sort {$a->{Package} cmp $b->{Package} } @$pkglist) {
337 next if $p->{NotifyStatus} && $p->{NotifyStatus} eq $p->{Version};
338 $count++;
339 if ($p->{OldVersion}) {
340 $text .= "$p->{Package}: $p->{OldVersion} ==> $p->{Version}\n";
341 } else {
342 $text .= "$p->{Package}: $p->{Version} (new)\n";
343 }
344 }
345
346 return if !$count;
347
348 my $hostname = `hostname -f` || PVE::INotify::nodename();
349 chomp $hostname;
350
351 my $subject = "New software packages available ($hostname)";
352 PVE::Tools::sendmail($mailto, $subject, $text, undef,
353 $mailfrom, 'Proxmox Mail Gateway');
354
355 foreach my $pi (@$pkglist) {
356 $pi->{NotifyStatus} = $pi->{Version};
357 }
358
359 PVE::Tools::file_set_contents($pmg_pkgstatus_fn, encode_json($pkglist));
360 }
361 }
362
363 return;
364 };
365
366 return $rpcenv->fork_worker('aptupdate', undef, $authuser, $realcmd);
367
368 }});
369
370 __PACKAGE__->register_method({
371 name => 'changelog',
372 path => 'changelog',
373 method => 'GET',
374 description => "Get package changelogs.",
375 proxyto => 'node',
376 parameters => {
377 additionalProperties => 0,
378 properties => {
379 node => get_standard_option('pve-node'),
380 name => {
381 description => "Package name.",
382 type => 'string',
383 },
384 version => {
385 description => "Package version.",
386 type => 'string',
387 optional => 1,
388 },
389 },
390 },
391 returns => {
392 type => "string",
393 },
394 code => sub {
395 my ($param) = @_;
396
397 my $pkgname = $param->{name};
398
399 my $cache = &$get_apt_cache();
400 my $policy = $cache->policy;
401 my $p = $cache->{$pkgname} || die "no such package '$pkgname'\n";
402 my $pkgrecords = $cache->packages();
403
404 my $ver;
405 if ($param->{version}) {
406 if (my $available = $p->{VersionList}) {
407 for my $v (@$available) {
408 if ($v->{VerStr} eq $param->{version}) {
409 $ver = $v;
410 last;
411 }
412 }
413 }
414 die "package '$pkgname' version '$param->{version}' is not avalable\n" if !$ver;
415 } else {
416 $ver = $policy->candidate($p) || die "no installation candidate for package '$pkgname'\n";
417 }
418
419 my $info = $pkgrecords->lookup($pkgname);
420
421 my $pkgfile = &$get_pkgfile($ver);
422 my $url;
423
424 die "changelog for '${pkgname}_$ver->{VerStr}' not available\n"
425 if !($pkgfile && ($url = &$get_changelog_url($pkgname, $info, $ver->{VerStr}, $pkgfile->{Origin}, $pkgfile->{Component})));
426
427 my $data = "";
428
429 my $pmg_cfg = PMG::Config->new();
430 my $proxy = $pmg_cfg->get('admin', 'http_proxy');
431
432 my $ua = LWP::UserAgent->new;
433 $ua->agent("PMG/1.0");
434 $ua->timeout(10);
435 $ua->max_size(1024*1024);
436 $ua->ssl_opts(verify_hostname => 0); # don't care for changelogs
437
438 if ($proxy) {
439 $ua->proxy(['http', 'https'], $proxy);
440 } else {
441 $ua->env_proxy;
442 }
443
444 my $username;
445 my $pw;
446
447 if ($pkgfile->{Origin} eq 'Proxmox' && $pkgfile->{Component} eq 'pmg-enterprise') {
448 my $info = PVE::INotify::read_file('subscription');
449 if ($info->{status} eq 'Active') {
450 $username = $info->{key};
451 $pw = PMG::Utils::get_hwaddress();
452 $ua->credentials("enterprise.proxmox.com:443", 'pmg-enterprise-repository',
453 $username, $pw);
454 }
455 }
456
457 syslog('info', "GET $url\n");
458 my $response = $ua->get($url);
459
460 if ($response->is_success) {
461 $data = $response->decoded_content;
462 } else {
463 PVE::Exception::raise($response->message, code => $response->code);
464 }
465
466 return $data;
467 }});
468
469 __PACKAGE__->register_method({
470 name => 'versions',
471 path => 'versions',
472 method => 'GET',
473 proxyto => 'node',
474 description => "Get package information for important Proxmox packages.",
475 permissions => {
476 check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
477 },
478 parameters => {
479 additionalProperties => 0,
480 properties => {
481 node => get_standard_option('pve-node'),
482 },
483 },
484 returns => {
485 type => "array",
486 items => {
487 type => "object",
488 properties => {},
489 },
490 },
491 code => sub {
492 my ($param) = @_;
493
494 my $pkgname = $param->{name};
495
496 my $cache = &$get_apt_cache();
497 my $policy = $cache->policy;
498 my $pkgrecords = $cache->packages();
499
500 # try to use a resonable ordering (most important things first)
501 my @list = qw(proxmox-mailgateway proxmox-mailgateway-gui proxmox-spamassassin proxmox-widget-toolkit);
502
503 foreach my $pkgname (keys %$cache) {
504 if ($pkgname =~ m/pve-kernel-/) {
505 my $p = $cache->{$pkgname};
506 push @list, $pkgname if $p && $p->{CurrentState} eq 'Installed';
507 }
508 }
509
510
511 my @opt_pack = ('zfsutils-linux', 'libpve-apiclient-perl');
512
513 push @list, qw(libpve-http-server-perl lvm2 pve-firmware libpve-common-perl vncterm pmg-docs novnc-pve libarchive-perl libxdgmime-perl );
514
515 @list = (@list, @opt_pack);
516 my $pkglist = [];
517
518 my (undef, undef, $kernel_release) = POSIX::uname();
519 my $pmgver = PMG::pmgcfg::version_text();
520
521 foreach my $pkgname (@list) {
522 my $p = $cache->{$pkgname};
523 my $info = $pkgrecords->lookup($pkgname);
524 my $candidate_ver = defined($p) ? $policy->candidate($p) : undef;
525 my $res;
526 if (my $current_ver = $p->{CurrentVer}) {
527 $res = &$assemble_pkginfo($pkgname, $info, $current_ver,
528 $candidate_ver || $current_ver);
529 } elsif ($candidate_ver) {
530 $res = &$assemble_pkginfo($pkgname, $info, $candidate_ver,
531 $candidate_ver);
532 delete $res->{OldVersion};
533 } else {
534 next;
535 }
536 $res->{CurrentState} = $p->{CurrentState};
537
538 # hack: add some useful information (used by 'pmgversion -v')
539 if ($pkgname eq 'proxmox-mailgateway-gui') {
540 $res->{ManagerVersion} = $pmgver;
541 } elsif ($pkgname eq 'proxmox-mailgateway') {
542 $res->{RunningKernel} = $kernel_release;
543 }
544
545 if (grep( /^$pkgname$/, @opt_pack)) {
546 next if $res->{CurrentState} eq 'NotInstalled';
547 }
548
549 push @$pkglist, $res;
550 }
551
552 return $pkglist;
553 }});
554
555 1;