]> git.proxmox.com Git - pmg-api.git/blob - PMG/CLI/pmgupgrade.pm
style fixup
[pmg-api.git] / PMG / CLI / pmgupgrade.pm
1 package PMG::CLI::pmgupgrade;
2
3 use strict;
4 use warnings;
5 use File::stat ();
6
7 use PVE::SafeSyslog;
8 use PVE::Tools qw(extract_param);
9 use PVE::INotify;
10 use PVE::CLIHandler;
11
12 use PMG::RESTEnvironment;
13 use PMG::API2::APT;
14
15 use base qw(PVE::CLIHandler);
16
17 sub setup_environment {
18 PMG::RESTEnvironment->setup_default_cli_env();
19 }
20
21 __PACKAGE__->register_method ({
22 name => 'pmgupgrade',
23 path => 'pmgupgrade',
24 method => 'POST',
25 description => "Upgrade Proxmox Mail Gateway",
26 parameters => {
27 additionalProperties => 0,
28 properties => {
29 shell => {
30 type => 'boolean',
31 description => "Run an interactive shell after the update.",
32 optional => 1,
33 default => 0,
34 },
35 }
36 },
37 returns => { type => 'null'},
38 code => sub {
39 my ($param) = @_;
40
41 my $nodename = PVE::INotify::nodename();
42
43 my $st = File::stat::stat("/var/cache/apt/pkgcache.bin");
44 if (!$st || (time() - $st->mtime) > (3*24*3600)) {
45 print "\nYour package database is out of date. " .
46 "Please update that first.\n\n";
47 return undef;
48 }
49
50 my $cmdstr = 'apt-get dist-upgrade';
51
52 print "Starting system upgrade: apt-get dist-upgrade\n";
53
54 my $oldlist = PMG::API2::APT->list_updates({ node => $nodename});
55
56 system('apt-get', 'dist-upgrade');
57
58 my $pkglist = PMG::API2::APT->list_updates({ node => $nodename});
59
60 print "\n";
61 if (my $count = scalar(@$pkglist)) {
62 print "System not fully up to date (found $count new packages)\n\n";
63 } else {
64 print "Your System is up-to-date\n\n";
65 }
66
67 my $newkernel;
68 foreach my $p (@$oldlist) {
69 if (($p->{Package} =~ m/^pve-kernel/) &&
70 !grep { $_->{Package} eq $p->{Package} } @$pkglist) {
71 $newkernel = 1;
72 last;
73 }
74 }
75
76 if ($newkernel) {
77 print "\n";
78 print "Seems you installed a kernel update - Please consider rebooting\n" .
79 "this node to activate the new kernel.\n\n";
80 }
81
82 if ($param->{shell}) {
83 print "starting shell\n";
84 system('/bin/bash -l');
85 }
86
87 return undef;
88 }});
89
90 our $cmddef = [ __PACKAGE__, 'pmgupgrade', []];
91
92 1;