The PVE::Notify module 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.

Also, PVE::Notify contains some useful helpers.

Signed-off-by: Lukas Wagner <l.wag...@proxmox.com>
---
 PVE/Makefile  |  1 +
 PVE/Notify.pm | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 PVE/Notify.pm

diff --git a/PVE/Makefile b/PVE/Makefile
index 48b85d33..d6317dde 100644
--- a/PVE/Makefile
+++ b/PVE/Makefile
@@ -13,6 +13,7 @@ PERLSOURCE =                  \
        HTTPServer.pm           \
        Jobs.pm                 \
        NodeConfig.pm           \
+       Notify.pm               \
        Report.pm               \
        VZDump.pm
 
diff --git a/PVE/Notify.pm b/PVE/Notify.pm
new file mode 100644
index 00000000..01609b83
--- /dev/null
+++ b/PVE/Notify.pm
@@ -0,0 +1,84 @@
+package PVE::Notify;
+
+use strict;
+use warnings;
+
+use PVE::Cluster;
+use PVE::RS::Notify;
+
+PVE::Cluster::cfs_register_file(
+    'notifications.cfg',
+    \&parse_notification_config,
+    \&write_notification_config,
+);
+
+PVE::Cluster::cfs_register_file(
+    'priv/notifications.cfg',
+    \&parse_notification_config,
+    \&write_notification_config,
+);
+
+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) = @_;
+
+    PVE::Cluster::cfs_lock_file('notifications.cfg', $timeout, sub {
+        PVE::Cluster::cfs_lock_file('priv/notifications.cfg', $timeout, $code);
+        die $@ if $@;
+    });
+    die $@ if $@;
+}
+
+sub read_config {
+    my $config = PVE::Cluster::cfs_read_file('notifications.cfg');
+    my $priv_config = PVE::Cluster::cfs_read_file('priv/notifications.cfg');
+
+    return PVE::RS::Notify->parse_config($config, $priv_config);
+}
+
+sub write_config {
+    my ($rust_config) = @_;
+
+    my ($config, $priv_config) = $rust_config->write_config();
+    PVE::Cluster::cfs_write_file('notifications.cfg', $config);
+    PVE::Cluster::cfs_write_file('priv/notifications.cfg', $priv_config);
+}
+
+sub send_notification {
+    my ($channel, $severity, $title, $message, $properties, $config) = @_;
+    $config = read_config() if !defined($config);
+    $config->send($channel, $severity, $title, $message, $properties);
+}
+
+sub info {
+    my ($channel, $title, $message, $properties, $config) = @_;
+    send_notification($channel, 'info', $title, $message, $properties, 
$config);
+}
+
+sub notice {
+    my ($channel, $title, $message, $properties, $config) = @_;
+    send_notification($channel, 'notice', $title, $message, $properties, 
$config);
+}
+
+sub warning {
+    my ($channel, $title, $message, $properties, $config) = @_;
+    send_notification($channel, 'warning', $title, $message, $properties, 
$config);
+}
+
+sub error {
+    my ($channel, $title, $message, $properties, $config) = @_;
+    send_notification($channel, 'error', $title, $message, $properties, 
$config);
+}
+
+1;
-- 
2.30.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to