]> git.proxmox.com Git - pve-ha-manager.git/blobdiff - src/PVE/HA/Tools.pm
Tools/Config: refactor lrm status json reading
[pve-ha-manager.git] / src / PVE / HA / Tools.pm
index 4a5c2e351f3e7f961f25b2f2a8882a5ac1f92985..2bdd6eaff65e05dabaef2561b052b1f880bb793e 100644 (file)
@@ -6,6 +6,26 @@ use JSON;
 use PVE::JSONSchema;
 use PVE::Tools;
 use PVE::Cluster;
+use PVE::ProcFSTools;
+
+# return codes used in the ha environment
+# mainly by the resource agents
+use constant {
+    SUCCESS => 0, # action finished as expected
+    ERROR => 1, # action was erroneous
+    ETRY_AGAIN => 2, # action was erroneous and needs to be repeated
+    EWRONG_NODE => 3, # needs to fixup the service location
+    EUNKNOWN_SERVICE_TYPE => 4, # no plugin for this type service found
+    EUNKNOWN_COMMAND => 5,
+    EINVALID_PARAMETER => 6,
+    EUNKNOWN_SERVICE => 7, # service not found
+};
+
+# get constants out of package in a somewhat easy way
+use base 'Exporter';
+our @EXPORT_OK = qw(SUCCESS ERROR EWRONG_NODE EUNKNOWN_SERVICE_TYPE
+ EUNKNOWN_COMMAND EINVALID_PARAMETER ETRY_AGAIN EUNKNOWN_SERVICE);
+our %EXPORT_TAGS = ( 'exit_codes' => [@EXPORT_OK] );
 
 PVE::JSONSchema::register_format('pve-ha-resource-id', \&pve_verify_ha_resource_id);
 sub pve_verify_ha_resource_id {
@@ -53,7 +73,8 @@ sub pve_verify_ha_group_node {
 }
 
 PVE::JSONSchema::register_standard_option('pve-ha-group-node-list', {
-    description => "List of cluster node names with optional priority. We use priority '0' as default. The CRM tries to run services on the node with highest priority (also see option 'nofailback').",
+    description => "List of cluster node names with optional priority.",
+    verbose_description => "List of cluster node members, where a priority can be given to each node. A resource bound to a group will run on the available nodes with the highest priority. If there are more nodes in the highest priority class, the services will get distributed to those nodes. The priorities have a relative meaning only.",
     type => 'string', format => 'pve-ha-group-node-list',
     typetext => '<node>[:<pri>]{,<node>[:<pri>]}*',
 });
@@ -138,6 +159,51 @@ sub count_fenced_services {
     return $count;
 }
 
+sub get_verbose_service_state {
+    my ($service_state, $service_conf) = @_;
+
+    my $req = $service_conf->{state} // 'ignored';
+    return 'ignored' if $req eq 'ignored';
+
+    # service not yet processed by manager
+    return 'queued' if !defined($service_state);
+    my $cur = $service_state->{state};
+
+    # give fast feedback to the user
+    my $state = $cur;
+    if (!defined($cur)) {
+       $state = 'queued';
+    } elsif ($cur eq 'stopped') {
+       if ($req eq 'started') {
+           $state = 'starting';
+       } elsif ($req eq 'disabled') {
+           $state = 'disabled';
+       }
+    } elsif ($cur eq 'started') {
+       if ($req eq 'stopped' || $req eq 'disabled') {
+           $state = 'stopping';
+       }
+       $state = 'starting' if !$service_state->{running};
+    } elsif ($cur eq 'error') {
+       if ($req eq 'disabled') {
+           $state = 'clearing error flag';
+       }
+    }
+
+    return $state;
+}
+
+sub upid_wait {
+    my ($upid, $haenv) = @_;
+
+    my $waitfunc = sub {
+       my $task = PVE::Tools::upid_encode(shift);
+       $haenv->log('info', "Task '$task' still active, waiting");
+    };
+
+    PVE::ProcFSTools::upid_wait($upid, $waitfunc, 5);
+}
+
 # bash auto completion helper
 
 sub complete_sid {
@@ -184,8 +250,8 @@ sub complete_enabled_sid {
 
     my $res = [];
     foreach my $sid (keys %{$cfg->{ids}}) {
-       my $state = $cfg->{ids}->{$sid}->{state} // 'enabled';
-       next if $state ne 'enabled';
+       my $state = $cfg->{ids}->{$sid}->{state} // 'started';
+       next if $state ne 'started';
        push @$res, $sid;
     }
 
@@ -198,8 +264,8 @@ sub complete_disabled_sid {
 
     my $res = [];
     foreach my $sid (keys %{$cfg->{ids}}) {
-       my $state = $cfg->{ids}->{$sid}->{state} // 'enabled';
-       next if $state eq 'enabled';
+       my $state = $cfg->{ids}->{$sid}->{state} // 'started';
+       next if $state eq 'started';
        push @$res, $sid;
     }