]> git.proxmox.com Git - pve-manager.git/commitdiff
PVE::Replication - remove dependency to PVE::LXC/PVE::QemuServer
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 9 Jun 2017 07:45:44 +0000 (09:45 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 12 Jun 2017 03:59:51 +0000 (05:59 +0200)
So that we can move the whole class to package pve-guest-common.

PVE/API2/Replication.pm
PVE/CLI/pvesr.pm
PVE/Replication.pm
bin/test/ReplicationTestEnv.pm
bin/test/replication_test2.pl
bin/test/replication_test3.pl

index 6731f096071705509be02be16175380a8da307ec..a1ffa318a439822ad74df460677baf403b435395 100644 (file)
@@ -8,11 +8,89 @@ use PVE::RPCEnvironment;
 use PVE::ProcFSTools;
 use PVE::ReplicationConfig;
 use PVE::Replication;
+use PVE::QemuConfig;
+use PVE::QemuServer;
+use PVE::LXC::Config;
+use PVE::LXC;
 
 use PVE::RESTHandler;
 
 use base qw(PVE::RESTHandler);
 
+my $pvesr_lock_path = "/var/lock/pvesr.lck";
+
+my $lookup_guest_class = sub {
+    my ($vmtype) = @_;
+
+    if ($vmtype eq 'qemu') {
+       return 'PVE::QemuConfig';
+    } elsif ($vmtype eq 'lxc') {
+       return 'PVE::LXC::Config';
+    } else {
+       die "unknown guest type '$vmtype' - internal error";
+    }
+};
+
+# passing $now is useful for regression testing
+sub run_single_job {
+    my ($jobid, $now, $logfunc) = @_;
+
+    my $local_node = PVE::INotify::nodename();
+
+    my $code = sub {
+       $now //= time();
+
+       my $cfg = PVE::ReplicationConfig->new();
+
+       my $jobcfg = $cfg->{ids}->{$jobid};
+       die "no such job '$jobid'\n" if !$jobcfg;
+
+       die "internal error - not implemented" if $jobcfg->{type} ne 'local';
+
+       die "job '$jobid' is disabled\n" if $jobcfg->{disable};
+
+       my $vms = PVE::Cluster::get_vmlist();
+       my $vmid = $jobcfg->{guest};
+
+       die "no such guest '$vmid'\n" if !$vms->{ids}->{$vmid};
+
+       die "guest '$vmid' is not on local node\n"
+           if $vms->{ids}->{$vmid}->{node} ne $local_node;
+
+       die "unable to sync to local node\n" if $jobcfg->{target} eq $local_node;
+
+       $jobcfg->{id} = $jobid;
+
+       $jobcfg->{vmtype} = $vms->{ids}->{$vmid}->{type};
+
+       my $guest_class = $lookup_guest_class->($jobcfg->{vmtype});
+       PVE::Replication::run_replication($guest_class, $jobcfg, $now, $now, $logfunc);
+    };
+
+    my $res = PVE::Tools::lock_file($pvesr_lock_path, 60, $code);
+    die $@ if $@;
+}
+
+# passing $now is useful for regression testing
+sub run_jobs {
+    my ($now, $logfunc) = @_;
+
+    my $iteration = $now // time();
+
+    my $code = sub {
+       my $start_time = $now // time();
+
+       while (my $jobcfg = PVE::Replication::get_next_job($iteration, $start_time)) {
+           my $guest_class = $lookup_guest_class->($jobcfg->{vmtype});
+           PVE::Replication::run_replication($guest_class, $jobcfg, $iteration, $start_time, $logfunc, 1);
+           $start_time = $now // time();
+       }
+    };
+
+    my $res = PVE::Tools::lock_file($pvesr_lock_path, 60, $code);
+    die $@ if $@;
+}
+
 my $extract_job_status = sub {
     my ($jobcfg, $jobid) = @_;
 
index f64a3103a60a63030f3e7acf0f4badbeb973fdab..078dba05ddd7972c7cedc3d6f8c67e9270c1ac7a 100644 (file)
@@ -238,11 +238,11 @@ __PACKAGE__->register_method ({
 
        if (my $id = extract_param($param, 'id')) {
 
-           PVE::Replication::run_single_job($id, undef, $logfunc);
+           PVE::API2::Replication::run_single_job($id, undef, $logfunc);
 
        } else {
 
-           PVE::Replication::run_jobs(undef, $logfunc);
+           PVE::API2::Replication::run_jobs(undef, $logfunc);
        }
 
        return undef;
index 0e34dc53f994e2bd4e7d5a63ba15f5f791b14536..ee3245a0c2383b50d80fbd45f55d3b3188059365 100644 (file)
@@ -12,16 +12,11 @@ use PVE::Tools;
 use PVE::CalendarEvent;
 use PVE::Cluster;
 use PVE::AbstractConfig;
-use PVE::QemuConfig;
-use PVE::QemuServer;
-use PVE::LXC::Config;
-use PVE::LXC;
 use PVE::Storage;
 use PVE::GuestHelpers;
 use PVE::ReplicationConfig;
 use PVE::ReplicationState;
 
-our $pvesr_lock_path = "/var/lock/pvesr.lck";
 our $replicate_logdir = "/var/log/pve/replicate";
 
 # regression tests should overwrite this
@@ -98,7 +93,7 @@ sub job_status {
     return $jobs;
 }
 
-my $get_next_job = sub {
+sub get_next_job {
     my ($iteration, $start_time) = @_;
 
     my $jobs = job_status();
@@ -124,7 +119,7 @@ my $get_next_job = sub {
     }
 
     return undef;
-};
+}
 
 sub remote_prepare_local_job {
     my ($ssh_info, $jobid, $vmid, $volumes, $storeid_list, $last_sync, $parent_snapname, $force, $logfunc) = @_;
@@ -453,7 +448,7 @@ my $run_replication_nolock = sub {
     }
 };
 
-my $run_replication = sub {
+sub run_replication {
     my ($guest_class, $jobcfg, $iteration, $start_time, $logfunc, $noerr) = @_;
 
     eval {
@@ -466,76 +461,6 @@ my $run_replication = sub {
        return undef if $noerr;
        die $err;
     }
-};
-
-my $lookup_guest_class = sub {
-    my ($vmtype) = @_;
-
-    if ($vmtype eq 'qemu') {
-       return 'PVE::QemuConfig';
-    } elsif ($vmtype eq 'lxc') {
-       return 'PVE::LXCConfig';
-    } else {
-       die "unknown guest type '$vmtype' - internal error";
-    }
-};
-
-sub run_single_job {
-    my ($jobid, $now, $logfunc) = @_; # passing $now useful for regression testing
-
-    my $local_node = PVE::INotify::nodename();
-
-    my $code = sub {
-       $now //= time();
-
-       my $cfg = PVE::ReplicationConfig->new();
-
-       my $jobcfg = $cfg->{ids}->{$jobid};
-       die "no such job '$jobid'\n" if !$jobcfg;
-
-       die "internal error - not implemented" if $jobcfg->{type} ne 'local';
-
-       die "job '$jobid' is disabled\n" if $jobcfg->{disable};
-
-       my $vms = PVE::Cluster::get_vmlist();
-       my $vmid = $jobcfg->{guest};
-
-       die "no such guest '$vmid'\n" if !$vms->{ids}->{$vmid};
-
-       die "guest '$vmid' is not on local node\n"
-           if $vms->{ids}->{$vmid}->{node} ne $local_node;
-
-       die "unable to sync to local node\n" if $jobcfg->{target} eq $local_node;
-
-       $jobcfg->{id} = $jobid;
-
-       $jobcfg->{vmtype} = $vms->{ids}->{$vmid}->{type};
-
-       my $guest_class = $lookup_guest_class->($jobcfg->{vmtype});
-       $run_replication->($guest_class, $jobcfg, $now, $now, $logfunc);
-    };
-
-    my $res = PVE::Tools::lock_file($pvesr_lock_path, 60, $code);
-    die $@ if $@;
-}
-
-sub run_jobs {
-    my ($now, $logfunc) = @_; # useful for regression testing
-
-    my $iteration = $now // time();
-
-    my $code = sub {
-       my $start_time = $now // time();
-
-       while (my $jobcfg = $get_next_job->($iteration, $start_time)) {
-           my $guest_class = $lookup_guest_class->($jobcfg->{vmtype});
-           $run_replication->($guest_class, $jobcfg, $iteration, $start_time, $logfunc, 1);
-           $start_time = $now // time();
-       }
-    };
-
-    my $res = PVE::Tools::lock_file($pvesr_lock_path, 60, $code);
-    die $@ if $@;
 }
 
 1;
index cdf81f05ebc23b8956f2b27e6aabe6d1403b924f..d435f1b1513710c6dca9c53da89bc8ad3c3da103 100755 (executable)
@@ -15,10 +15,12 @@ use PVE::Cluster;
 use PVE::Storage;
 use PVE::ReplicationConfig;
 use PVE::ReplicationState;
+use PVE::API2::Replication;
 use PVE::Replication;
 use PVE::QemuConfig;
 use PVE::LXC::Config;
 
+
 use Test::MockModule;
 
 our $mocked_nodename = 'node1';
@@ -290,7 +292,7 @@ sub track_jobs {
        }
     }
 
-    PVE::Replication::run_jobs($ctime, $logmsg);
+    PVE::API2::Replication::run_jobs($ctime, $logmsg);
 
     my $new = PVE::Replication::job_status();
 
index 32fe2aa1505707cfc38bbcec000f0c10ef48f674..45479b41135c144373cd5708b8dfa86d0c08dcf6 100755 (executable)
@@ -14,6 +14,8 @@ use Test::MockModule;
 use ReplicationTestEnv;
 use Test::More tests => 1;
 
+use PVE::API2::Replication;
+
 $ReplicationTestEnv::mocked_nodename = 'node1';
 
 my $schedule = [];
@@ -60,7 +62,7 @@ $ReplicationTestEnv::mocked_vm_configs = {
 ReplicationTestEnv::setup();
 
 for (my $i = 0; $i < 61; $i++) {
-    PVE::Replication::run_jobs($i*60);
+    PVE::API2::Replication::run_jobs($i*60);
 }
 
 #print Dumper($schedule);
index 69d09f072df60f3ca84a2945ac013e0e25cbe88b..d6b1f31ffeafb92c5b1d4b41c2e7eebc73e38d8e 100755 (executable)
@@ -12,6 +12,7 @@ use Data::Dumper;
 
 use Test::MockModule;
 use ReplicationTestEnv;
+use PVE::API2::Replication;
 
 use Test::More;
 
@@ -43,7 +44,7 @@ $ReplicationTestEnv::mocked_vm_configs = {
 
 ReplicationTestEnv::setup();
 
-eval { PVE::Replication::run_single_job('job_900_to_node1', 1000); };
+eval { PVE::API2::Replication::run_single_job('job_900_to_node1', 1000); };
 my $err = $@;
 
 is($err, "unable to sync to local node\n", "test error message");