[pve-devel] [PATCH v4 ha-manager 3/5] Add crm command 'stop'

Fabian Ebner f.ebner at proxmox.com
Thu Oct 10 12:25:07 CEST 2019


Not every command parameter is 'target' anymore, so
it was necessary to modify the parsing of $sd->{cmd}.

Just changing the state to request_stop is not enough,
we need to actually update the service configuration as well.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 src/PVE/HA/Manager.pm      | 27 +++++++++++++++++++++++----
 src/PVE/HA/Sim/Hardware.pm |  8 ++++++++
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 5137de8..617e369 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -349,6 +349,14 @@ sub update_crm_commands {
 		$haenv->log('err', "crm command error - no such service: $cmd");
 	    }
 
+	} elsif ($cmd =~ m/^stop\s+(\S+)\s+(\S+)$/) {
+	    my ($sid, $timeout) = ($1, $2);
+	    if (my $sd = $ss->{$sid}) {
+		$haenv->log('info', "got crm command: $cmd");
+		$ss->{$sid}->{cmd} = [ 'stop', $timeout ];
+	    } else {
+		$haenv->log('err', "crm command error - no such service: $cmd");
+	    }
 	} else {
 	    $haenv->log('err', "unable to parse crm command: $cmd");
 	}
@@ -561,10 +569,10 @@ sub next_state_stopped {
     }
 
     if ($sd->{cmd}) {
-	my ($cmd, $target) = @{$sd->{cmd}};
-	delete $sd->{cmd};
+	my $cmd = shift @{$sd->{cmd}};
 
 	if ($cmd eq 'migrate' || $cmd eq 'relocate') {
+	    my $target = shift @{$sd->{cmd}};
 	    if (!$ns->node_is_online($target)) {
 		$haenv->log('err', "ignore service '$sid' $cmd request - node '$target' not online");
 	    } elsif ($sd->{node} eq $target) {
@@ -574,9 +582,12 @@ sub next_state_stopped {
 				       target => $target);
 		return;
 	    }
+	} elsif ($cmd eq 'stop') {
+		$haenv->log('info', "ignore service '$sid' $cmd request - service already stopped");
 	} else {
 	    $haenv->log('err', "unknown command '$cmd' for service '$sid'");
 	}
+	delete $sd->{cmd};
     }
 
     if ($cd->{state} eq 'disabled') {
@@ -638,10 +649,10 @@ sub next_state_started {
     if ($cd->{state} eq 'started') {
 
 	if ($sd->{cmd}) {
-	    my ($cmd, $target) = @{$sd->{cmd}};
-	    delete $sd->{cmd};
+	    my $cmd = shift @{$sd->{cmd}};
 
 	    if ($cmd eq 'migrate' || $cmd eq 'relocate') {
+		my $target = shift @{$sd->{cmd}};
 		if (!$ns->node_is_online($target)) {
 		    $haenv->log('err', "ignore service '$sid' $cmd request - node '$target' not online");
 		} elsif ($sd->{node} eq $target) {
@@ -650,9 +661,17 @@ sub next_state_started {
 		    $haenv->log('info', "$cmd service '$sid' to node '$target'");
 		    &$change_service_state($self, $sid, $cmd, node => $sd->{node}, target => $target);
 		}
+	    } elsif ($cmd eq 'stop') {
+		my $timeout = shift @{$sd->{cmd}};
+		$haenv->log('info', "$cmd service with timeout '$timeout'");
+		&$change_service_state($self, $sid, 'request_stop', timeout => $timeout);
+		$haenv->update_service_config($sid, {'state' => 'stopped'});
 	    } else {
 		$haenv->log('err', "unknown command '$cmd' for service '$sid'");
 	    }
+
+	    delete $sd->{cmd};
+
 	} else {
 
 	    my $try_next = 0;
diff --git a/src/PVE/HA/Sim/Hardware.pm b/src/PVE/HA/Sim/Hardware.pm
index 3cdc85b..121cd1b 100644
--- a/src/PVE/HA/Sim/Hardware.pm
+++ b/src/PVE/HA/Sim/Hardware.pm
@@ -541,6 +541,7 @@ sub get_cfs_state {
 # restart-lrm <node>
 # service <sid> <started|disabled|stopped|ignored>
 # service <sid> <migrate|relocate> <target>
+# service <sid> stop <timeout>
 # service <sid> lock/unlock [lockname]
 
 sub sim_hardware_cmd {
@@ -658,6 +659,13 @@ sub sim_hardware_cmd {
 
 		$self->queue_crm_commands_nolock("$action $sid $param");
 
+	    } elsif ($action eq 'stop') {
+
+		die "sim_hardware_cmd: missing timeout for '$action' command"
+		    if !defined($param);
+
+		$self->queue_crm_commands_nolock("$action $sid $param");
+
 	    } elsif ($action eq 'add') {
 
 		$self->add_service($sid, {state => 'started', node => $param});
-- 
2.20.1





More information about the pve-devel mailing list