]> git.proxmox.com Git - pve-manager.git/blame - PVE/API2/APT.pm
add API for apt using libapt-pkg-perl
[pve-manager.git] / PVE / API2 / APT.pm
CommitLineData
21299915
DM
1package PVE::API2::APT;
2
3use strict;
4use warnings;
5
6use PVE::Tools qw(extract_param);
7use PVE::SafeSyslog;
8use PVE::INotify;
9use PVE::Exception qw(raise_param_exc);
10use PVE::RESTHandler;
11use PVE::RPCEnvironment;
12
13use PVE::JSONSchema qw(get_standard_option);
14
15use AptPkg::Cache;
16use AptPkg::Version;
17use AptPkg::PkgRecords;
18
19my $apt_cache;
20
21my $get_apt_cache = sub {
22
23 return $apt_cache if $apt_cache;
24
25 $apt_cache = AptPkg::Cache->new() || die "unable to initialize AptPkg::Cache\n";
26
27 return $apt_cache;
28};
29
30use base qw(PVE::RESTHandler);
31
32__PACKAGE__->register_method({
33 name => 'index',
34 path => '',
35 method => 'GET',
36 description => "Directory index for apt (Advanced Package Tool).",
37 permissions => {
38 user => 'all',
39 },
40 parameters => {
41 additionalProperties => 0,
42 properties => {
43 node => get_standard_option('pve-node'),
44 },
45 },
46 returns => {
47 type => "array",
48 items => {
49 type => "object",
50 properties => {
51 id => { type => 'string' },
52 },
53 },
54 links => [ { rel => 'child', href => "{id}" } ],
55 },
56 code => sub {
57 my ($param) = @_;
58
59 my $res = [
60 { id => 'update' },
61 { id => 'upgrade' },
62 { id => 'changelog' },
63 ];
64
65 return $res;
66 }});
67
68my $assemble_pkginfo = sub {
69 my ($pkgname, $info, $current_ver, $candidate_ver) = @_;
70
71 my $data = {
72 Package => $info->{Name},
73 Title => $info->{ShortDesc},
74 };
75
76 if (my $desc = $info->{LongDesc}) {
77 $desc =~ s/^.*\n\s?//; # remove first line
78 $desc =~ s/\n / /g;
79 $data->{Description} = $desc;
80 }
81
82 foreach my $k (qw(Section Arch Priority)) {
83 $data->{$k} = $candidate_ver->{$k};
84 }
85
86 $data->{Version} = $candidate_ver->{VerStr};
87 $data->{OldVersion} = $current_ver->{VerStr};
88
89 return $data;
90};
91
92__PACKAGE__->register_method({
93 name => 'list_updates',
94 path => 'update',
95 method => 'GET',
96 description => "List available updates.",
97 permissions => {
98 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
99 },
100 protected => 1,
101 proxyto => 'node',
102 parameters => {
103 additionalProperties => 0,
104 properties => {
105 node => get_standard_option('pve-node'),
106 },
107 },
108 returns => {
109 type => "array",
110 items => {
111 type => "object",
112 properties => {},
113 },
114 },
115 code => sub {
116 my ($param) = @_;
117
118 my $pkglist = [];
119
120 my $cache = &$get_apt_cache();
121 my $policy = $cache->policy;
122 my $pkgrecords = $cache->packages();
123
124 foreach my $pkgname (keys %$cache) {
125 my $p = $cache->{$pkgname};
126 next if $p->{SelectedState} ne 'Install';
127 my $current_ver = $p->{CurrentVer};
128 my $candidate_ver = $policy->candidate($p);
129
130 if ($current_ver->{VerStr} ne $candidate_ver->{VerStr}) {
131 my $info = $pkgrecords->lookup($pkgname);
132 my $res = &$assemble_pkginfo($pkgname, $info, $current_ver, $candidate_ver);
133 push @$pkglist, $res;
134 }
135 }
136
137 return $pkglist;
138 }});
139
1401;