[pve-devel] [PATCH ha-manager 1/2] factor out resource config check and default set code

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Oct 28 10:39:37 CEST 2016


Factor the code which does basic checks and sets default settings
out from the PVE2 environment class to the Config class as
read_and_check_resources_config method.

We can reuse this in the HA API status call.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

This was the only thing I need to do to remove the haenv usage in the
API calls.

 src/PVE/HA/Config.pm   | 35 +++++++++++++++++++++++++++++++++++
 src/PVE/HA/Env/PVE2.pm | 31 +------------------------------
 2 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 00b338d..1802a7d 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -84,6 +84,41 @@ sub read_resources_config {
     return cfs_read_file($ha_resources_config);
 }
 
+# checks if resource exists and sets defaults for unset values
+sub read_and_check_resources_config {
+
+    my $res = cfs_read_file($ha_resources_config);
+
+    my $vmlist = PVE::Cluster::get_vmlist();
+    my $conf = {};
+
+    foreach my $sid (keys %{$res->{ids}}) {
+	my $d = $res->{ids}->{$sid};
+	my (undef, undef, $name) = PVE::HA::Tools::parse_sid($sid);
+	$d->{state} = 'enabled' if !defined($d->{state});
+	$d->{max_restart} = 1 if !defined($d->{max_restart});
+	$d->{max_relocate} = 1 if !defined($d->{max_relocate});
+	if (PVE::HA::Resources->lookup($d->{type})) {
+	    if (my $vmd = $vmlist->{ids}->{$name}) {
+		if (!$vmd) {
+		    warn "no such VM '$name'\n";
+		} else {
+		    $d->{node} = $vmd->{node};
+		    $conf->{$sid} = $d;
+		}
+	    } else {
+		if (defined($d->{node})) {
+		    $conf->{$sid} = $d;
+		} else {
+		    warn "service '$sid' without node\n";
+		}
+	    }
+	}
+    }
+
+    return $conf;
+}
+
 sub read_group_config {
 
     return cfs_read_file($ha_groups_config);
diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm
index ef6485d..6b8802e 100644
--- a/src/PVE/HA/Env/PVE2.pm
+++ b/src/PVE/HA/Env/PVE2.pm
@@ -113,36 +113,7 @@ sub read_crm_commands {
 sub read_service_config {
     my ($self) = @_;
 
-    my $res = PVE::HA::Config::read_resources_config();
-
-    my $vmlist = PVE::Cluster::get_vmlist();
-    my $conf = {};
-
-    foreach my $sid (keys %{$res->{ids}}) {
-	my $d = $res->{ids}->{$sid};
-	my (undef, undef, $name) = PVE::HA::Tools::parse_sid($sid);
-	$d->{state} = 'enabled' if !defined($d->{state});
-	$d->{max_restart} = 1 if !defined($d->{max_restart});
-	$d->{max_relocate} = 1 if !defined($d->{max_relocate});
-	if (PVE::HA::Resources->lookup($d->{type})) {
-	    if (my $vmd = $vmlist->{ids}->{$name}) {
-		if (!$vmd) {
-		    warn "no such VM '$name'\n";
-		} else {
-		    $d->{node} = $vmd->{node};
-		    $conf->{$sid} = $d;
-		}
-	    } else {
-		if (defined($d->{node})) {
-		    $conf->{$sid} = $d;
-		} else {
-		    warn "service '$sid' without node\n";
-		}
-	    }
-	}
-    }
-
-    return $conf;
+    return PVE::HA::Config::read_and_check_resources_config();
 }
 
 sub read_fence_config {
-- 
2.1.4





More information about the pve-devel mailing list