]>
Commit | Line | Data |
---|---|---|
b6973a89 TL |
1 | package PVE::Corosync; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Digest::SHA; | |
496de919 | 7 | use Clone 'clone'; |
b6973a89 TL |
8 | |
9 | use PVE::Cluster; | |
10 | ||
11 | my $basedir = "/etc/pve"; | |
12 | ||
496de919 TL |
13 | my $conf_array_sections = { |
14 | node => 1, | |
15 | interface => 1, | |
16 | }; | |
17 | ||
b6973a89 TL |
18 | # a very simply parser ... |
19 | sub parse_conf { | |
20 | my ($filename, $raw) = @_; | |
21 | ||
22 | return {} if !$raw; | |
23 | ||
24 | my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : ''); | |
25 | ||
26 | $raw =~ s/#.*$//mg; | |
27 | $raw =~ s/\r?\n/ /g; | |
28 | $raw =~ s/\s+/ /g; | |
29 | $raw =~ s/^\s+//; | |
30 | $raw =~ s/\s*$//; | |
31 | ||
32 | my @tokens = split(/\s/, $raw); | |
33 | ||
496de919 | 34 | my $conf = { 'main' => {} }; |
b6973a89 TL |
35 | |
36 | my $stack = []; | |
496de919 | 37 | my $section = $conf->{main}; |
b6973a89 TL |
38 | |
39 | while (defined(my $token = shift @tokens)) { | |
40 | my $nexttok = $tokens[0]; | |
41 | ||
42 | if ($nexttok && ($nexttok eq '{')) { | |
43 | shift @tokens; # skip '{' | |
496de919 TL |
44 | my $new_section = {}; |
45 | if ($conf_array_sections->{$token}) { | |
46 | $section->{$token} = [] if !defined($section->{$token}); | |
47 | push @{$section->{$token}}, $new_section; | |
48 | } elsif (!defined($section->{$token})) { | |
49 | $section->{$token} = $new_section; | |
50 | } else { | |
51 | die "section '$token' already exists and not marked as array!\n"; | |
52 | } | |
b6973a89 TL |
53 | push @$stack, $section; |
54 | $section = $new_section; | |
55 | next; | |
56 | } | |
57 | ||
58 | if ($token eq '}') { | |
59 | $section = pop @$stack; | |
60 | die "parse error - uncexpected '}'\n" if !$section; | |
61 | next; | |
62 | } | |
63 | ||
64 | my $key = $token; | |
65 | die "missing ':' after key '$key'\n" if ! ($key =~ s/:$//); | |
66 | ||
67 | die "parse error - no value for '$key'\n" if !defined($nexttok); | |
68 | my $value = shift @tokens; | |
69 | ||
496de919 | 70 | $section->{$key} = $value; |
b6973a89 TL |
71 | } |
72 | ||
e7ecad20 TL |
73 | # make working with the config way easier |
74 | my ($totem, $nodelist) = $conf->{main}->@{"totem", "nodelist"}; | |
75 | $nodelist->{node} = { map { $_->{name} // $_->{ring0_addr} => $_ } @{$nodelist->{node}} }; | |
76 | $totem->{interface} = { map { $_->{ringnumber} => $_ } @{$totem->{interface}} }; | |
77 | ||
b6973a89 TL |
78 | $conf->{digest} = $digest; |
79 | ||
80 | return $conf; | |
81 | } | |
82 | ||
83 | my $dump_section; | |
84 | $dump_section = sub { | |
85 | my ($section, $prefix) = @_; | |
86 | ||
496de919 | 87 | my $raw = ''; |
b6973a89 | 88 | |
496de919 TL |
89 | foreach my $k (sort keys %$section) { |
90 | my $v = $section->{$k}; | |
91 | if (ref($v) eq 'HASH') { | |
92 | $raw .= $prefix . "$k {\n"; | |
93 | $raw .= &$dump_section($v, "$prefix "); | |
94 | $raw .= $prefix . "}\n"; | |
95 | $raw .= "\n" if !$prefix; # add extra newline at 1st level only | |
96 | } elsif (ref($v) eq 'ARRAY') { | |
97 | foreach my $child (@$v) { | |
98 | $raw .= $prefix . "$k {\n"; | |
99 | $raw .= &$dump_section($child, "$prefix "); | |
100 | $raw .= $prefix . "}\n"; | |
101 | } | |
102 | } elsif (!ref($v)) { | |
103 | $raw .= $prefix . "$k: $v\n"; | |
104 | } else { | |
105 | die "unexpected reference in config hash: $k => ". ref($v) ."\n"; | |
106 | } | |
b6973a89 TL |
107 | } |
108 | ||
b6973a89 | 109 | return $raw; |
b6973a89 TL |
110 | }; |
111 | ||
112 | sub write_conf { | |
113 | my ($filename, $conf) = @_; | |
114 | ||
e7ecad20 TL |
115 | my $c = clone($conf->{main}) // die "no main section"; |
116 | ||
117 | # retransform back for easier dumping | |
118 | my $hash_to_array = sub { | |
119 | my ($hash) = @_; | |
120 | return [ $hash->@{sort keys %$hash} ]; | |
121 | }; | |
b6973a89 | 122 | |
e7ecad20 TL |
123 | $c->{nodelist}->{node} = &$hash_to_array($c->{nodelist}->{node}); |
124 | $c->{totem}->{interface} = &$hash_to_array($c->{totem}->{interface}); | |
125 | ||
126 | my $raw = &$dump_section($c, ''); | |
b6973a89 TL |
127 | |
128 | return $raw; | |
129 | } | |
130 | ||
2b28b160 | 131 | # read only - use atomic_write_conf method to write |
b6973a89 TL |
132 | PVE::Cluster::cfs_register_file('corosync.conf', \&parse_conf); |
133 | # this is read/write | |
134 | PVE::Cluster::cfs_register_file('corosync.conf.new', \&parse_conf, | |
135 | \&write_conf); | |
136 | ||
137 | sub check_conf_exists { | |
138 | my ($silent) = @_; | |
139 | ||
140 | $silent = $silent // 0; | |
141 | ||
142 | my $exists = -f "$basedir/corosync.conf"; | |
143 | ||
144 | warn "Corosync config '$basedir/corosync.conf' does not exist - is this node part of a cluster?\n" | |
145 | if !$silent && !$exists; | |
146 | ||
147 | return $exists; | |
148 | } | |
149 | ||
150 | sub update_nodelist { | |
151 | my ($conf, $nodelist) = @_; | |
152 | ||
e7ecad20 | 153 | $conf->{main}->{nodelist}->{node} = $nodelist; |
b6973a89 | 154 | |
2b28b160 | 155 | atomic_write_conf($conf); |
b6973a89 TL |
156 | } |
157 | ||
158 | sub nodelist { | |
159 | my ($conf) = @_; | |
e7ecad20 | 160 | return clone($conf->{main}->{nodelist}->{node}); |
b6973a89 TL |
161 | } |
162 | ||
b6973a89 TL |
163 | sub totem_config { |
164 | my ($conf) = @_; | |
e7ecad20 | 165 | return clone($conf->{main}->{totem}); |
b6973a89 TL |
166 | } |
167 | ||
2b28b160 TL |
168 | # caller must hold corosync.conf cfs lock if used in read-modify-write cycle |
169 | sub atomic_write_conf { | |
170 | my ($conf, $no_increase_version) = @_; | |
171 | ||
172 | if (!$no_increase_version) { | |
173 | die "invalid corosync config: unable to read config version\n" | |
174 | if !defined($conf->{main}->{totem}->{config_version}); | |
175 | $conf->{main}->{totem}->{config_version}++; | |
176 | } | |
177 | ||
178 | PVE::Cluster::cfs_write_file("corosync.conf.new", $conf); | |
179 | ||
180 | rename("/etc/pve/corosync.conf.new", "/etc/pve/corosync.conf") | |
181 | || die "activating corosync.conf.new failed - $!\n"; | |
182 | } | |
183 | ||
b6973a89 | 184 | 1; |