]> git.proxmox.com Git - pve-manager.git/blame - bin/pveupdate
pveupdate: add ACME certificate renewal
[pve-manager.git] / bin / pveupdate
CommitLineData
19407634 1#!/usr/bin/perl
aff192e6
DM
2
3use strict;
19407634
DM
4use warnings;
5
aff192e6
DM
6use IO::File;
7use File::Find;
8use File::stat;
9
4a57db55
FG
10use PVE::CertHelpers;
11use PVE::Certificate;
12use PVE::NodeConfig;
fdc94486 13use PVE::INotify;
4027c59b
DM
14use PVE::Cluster;
15use PVE::APLInfo;
16use PVE::SafeSyslog;
17use PVE::RPCEnvironment;
18use PVE::API2::Subscription;
fdc94486 19use PVE::API2::APT;
4a57db55 20use PVE::API2::ACME;
4027c59b 21
c8969ecb 22initlog ('pveupdate', 'daemon');
aff192e6 23
4027c59b
DM
24die "please run as root\n" if $> != 0;
25
26$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
27
28PVE::INotify::inotify_init();
29
30my $rpcenv = PVE::RPCEnvironment->init('cli');
31
32$rpcenv->init_request();
33$rpcenv->set_language($ENV{LANG});
c9355915 34$rpcenv->set_user('root@pam');
4027c59b
DM
35
36my $nodename = PVE::INotify::nodename();
37
38eval { PVE::API2::Subscription->update({ node => $nodename }); };
39if (my $err = $@) {
a88002cf 40 syslog ('err', "update subscription info failed: $err");
4027c59b
DM
41}
42
c9164975
DM
43my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
44eval { PVE::APLInfo::update($dccfg->{http_proxy}); };
45if (my $err = $@) {
a88002cf
DM
46 syslog ('err', "update appliance info failed - see /var/log/pveam.log for details");
47}
48
d916c09d
DM
49my $info = PVE::INotify::read_file('subscription');
50# We assume that users with subscriptions want informations
51# about new packages.
52my $notify = ($info && $info->{status} eq 'Active') ? 1 : 0;
53eval { PVE::API2::APT->update_database({ node => $nodename, notify => $notify, quiet => 1 }); };
54if (my $err = $@) {
55 syslog ('err', "update apt database failed: $err");
c9164975 56}
aff192e6 57
4a57db55
FG
58eval {
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};
73syslog ('err', "Renewing ACME certificate failed: $@") if $@;
74
aff192e6
DM
75sub 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
b0a65294 110 foreach my $subdir (qw(0 1 2 3 4 5 6 7 8 9 A B C D E F)) {
aff192e6
DM
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
120cleanup_tasks();
121
122exit (0);