]> git.proxmox.com Git - pve-manager.git/blob - bin/pveupdate
test: replication: mock cfs_(write|lock)_file
[pve-manager.git] / bin / pveupdate
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7 use File::Find;
8 use File::stat;
9
10 use PVE::CertHelpers;
11 use PVE::Certificate;
12 use PVE::NodeConfig;
13 use PVE::INotify;
14 use PVE::Cluster;
15 use PVE::APLInfo;
16 use PVE::SafeSyslog;
17 use PVE::RPCEnvironment;
18 use PVE::API2::Subscription;
19 use PVE::API2::APT;
20 use PVE::API2::ACME;
21
22 initlog ('pveupdate', 'daemon');
23
24 die "please run as root\n" if $> != 0;
25
26 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
27
28 PVE::INotify::inotify_init();
29
30 my $rpcenv = PVE::RPCEnvironment->init('cli');
31
32 $rpcenv->init_request();
33 $rpcenv->set_language($ENV{LANG});
34 $rpcenv->set_user('root@pam');
35
36 my $nodename = PVE::INotify::nodename();
37
38 eval { PVE::API2::Subscription->update({ node => $nodename }); };
39 if (my $err = $@) {
40 syslog ('err', "update subscription info failed: $err");
41 }
42
43 my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
44 eval { PVE::APLInfo::update($dccfg->{http_proxy}); };
45 if (my $err = $@) {
46 syslog ('err', "update appliance info failed - see /var/log/pveam.log for details");
47 }
48
49 my $info = PVE::INotify::read_file('subscription');
50 # We assume that users with subscriptions want informations
51 # about new packages.
52 my $notify = ($info && $info->{status} eq 'Active') ? 1 : 0;
53 eval { PVE::API2::APT->update_database({ node => $nodename, notify => $notify, quiet => 1 }); };
54 if (my $err = $@) {
55 syslog ('err', "update apt database failed: $err");
56 }
57
58 eval {
59 my $node_config = PVE::NodeConfig::load_config($nodename);
60 if ($node_config && $node_config->{acme}) {
61 my $cert = PVE::CertHelpers::cert_path_prefix($nodename).".pem";
62 if (-e $cert) {
63 if (PVE::Certificate::check_expiry($cert, time() + 30*24*60*60)) {
64 PVE::API2::ACME->renew_certificate({ node => $nodename });
65 } else {
66 syslog ('info', 'Custom certificate does not expire soon, skipping ACME renewal.');
67 }
68 } else {
69 syslog ('info', 'ACME config found for node, but no custom certificate exists. Skipping ACME renewal until initial certificate has been deployed.');
70 }
71 }
72 };
73 syslog ('err', "Renewing ACME certificate failed: $@") if $@;
74
75 sub cleanup_tasks {
76
77 my $taskdir = "/var/log/pve/tasks";
78 my $filename = "$taskdir/index.1";
79
80 my $fh = IO::File->new($filename, O_RDONLY);
81 return if !$fh;
82
83 my $endtime = 0;
84 while (defined(my $line = <$fh>)) {
85 if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
86 $endtime = hex($3);
87 last;
88 }
89 }
90 close($fh);
91
92 return if !$endtime;
93
94 # print "delete task older that $endtime\n" . localtime($endtime) . "\n";
95
96 my $count = 0;
97
98 my $wanted = sub {
99 my $filename = $_;
100
101 return if $filename !~ m/^UPID:/;
102
103 my $st;
104 if (($st = stat($filename)) && ($st->mtime < $endtime)) {
105 unlink($filename);
106 $count++;
107 }
108 };
109
110 foreach my $subdir (qw(0 1 2 3 4 5 6 7 8 9 A B C D E F)) {
111 my $path = "$taskdir/$subdir";
112 find($wanted, $path);
113 }
114
115 if ($count) {
116 syslog('info', "cleanup removed $count task logs");
117 }
118 }
119
120 cleanup_tasks();
121
122 exit (0);