]> git.proxmox.com Git - pve-cluster.git/commitdiff
add libpve-notify-perl package
authorLukas Wagner <l.wagner@proxmox.com>
Thu, 20 Jul 2023 14:32:05 +0000 (16:32 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 24 Jul 2023 10:02:27 +0000 (12:02 +0200)
The package contains the  PVE::Notify. It is a very thin wrapper
around the Proxmox::RS::Notify module, feeding the configuration
from the new 'notifications.cfg' and 'priv/notifications.cfg' files
into it.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
debian/control
debian/libpve-notify-perl.docs [new file with mode: 0644]
debian/libpve-notify-perl.install [new file with mode: 0644]
src/PVE/Makefile
src/PVE/Notify.pm [new file with mode: 0644]

index f1c13cb702ccd1890b530177617f31a0941714ad..77d7f8ba66a0fefefdceaceeaca4d06a31fe3ab6 100644 (file)
@@ -85,3 +85,12 @@ Breaks: pve-cluster (<= 6.0-7),
 Replaces: pve-cluster (<= 6.0-7),
 Description: Proxmox Virtual Environment cluster Perl API modules.
  This package contains the API2 endpoints and CLI binary 'pvecm'.
+
+Package: libpve-notify-perl
+Architecture: all
+Pre-Depends: ${misc:Pre-Depends},
+Depends: libpve-cluster-perl (= ${binary:Version}),
+         libpve-rs-perl (>= 0.7.1),
+         ${misc:Depends},
+         ${perl:Depends},
+Description: Notify helper module
diff --git a/debian/libpve-notify-perl.docs b/debian/libpve-notify-perl.docs
new file mode 100644 (file)
index 0000000..8696672
--- /dev/null
@@ -0,0 +1 @@
+debian/SOURCE
diff --git a/debian/libpve-notify-perl.install b/debian/libpve-notify-perl.install
new file mode 100644 (file)
index 0000000..b590d07
--- /dev/null
@@ -0,0 +1 @@
+usr/share/perl5/PVE/Notify.pm
index 10291a63643ccdf7f4d22cc1649386aee593085b..ac4a9cee7184def901407fa4082a707b9643988a 100644 (file)
@@ -11,7 +11,7 @@ PVE_VENDORARCH=$(DESTDIR)/$(PERL_VENDORARCH)/auto/PVE/IPCC
 PERL_DOC_INC_DIRS:=..
 
 SUBDIRS=Cluster CLI API2
-SOURCES=IPCC.pm Cluster.pm Corosync.pm RRD.pm DataCenterConfig.pm SSHInfo.pm
+SOURCES=IPCC.pm Cluster.pm Corosync.pm RRD.pm DataCenterConfig.pm Notify.pm SSHInfo.pm
 
 all:
 
diff --git a/src/PVE/Notify.pm b/src/PVE/Notify.pm
new file mode 100644 (file)
index 0000000..48ef772
--- /dev/null
@@ -0,0 +1,145 @@
+package PVE::Notify;
+
+use strict;
+use warnings;
+
+use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_lock_file cfs_write_file);
+use PVE::RS::Notify;
+
+cfs_register_file(
+    'notifications.cfg',
+    \&parse_notification_config,
+    \&write_notification_config,
+);
+
+cfs_register_file(
+    'priv/notifications.cfg',
+    \&parse_notification_config,
+    \&write_notification_config,
+);
+
+my $mail_to_root_target = 'mail-to-root';
+
+sub parse_notification_config {
+    my ($filename, $raw) = @_;
+
+    $raw = '' if !defined($raw);
+    return $raw;
+}
+
+sub write_notification_config {
+    my ($filename, $config) = @_;
+    return $config;
+}
+
+sub lock_config {
+    my ($code, $timeout) = @_;
+
+    cfs_lock_file('notifications.cfg', $timeout, sub {
+       cfs_lock_file('priv/notifications.cfg', $timeout, $code);
+       die $@ if $@;
+    });
+    die $@ if $@;
+}
+
+sub read_config {
+    my $config = cfs_read_file('notifications.cfg');
+    my $priv_config = cfs_read_file('priv/notifications.cfg');
+
+    my $notification_config = PVE::RS::Notify->parse_config($config, $priv_config);
+
+    eval {
+       # This target should always be available...
+       $notification_config->add_sendmail_endpoint(
+           $mail_to_root_target,
+           undef,
+           ['root@pam'],
+           undef,
+           undef,
+           'Send mail to root@pam\'s email address'
+       );
+    };
+
+    return $notification_config;
+}
+
+sub write_config {
+    my ($notification_config) = @_;
+
+    eval {
+       # ... but don't persist it to the config.
+       # Rationale: If it is in the config, the user might think
+       # that it can be changed by editing the configuration there.
+       # However, since we always add it in `read_config`, any changes
+       # will be implicitly overridden by the default.
+
+       # If users want's to change the configuration, they are supposed to
+       # create a new sendmail endpoint.
+       $notification_config->delete_sendmail_endpoint($mail_to_root_target);
+    };
+
+    my ($config, $priv_config) = $notification_config->write_config();
+    cfs_write_file('notifications.cfg', $config);
+    cfs_write_file('priv/notifications.cfg', $priv_config);
+}
+
+sub default_target {
+    return $mail_to_root_target;
+}
+
+my $send_notification = sub {
+    my ($target, $severity, $title, $message, $properties, $config) = @_;
+    $config = read_config() if !defined($config);
+    my ($module, $file, $line) = caller(1);
+
+    # Augment properties with the source code location of the notify call
+    my $props_with_source = {
+       %$properties,
+       source => {
+           module => $module,
+           file => $file,
+           line => $line,
+       }
+    };
+
+    $config->send($target, $severity, $title, $message, $props_with_source);
+};
+
+sub notify {
+    my ($target, $severity, $title, $message, $properties, $config) = @_;
+    $send_notification->($target, $severity, $title, $message, $properties, $config);
+}
+
+sub info {
+    my ($target, $title, $message, $properties, $config) = @_;
+    $send_notification->($target, 'info', $title, $message, $properties, $config);
+}
+
+sub notice {
+    my ($target, $title, $message, $properties, $config) = @_;
+    $send_notification->($target, 'notice', $title, $message, $properties, $config);
+}
+
+sub warning {
+    my ($target, $title, $message, $properties, $config) = @_;
+    $send_notification->($target, 'warning', $title, $message, $properties, $config);
+}
+
+sub error {
+    my ($target, $title, $message, $properties, $config) = @_;
+    $send_notification->($target, 'error', $title, $message, $properties, $config);
+}
+
+sub check_may_use_target {
+    my ($target, $rpcenv) = @_;
+    my $user = $rpcenv->get_user();
+
+    my $config = read_config();
+    my $entities = $config->get_referenced_entities($target);
+
+    for my $entity (@$entities) {
+       $rpcenv->check($user, "/mapping/notification/$entity", [ 'Mapping.Use' ]);
+    }
+}
+
+1;