From a88002cf76d0924b93f3ee9e1f259e30de1053d4 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 19 Jun 2013 13:01:10 +0200 Subject: [PATCH] Allow to send notification mail about new packages. --- PVE/API2/APT.pm | 41 ++++++++++++++++++++++++++++++++++++++++- bin/cron/daily/pve | 15 +++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/PVE/API2/APT.pm b/PVE/API2/APT.pm index c23695a8..24e89014 100644 --- a/PVE/API2/APT.pm +++ b/PVE/API2/APT.pm @@ -3,6 +3,7 @@ package PVE::API2::APT; use strict; use warnings; use File::stat (); +use IO::File; use File::Basename; use LWP::UserAgent; @@ -223,6 +224,12 @@ __PACKAGE__->register_method({ additionalProperties => 0, properties => { node => get_standard_option('pve-node'), + notify => { + type => 'boolean', + description => "Send notification mail about new packages (to email address specified for user 'root\@pam').", + optional => 1, + default => 0, + }, }, }, returns => { @@ -244,7 +251,39 @@ __PACKAGE__->register_method({ PVE::Tools::run_command($cmd); - &$update_pve_pkgstatus(); + my $pkglist = &$update_pve_pkgstatus(); + + if ($param->{notify} && scalar(@$pkglist)) { + + my $usercfg = PVE::Cluster::cfs_read_file("user.cfg"); + my $rootcfg = $usercfg->{users}->{'root@pam'} || {}; + my $mailto = $rootcfg->{email}; + + if ($mailto) { + my $hostname = `hostname -f` || PVE::INotify::nodename(); + chomp $hostname; + + my $data = "Content-Type: text/plain;charset=\"UTF8\"\n"; + $data .= "Content-Transfer-Encoding: 8bit\n"; + $data .= "FROM: \n"; + $data .= "TO: $mailto\n"; + $data .= "SUBJECT: New software packages available ($hostname)\n"; + $data .= "\n"; + + $data .= "The following updates are available:\n\n"; + + foreach my $p (sort {$a->{Package} cmp $b->{Package} } @$pkglist) { + $data .= "$p->{Package}: $p->{OldVersion} ==> $p->{Version}\n"; + } + + my $fh = IO::File->new("|sendmail -B 8BITMIME $mailto") || + die "unable to open 'sendmail' - $!"; + + print $fh $data; + + $fh->close(); + } + } return; }; diff --git a/bin/cron/daily/pve b/bin/cron/daily/pve index d8d7a7ca..7d156156 100644 --- a/bin/cron/daily/pve +++ b/bin/cron/daily/pve @@ -29,13 +29,24 @@ my $nodename = PVE::INotify::nodename(); eval { PVE::API2::Subscription->update({ node => $nodename }); }; if (my $err = $@) { - syslog ('err', "update subscription info failed: $err"); + syslog ('err', "update subscription info failed: $err"); } my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg'); eval { PVE::APLInfo::update($dccfg->{http_proxy}); }; if (my $err = $@) { - syslog ('err', "update appliance info failed - see /var/log/pveam.log for details"); + syslog ('err', "update appliance info failed - see /var/log/pveam.log for details"); +} + +if (my $info = PVE::INotify::read_file('subscription')) { + # We assume that users with subscriptions want informations + # about new packages. + if ($info->{status} eq 'Active') { + eval { PVE::APL2::APT->update_database({ node => $nodename, notify => 1 }); }; + if (my $err = $@) { + syslog ('err', "update apt database failed: $err"); + } + } } sub cleanup_tasks {