]> git.proxmox.com Git - pve-guest-common.git/blobdiff - PVE/ReplicationConfig.pm
lock_config: rename lock_config_mode -> lock_config_shared
[pve-guest-common.git] / PVE / ReplicationConfig.pm
index 845d9c35efa510d715717bf02472951479aef4ba..66ef84246a5a5ccec13559cc2a6646a880e43ffd 100644 (file)
@@ -29,7 +29,7 @@ sub parse_replication_job_id {
 
     if ($id =~ m/^(\d+)-(\d+)$/) {
        my ($guest, $jobnum) = (int($1), int($2));
-       die "$msg (guest IDs < 100 are reseved)\n" if $guest < 100;
+       die "$msg (guest IDs < 100 are reserved)\n" if $guest < 100;
        my $parsed_id = "$guest-$jobnum"; # use parsed integers
        return wantarray ? ($guest, $jobnum, $parsed_id) :  $parsed_id;
     }
@@ -73,12 +73,17 @@ my $defaultData = {
            optional => 1,
        },
        schedule => {
-           description => "Storage replication schedule. The format is a subset of `systemd` calender events.",
+           description => "Storage replication schedule. The format is a subset of `systemd` calendar events.",
            type => 'string', format => 'pve-calendar-event',
            maxLength => 128,
            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});
@@ -164,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 {
@@ -209,6 +215,37 @@ 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) = @_;
 
@@ -221,6 +258,33 @@ sub delete_job {
     lock($code);
 }
 
+sub remove_vmid_jobs {
+    my ($vmid) = @_;
+
+    my $code = sub {
+       my $cfg = __PACKAGE__->new();
+       foreach my $id (keys %{$cfg->{ids}}) {
+           delete $cfg->{ids}->{$id} if ($cfg->{ids}->{$id}->{guest} == $vmid);
+       }
+       $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);
@@ -246,6 +310,7 @@ sub options {
        rate => { optional => 1 },
        schedule => { optional => 1 },
        remove_job => { optional => 1 },
+       source => { optional => 1 },
     };
 }