]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/APT.pm
1 package PMG
::API2
::APT
;
13 use PVE
::Tools
qw(extract_param);
18 use PVE
::JSONSchema
qw(get_standard_option);
20 use PMG
::RESTEnvironment
;
26 use AptPkg
::PkgRecords
;
28 my $get_apt_cache = sub {
30 my $apt_cache = AptPkg
::Cache-
>new() || die "unable to initialize AptPkg::Cache\n";
35 use base
qw(PVE::RESTHandler);
37 __PACKAGE__-
>register_method({
41 description
=> "Directory index for apt (Advanced Package Tool).",
46 additionalProperties
=> 0,
48 node
=> get_standard_option
('pve-node'),
56 id
=> { type
=> 'string' },
59 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
65 { id
=> 'changelog' },
73 my $get_pkgfile = sub {
76 foreach my $verfile (@{$veriter->{FileList
}}) {
77 my $pkgfile = $verfile->{File
};
78 next if !$pkgfile->{Origin
};
85 my $get_changelog_url =sub {
86 my ($pkgname, $info, $pkgver, $origin, $component) = @_;
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 'pmg-enterprise') {
99 $changelog_url = "https://enterprise.proxmox.com/debian/pmg/$base/" .
100 "${pkgname}_${pkgver}.changelog";
102 $changelog_url = "http://download.proxmox.com/debian/pmg/$base/" .
103 "${pkgname}_${pkgver}.changelog";
108 return $changelog_url;
111 my $assemble_pkginfo = sub {
112 my ($pkgname, $info, $current_ver, $candidate_ver) = @_;
115 Package
=> $info->{Name
},
116 Title
=> $info->{ShortDesc
},
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;
128 if (my $desc = $info->{LongDesc
}) {
129 $desc =~ s/^.*\n\s?//; # remove first line
131 $data->{Description
} = $desc;
134 foreach my $k (qw(Section Arch Priority)) {
135 $data->{$k} = $candidate_ver->{$k};
138 $data->{Version
} = $candidate_ver->{VerStr
};
139 $data->{OldVersion
} = $current_ver->{VerStr
} if $current_ver;
144 # we try to cache results
145 my $pmg_pkgstatus_fn = "/var/lib/pmg/pkgupdates";
147 my $read_cached_pkgstatus = sub {
150 my $jsonstr = PVE
::Tools
::file_get_contents
($pmg_pkgstatus_fn, 5*1024*1024);
151 $data = decode_json
($jsonstr);
154 warn "error reading cached package status in $pmg_pkgstatus_fn\n";
159 my $update_pmg_pkgstatus = sub {
161 syslog
('info', "update new package list: $pmg_pkgstatus_fn");
163 my $notify_status = {};
164 my $oldpkglist = &$read_cached_pkgstatus();
165 foreach my $pi (@$oldpkglist) {
166 $notify_status->{$pi->{Package
}} = $pi->{NotifyStatus
};
171 my $cache = &$get_apt_cache();
172 my $policy = $cache->policy;
173 my $pkgrecords = $cache->packages();
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;
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;
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
}) {
193 if ($d->{DepType
} eq 'Depends') {
194 $found = $d->{TargetPkg
}->{SelectedState
} eq 'Install' if !$found;
195 $req = $d->{TargetPkg
} if !$req;
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;
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
};
223 PVE
::Tools
::file_set_contents
($pmg_pkgstatus_fn, encode_json
($pkglist));
228 __PACKAGE__-
>register_method({
229 name
=> 'list_updates',
232 description
=> "List available updates.",
235 permissions
=> { check
=> [ 'admin', 'audit' ] },
237 additionalProperties
=> 0,
239 node
=> get_standard_option
('pve-node'),
252 if (my $st1 = File
::stat::stat($pmg_pkgstatus_fn)) {
253 my $st2 = File
::stat::stat("/var/cache/apt/pkgcache.bin");
254 my $st3 = File
::stat::stat("/var/lib/dpkg/status");
256 if ($st2 && $st3 && $st2->mtime <= $st1->mtime && $st3->mtime <= $st1->mtime) {
257 if (my $data = &$read_cached_pkgstatus()) {
263 my $pkglist = &$update_pmg_pkgstatus();
268 __PACKAGE__-
>register_method({
269 name
=> 'update_database',
272 description
=> "This is used to resynchronize the package index files from their sources (apt-get update).",
275 permissions
=> { check
=> [ 'admin' ] },
277 additionalProperties
=> 0,
279 node
=> get_standard_option
('pve-node'),
282 description
=> "Send notification mail about new packages (to email address specified for user 'root\@pam').",
288 description
=> "Only produces output suitable for logging, omitting progress indicators.",
300 my $rpcenv = PMG
::RESTEnvironment-
>get();
302 my $authuser = $rpcenv->get_user();
307 my $pmg_cfg = PMG
::Config-
>new();
309 my $http_proxy = $pmg_cfg->get('admin', 'http_proxy');
310 my $aptconf = "// no proxy configured\n";
312 $aptconf = "Acquire::http::Proxy \"${http_proxy}\";\n";
314 my $aptcfn = "/etc/apt/apt.conf.d/76pmgproxy";
315 PVE
::Tools
::file_set_contents
($aptcfn, $aptconf);
317 my $cmd = ['apt-get', 'update'];
319 print "starting apt-get update\n" if !$param->{quiet
};
321 if ($param->{quiet
}) {
322 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {});
324 PVE
::Tools
::run_command
($cmd);
327 my $pkglist = &$update_pmg_pkgstatus();
329 if ($param->{notify
} && scalar(@$pkglist)) {
331 my $mailfrom = "root";
333 if (my $mailto = $pmg_cfg->get('admin', 'email', 1)) {
335 my $text .= "The following updates are available:\n\n";
338 foreach my $p (sort {$a->{Package
} cmp $b->{Package
} } @$pkglist) {
339 next if $p->{NotifyStatus
} && $p->{NotifyStatus
} eq $p->{Version
};
341 if ($p->{OldVersion
}) {
342 $text .= "$p->{Package}: $p->{OldVersion} ==> $p->{Version}\n";
344 $text .= "$p->{Package}: $p->{Version} (new)\n";
350 my $hostname = `hostname -f` || PVE
::INotify
::nodename
();
353 my $subject = "New software packages available ($hostname)";
354 PVE
::Tools
::sendmail
($mailto, $subject, $text, undef,
355 $mailfrom, 'Proxmox Mail Gateway');
357 foreach my $pi (@$pkglist) {
358 $pi->{NotifyStatus
} = $pi->{Version
};
361 PVE
::Tools
::file_set_contents
($pmg_pkgstatus_fn, encode_json
($pkglist));
368 return $rpcenv->fork_worker('aptupdate', undef, $authuser, $realcmd);
372 __PACKAGE__-
>register_method({
376 description
=> "Get package changelogs.",
378 permissions
=> { check
=> [ 'admin', 'audit' ] },
380 additionalProperties
=> 0,
382 node
=> get_standard_option
('pve-node'),
384 description
=> "Package name.",
388 description
=> "Package version.",
400 my $pkgname = $param->{name
};
402 my $cache = &$get_apt_cache();
403 my $policy = $cache->policy;
404 my $p = $cache->{$pkgname} || die "no such package '$pkgname'\n";
405 my $pkgrecords = $cache->packages();
408 if ($param->{version
}) {
409 if (my $available = $p->{VersionList
}) {
410 for my $v (@$available) {
411 if ($v->{VerStr
} eq $param->{version
}) {
417 die "package '$pkgname' version '$param->{version}' is not avalable\n" if !$ver;
419 $ver = $policy->candidate($p) || die "no installation candidate for package '$pkgname'\n";
422 my $info = $pkgrecords->lookup($pkgname);
424 my $pkgfile = &$get_pkgfile($ver);
427 die "changelog for '${pkgname}_$ver->{VerStr}' not available\n"
428 if !($pkgfile && ($url = &$get_changelog_url($pkgname, $info, $ver->{VerStr
}, $pkgfile->{Origin
}, $pkgfile->{Component
})));
432 my $pmg_cfg = PMG
::Config-
>new();
433 my $proxy = $pmg_cfg->get('admin', 'http_proxy');
435 my $ua = LWP
::UserAgent-
>new;
436 $ua->agent("PMG/1.0");
438 $ua->max_size(1024*1024);
439 $ua->ssl_opts(verify_hostname
=> 0); # don't care for changelogs
442 $ua->proxy(['http', 'https'], $proxy);
450 if ($pkgfile->{Origin
} eq 'Proxmox' && $pkgfile->{Component
} eq 'pmg-enterprise') {
451 my $info = PVE
::INotify
::read_file
('subscription');
452 if ($info->{status
} eq 'Active') {
453 $username = $info->{key
};
454 $pw = PMG
::Utils
::get_hwaddress
();
455 $ua->credentials("enterprise.proxmox.com:443", 'pmg-enterprise-repository',
460 syslog
('info', "GET $url\n");
461 my $response = $ua->get($url);
463 if ($response->is_success) {
464 $data = $response->decoded_content;
466 PVE
::Exception
::raise
($response->message, code
=> $response->code);
472 __PACKAGE__-
>register_method({
477 description
=> "Get package information for important Proxmox packages.",
478 permissions
=> { check
=> [ 'admin', 'audit' ] },
480 additionalProperties
=> 0,
482 node
=> get_standard_option
('pve-node'),
495 my $cache = &$get_apt_cache();
496 my $policy = $cache->policy;
497 my $pkgrecords = $cache->packages();
499 # order most important things first
500 my @list = qw(proxmox-mailgateway pmg-api pmg-gui);
502 my $aptver = $AptPkg::System
::_system-
>versioning();
503 my $byver = sub { $aptver->compare($cache->{$b}->{CurrentVer
}->{VerStr
}, $cache->{$a}->{CurrentVer
}->{VerStr
}) };
504 push @list, sort $byver grep { /^pve-kernel-/ && $cache->{$_}->{CurrentState
} eq 'Installed' } keys %$cache;
507 libpve-apiclient-perl
508 proxmox-mailgateway-container
518 libpve-http-server-perl
522 proxmox-mini-journalreader
524 proxmox-widget-toolkit
529 push @list, (sort @pkgs, @opt_pack);
531 my (undef, undef, $kernel_release) = POSIX
::uname
();
532 my $pmgver = PMG
::pmgcfg
::version_text
();
535 foreach my $pkgname (@list) {
536 my $p = $cache->{$pkgname};
537 my $info = $pkgrecords->lookup($pkgname);
538 my $candidate_ver = defined($p) ?
$policy->candidate($p) : undef;
540 if (my $current_ver = $p->{CurrentVer
}) {
541 $res = &$assemble_pkginfo($pkgname, $info, $current_ver,
542 $candidate_ver || $current_ver);
543 } elsif ($candidate_ver) {
544 $res = &$assemble_pkginfo($pkgname, $info, $candidate_ver,
546 delete $res->{OldVersion
};
550 $res->{CurrentState
} = $p->{CurrentState
};
552 if (grep( /^$pkgname$/, @opt_pack)) {
553 next if $res->{CurrentState
} eq 'NotInstalled';
556 # hack: add some useful information (used by 'pmgversion -v')
557 if ($pkgname =~ /^proxmox-mailgateway(-container)?$/) {
558 $res->{ManagerVersion
} = $pmgver;
559 $res->{RunningKernel
} = $kernel_release;
560 if ($pkgname eq 'proxmox-mailgateway-container') {
561 # another hack: replace proxmox-mailgateway with CT meta pkg
563 unshift @$pkglist, $res;
568 push @$pkglist, $res;