[pve-devel] [PATCH cluster v3 4/7] corosync: transform config to allow easier access

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Sep 18 10:32:55 CEST 2017


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

changes v2 -> v3:
 * use map instead of foreach in transform helpers
 * rename those helper to conciser names

 data/PVE/Corosync.pm | 51 +++++++++++++++++++++++----------------------------
 1 file changed, 23 insertions(+), 28 deletions(-)

diff --git a/data/PVE/Corosync.pm b/data/PVE/Corosync.pm
index eaa5e39..9041d87 100644
--- a/data/PVE/Corosync.pm
+++ b/data/PVE/Corosync.pm
@@ -70,6 +70,15 @@ sub parse_conf {
 	$section->{$key} = $value;
     }
 
+    # make working with the config way easier
+    my $array_to_hash = sub {
+	my ($section_array, $key_id) = @_;
+	return { map { $_->{$key_id} => $_ } @$section_array };
+    };
+    my ($totem, $nodelist) = $conf->{main}->@{"totem", "nodelist"};
+    $nodelist->{node} = &$array_to_hash($nodelist->{node}, 'name');
+    $totem->{interface} = &$array_to_hash($totem->{interface}, 'ringnumber');
+
     $conf->{digest} = $digest;
 
     return $conf;
@@ -107,9 +116,18 @@ $dump_section = sub {
 sub write_conf {
     my ($filename, $conf) = @_;
 
-    die "no main section" if !defined($conf->{main});
+    my $c = clone($conf->{main}) // die "no main section";
+
+    # retransform back for easier dumping
+    my $hash_to_array = sub {
+	my ($hash) = @_;
+	return [ $hash->@{sort keys %$hash} ];
+    };
 
-    my $raw = &$dump_section($conf->{main}, '');
+    $c->{nodelist}->{node} = &$hash_to_array($c->{nodelist}->{node});
+    $c->{totem}->{interface} = &$hash_to_array($c->{totem}->{interface});
+
+    my $raw = &$dump_section($c, '');
 
     return $raw;
 }
@@ -155,7 +173,7 @@ sub update_nodelist {
     my $version = conf_version($conf);
     conf_version($conf, undef, $version + 1);
 
-    $conf->{main}->{nodelist}->{node} = [values %$nodelist];
+    $conf->{main}->{nodelist}->{node} = $nodelist;
 
     PVE::Cluster::cfs_write_file("corosync.conf.new", $conf);
 
@@ -165,35 +183,12 @@ sub update_nodelist {
 
 sub nodelist {
     my ($conf) = @_;
-
-    my $nodelist = {};
-
-    my $nodes = $conf->{main}->{nodelist}->{node};
-
-    foreach my $node (@$nodes) {
-	# use 'name' over 'ring0_addr' if set
-	my $name = $node->{name} // $node->{ring0_addr};
-	if ($name) {
-	    $nodelist->{$name} = $node;
-	}
-    }
-
-    return $nodelist;
+    return clone($conf->{main}->{nodelist}->{node});
 }
 
 sub totem_config {
     my ($conf) = @_;
-
-    # we reorder elements from totem->interface and don't want to change $conf
-    my $totem = clone($conf->{main}->{totem});
-    my $ifs = $totem->{interface};
-
-    $totem->{interface} = {};
-    foreach my $if (@$ifs) {
-	$totem->{interface}->{$if->{ringnumber}} = $if;
-    }
-
-    return $totem;
+    return clone($conf->{main}->{totem});
 }
 
 1;
-- 
2.11.0





More information about the pve-devel mailing list