]>
Commit | Line | Data |
---|---|---|
9f67f5b3 DM |
1 | package PMG::ClusterConfig::Base; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use PVE::Tools; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | use PVE::SectionConfig; | |
10 | ||
11 | use base qw(PVE::SectionConfig); | |
12 | ||
13 | my $defaultData = { | |
14 | propertyList => { | |
15 | type => { description => "Cluster node type." }, | |
16 | cid => { | |
17 | description => "Cluster Node ID.", | |
18 | type => 'integer', | |
19 | minimum => 1, | |
20 | }, | |
21 | }, | |
22 | }; | |
23 | ||
24 | sub private { | |
25 | return $defaultData; | |
26 | } | |
27 | ||
28 | sub parse_section_header { | |
29 | my ($class, $line) = @_; | |
30 | ||
31 | if ($line =~ m/^(node|master):\s*(\d+)\s*$/) { | |
32 | my ($type, $sectionId) = ($1, $2); | |
33 | my $errmsg = undef; # set if you want to skip whole section | |
34 | my $config = {}; # to return additional attributes | |
35 | return ($type, $sectionId, $errmsg, $config); | |
36 | } | |
37 | return undef; | |
38 | } | |
39 | ||
9f67f5b3 DM |
40 | package PMG::ClusterConfig::Node; |
41 | ||
42 | use strict; | |
43 | use warnings; | |
44 | ||
45 | use base qw(PMG::ClusterConfig::Base); | |
46 | ||
47 | sub type { | |
48 | return 'node'; | |
49 | } | |
50 | sub properties { | |
51 | return { | |
52 | ip => { | |
53 | description => "IP address.", | |
b9b02806 | 54 | type => 'string', format => 'ip', |
9f67f5b3 DM |
55 | }, |
56 | name => { | |
57 | description => "Node name.", | |
58 | type => 'string', format =>'pve-node', | |
59 | }, | |
60 | hostrsapubkey => { | |
61 | description => "Public SSH RSA key for the host.", | |
62 | type => 'string', | |
cba17aeb | 63 | pattern => '^[A-Za-z0-9\.\/\+]{200,}$', |
9f67f5b3 DM |
64 | }, |
65 | rootrsapubkey => { | |
66 | description => "Public SSH RSA key for the root user.", | |
67 | type => 'string', | |
cba17aeb DM |
68 | pattern => '^[A-Za-z0-9\.\/\+]{200,}$', |
69 | }, | |
70 | fingerprint => { | |
71 | description => "SSL certificate fingerprint.", | |
72 | type => 'string', | |
73 | pattern => '^(:?[A-Z0-9][A-Z0-9]:){31}[A-Z0-9][A-Z0-9]$', | |
9f67f5b3 DM |
74 | }, |
75 | }; | |
76 | } | |
77 | ||
78 | sub options { | |
79 | return { | |
80 | ip => { fixed => 1 }, | |
81 | name => { fixed => 1 }, | |
82 | hostrsapubkey => {}, | |
83 | rootrsapubkey => {}, | |
cba17aeb | 84 | fingerprint => {}, |
9f67f5b3 DM |
85 | }; |
86 | } | |
87 | ||
88 | package PMG::ClusterConfig::Master; | |
89 | ||
90 | use strict; | |
91 | use warnings; | |
92 | ||
93 | use base qw(PMG::ClusterConfig::Base); | |
94 | ||
95 | sub type { | |
96 | return 'master'; | |
97 | } | |
98 | ||
99 | sub properties { | |
100 | return { | |
101 | maxcid => { | |
102 | description => "Maximum used cluster node ID (used internally, do not modify).", | |
103 | type => 'integer', | |
104 | minimum => 1, | |
105 | }, | |
106 | }; | |
107 | } | |
108 | ||
109 | sub options { | |
110 | return { | |
111 | maxcid => { fixed => 1 }, | |
112 | ip => { fixed => 1 }, | |
113 | name => { fixed => 1 }, | |
114 | hostrsapubkey => {}, | |
115 | rootrsapubkey => {}, | |
cba17aeb | 116 | fingerprint => {}, |
9f67f5b3 DM |
117 | }; |
118 | } | |
119 | ||
120 | package PMG::ClusterConfig; | |
121 | ||
122 | use strict; | |
123 | use warnings; | |
124 | use Data::Dumper; | |
125 | ||
126 | use PVE::SafeSyslog; | |
127 | use PVE::Tools; | |
128 | use PVE::INotify; | |
129 | ||
130 | use PMG::Utils; | |
131 | ||
132 | PMG::ClusterConfig::Node->register; | |
133 | PMG::ClusterConfig::Master->register; | |
134 | PMG::ClusterConfig::Base->init(); | |
135 | ||
136 | ||
137 | sub new { | |
138 | my ($type) = @_; | |
139 | ||
140 | my $class = ref($type) || $type; | |
141 | ||
142 | my $cfg = PVE::INotify::read_file("cluster.conf"); | |
143 | ||
144 | return bless $cfg, $class; | |
145 | } | |
146 | ||
147 | sub write { | |
148 | my ($self) = @_; | |
149 | ||
150 | PVE::INotify::write_file("cluster.conf", $self); | |
151 | } | |
152 | ||
153 | my $lockfile = "/var/lock/pmgcluster.lck"; | |
154 | ||
155 | sub lock_config { | |
156 | my ($code, $errmsg) = @_; | |
157 | ||
275d6d6d | 158 | my $res = PVE::Tools::lock_file($lockfile, undef, $code); |
9f67f5b3 DM |
159 | if (my $err = $@) { |
160 | $errmsg ? die "$errmsg: $err" : die $err; | |
161 | } | |
275d6d6d | 162 | return $res; |
9f67f5b3 DM |
163 | } |
164 | ||
165 | sub read_cluster_conf { | |
166 | my ($filename, $fh) = @_; | |
167 | ||
168 | local $/ = undef; # slurp mode | |
169 | ||
170 | my $raw = defined($fh) ? <$fh> : undef; | |
171 | ||
172 | my $cinfo = PMG::ClusterConfig::Base->parse_config($filename, $raw); | |
9f67f5b3 DM |
173 | |
174 | my $localname = PVE::INotify::nodename(); | |
175 | my $localip = PMG::Utils::lookup_node_ip($localname); | |
176 | ||
177 | $cinfo->{remnodes} = []; | |
178 | $cinfo->{configport}->{0} = 83; | |
179 | $cinfo->{dbport}->{0} = 5432; | |
180 | ||
181 | $cinfo->{local} = { | |
182 | cid => 0, | |
183 | ip => $localip, | |
184 | name => $localname, | |
185 | }; | |
186 | ||
187 | my $maxcid = 0; | |
188 | my $names_hash = {}; | |
189 | ||
190 | my $errprefix = "unable to parse $filename"; | |
191 | ||
192 | foreach my $cid (keys %{$cinfo->{ids}}) { | |
193 | my $d = $cinfo->{ids}->{$cid}; | |
194 | ||
195 | die "$errprefix: duplicate use of name '$d->{name}'\n" if $names_hash->{$d->{name}}; | |
196 | $names_hash->{$d->{name}} = 1; | |
197 | ||
198 | $d->{cid} = $cid; | |
199 | $maxcid = $cid > $maxcid ? $cid : $maxcid; | |
200 | $maxcid = $d->{maxcid} if defined($d->{maxcid}) && $d->{maxcid} > $maxcid; | |
201 | $cinfo->{master} = $d if $d->{type} eq 'master'; | |
202 | $cinfo->{'local'} = $d if $d->{name} eq $localname; | |
203 | } | |
204 | ||
205 | if ($maxcid) { | |
206 | die "$errprefix: cluster without master node\n" | |
207 | if !defined($cinfo->{master}); | |
208 | $cinfo->{master}->{maxcid} = $maxcid; | |
209 | } | |
210 | ||
211 | my $ind = 0; | |
212 | foreach my $cid (sort keys %{$cinfo->{ids}}) { | |
213 | if ($cinfo->{'local'}->{cid} == $cid) { # local CID | |
214 | $cinfo->{configport}->{$cid} = 83; | |
215 | $cinfo->{dbport}->{$cid} = 5432; | |
216 | } else { | |
217 | my $portid = $ind++; | |
218 | $cinfo->{configport}->{$cid} = 50000 + $portid; | |
219 | $cinfo->{dbport}->{$cid} = 50100 + $portid; | |
cf07e7c3 | 220 | push @{$cinfo->{remnodes}}, $cid; |
9f67f5b3 DM |
221 | } |
222 | } | |
223 | ||
224 | return $cinfo; | |
225 | } | |
226 | ||
227 | sub write_cluster_conf { | |
228 | my ($filename, $fh, $cfg) = @_; | |
229 | ||
230 | my $raw = PMG::ClusterConfig::Base->write_config($filename, $cfg); | |
231 | ||
232 | PVE::Tools::safe_print($filename, $fh, $raw); | |
233 | } | |
234 | ||
3278b571 | 235 | PVE::INotify::register_file('cluster.conf', "/etc/pmg/cluster.conf", |
9f67f5b3 DM |
236 | \&read_cluster_conf, |
237 | \&write_cluster_conf, | |
238 | undef, | |
239 | always_call_parser => 1); | |
240 | ||
241 | 1; |