[pve-devel] [RFC ha-manager 8/8] Move exec_resource_agent from environment classes to LRM

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Jan 22 17:06:42 CET 2016


With the changes and preparation work from the previous commits
we can now move the quite important method exec_resource_agent
from the Env classes to the LRM where it get's called.

The main advantage of this is that it now underlies regression
tests and that we do not have two separate methods where it
- does not make sense as agents them self should be virtualized
  not the method executing them
- adds more work as the must (or at least should) be in sync

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/HA/Env.pm      |   6 ---
 src/PVE/HA/Env/PVE2.pm | 107 ---------------------------------------------
 src/PVE/HA/LRM.pm      | 115 ++++++++++++++++++++++++++++++++++++++++++++++++-
 src/PVE/HA/Sim/Env.pm  |  98 -----------------------------------------
 4 files changed, 113 insertions(+), 213 deletions(-)

diff --git a/src/PVE/HA/Env.pm b/src/PVE/HA/Env.pm
index af3b31c..812346b 100644
--- a/src/PVE/HA/Env.pm
+++ b/src/PVE/HA/Env.pm
@@ -217,12 +217,6 @@ sub lookup_service_plugin {
     return $self->{plug}->lookup_service_plugin($service_type);
 }
 
-sub exec_resource_agent {
-    my ($self, $sid, $service_config, $cmd, @params) = @_;
-
-    return $self->{plug}->exec_resource_agent($sid, $service_config, $cmd, @params)
-}
-
 # hack to support regression tests
 sub can_fork {
     my ($self) = @_;
diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm
index e2a4e8e..4968eba 100644
--- a/src/PVE/HA/Env/PVE2.pm
+++ b/src/PVE/HA/Env/PVE2.pm
@@ -400,112 +400,5 @@ sub lookup_service_plugin {
     return PVE::HA::Resources->lookup($service_type);
 }
 
-sub exec_resource_agent {
-    my ($self, $sid, $service_config, $cmd, @params) = @_;
-
-    # setup execution environment
-
-    $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
-
-    PVE::INotify::inotify_close();
-
-    PVE::INotify::inotify_init();
-
-    PVE::Cluster::cfs_update();
-
-    my $nodename = $self->{nodename};
-
-    my (undef, $service_type, $service_name) = PVE::HA::Tools::parse_sid($sid);
-
-    my $plugin = PVE::HA::Resources->lookup($service_type);
-    if (!$plugin) {
-	$self->log('err', "service type '$service_type' not implemented");
-	return EUNKNOWN_SERVICE_TYPE;
-    }
-
-    if ($service_config->{node} ne $nodename) {
-	$self->log('err', "service '$sid' not on this node");
-	return EWRONG_NODE;
-    }
-
-    my $vmid = $service_name;
-
-    my $running = $plugin->check_running($self, $vmid);
-
-    if ($cmd eq 'started') {
-
-	return SUCCESS if $running;
-
-	$self->log("info", "starting service $sid");
-
-	$plugin->start($self, $vmid);
-
-	$running = $plugin->check_running($self, $vmid);
-
-	if ($running) {
-	    $self->log("info", "service status $sid started");
-	    return SUCCESS;
-	} else {
-	    $self->log("warning", "unable to start service $sid");
-	    return ERROR;
-	}
-
-    } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
-
-	return SUCCESS if !$running;
-
-	$self->log("info", "stopping service $sid");
-
-	$plugin->shutdown($self, $vmid);
-
-	$running = $plugin->check_running($self, $vmid);
-
-	if (!$running) {
-	    $self->log("info", "service status $sid stopped");
-	    return SUCCESS;
-	} else {
-	    $self->log("info", "unable to stop stop service $sid (still running)");
-	    return ERROR;
-	}
-
-    } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
-
-	my $target = $params[0];
-	if (!defined($target)) {
-	    die "$cmd '$sid' failed - missing target\n" if !defined($target);
-	    return EINVALID_PARAMETER;
-	}
-
-	if ($service_config->{node} eq $target) {
-	    # already there
-	    return SUCCESS;
-	}
-
-	my $online = ($cmd eq 'migrate') ? 1 : 0;
-
-	$plugin->migrate($self, $vmid, $target, $online);
-
-	# something went wrong if service is still on this node
-	if ($plugin->is_on_node($self, $nodename, $vmid)) {
-	    $self->log("err", "service $sid not moved (migration error)");
-	    return ERROR;
-	}
-
-	return SUCCESS;
-
-    } elsif ($cmd eq 'error') {
-
-	if ($running) {
-	    $self->log("err", "service $sid is in an error state while running");
-	} else {
-	    $self->log("warning", "service $sid is not running and in an error state");
-	}
-	return SUCCESS; # error always succeeds
-
-    }
-
-    $self->log("err", "implement me (cmd '$cmd')");
-    return EUNKNOWN_COMMAND;
-}
 
 1;
