From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 48565E8FB for ; Wed, 19 Jul 2023 14:27:42 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 29A2273A6 for ; Wed, 19 Jul 2023 14:27:42 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 19 Jul 2023 14:27:41 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 14C6A41017 for ; Wed, 19 Jul 2023 14:27:41 +0200 (CEST) Date: Wed, 19 Jul 2023 14:27:39 +0200 From: Wolfgang Bumiller To: Lukas Wagner Cc: pve-devel@lists.proxmox.com Message-ID: References: <20230717150051.710464-1-l.wagner@proxmox.com> <20230717150051.710464-37-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230717150051.710464-37-l.wagner@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.117 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [libpve-notify-perl.docs, corosync.pm, sshinfo.pm, notify.pm, rrd.pm, datacenterconfig.pm, cluster.pm, ipcc.pm] Subject: Re: [pve-devel] [PATCH v3 pve-cluster 36/66] add libpve-notify-perl package X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 19 Jul 2023 12:27:42 -0000 On Mon, Jul 17, 2023 at 05:00:21PM +0200, Lukas Wagner wrote: > 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 > --- > debian/control | 9 ++ > debian/libpve-notify-perl.docs | 1 + > debian/libpve-notify-perl.install | 1 + > src/PVE/Makefile | 2 +- > src/PVE/Notify.pm | 145 ++++++++++++++++++++++++++++++ > 5 files changed, 157 insertions(+), 1 deletion(-) > create mode 100644 debian/libpve-notify-perl.docs > create mode 100644 debian/libpve-notify-perl.install > create mode 100644 src/PVE/Notify.pm > > diff --git a/debian/control b/debian/control > index f1c13cb..77d7f8b 100644 > --- a/debian/control > +++ b/debian/control > @@ -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 > index 0000000..8696672 > --- /dev/null > +++ b/debian/libpve-notify-perl.docs > @@ -0,0 +1 @@ > +debian/SOURCE > diff --git a/debian/libpve-notify-perl.install b/debian/libpve-notify-perl.install > new file mode 100644 > index 0000000..b590d07 > --- /dev/null > +++ b/debian/libpve-notify-perl.install > @@ -0,0 +1 @@ > +usr/share/perl5/PVE/Notify.pm > diff --git a/src/PVE/Makefile b/src/PVE/Makefile > index 10291a6..ac4a9ce 100644 > --- a/src/PVE/Makefile > +++ b/src/PVE/Makefile > @@ -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 > index 0000000..a3bcfbb > --- /dev/null > +++ b/src/PVE/Notify.pm > @@ -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; > +} > + > +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 _send_notification { ^ do we need this accessible to the outside? If not, you could move it above its users and turn it into a `my 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 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; > \ No newline at end of file ^ your editor has some kind of disease ;-)