[pve-devel] [PATCH cluster 4/7] corosync: transform config to allow easier access
Thomas Lamprecht
t.lamprecht at proxmox.com
Thu Aug 3 10:03:08 CEST 2017
Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
changes v1 -> v2:
* new, makes working with the config now way more easier as the old
cumbersome representation
data/PVE/Corosync.pm | 64 +++++++++++++++++++++++++++++-----------------------
1 file changed, 36 insertions(+), 28 deletions(-)
diff --git a/data/PVE/Corosync.pm b/data/PVE/Corosync.pm
index 3c4c8c0..1180316 100644
--- a/data/PVE/Corosync.pm
+++ b/data/PVE/Corosync.pm
@@ -15,6 +15,29 @@ my $conf_array_sections = {
interface => 1,
};
+my $transform_section_to_hash = sub {
+ my ($section, $id_key) = @_;
+
+ my $transformed = {};
+ foreach my $v (@$section) {
+ my $key = $v->{$id_key} // die "section entry misses index key '$id_key'\n";
+ $transformed->{$key} = $v;
+ }
+
+ return $transformed;
+};
+
+my $transform_hash_to_section = sub {
+ my ($hash) = @_;
+
+ my $transformed = [];
+ foreach my $k (sort keys %$hash) {
+ push @$transformed, $hash->{$k};
+ }
+
+ return $transformed;
+};
+
# a very simply parser ...
sub parse_conf {
my ($filename, $raw) = @_;
@@ -70,6 +93,10 @@ sub parse_conf {
$section->{$key} = $value;
}
+ # makes working with the config way more easier
+ $conf->{main}->{nodelist}->{node} = &$transform_section_to_hash($conf->{main}->{nodelist}->{node}, 'name');
+ $conf->{main}->{totem}->{interface} = &$transform_section_to_hash($conf->{main}->{totem}->{interface}, 'ringnumber');
+
$conf->{digest} = $digest;
return $conf;
@@ -108,9 +135,13 @@ $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";
- my $raw = &$dump_section($conf->{main}, '');
+ # retransform back for easier dumping
+ $c->{nodelist}->{node} = &$transform_hash_to_section($c->{nodelist}->{node});
+ $c->{totem}->{interface} = &$transform_hash_to_section($c->{totem}->{interface});
+
+ my $raw = &$dump_section($c, '');
return $raw;
}
@@ -156,7 +187,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);
@@ -166,35 +197,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