]> git.proxmox.com Git - pmg-api.git/commitdiff
correctly update /etc/default/fetchmail and /etc/fetchmailrc symlink
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 24 Oct 2017 08:57:43 +0000 (10:57 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 24 Oct 2017 08:57:43 +0000 (10:57 +0200)
PMG/Cluster.pm
PMG/Fetchmail.pm

index 1398a1da31c0849bf42c801c00d1953a2d639cab..23da15463743cfc71e8ed0b68d03c45bfbd416c7 100644 (file)
@@ -10,6 +10,7 @@ use Time::HiRes qw (gettimeofday tv_interval);
 use PVE::SafeSyslog;
 use PVE::Tools;
 use PVE::INotify;
+use PVE::APIClient::LWP;
 
 use PMG::Utils;
 use PMG::Config;
@@ -17,7 +18,7 @@ use PMG::ClusterConfig;
 use PMG::RuleDB;
 use PMG::RuleCache;
 use PMG::MailQueue;
-use PVE::APIClient::LWP;
+use PMG::Fetchmail;
 
 sub remote_node_ip {
     my ($nodename, $noerr) = @_;
@@ -364,6 +365,8 @@ sub sync_config_from_master {
 
     $cond_commit_synced_file->('cluster.conf');
 
+    PMG::Fetchmail::update_fetchmail_default(0); # disable on slave
+
     my $files = [
        'pmg-authkey.key',
        'pmg-authkey.pub',
@@ -380,6 +383,7 @@ sub sync_config_from_master {
        $cond_commit_synced_file->($filename);
     }
 
+
     my $force_restart = {};
 
     if ($cond_commit_synced_file->($sa_custom_cf, "${sa_conf_dir}/${sa_custom_cf}")) {
index 413e7fc096f8cda0ff6ad06c42bacc726f729f1e..5e44aa012bb12ec4039af104e959bfabaab31aaa 100644 (file)
@@ -8,9 +8,67 @@ use PVE::Tools;
 use PVE::INotify;
 
 use PMG::Config;
+use PMG::ClusterConfig;
 
 my $inotify_file_id = 'fetchmailrc';
 my $config_filename = '/etc/pmg/fetchmailrc';
+my $config_link_filename = '/etc/fetchmailrc';
+
+my $fetchmail_default_id = 'fetchmail_default';
+my $fetchmail_default_filename = '/etc/default/fetchmail';
+
+sub read_fetchmail_default {
+    my ($filename, $fh) = @_;
+
+    if (defined($fh)) {
+       while (defined(my $line = <$fh>)) {
+           if ($line =~ m/^START_DAEMON=yes\s*$/) {
+               return 1;
+           }
+       }
+    }
+
+    return 0;
+}
+
+sub write_fetchmail_default {
+    my ($filename, $fh, $enable) = @_;
+
+    open (my $orgfh, "<", $filename);
+
+    my $wrote_start_daemon = 0;
+
+    my $write_start_daemon_line = sub {
+
+       return if $wrote_start_daemon;  # only once
+       $wrote_start_daemon = 1;
+
+       if ($enable) {
+           print $fh "START_DAEMON=yes\n";
+       } else {
+           print $fh "START_DAEMON=no\n";
+       }
+    };
+
+    if (defined($orgfh)) {
+       while (defined(my $line = <$orgfh>)) {
+           if ($line =~ m/^#?START_DAEMON=.*$/) {
+               $write_start_daemon_line->();
+           } else {
+               print $fh $line;
+           }
+       }
+    } else {
+       $write_start_daemon_line->();
+    }
+}
+
+PVE::INotify::register_file(
+    $fetchmail_default_id, $fetchmail_default_filename,
+    \&read_fetchmail_default,
+    \&write_fetchmail_default,
+    undef,
+    always_call_parser => 1);
 
 my $set_fetchmail_defaults = sub {
     my ($item) = @_;
@@ -133,9 +191,12 @@ sub write_fetchmail_conf {
 
     # Note: we correctly quote data here to make fetchmailrc.tt simpler
 
+    my $entry_count = 0;
+
     foreach my $id (keys %$fmcfg) {
        my $org = $fmcfg->{$id};
        my $item = { id => $id };
+       $entry_count++;
        foreach my $k (keys %$org) {
            my $v = $org->{$k};
            $v =~ s/([^A-Za-z0-9\:\@\-\._~])/sprintf "\\x%02x",ord($1)/eg;
@@ -164,6 +225,32 @@ sub write_fetchmail_conf {
     chmod(0600, $fh);
 
     PVE::Tools::safe_print($filename, $fh, $raw);
+
+    update_fetchmail_default($entry_count);
+}
+
+sub update_fetchmail_default {
+    my ($enable) = @_;
+
+    my $cinfo = PMG::ClusterConfig->new();
+
+    my $is_enabled = PVE::INotify::read_file('fetchmail_default');
+    my $role = $cinfo->{local}->{type} // '-';
+    if (($role eq '-') || ($role eq 'master')) {
+       if (!!$enable != !!$is_enabled) {
+           PVE::INotify::write_file('fetchmail_default', $enable);
+       }
+       if (! -e $config_link_filename) {
+           symlink ($config_filename, $config_link_filename);
+       }
+    } else {
+       if ($is_enabled) {
+           PVE::INotify::write_file('fetchmail_default', 0);
+       }
+       if (-e $config_link_filename) {
+           unlink $config_link_filename;
+       }
+    }
 }
 
 PVE::INotify::register_file(