]> git.proxmox.com Git - pve-cluster.git/commitdiff
corosync: transform config to allow easier access
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 20 Sep 2017 13:11:03 +0000 (15:11 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 20 Sep 2017 13:16:49 +0000 (15:16 +0200)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
data/PVE/Corosync.pm

index eaa5e39a1e58fbf56c4cca9081f76755719a62c3..0ee74b8f9073097f529d13522cf147952beaf303 100644 (file)
@@ -70,6 +70,11 @@ sub parse_conf {
        $section->{$key} = $value;
     }
 
+    # make working with the config way easier
+    my ($totem, $nodelist) = $conf->{main}->@{"totem", "nodelist"};
+    $nodelist->{node} = { map { $_->{name} // $_->{ring0_addr} => $_ } @{$nodelist->{node}} };
+    $totem->{interface} = { map { $_->{ringnumber} => $_ } @{$totem->{interface}} };
+
     $conf->{digest} = $digest;
 
     return $conf;
@@ -107,9 +112,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 +169,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 +179,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;