diff --git a/src/PVE/HA/LRM.pm b/src/PVE/HA/LRM.pm
index ce076be..e7ac663 100644
--- a/src/PVE/HA/LRM.pm
+++ b/src/PVE/HA/LRM.pm
@@ -390,7 +390,7 @@ sub run_workers {
 			# do work
 			my $res = -1;
 			eval {
-			    $res = $haenv->exec_resource_agent($sid, $cd, $w->{state}, $w->{target});
+			    $res = $self->exec_resource_agent($sid, $cd, $w->{state}, $w->{target});
 			};
 			if (my $err = $@) {
 			    $haenv->log('err', $err);
@@ -404,7 +404,7 @@ sub run_workers {
 		} else {
 		    my $res = -1;
 		    eval {
-			$res = $haenv->exec_resource_agent($sid, $cd, $w->{state}, $w->{target});
+			$res = $self->exec_resource_agent($sid, $cd, $w->{state}, $w->{target});
 			$res = $res << 8 if $res > 0;
 		    };
 		    if (my $err = $@) {
@@ -601,4 +601,115 @@ sub handle_service_exitcode {
 
 }
 
+sub exec_resource_agent {
+    my ($self, $sid, $service_config, $cmd, @params) = @_;
+
+    # setup execution environment
+
+    $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+
+    PVE::INotify::inotify_close();
+
+    PVE::INotify::inotify_init();
+
+    PVE::Cluster::cfs_update();
+
+    my $haenv = $self->{haenv};
+
+    my $nodename = $haenv->nodename();
+
+    my (undef, $service_type, $service_name) = PVE::HA::Tools::parse_sid($sid);
+
+    my $plugin = $haenv->lookup_service_plugin($service_type);
+    if (!$plugin) {
+	$haenv->log('err', "service type '$service_type' not implemented");
+	return EUNKNOWN_SERVICE_TYPE;
+    }
+
+    if ($service_config->{node} ne $nodename) {
+	$haenv->log('err', "service '$sid' not on this node");
+	return EWRONG_NODE;
+    }
+
+    my $id = $service_name;
+
+    my $running = $plugin->check_running($haenv, $id);
+
+    if ($cmd eq 'started') {
+
+	return SUCCESS if $running;
+
+	$haenv->log("info", "starting service $sid");
+
+	$plugin->start($haenv, $id);
+
+	$running = $plugin->check_running($haenv, $id);
+
+	if ($running) {
+	    $haenv->log("info", "service status $sid started");
+	    return SUCCESS;
+	} else {
+	    $haenv->log("warning", "unable to start service $sid");
+	    return ERROR;
+	}
+
+    } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
+
+	return SUCCESS if !$running;
+
+	$haenv->log("info", "stopping service $sid");
+
+	$plugin->shutdown($haenv, $id);
+
+	$running = $plugin->check_running($haenv, $id);
+
+	if (!$running) {
+	    $haenv->log("info", "service status $sid stopped");
+	    return SUCCESS;
+	} else {
+	    $haenv->log("info", "unable to stop stop service $sid (still running)");
+	    return ERROR;
+	}
+
+    } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
+
+	my $target = $params[0];
+	if (!defined($target)) {
+	    die "$cmd '$sid' failed - missing target\n" if !defined($target);
+	    return EINVALID_PARAMETER;
+	}
+
+	if ($service_config->{node} eq $target) {
+	    # already there
+	    return SUCCESS;
+	}
+
+	my $online = ($cmd eq 'migrate') ? 1 : 0;
+
+	$plugin->migrate($haenv, $id, $target, $online);
+
+	# something went wrong if service is still on this node
+	if ($plugin->is_on_node($haenv, $nodename, $id)) {
+	    $haenv->log("err", "service $sid not moved (migration error)");
+	    return ERROR;
+	}
+
+	return SUCCESS;
+
+    } elsif ($cmd eq 'error') {
+
+	if ($running) {
+	    $haenv->log("err", "service $sid is in an error state while running");
+	} else {
+	    $haenv->log("warning", "service $sid is not running and in an error state");
+	}
+	return SUCCESS; # error always succeeds
+
+    }
+
+    $haenv->log("err", "implement me (cmd '$cmd')");
+    return EUNKNOWN_COMMAND;
+}
+
+
 1;
diff --git a/src/PVE/HA/Sim/Env.pm b/src/PVE/HA/Sim/Env.pm
index dece14e..0628c03 100644
--- a/src/PVE/HA/Sim/Env.pm
+++ b/src/PVE/HA/Sim/Env.pm
@@ -318,103 +318,5 @@ sub lookup_service_plugin {
     return PVE::HA::Sim::Resources->lookup($service_type);
 }
 
-sub exec_resource_agent {
-    my ($self, $sid, $cd, $cmd, @params) = @_;
-
-    my $hardware = $self->{hardware};
-
-    my $nodename = $self->{nodename};
-
-    # fixme: return valid_exit code (instead of using die)
-
-    my $ss = $hardware->read_service_status($nodename);
-
-    if ($cmd eq 'started') {
-
-	# fixme: return valid_exit code
-	die "service '$sid' not on this node" if $cd->{node} ne $nodename;
-
-	if ($ss->{$sid}) {
-	    return 0;
-	}
-	$self->log("info", "starting service $sid");
-	
-	$self->sleep(2);
-
-	$ss->{$sid} = 1;
-	$hardware->write_service_status($nodename, $ss);
-
-	$self->log("info", "service status $sid started");
-
-	return 0;
-
-    } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
-
-	# fixme: return valid_exit code
-	die "service '$sid' not on this node" if $cd->{node} ne $nodename;
-
-	if (!$ss->{$sid}) {
-	    return 0;
-	}
-	$self->log("info", "stopping service $sid");
-	
-	$self->sleep(2);
-
-	$ss->{$sid} = 0;
-	$hardware->write_service_status($nodename, $ss);
-
-	$self->log("info", "service status $sid stopped");
-
-	return 0;
-
-    } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
-
-	if ($cd->{type} eq 'ct' && $cmd eq 'migrate' && $ss->{$sid}) {
-	    $self->log('err', "unable to live migrate running container");
-	    return 1;
-	}
-
-	my $target = $params[0];
-	die "$cmd '$sid' failed - missing target\n" if !defined($target);
-
-	if ($cd->{node} eq $target) {
-	    # already migrate
-	    return 0;
-	} elsif ($cd->{node} eq $nodename) {
-
-	    $self->log("info", "service $sid - start $cmd to node '$target'");
-
-	    if ($cmd eq 'relocate') {
-
-		if ($ss->{$sid}) {
-		    $self->log("info", "stopping service $sid (relocate)");
-		    $self->sleep(1); # time to stop service
-		    $ss->{$sid} = 0;
-		    $hardware->write_service_status($nodename, $ss);
-		}
-
-		$self->log("info", "service status $sid stopped");
-
-	    } else {
-		$self->sleep(2); # (live) migration time
-	    }
-
-	    $self->change_service_location($sid, $nodename, $target);
-	    $self->log("info", "service $sid - end $cmd to node '$target'");
-	    # ensure that the old node doesn't has the service anymore
-	    $ss->{$sid} = 0;
-	    $hardware->write_service_status($nodename, $ss);
-
-	    return 0;
-
-	} else {
-	    die "migrate '$sid'  failed - service is not on this node\n";
-	}
-	
-	
-    }
-
-    die "implement me (cmd '$cmd')";
-}
 
 1;
-- 
2.1.4





More information about the pve-devel mailing list