[pve-devel] [PATCH 2/4] Use new exitcodes instead of integers

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Oct 23 14:04:24 CEST 2015


Use the new exitcodes introduced in the previous commit to gain
some readability.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/HA/Env/PVE2.pm | 22 +++++++++++-----------
 src/PVE/HA/LRM.pm      | 11 ++++++-----
 src/PVE/HA/Manager.pm  |  9 +++++----
 3 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm
index d053dcc..7db8dac 100644
--- a/src/PVE/HA/Env/PVE2.pm
+++ b/src/PVE/HA/Env/PVE2.pm
@@ -12,7 +12,7 @@ use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file
 use PVE::INotify;
 use PVE::RPCEnvironment;
 
-use PVE::HA::Tools;
+use PVE::HA::Tools ':exit_codes';
 use PVE::HA::Env;
 use PVE::HA::Config;
 
@@ -394,7 +394,7 @@ sub exec_resource_agent {
 
     if ($cmd eq 'started') {
 
-	return 0 if $running;
+	return SUCCESS if $running;
 
 	$self->log("info", "starting service $sid");
 
@@ -409,15 +409,15 @@ sub exec_resource_agent {
 
 	if ($running) {
 	    $self->log("info", "service status $sid started");
-	    return 0;
+	    return SUCCESS;
 	} else {
 	    $self->log("warning", "unable to start service $sid");
-	    return 1;
+	    return ERROR;
 	}
 
     } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
 
-	return 0 if !$running;
+	return SUCCESS if !$running;
 
 	$self->log("info", "stopping service $sid");
 
@@ -436,9 +436,9 @@ sub exec_resource_agent {
 
 	if (!$running) {
 	    $self->log("info", "service status $sid stopped");
-	    return 0;
+	    return SUCCESS;
 	} else {
-	    return 1;
+	    return ERROR;
 	}
 
     } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
@@ -448,7 +448,7 @@ sub exec_resource_agent {
 
 	if ($service_config->{node} eq $target) {
 	    # already there
-	    return 0;
+	    return SUCCESS;
 	}
 
 	# we always do (live) migration
@@ -466,10 +466,10 @@ sub exec_resource_agent {
 	# something went wrong if old config file is still there
 	if (-f $oldconfig) {
 	    $self->log("err", "service $sid not moved (migration error)");
-	    return 1;
+	    return ERROR;
 	}
 
-	return 0;
+	return SUCCESS;
 
     } elsif ($cmd eq 'error') {
 
@@ -478,7 +478,7 @@ sub exec_resource_agent {
 	} else {
 	    $self->log("warning", "service $sid is not running and in an error state");
 	}
-	return 0;
+	return SUCCESS; # error always succeeds
 
     }
 
diff --git a/src/PVE/HA/LRM.pm b/src/PVE/HA/LRM.pm
index ed2885f..89177ee 100644
--- a/src/PVE/HA/LRM.pm
+++ b/src/PVE/HA/LRM.pm
@@ -9,7 +9,7 @@ use POSIX qw(:sys_wait_h);
 
 use PVE::SafeSyslog;
 use PVE::Tools;
-use PVE::HA::Tools;
+use PVE::HA::Tools ':exit_codes';
 
 # Server can have several states:
 
@@ -489,13 +489,13 @@ sub handle_service_exitcode {
 
     if ($cmd eq 'started') {
 
-	if ($exit_code == 0) {
+	if ($exit_code == SUCCESS) {
 
 	    $tries->{$sid} = 0;
 
 	    return $exit_code;
 
-	} elsif ($exit_code == 1) {
+	} elsif ($exit_code == ERROR) {
 
 	    $tries->{$sid} = 0 if !defined($tries->{$sid});
 
@@ -504,10 +504,11 @@ sub handle_service_exitcode {
 		$haenv->log('err', "unable to start service $sid on local node".
 			   " after $tries->{$sid} retries");
 		$tries->{$sid} = 0;
-		return 1;
+		return ERROR;
 	    }
 
-	    return 2;
+	    # tell CRM that we retry the start
+	    return ETRY_AGAIN;
 	}
     }
 
diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 3aaecd6..6a98cd3 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -6,6 +6,7 @@ use Digest::MD5 qw(md5_base64);
 
 use Data::Dumper;
 use PVE::Tools;
+use PVE::HA::Tools ':exit_codes';
 use PVE::HA::NodeStatus;
 
 my $fence_delay = 60;
@@ -405,7 +406,7 @@ sub next_state_request_stop {
     # check result from LRM daemon
     if ($lrm_res) {
 	my $exit_code = $lrm_res->{exit_code};
-	if ($exit_code == 0) {
+	if ($exit_code == SUCCESS) {
 	    &$change_service_state($self, $sid, 'stopped');
 	    return;
 	} else {
@@ -429,7 +430,7 @@ sub next_state_migrate_relocate {
     # check result from LRM daemon
     if ($lrm_res) {
 	my $exit_code = $lrm_res->{exit_code};
-	if ($exit_code == 0) {
+	if ($exit_code == SUCCESS) {
 	    &$change_service_state($self, $sid, 'started', node => $sd->{target});
 	    return;
 	} else {
@@ -554,7 +555,7 @@ sub next_state_started {
 
 	    my $try_next = 0;
 	    if ($lrm_res) {
-		if ($lrm_res->{exit_code} == 1) {
+		if ($lrm_res->{exit_code} == ERROR) {
 
 		    my $try = $master_status->{relocate_trial}->{$sid} || 0;
 
@@ -575,7 +576,7 @@ sub next_state_started {
 			return;
 
 		    }
-		} elsif ($lrm_res->{exit_code} == 0) {
+		} elsif ($lrm_res->{exit_code} == SUCCESS) {
 		    $master_status->{relocate_trial}->{$sid} = 0;
 		}
 	    }
-- 
2.1.4




More information about the pve-devel mailing list