[pve-devel] [RFC pve-manager 2/2] PVE::Replication - remove dependency to PVE::LXC/PVE::QemuServer

Dietmar Maurer dietmar at proxmox.com
Fri Jun 9 09:55:34 CEST 2017


So that we can move the whole class to package pve-guest-common.

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/API2/Replication.pm        | 78 ++++++++++++++++++++++++++++++++++++++++
 PVE/CLI/pvesr.pm               |  4 +--
 PVE/Replication.pm             | 81 ++----------------------------------------
 bin/test/ReplicationTestEnv.pm |  4 ++-
 bin/test/replication_test2.pl  |  4 ++-
 bin/test/replication_test3.pl  |  3 +-
 6 files changed, 91 insertions(+), 83 deletions(-)

diff --git a/PVE/API2/Replication.pm b/PVE/API2/Replication.pm
index 6731f096..a1ffa318 100644
--- a/PVE/API2/Replication.pm
+++ b/PVE/API2/Replication.pm
@@ -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) = @_;
 
diff --git a/PVE/CLI/pvesr.pm b/PVE/CLI/pvesr.pm
index f64a3103..078dba05 100644
--- a/PVE/CLI/pvesr.pm
+++ b/PVE/CLI/pvesr.pm
@@ -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;
diff --git a/PVE/Replication.pm b/PVE/Replication.pm
index 0e34dc53..ee3245a0 100644
--- a/PVE/Replication.pm
+++ b/PVE/Replication.pm
@@ -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;
diff --git a/bin/test/ReplicationTestEnv.pm b/bin/test/ReplicationTestEnv.pm
index ce72fa9c..178f4aab 100755
--- a/bin/test/ReplicationTestEnv.pm
+++ b/bin/test/ReplicationTestEnv.pm
@@ -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';
@@ -283,7 +285,7 @@ sub track_jobs {
 	}
     }
 
-    PVE::Replication::run_jobs($ctime, $logmsg);
+    PVE::API2::Replication::run_jobs($ctime, $logmsg);
 
     my $new = PVE::Replication::job_status();
 
diff --git a/bin/test/replication_test2.pl b/bin/test/replication_test2.pl
index 32fe2aa1..45479b41 100755
--- a/bin/test/replication_test2.pl
+++ b/bin/test/replication_test2.pl
@@ -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);
diff --git a/bin/test/replication_test3.pl b/bin/test/replication_test3.pl
index 69d09f07..d6b1f31f 100755
--- a/bin/test/replication_test3.pl
+++ b/bin/test/replication_test3.pl
@@ -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");
-- 
2.11.0




More information about the pve-devel mailing list