]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/APT.pm
1 package PVE
::API2
::APT
;
13 use Proxmox
::RS
::APT
::Repositories
;
16 use PVE
::Tools
qw(extract_param);
18 use PVE
::DataCenterConfig
;
23 use PVE
::RPCEnvironment
;
27 use PVE
::JSONSchema
qw(get_standard_option);
30 use AptPkg
::PkgRecords
;
33 my $get_apt_cache = sub {
35 my $apt_cache = AptPkg
::Cache-
>new() || die "unable to initialize AptPkg::Cache\n";
40 use base
qw(PVE::RESTHandler);
42 __PACKAGE__-
>register_method({
46 description
=> "Directory index for apt (Advanced Package Tool).",
51 additionalProperties
=> 0,
53 node
=> get_standard_option
('pve-node'),
61 id
=> { type
=> 'string' },
64 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
70 { id
=> 'changelog' },
71 { id
=> 'repositories' },
79 my $get_pkgfile = sub {
82 foreach my $verfile (@{$veriter->{FileList
}}) {
83 my $pkgfile = $verfile->{File
};
84 next if !$pkgfile->{Origin
};
91 my $get_changelog_url =sub {
92 my ($pkgname, $info, $pkgver, $origin, $component) = @_;
96 $base = dirname
($info->{FileName
}) if defined($info->{FileName
});
97 if ($origin && $base) {
98 $pkgver =~ s/^\d+://; # strip epoch
99 my $srcpkg = $info->{SourcePkg
} || $pkgname;
100 if ($origin eq 'Debian') {
101 $base =~ s!pool/updates/!pool/!; # for security channel
102 $changelog_url = "http://packages.debian.org/changelogs/$base/${srcpkg}_${pkgver}/changelog";
103 } elsif ($origin eq 'Proxmox') {
104 if ($component eq 'pve-enterprise') {
105 $changelog_url = "https://enterprise.proxmox.com/debian/$base/${pkgname}_${pkgver}.changelog";
107 $changelog_url = "http://download.proxmox.com/debian/$base/${pkgname}_${pkgver}.changelog";
112 return $changelog_url;
115 my $assemble_pkginfo = sub {
116 my ($pkgname, $info, $current_ver, $candidate_ver) = @_;
119 Package
=> $info->{Name
},
120 Title
=> $info->{ShortDesc
},
124 if (my $pkgfile = &$get_pkgfile($candidate_ver)) {
125 $data->{Origin
} = $pkgfile->{Origin
};
126 if (my $changelog_url = &$get_changelog_url($pkgname, $info, $candidate_ver->{VerStr
},
127 $pkgfile->{Origin
}, $pkgfile->{Component
})) {
128 $data->{ChangeLogUrl
} = $changelog_url;
132 if (my $desc = $info->{LongDesc
}) {
133 $desc =~ s/^.*\n\s?//; # remove first line
135 $data->{Description
} = $desc;
138 foreach my $k (qw(Section Arch Priority)) {
139 $data->{$k} = $candidate_ver->{$k};
142 $data->{Version
} = $candidate_ver->{VerStr
};
143 $data->{OldVersion
} = $current_ver->{VerStr
} if $current_ver;
148 # we try to cache results
149 my $pve_pkgstatus_fn = "/var/lib/pve-manager/pkgupdates";
151 my $read_cached_pkgstatus = sub {
154 my $jsonstr = PVE
::Tools
::file_get_contents
($pve_pkgstatus_fn, 5*1024*1024);
155 $data = decode_json
($jsonstr);
158 warn "error reading cached package status in $pve_pkgstatus_fn\n";
163 my $update_pve_pkgstatus = sub {
165 syslog
('info', "update new package list: $pve_pkgstatus_fn");
167 my $notify_status = {};
168 my $oldpkglist = &$read_cached_pkgstatus();
169 foreach my $pi (@$oldpkglist) {
170 $notify_status->{$pi->{Package
}} = $pi->{NotifyStatus
};
175 my $cache = &$get_apt_cache();
176 my $policy = $cache->policy;
177 my $pkgrecords = $cache->packages();
179 foreach my $pkgname (keys %$cache) {
180 my $p = $cache->{$pkgname};
181 next if !$p->{SelectedState
} || ($p->{SelectedState
} ne 'Install');
182 my $current_ver = $p->{CurrentVer
} || next;
183 my $candidate_ver = $policy->candidate($p) || next;
185 if ($current_ver->{VerStr
} ne $candidate_ver->{VerStr
}) {
186 my $info = $pkgrecords->lookup($pkgname);
187 my $res = &$assemble_pkginfo($pkgname, $info, $current_ver, $candidate_ver);
188 push @$pkglist, $res;
190 # also check if we need any new package
191 # Note: this is just a quick hack (not recursive as it should be), because
192 # I found no way to get that info from AptPkg
193 if (my $deps = $candidate_ver->{DependsList
}) {
197 if ($d->{DepType
} eq 'Depends') {
198 $found = $d->{TargetPkg
}->{SelectedState
} eq 'Install' if !$found;
199 # need to check ProvidesList for virtual packages
200 if (!$found && (my $provides = $d->{TargetPkg
}->{ProvidesList
})) {
201 for my $provide ($provides->@*) {
202 $found = $provide->{OwnerPkg
}->{SelectedState
} eq 'Install';
206 $req = $d->{TargetPkg
} if !$req;
208 if (!($d->{CompType
} & AptPkg
::Dep
::Or
)) {
209 if (!$found && $req) { # New required Package
210 my $tpname = $req->{Name
};
211 my $tpinfo = $pkgrecords->lookup($tpname);
212 my $tpcv = $policy->candidate($req);
213 if ($tpinfo && $tpcv) {
214 my $res = &$assemble_pkginfo($tpname, $tpinfo, undef, $tpcv);
215 push @$pkglist, $res;
227 # keep notification status (avoid sending mails abou new packages more than once)
228 foreach my $pi (@$pkglist) {
229 if (my $ns = $notify_status->{$pi->{Package
}}) {
230 $pi->{NotifyStatus
} = $ns if $ns eq $pi->{Version
};
234 PVE
::Tools
::file_set_contents
($pve_pkgstatus_fn, encode_json
($pkglist));
239 __PACKAGE__-
>register_method({
240 name
=> 'list_updates',
243 description
=> "List available updates.",
245 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
250 additionalProperties
=> 0,
252 node
=> get_standard_option
('pve-node'),
265 if (my $st1 = File
::stat::stat($pve_pkgstatus_fn)) {
266 my $st2 = File
::stat::stat("/var/cache/apt/pkgcache.bin");
267 my $st3 = File
::stat::stat("/var/lib/dpkg/status");
269 if ($st2 && $st3 && $st2->mtime <= $st1->mtime && $st3->mtime <= $st1->mtime) {
270 if (my $data = &$read_cached_pkgstatus()) {
276 my $pkglist = &$update_pve_pkgstatus();
281 __PACKAGE__-
>register_method({
282 name
=> 'update_database',
285 description
=> "This is used to resynchronize the package index files from their sources (apt-get update).",
287 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
292 additionalProperties
=> 0,
294 node
=> get_standard_option
('pve-node'),
297 description
=> "Send notification mail about new packages (to email address specified for user 'root\@pam').",
303 description
=> "Only produces output suitable for logging, omitting progress indicators.",
315 my $rpcenv = PVE
::RPCEnvironment
::get
();
317 my $authuser = $rpcenv->get_user();
322 # setup proxy for apt
323 my $dcconf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
325 my $aptconf = "// no proxy configured\n";
326 if ($dcconf->{http_proxy
}) {
327 $aptconf = "Acquire::http::Proxy \"$dcconf->{http_proxy}\";\n";
329 my $aptcfn = "/etc/apt/apt.conf.d/76pveproxy";
330 PVE
::Tools
::file_set_contents
($aptcfn, $aptconf);
332 my $cmd = ['apt-get', 'update'];
334 print "starting apt-get update\n" if !$param->{quiet
};
336 if ($param->{quiet
}) {
337 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {});
339 PVE
::Tools
::run_command
($cmd);
342 my $pkglist = &$update_pve_pkgstatus();
344 if ($param->{notify
} && scalar(@$pkglist)) {
346 my $usercfg = PVE
::Cluster
::cfs_read_file
("user.cfg");
347 my $rootcfg = $usercfg->{users
}->{'root@pam'} || {};
348 my $mailto = $rootcfg->{email
};
351 my $hostname = `hostname -f` || PVE
::INotify
::nodename
();
353 my $mailfrom = $dcconf->{email_from
} || "root";
354 my $subject = "New software packages available ($hostname)";
356 my $data = "The following updates are available:\n\n";
359 foreach my $p (sort {$a->{Package
} cmp $b->{Package
} } @$pkglist) {
360 next if $p->{NotifyStatus
} && $p->{NotifyStatus
} eq $p->{Version
};
362 if ($p->{OldVersion
}) {
363 $data .= "$p->{Package}: $p->{OldVersion} ==> $p->{Version}\n";
365 $data .= "$p->{Package}: $p->{Version} (new)\n";
371 PVE
::Tools
::sendmail
($mailto, $subject, $data, undef, $mailfrom, '');
373 foreach my $pi (@$pkglist) {
374 $pi->{NotifyStatus
} = $pi->{Version
};
376 PVE
::Tools
::file_set_contents
($pve_pkgstatus_fn, encode_json
($pkglist));
383 return $rpcenv->fork_worker('aptupdate', undef, $authuser, $realcmd);
387 __PACKAGE__-
>register_method({
391 description
=> "Get package changelogs.",
393 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
397 additionalProperties
=> 0,
399 node
=> get_standard_option
('pve-node'),
401 description
=> "Package name.",
405 description
=> "Package version.",
417 my $pkgname = $param->{name
};
419 my $cache = &$get_apt_cache();
420 my $policy = $cache->policy;
421 my $p = $cache->{$pkgname} || die "no such package '$pkgname'\n";
422 my $pkgrecords = $cache->packages();
425 if ($param->{version
}) {
426 if (my $available = $p->{VersionList
}) {
427 for my $v (@$available) {
428 if ($v->{VerStr
} eq $param->{version
}) {
434 die "package '$pkgname' version '$param->{version}' is not available\n" if !$ver;
436 $ver = $policy->candidate($p) || die "no installation candidate for package '$pkgname'\n";
439 my $info = $pkgrecords->lookup($pkgname);
441 my $pkgfile = &$get_pkgfile($ver);
444 die "changelog for '${pkgname}_$ver->{VerStr}' not available\n"
445 if !($pkgfile && ($url = &$get_changelog_url($pkgname, $info, $ver->{VerStr
}, $pkgfile->{Origin
}, $pkgfile->{Component
})));
449 my $dccfg = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
450 my $proxy = $dccfg->{http_proxy
};
452 my $ua = LWP
::UserAgent-
>new;
453 $ua->agent("PVE/1.0");
455 $ua->max_size(1024*1024);
456 $ua->ssl_opts(verify_hostname
=> 0); # don't care for changelogs
459 $ua->proxy(['http', 'https'], $proxy);
467 if ($pkgfile->{Origin
} eq 'Proxmox' && $pkgfile->{Component
} eq 'pve-enterprise') {
468 my $info = PVE
::INotify
::read_file
('subscription');
469 if ($info->{status
} eq 'Active') {
470 $username = $info->{key
};
471 $pw = PVE
::API2Tools
::get_hwaddress
();
472 $ua->credentials("enterprise.proxmox.com:443", 'pve-enterprise-repository', $username, $pw);
476 my $response = $ua->get($url);
478 if ($response->is_success) {
479 $data = $response->decoded_content;
481 PVE
::Exception
::raise
($response->message, code
=> $response->code);
487 __PACKAGE__-
>register_method({
488 name
=> 'repositories',
489 path
=> 'repositories',
492 description
=> "Get APT repository information.",
494 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
497 additionalProperties
=> 0,
499 node
=> get_standard_option
('pve-node'),
504 description
=> "Result from parsing the APT repository files in /etc/apt/.",
508 description
=> "List of parsed repository files.",
514 description
=> "Path to the problematic file.",
518 enum
=> [ 'list', 'sources' ],
519 description
=> "Format of the file.",
523 description
=> "The parsed repositories.",
529 description
=> "List of package types.",
532 enum
=> [ 'deb', 'deb-src' ],
536 description
=> "List of repository URIs.",
544 description
=> "List of package distribuitions",
551 description
=> "List of repository components",
552 optional
=> 1, # not present if suite is absolute
559 description
=> "Additional options",
578 description
=> "Associated comment",
583 enum
=> [ 'list', 'sources' ],
584 description
=> "Format of the defining file.",
588 description
=> "Whether the repository is enabled or not",
595 description
=> "Digest of the file as bytes.",
605 description
=> "List of problematic repository files.",
611 description
=> "Path to the problematic file.",
615 description
=> "The error message",
622 description
=> "Common digest of all files.",
626 description
=> "Additional information/warnings for APT repositories.",
632 description
=> "Path to the associated file.",
636 description
=> "Index of the associated repository within the file.",
640 description
=> "Property from which the info originates.",
645 description
=> "Kind of the information (e.g. warning).",
649 description
=> "Information message.",
654 'standard-repos' => {
656 description
=> "List of standard repositories and their configuration status",
662 description
=> "Handle to identify the repository.",
666 description
=> "Full name of the repository.",
671 description
=> "Indicating enabled/disabled status, if the " .
672 "repository is configured.",
682 return Proxmox
::RS
::APT
::Repositories
::repositories
("pve");
685 __PACKAGE__-
>register_method({
686 name
=> 'add_repository',
687 path
=> 'repositories',
689 description
=> "Add a standard repository to the configuration",
691 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
696 additionalProperties
=> 0,
698 node
=> get_standard_option
('pve-node'),
701 description
=> "Handle that identifies a repository.",
705 description
=> "Digest to detect modifications.",
717 Proxmox
::RS
::APT
::Repositories
::add_repository
($param->{handle
}, "pve", $param->{digest
});
720 __PACKAGE__-
>register_method({
721 name
=> 'change_repository',
722 path
=> 'repositories',
724 description
=> "Change the properties of a repository. Currently only allows enabling/disabling.",
726 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
731 additionalProperties
=> 0,
733 node
=> get_standard_option
('pve-node'),
736 description
=> "Path to the containing file.",
740 description
=> "Index within the file (starting from 0).",
744 description
=> "Whether the repository should be enabled or not.",
749 description
=> "Digest to detect modifications.",
763 my $enabled = $param->{enabled
};
764 $options->{enabled
} = int($enabled) if defined($enabled);
766 Proxmox
::RS
::APT
::Repositories
::change_repository
(
768 int($param->{index}),
774 __PACKAGE__-
>register_method({
779 description
=> "Get package information for important Proxmox packages.",
781 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
784 additionalProperties
=> 0,
786 node
=> get_standard_option
('pve-node'),
799 my $cache = &$get_apt_cache();
800 my $policy = $cache->policy;
801 my $pkgrecords = $cache->packages();
803 # order most important things first
804 my @list = qw(proxmox-ve pve-manager);
806 my $aptver = $AptPkg::System
::_system-
>versioning();
807 my $byver = sub { $aptver->compare($cache->{$b}->{CurrentVer
}->{VerStr
}, $cache->{$a}->{CurrentVer
}->{VerStr
}) };
808 push @list, sort $byver grep { /^pve-kernel-/ && $cache->{$_}->{CurrentState
} eq 'Installed' } keys %$cache;
818 libpve-apiclient-perl
820 proxmox-backup-file-restore
832 libpve-access-control
834 libpve-guest-common-perl
835 libpve-http-server-perl
838 libproxmox-backup-qemu0
845 proxmox-backup-client
846 proxmox-mini-journalreader
847 proxmox-widget-toolkit
865 # add the rest ordered by name, easier to find for humans
866 push @list, (sort @pkgs, @opt_pack);
868 my (undef, undef, $kernel_release) = POSIX
::uname
();
869 my $pvever = PVE
::pvecfg
::version_text
();
872 foreach my $pkgname (@list) {
873 my $p = $cache->{$pkgname};
874 my $info = $pkgrecords->lookup($pkgname);
875 my $candidate_ver = defined($p) ?
$policy->candidate($p) : undef;
877 if (my $current_ver = $p->{CurrentVer
}) {
878 $res = $assemble_pkginfo->($pkgname, $info, $current_ver, $candidate_ver || $current_ver);
879 } elsif ($candidate_ver) {
880 $res = $assemble_pkginfo->($pkgname, $info, $candidate_ver, $candidate_ver);
881 delete $res->{OldVersion
};
885 $res->{CurrentState
} = $p->{CurrentState
};
887 # hack: add some useful information (used by 'pveversion -v')
888 if ($pkgname eq 'pve-manager') {
889 $res->{ManagerVersion
} = $pvever;
890 } elsif ($pkgname eq 'proxmox-ve') {
891 $res->{RunningKernel
} = $kernel_release;
893 if (grep( /^$pkgname$/, @opt_pack)) {
894 next if $res->{CurrentState
} eq 'NotInstalled';
897 push @$pkglist, $res;