]>
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
;
23 use PMG
::RS
::APT
::Repositories
;
27 use AptPkg
::PkgRecords
;
29 my $get_apt_cache = sub {
31 my $apt_cache = AptPkg
::Cache-
>new() || die "unable to initialize AptPkg::Cache\n";
36 use base
qw(PVE::RESTHandler);
38 __PACKAGE__-
>register_method({
42 description
=> "Directory index for apt (Advanced Package Tool).",
47 additionalProperties
=> 0,
49 node
=> get_standard_option
('pve-node'),
57 id
=> { type
=> 'string' },
60 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
66 { id
=> 'changelog' },
67 { id
=> 'repositories' },
75 my $get_pkgfile = sub {
78 foreach my $verfile (@{$veriter->{FileList
}}) {
79 my $pkgfile = $verfile->{File
};
80 next if !$pkgfile->{Origin
};
87 my $get_changelog_url =sub {
88 my ($pkgname, $info, $pkgver, $origin, $component) = @_;
91 my $base = dirname
($info->{FileName
});
92 if ($origin && $base) {
93 $pkgver =~ s/^\d+://; # strip epoch
94 my $srcpkg = $info->{SourcePkg
} || $pkgname;
95 if ($origin eq 'Debian') {
96 $base =~ s!pool/updates/!pool/!; # for security channel
97 $changelog_url = "http://packages.debian.org/changelogs/$base/" .
98 "${srcpkg}_${pkgver}/changelog";
99 } elsif ($origin eq 'Proxmox') {
100 if ($component eq 'pmg-enterprise') {
101 $changelog_url = "https://enterprise.proxmox.com/debian/pmg/$base/" .
102 "${pkgname}_${pkgver}.changelog";
104 $changelog_url = "http://download.proxmox.com/debian/pmg/$base/" .
105 "${pkgname}_${pkgver}.changelog";
110 return $changelog_url;
113 my $assemble_pkginfo = sub {
114 my ($pkgname, $info, $current_ver, $candidate_ver) = @_;
117 Package
=> $info->{Name
},
118 Title
=> $info->{ShortDesc
},
122 if (my $pkgfile = &$get_pkgfile($candidate_ver)) {
123 $data->{Origin
} = $pkgfile->{Origin
};
124 if (my $changelog_url = &$get_changelog_url($pkgname, $info, $candidate_ver->{VerStr
},
125 $pkgfile->{Origin
}, $pkgfile->{Component
})) {
126 $data->{ChangeLogUrl
} = $changelog_url;
130 if (my $desc = $info->{LongDesc
}) {
131 $desc =~ s/^.*\n\s?//; # remove first line
133 $data->{Description
} = $desc;
136 foreach my $k (qw(Section Arch Priority)) {
137 $data->{$k} = $candidate_ver->{$k};
140 $data->{Version
} = $candidate_ver->{VerStr
};
141 $data->{OldVersion
} = $current_ver->{VerStr
} if $current_ver;
146 # we try to cache results
147 my $pmg_pkgstatus_fn = "/var/lib/pmg/pkgupdates";
149 my $read_cached_pkgstatus = sub {
152 my $jsonstr = PVE
::Tools
::file_get_contents
($pmg_pkgstatus_fn, 5*1024*1024);
153 $data = decode_json
($jsonstr);
156 warn "error reading cached package status in $pmg_pkgstatus_fn\n";
161 my $update_pmg_pkgstatus = sub {
163 syslog
('info', "update new package list: $pmg_pkgstatus_fn");
165 my $notify_status = {};
166 my $oldpkglist = &$read_cached_pkgstatus();
167 foreach my $pi (@$oldpkglist) {
168 $notify_status->{$pi->{Package
}} = $pi->{NotifyStatus
};
173 my $cache = &$get_apt_cache();
174 my $policy = $cache->policy;
175 my $pkgrecords = $cache->packages();
177 foreach my $pkgname (keys %$cache) {
178 my $p = $cache->{$pkgname};
179 next if !$p->{SelectedState
} || ($p->{SelectedState
} ne 'Install');
180 my $current_ver = $p->{CurrentVer
} || next;
181 my $candidate_ver = $policy->candidate($p) || next;
183 if ($current_ver->{VerStr
} ne $candidate_ver->{VerStr
}) {
184 my $info = $pkgrecords->lookup($pkgname);
185 my $res = &$assemble_pkginfo($pkgname, $info, $current_ver, $candidate_ver);
186 push @$pkglist, $res;
188 # also check if we need any new package
189 # Note: this is just a quick hack (not recursive as it should be), because
190 # I found no way to get that info from AptPkg
191 if (my $deps = $candidate_ver->{DependsList
}) {
195 if ($d->{DepType
} eq 'Depends') {
196 $found = $d->{TargetPkg
}->{SelectedState
} eq 'Install' if !$found;
197 $req = $d->{TargetPkg
} if !$req;
199 if (!($d->{CompType
} & AptPkg
::Dep
::Or
)) {
200 if (!$found && $req) { # New required Package
201 my $tpname = $req->{Name
};
202 my $tpinfo = $pkgrecords->lookup($tpname);
203 my $tpcv = $policy->candidate($req);
204 if ($tpinfo && $tpcv) {
205 my $res = &$assemble_pkginfo($tpname, $tpinfo, undef, $tpcv);
206 push @$pkglist, $res;
218 # keep notification status (avoid sending mails abou new packages more than once)
219 foreach my $pi (@$pkglist) {
220 if (my $ns = $notify_status->{$pi->{Package
}}) {
221 $pi->{NotifyStatus
} = $ns if $ns eq $pi->{Version
};
225 PVE
::Tools
::file_set_contents
($pmg_pkgstatus_fn, encode_json
($pkglist));
230 __PACKAGE__-
>register_method({
231 name
=> 'list_updates',
234 description
=> "List available updates.",
237 permissions
=> { check
=> [ 'admin', 'audit' ] },
239 additionalProperties
=> 0,
241 node
=> get_standard_option
('pve-node'),
254 if (my $st1 = File
::stat::stat($pmg_pkgstatus_fn)) {
255 my $st2 = File
::stat::stat("/var/cache/apt/pkgcache.bin");
256 my $st3 = File
::stat::stat("/var/lib/dpkg/status");
258 if ($st2 && $st3 && $st2->mtime <= $st1->mtime && $st3->mtime <= $st1->mtime) {
259 if (my $data = &$read_cached_pkgstatus()) {
265 my $pkglist = &$update_pmg_pkgstatus();
270 __PACKAGE__-
>register_method({
271 name
=> 'update_database',
274 description
=> "This is used to resynchronize the package index files from their sources (apt-get update).",
277 permissions
=> { check
=> [ 'admin' ] },
279 additionalProperties
=> 0,
281 node
=> get_standard_option
('pve-node'),
284 description
=> "Send notification mail about new packages (to email address specified for user 'root\@pam').",
290 description
=> "Only produces output suitable for logging, omitting progress indicators.",
302 my $rpcenv = PMG
::RESTEnvironment-
>get();
304 my $authuser = $rpcenv->get_user();
309 my $pmg_cfg = PMG
::Config-
>new();
311 my $http_proxy = $pmg_cfg->get('admin', 'http_proxy');
312 my $aptconf = "// no proxy configured\n";
314 $aptconf = "Acquire::http::Proxy \"${http_proxy}\";\n";
316 my $aptcfn = "/etc/apt/apt.conf.d/76pmgproxy";
317 PVE
::Tools
::file_set_contents
($aptcfn, $aptconf);
319 my $cmd = ['apt-get', 'update'];
321 print "starting apt-get update\n" if !$param->{quiet
};
323 if ($param->{quiet
}) {
324 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {});
326 PVE
::Tools
::run_command
($cmd);
329 my $pkglist = &$update_pmg_pkgstatus();
331 if ($param->{notify
} && scalar(@$pkglist)) {
333 my $mailfrom = "root";
335 if (my $mailto = $pmg_cfg->get('admin', 'email', 1)) {
337 my $text .= "The following updates are available:\n\n";
340 foreach my $p (sort {$a->{Package
} cmp $b->{Package
} } @$pkglist) {
341 next if $p->{NotifyStatus
} && $p->{NotifyStatus
} eq $p->{Version
};
343 if ($p->{OldVersion
}) {
344 $text .= "$p->{Package}: $p->{OldVersion} ==> $p->{Version}\n";
346 $text .= "$p->{Package}: $p->{Version} (new)\n";
352 my $hostname = `hostname -f` || PVE
::INotify
::nodename
();
355 my $subject = "New software packages available ($hostname)";
356 PVE
::Tools
::sendmail
($mailto, $subject, $text, undef,
357 $mailfrom, 'Proxmox Mail Gateway');
359 foreach my $pi (@$pkglist) {
360 $pi->{NotifyStatus
} = $pi->{Version
};
363 PVE
::Tools
::file_set_contents
($pmg_pkgstatus_fn, encode_json
($pkglist));
370 return $rpcenv->fork_worker('aptupdate', undef, $authuser, $realcmd);
374 __PACKAGE__-
>register_method({
378 description
=> "Get package changelogs.",
380 permissions
=> { check
=> [ 'admin', 'audit' ] },
382 additionalProperties
=> 0,
384 node
=> get_standard_option
('pve-node'),
386 description
=> "Package name.",
390 description
=> "Package version.",
402 my $pkgname = $param->{name
};
404 my $cache = &$get_apt_cache();
405 my $policy = $cache->policy;
406 my $p = $cache->{$pkgname} || die "no such package '$pkgname'\n";
407 my $pkgrecords = $cache->packages();
410 if ($param->{version
}) {
411 if (my $available = $p->{VersionList
}) {
412 for my $v (@$available) {
413 if ($v->{VerStr
} eq $param->{version
}) {
419 die "package '$pkgname' version '$param->{version}' is not available\n" if !$ver;
421 $ver = $policy->candidate($p) || die "no installation candidate for package '$pkgname'\n";
424 my $info = $pkgrecords->lookup($pkgname);
426 my $pkgfile = &$get_pkgfile($ver);
429 die "changelog for '${pkgname}_$ver->{VerStr}' not available\n"
430 if !($pkgfile && ($url = &$get_changelog_url($pkgname, $info, $ver->{VerStr
}, $pkgfile->{Origin
}, $pkgfile->{Component
})));
434 my $pmg_cfg = PMG
::Config-
>new();
435 my $proxy = $pmg_cfg->get('admin', 'http_proxy');
437 my $ua = LWP
::UserAgent-
>new;
438 $ua->agent("PMG/1.0");
440 $ua->max_size(1024*1024);
441 $ua->ssl_opts(verify_hostname
=> 0); # don't care for changelogs
444 $ua->proxy(['http', 'https'], $proxy);
452 if ($pkgfile->{Origin
} eq 'Proxmox' && $pkgfile->{Component
} eq 'pmg-enterprise') {
453 my $info = PVE
::INotify
::read_file
('subscription');
454 if ($info->{status
} eq 'Active') {
455 $username = $info->{key
};
456 $pw = PMG
::Utils
::get_hwaddress
();
457 $ua->credentials("enterprise.proxmox.com:443", 'pmg-enterprise-repository',
462 syslog
('info', "GET $url\n");
463 my $response = $ua->get($url);
465 if ($response->is_success) {
466 $data = $response->decoded_content;
468 PVE
::Exception
::raise
($response->message, code
=> $response->code);
474 __PACKAGE__-
>register_method({
475 name
=> 'repositories',
476 path
=> 'repositories',
479 description
=> "Get APT repository information.",
481 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
484 additionalProperties
=> 0,
486 node
=> get_standard_option
('pve-node'),
491 description
=> "Result from parsing the APT repository files in /etc/apt/.",
495 description
=> "List of parsed repository files.",
501 description
=> "Path to the problematic file.",
505 enum
=> [ 'list', 'sources' ],
506 description
=> "Format of the file.",
510 description
=> "The parsed repositories.",
516 description
=> "List of package types.",
519 enum
=> [ 'deb', 'deb-src' ],
523 description
=> "List of repository URIs.",
531 description
=> "List of package distribuitions",
538 description
=> "List of repository components",
539 optional
=> 1, # not present if suite is absolute
546 description
=> "Additional options",
565 description
=> "Associated comment",
570 enum
=> [ 'list', 'sources' ],
571 description
=> "Format of the defining file.",
575 description
=> "Whether the repository is enabled or not",
582 description
=> "Digest of the file as bytes.",
592 description
=> "List of problematic repository files.",
598 description
=> "Path to the problematic file.",
602 description
=> "The error message",
609 description
=> "Common digest of all files.",
613 description
=> "Additional information/warnings for APT repositories.",
619 description
=> "Path to the associated file.",
623 description
=> "Index of the associated repository within the file.",
627 description
=> "Property from which the info originates.",
632 description
=> "Kind of the information (e.g. warning).",
636 description
=> "Information message.",
641 'standard-repos' => {
643 description
=> "List of standard repositories and their configuration status",
649 description
=> "Handle to identify the repository.",
653 description
=> "Display name of the repository.",
657 description
=> "Description of the repository.",
662 description
=> "Indicating enabled/disabled status, if the " .
663 "repository is configured.",
673 return PMG
::RS
::APT
::Repositories
::repositories
();
676 __PACKAGE__-
>register_method({
677 name
=> 'add_repository',
678 path
=> 'repositories',
680 description
=> "Add a standard repository to the configuration",
682 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
687 additionalProperties
=> 0,
689 node
=> get_standard_option
('pve-node'),
692 description
=> "Handle that identifies a repository.",
696 description
=> "Digest to detect modifications.",
708 PMG
::RS
::APT
::Repositories
::add_repository
($param->{handle
}, $param->{digest
});
711 __PACKAGE__-
>register_method({
712 name
=> 'change_repository',
713 path
=> 'repositories',
715 description
=> "Change the properties of a repository. Currently only allows enabling/disabling.",
717 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
722 additionalProperties
=> 0,
724 node
=> get_standard_option
('pve-node'),
727 description
=> "Path to the containing file.",
731 description
=> "Index within the file (starting from 0).",
735 description
=> "Whether the repository should be enabled or not.",
740 description
=> "Digest to detect modifications.",
754 my $enabled = $param->{enabled
};
755 $options->{enabled
} = int($enabled) if defined($enabled);
757 PMG
::RS
::APT
::Repositories
::change_repository
(
759 int($param->{index}),
765 __PACKAGE__-
>register_method({
770 description
=> "Get package information for important Proxmox packages.",
771 permissions
=> { check
=> [ 'admin', 'audit' ] },
773 additionalProperties
=> 0,
775 node
=> get_standard_option
('pve-node'),
788 my $cache = &$get_apt_cache();
789 my $policy = $cache->policy;
790 my $pkgrecords = $cache->packages();
792 # order most important things first
793 my @list = qw(proxmox-mailgateway pmg-api pmg-gui);
795 my $aptver = $AptPkg::System
::_system-
>versioning();
796 my $byver = sub { $aptver->compare($cache->{$b}->{CurrentVer
}->{VerStr
}, $cache->{$a}->{CurrentVer
}->{VerStr
}) };
797 push @list, sort $byver grep { /^pve-kernel-/ && $cache->{$_}->{CurrentState
} eq 'Installed' } keys %$cache;
802 libpve-apiclient-perl
803 proxmox-mailgateway-container
814 libpve-http-server-perl
816 libproxmox-acme-plugins
823 proxmox-mini-journalreader
825 proxmox-widget-toolkit
830 push @list, (sort @pkgs, @opt_pack);
832 my (undef, undef, $kernel_release) = POSIX
::uname
();
833 my $pmgver = PMG
::pmgcfg
::version_text
();
836 foreach my $pkgname (@list) {
837 my $p = $cache->{$pkgname};
838 my $info = $pkgrecords->lookup($pkgname);
839 my $candidate_ver = defined($p) ?
$policy->candidate($p) : undef;
841 if (my $current_ver = $p->{CurrentVer
}) {
842 $res = &$assemble_pkginfo($pkgname, $info, $current_ver,
843 $candidate_ver || $current_ver);
844 } elsif ($candidate_ver) {
845 $res = &$assemble_pkginfo($pkgname, $info, $candidate_ver,
847 delete $res->{OldVersion
};
851 $res->{CurrentState
} = $p->{CurrentState
};
853 if (grep( /^$pkgname$/, @opt_pack)) {
854 next if $res->{CurrentState
} eq 'NotInstalled';
857 # hack: add some useful information (used by 'pmgversion -v')
858 if ($pkgname =~ /^proxmox-mailgateway(-container)?$/) {
859 $res->{ManagerVersion
} = $pmgver;
860 $res->{RunningKernel
} = $kernel_release;
861 if ($pkgname eq 'proxmox-mailgateway-container') {
862 # another hack: replace proxmox-mailgateway with CT meta pkg
864 unshift @$pkglist, $res;
869 push @$pkglist, $res;