[pve-devel] [PATCH 3/5] api2 : node : add migrate_all

Alexandre Derumier aderumier at odiso.com
Wed Mar 13 14:27:59 CET 2013


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/API2/Nodes.pm |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index da1737a..3c0fafb 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -1104,6 +1104,104 @@ __PACKAGE__->register_method ({
 	
     }});
 
+my $create_migrate_worker = sub {
+    my ($nodename, $type, $vmid, $target) = @_;
+
+    my $upid;
+    if ($type eq 'openvz') {
+	my $online = PVE::OpenVZ::check_running($vmid) ? 1 : 0;
+	print STDERR "Migrating CT $vmid\n";
+	$upid = PVE::API2::OpenVZ->migrate_vm({node => $nodename, vmid => $vmid, target => $target,
+						online => $online });
+    } elsif ($type eq 'qemu') {
+	my $online = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
+	print STDERR "Migrating VM $vmid\n";
+	$upid = PVE::API2::Qemu->migrate_vm({node => $nodename, vmid => $vmid, target => $target,
+					      online => $online });
+    } else {
+	die "unknown VM type '$type'\n";
+    }
+
+    my $res = PVE::Tools::upid_decode($upid);
+
+    return $res->{pid};
+};
+
+__PACKAGE__->register_method ({
+    name => 'migrateall',
+    path => 'migrateall',
+    method => 'POST',
+    protected => 1,
+    description => "Migrate all VMs and Containers.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+            target => get_standard_option('pve-node', { description => "Target node." }),
+            maxworkers => {
+                description => "Max parralel migration job.",
+                type => 'integer',
+                minimum => 1
+            },
+	},
+    },
+    returns => {
+	type => 'string',
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $authuser = $rpcenv->get_user();
+
+	my $nodename = $param->{node};
+	$nodename = PVE::INotify::nodename() if $nodename eq 'localhost';
+
+        my $target = $param->{target};
+        my $maxWorkers = $param->{maxworkers};
+
+	my $code = sub {
+
+	    $rpcenv->{type} = 'priv'; # to start tasks in background
+
+	    my $migrateList = &$get_start_stop_list($nodename);
+
+	    foreach my $order (sort {$b <=> $a} keys %$migrateList) {
+		my $vmlist = $migrateList->{$order};
+		my $workers = {};
+		foreach my $vmid (sort {$b <=> $a} keys %$vmlist) {
+		    my $d = $vmlist->{$vmid};
+		    my $pid;
+		    eval { $pid = &$create_migrate_worker($nodename, $d->{type}, $vmid, $target); };
+		    warn $@ if $@;
+		    next if !$pid;
+
+		    $workers->{$pid} = 1;
+		    while (scalar(keys %$workers) >= $maxWorkers) {
+			foreach my $p (keys %$workers) {
+			    if (!PVE::ProcFSTools::check_process_running($p)) {
+				delete $workers->{$p};
+			    }
+			}
+			sleep(1);
+		    }
+		}
+		while (scalar(keys %$workers)) {
+		    foreach my $p (keys %$workers) {
+			if (!PVE::ProcFSTools::check_process_running($p)) {
+			    delete $workers->{$p};
+			}
+		    }
+		    sleep(1);
+		}
+	    }
+	    return;
+	};
+
+	return $rpcenv->fork_worker('migrateall', undef, $authuser, $code);
+	
+    }});
+
 package PVE::API2::Nodes;
 
 use strict;
-- 
1.7.10.4




More information about the pve-devel mailing list