]> git.proxmox.com Git - pve-guest-common.git/blobdiff - PVE/ReplicationConfig.pm
Add function: swap source and target in replication config
[pve-guest-common.git] / PVE / ReplicationConfig.pm
index 01e7206338cdeb64562bb81715eebb8d8329097d..eb98f78f1a304691e1a356d85a7b4de3febf7cf9 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,
+       },
     },
 };
 
@@ -124,6 +129,7 @@ sub parse_config {
 
        $data->{guest} = $guest;
        $data->{jobnum} = $jobnum;
+       $data->{id} = $id;
 
        $data->{comment} = PVE::Tools::decode_text($data->{comment})
            if defined($data->{comment});
@@ -209,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);
@@ -234,6 +296,7 @@ sub options {
        rate => { optional => 1 },
        schedule => { optional => 1 },
        remove_job => { optional => 1 },
+       source => { optional => 1 },
     };
 }