]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/Cluster.pm
reload postfix instead of restart on config change
[pmg-api.git] / PMG / Cluster.pm
index 6528941d1a2f2fa030e167bc85fc31a3708c5dbc..319a45bb388aed65cc403a45c79421dd880eaf3b 100644 (file)
@@ -196,15 +196,22 @@ my $ssh_rsa_id = "/root/.ssh/id_rsa.pub";
 sub update_ssh_keys {
     my ($cinfo) = @_;
 
+    my $old = '';
     my $data = '';
+
     foreach my $node (values %{$cinfo->{ids}}) {
        $data .= "$node->{ip} ssh-rsa $node->{hostrsapubkey}\n";
        $data .= "$node->{name} ssh-rsa $node->{hostrsapubkey}\n";
     }
 
-    PVE::Tools::file_set_contents($sshglobalknownhosts, $data);
+    $old = PVE::Tools::file_get_contents($sshglobalknownhosts, 1024*1024)
+       if -f $sshglobalknownhosts;
+
+    PVE::Tools::file_set_contents($sshglobalknownhosts, $data)
+       if $old ne $data;
 
     $data = '';
+    $old = '';
 
     # always add ourself
     if (-f $ssh_rsa_id) {
@@ -233,7 +240,11 @@ sub update_ssh_keys {
        $newdata .= "$line\n";
     }
 
-    PVE::Tools::file_set_contents($rootsshauthkeys, $newdata, 0600);
+    $old = PVE::Tools::file_get_contents($rootsshauthkeys, 1024*1024)
+       if -f $rootsshauthkeys;
+
+    PVE::Tools::file_set_contents($rootsshauthkeys, $newdata, 0600)
+       if $old ne $newdata;
 }
 
 my $cfgdir = '/etc/pmg';
@@ -257,6 +268,9 @@ my $cond_commit_synced_file = sub {
        return 0 if $new eq $old;
     }
 
+    # set mtime (touch) to avoid time drift problems
+    utime(undef, undef, $srcfn);
+
     rename($srcfn, $dstfn) ||
        die "cond_rename_file '$filename' failed - $!\n";
 
@@ -277,12 +291,16 @@ my $rsync_command = sub {
 };
 
 sub sync_quarantine_files {
-    my ($host_ip, $host_name, $flistname) = @_;
+    my ($host_ip, $host_name, $flistname, $rcid) = @_;
 
     my $spooldir = $PMG::MailQueue::spooldir;
 
+    mkdir "$spooldir/cluster/";
+    my $syncdir = "$spooldir/cluster/$rcid";
+    mkdir $syncdir;
+
     my $cmd = $rsync_command->(
-       $host_name, '--timeout', '10', "${host_ip}:$spooldir", $spooldir,
+       $host_name, '--timeout', '10', "[${host_ip}]:$spooldir", $spooldir,
        '--files-from', $flistname);
 
     PVE::Tools::run_command($cmd);
@@ -298,7 +316,7 @@ sub sync_spooldir {
     mkdir $syncdir;
 
     my $cmd = $rsync_command->(
-       $host_name, '-aq', '--timeout', '10', "${host_ip}:$syncdir/", $syncdir);
+       $host_name, '-aq', '--timeout', '10', "[${host_ip}]:$syncdir/", $syncdir);
 
     foreach my $incl (('spam/', 'spam/*', 'spam/*/*', 'virus/', 'virus/*', 'virus/*/*')) {
        push @$cmd, '--include', $incl;
@@ -318,7 +336,7 @@ sub sync_master_quar {
     mkdir $syncdir;
 
     my $cmd = $rsync_command->(
-       $host_name, '-aq', '--timeout', '10', "${host_ip}:$syncdir", $syncdir);
+       $host_name, '-aq', '--timeout', '10', "[${host_ip}]:$syncdir", $syncdir);
 
     PVE::Tools::run_command($cmd);
 }
@@ -334,7 +352,7 @@ sub sync_config_from_master {
 
     my $cmd = $rsync_command->(
        $master_name, '-aq',
-       "${master_ip}:$cfgdir/* ${sa_conf_dir}/${sa_custom_cf}",
+       "[${master_ip}]:$cfgdir/* ${sa_conf_dir}/${sa_custom_cf}",
        "$syncdir/",
        '--exclude', 'master/',
        '--exclude', '*~',
@@ -365,6 +383,8 @@ sub sync_config_from_master {
 
     $cond_commit_synced_file->('cluster.conf');
 
+    update_ssh_keys($cinfo); # rewrite ssh keys
+
     PMG::Fetchmail::update_fetchmail_default(0); # disable on slave
 
     my $files = [
@@ -526,7 +546,10 @@ sub sync_quarantine_db {
            my $callback = sub {
                my $ref = shift;
                $maxid = $ref->{rid};
-               print $flistfh "$ref->{file}\n";
+               my $filename = $ref->{file};
+                # skip files generated before cluster was created
+               return if $filename !~ m!^cluster/!;
+               print $flistfh "$filename\n";
            };
 
            my $attrs = [qw(cid rid time qtype bytes spamlevel info sender header file)];
@@ -535,7 +558,7 @@ sub sync_quarantine_db {
            close($flistfh);
 
            my $starttime = [ gettimeofday() ];
-           sync_quarantine_files($ni->{ip}, $ni->{name}, $flistname);
+           sync_quarantine_files($ni->{ip}, $ni->{name}, $flistname, $rcid);
            $$rsynctime_ref += tv_interval($starttime);
 
            if ($maxid) {
@@ -547,7 +570,7 @@ sub sync_quarantine_db {
                    "AND CMailStore_RID <= ?");
                $sth->execute($rcid, $lastid, $maxid);
 
-               $attrs = [qw(cmailstore_cid cmailstore_rid pmail receiver status mtime)];
+               $attrs = [qw(cmailstore_cid cmailstore_rid pmail receiver ticketid status mtime)];
                PMG::DBTools::copy_selected_data($ldb, $sth, 'CMSReceivers', $attrs);
 
                PMG::DBTools::write_maxint_clusterinfo($ldb, $rcid, 'lastid_CMailStore', $maxid);
@@ -566,9 +589,7 @@ sub sync_quarantine_db {
 
        $mscount += $count;
 
-       last if $mscount >= $maxmails;
-
-    } while ($count >= $maxcount);
+    } while (($count >= $maxcount) && ($mscount < $maxmails));
 
     PMG::DBTools::create_clusterinfo_default($ldb, $rcid, 'lastmt_CMSReceivers', 0, undef);
 
@@ -582,10 +603,10 @@ sub sync_quarantine_db {
 
        my $update_sth = $ldb->prepare(
            "UPDATE CMSReceivers SET status = ? WHERE " .
-           "CMailstore_CID = ? AND CMailstore_RID = ? AND PMail = ?;");
+           "CMailstore_CID = ? AND CMailstore_RID = ? AND TicketID = ?");
        while (my $ref = $sth->fetchrow_hashref()) {
            $update_sth->execute($ref->{status}, $ref->{cmailstore_cid},
-                                $ref->{cmailstore_rid}, $ref->{pmail});
+                                $ref->{cmailstore_rid}, $ref->{ticketid});
        }
 
        PMG::DBTools::write_maxint_clusterinfo($ldb, $rcid, 'lastmt_CMSReceivers', $ctime);
@@ -653,10 +674,10 @@ sub sync_statistic_db {
 
                $attrs = [qw(cstatistic_cid cstatistic_rid blocked receiver)];
                PMG::DBTools::copy_selected_data($ldb, $sth, 'CReceivers', $attrs);
-
-               PMG::DBTools::write_maxint_clusterinfo ($ldb, $rcid, 'lastid_CStatistic', $maxid);
            }
 
+           PMG::DBTools::write_maxint_clusterinfo ($ldb, $rcid, 'lastid_CStatistic', $maxid);
+
            $ldb->commit;
        };
        if (my $err = $@) {
@@ -666,9 +687,7 @@ sub sync_statistic_db {
 
        $mscount += $count;
 
-       last if $mscount >= $maxmails;
-
-    } while ($count >= $maxcount);
+    } while (($count >= $maxcount) && ($mscount < $maxmails));
 
     return $mscount;
 }
@@ -774,7 +793,7 @@ sub sync_userprefs_db {
 
     my $merge_sth = $dbh->prepare(
        "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
-       'VALUES (?, ?, ?, 0) ' .
+       'VALUES (?, ?, ?, ?) ' .
        'ON CONFLICT (PMail, Name) DO UPDATE SET ' .
        # Note: MTime = 0 ==> this is just a copy from somewhere else, not modified
        'MTime = CASE WHEN excluded.MTime >= UserPrefs.MTime THEN 0 ELSE UserPrefs.MTime END, ' .
@@ -783,7 +802,7 @@ sub sync_userprefs_db {
     my $mergefunc = sub {
        my ($ref) = @_;
 
-       $merge_sth->execute($ref->{pmail}, $ref->{name}, $ref->{data});
+       $merge_sth->execute($ref->{pmail}, $ref->{name}, $ref->{data}, $ref->{mtime});
     };
 
     return $sync_generic_mtime_db->($dbh, $rdb, $ni, 'UserPrefs', $selectfunc, $mergefunc);