[pve-devel] [v2 qemu-server 02/10] migrate: refactor mtunnel read/write

Fabian Grünbichler f.gruenbichler at proxmox.com
Fri Aug 4 14:53:59 CEST 2017


to make adding new commands and reading replies easier

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 PVE/QemuMigrate.pm | 48 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/PVE/QemuMigrate.pm b/PVE/QemuMigrate.pm
index 0376a81..beaa1e3 100644
--- a/PVE/QemuMigrate.pm
+++ b/PVE/QemuMigrate.pm
@@ -91,6 +91,40 @@ sub finish_command_pipe {
 	if !&$collect_child_process();
 }
 
+sub read_tunnel {
+    my ($self, $tunnel, $timeout) = @_;
+
+    $timeout = 60 if !defined($timeout);
+
+    my $reader = $tunnel->{reader};
+
+    my $output;
+    eval {
+	PVE::Tools::run_with_timeout($timeout, sub { $output = <$reader>; });
+    };
+    die "reading from tunnel failed: $@\n" if $@;
+
+    chomp $output;
+
+    return $output;
+}
+
+sub write_tunnel {
+    my ($self, $tunnel, $timeout, $command) = @_;
+
+    $timeout = 60 if !defined($timeout);
+
+    my $writer = $tunnel->{writer};
+
+    eval {
+	PVE::Tools::run_with_timeout($timeout, sub {
+	    print $writer "$command\n";
+	    $writer->flush();
+	});
+    };
+    die "writing to tunnel failed: $@\n" if $@;
+}
+
 sub fork_tunnel {
     my ($self, $tunnel_addr) = @_;
 
@@ -100,11 +134,8 @@ sub fork_tunnel {
 
     my $tunnel = $self->fork_command_pipe($cmd);
 
-    my $reader = $tunnel->{reader};
-
-    my $helo;
     eval {
-	PVE::Tools::run_with_timeout(60, sub { $helo = <$reader>; });
+	my $helo = $self->read_tunnel($tunnel, 60);
 	die "no reply\n" if !$helo;
 	die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
 	die "got strange reply from mtunnel ('$helo')\n"
@@ -122,14 +153,7 @@ sub fork_tunnel {
 sub finish_tunnel {
     my ($self, $tunnel) = @_;
 
-    my $writer = $tunnel->{writer};
-
-    eval {
-	PVE::Tools::run_with_timeout(30, sub {
-	    print $writer "quit\n";
-	    $writer->flush();
-	});
-    };
+    eval { $self->write_tunnel($tunnel, 30, 'quit'); };
     my $err = $@;
 
     $self->finish_command_pipe($tunnel, 30);
-- 
2.11.0





More information about the pve-devel mailing list