]> git.proxmox.com Git - pve-guest-common.git/blobdiff - PVE/ReplicationConfig.pm
cleanup: ReplicationConfig: be specific about write_config
[pve-guest-common.git] / PVE / ReplicationConfig.pm
index de904ea6b1b822e35709f6d9f908b8ea7ece9d63..d597799308e045a076ee5ee36c06c08cec5d06c2 100644 (file)
@@ -79,6 +79,11 @@ my $defaultData = {
            default => '*/15',
            optional => 1,
        },
+       source => {
+           description => "Source of the replication.",
+           type => 'string', format => 'pve-node',
+           optional => 1,
+       },
     },
 };
 
@@ -95,7 +100,7 @@ sub parse_section_header {
        my $errmsg = undef; # set if you want to skip whole section
        eval { parse_replication_job_id($id); };
        $errmsg = $@ if $@;
-       my $config = { guest => $guest };
+       my $config = {};
        return ($type, $id, $errmsg, $config);
     }
     return undef;
@@ -120,6 +125,12 @@ sub parse_config {
     foreach my $id (sort keys %{$cfg->{ids}}) {
        my $data = $cfg->{ids}->{$id};
 
+       my ($guest, $jobnum) = parse_replication_job_id($id);
+
+       $data->{guest} = $guest;
+       $data->{jobnum} = $jobnum;
+       $data->{id} = $id;
+
        $data->{comment} = PVE::Tools::decode_text($data->{comment})
            if defined($data->{comment});
 
@@ -150,6 +161,7 @@ sub write_config {
        my $tid = $plugin->get_unique_target_id($data);
        my $vmid = $data->{guest};
 
+       die "property 'guest' has wrong value\n" if $id !~ m/^\Q$vmid\E-/;
        die "replication job for guest '$vmid' to target '$tid' already exists\n"
            if defined($target_hash->{$vmid}->{$tid});
        $target_hash->{$vmid}->{$tid} = 1;
@@ -158,7 +170,7 @@ sub write_config {
            if defined($data->{comment});
     }
 
-    $class->SUPER::write_config($filename, $cfg);
+    return $class->SUPER::write_config($filename, $cfg);
 }
 
 sub new {
@@ -203,6 +215,62 @@ sub check_for_existing_jobs {
     return undef;
 }
 
+sub find_local_replication_job {
+    my ($cfg, $vmid, $target) = @_;
+
+    foreach my $id (keys %{$cfg->{ids}}) {
+       my $data = $cfg->{ids}->{$id};
+
+       return $data if $data->{type} eq 'local' &&
+           $data->{guest} == $vmid && $data->{target} eq $target;
+    }
+
+    return undef;
+}
+
+# switch local replication job target
+sub switch_replication_job_target {
+    my ($vmid, $old_target, $new_target) = @_;
+
+    my $transfer_job = sub {
+       my $cfg = PVE::ReplicationConfig->new();
+       my $jobcfg = find_local_replication_job($cfg, $vmid, $old_target);
+
+       return if !$jobcfg;
+
+       $jobcfg->{target} = $new_target;
+
+       $cfg->write();
+    };
+
+    lock($transfer_job);
+};
+
+sub delete_job {
+    my ($jobid) = @_;
+
+    my $code = sub {
+       my $cfg = __PACKAGE__->new();
+       delete $cfg->{ids}->{$jobid};
+       $cfg->write();
+    };
+
+    lock($code);
+}
+
+sub swap_source_target_nolock {
+    my ($jobid) = @_;
+
+    my $cfg = __PACKAGE__->new();
+    my $job = $cfg->{ids}->{$jobid};
+    my $tmp = $job->{source};
+    $job->{source} = $job->{target};
+    $job->{target} = $tmp;
+    $cfg->write();
+
+    return $cfg->{ids}->{$jobid};
+}
+
 package PVE::ReplicationConfig::Cluster;
 
 use base qw(PVE::ReplicationConfig);
@@ -228,6 +296,7 @@ sub options {
        rate => { optional => 1 },
        schedule => { optional => 1 },
        remove_job => { optional => 1 },
+       source => { optional => 1 },
     };
 }