]>
Commit | Line | Data |
---|---|---|
1 | package PMG::API2::Cluster; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use PVE::SafeSyslog; | |
8 | use PVE::Tools qw(extract_param); | |
9 | use HTTP::Status qw(:constants); | |
10 | use Storable qw(dclone); | |
11 | use PVE::JSONSchema qw(get_standard_option); | |
12 | use PVE::RESTHandler; | |
13 | use PVE::INotify; | |
14 | use PVE::APIClient::LWP; | |
15 | ||
16 | use PMG::RESTEnvironment; | |
17 | use PMG::ClusterConfig; | |
18 | use PMG::Cluster; | |
19 | use PMG::DBTools; | |
20 | use PMG::MailQueue; | |
21 | ||
22 | use PMG::API2::Nodes; | |
23 | ||
24 | use base qw(PVE::RESTHandler); | |
25 | ||
26 | sub cluster_join { | |
27 | my ($cinfo, $conn_setup) = @_; | |
28 | ||
29 | my $conn = PVE::APIClient::LWP->new(%$conn_setup); | |
30 | ||
31 | my $info = PMG::Cluster::read_local_cluster_info(); | |
32 | ||
33 | my $res = $conn->post("/config/cluster/nodes", $info); | |
34 | ||
35 | foreach my $node (@$res) { | |
36 | $cinfo->{ids}->{$node->{cid}} = $node; | |
37 | } | |
38 | ||
39 | eval { | |
40 | print STDERR "stop all services accessing the database\n"; | |
41 | # stop all services accessing the database | |
42 | PMG::Utils::service_wait_stopped(40, $PMG::Utils::db_service_list); | |
43 | ||
44 | print STDERR "save new cluster configuration\n"; | |
45 | $cinfo->write(); | |
46 | ||
47 | PMG::Cluster::update_ssh_keys($cinfo); | |
48 | ||
49 | print STDERR "cluster node successfully joined\n"; | |
50 | ||
51 | $cinfo = PMG::ClusterConfig->new(); # reload | |
52 | ||
53 | my $role = $cinfo->{'local'}->{type} // '-'; | |
54 | die "local node '$cinfo->{local}->{name}' not part of cluster\n" | |
55 | if $role eq '-'; | |
56 | ||
57 | die "got unexpected role '$role' for local node '$cinfo->{local}->{name}'\n" | |
58 | if $role ne 'node'; | |
59 | ||
60 | my $cid = $cinfo->{'local'}->{cid}; | |
61 | ||
62 | PMG::MailQueue::create_spooldirs($cid); | |
63 | ||
64 | PMG::Cluster::sync_config_from_master($cinfo->{master}->{name}, $cinfo->{master}->{ip}); | |
65 | ||
66 | PMG::DBTools::init_nodedb($cinfo); | |
67 | ||
68 | my $cfg = PMG::Config->new(); | |
69 | my $ruledb = PMG::RuleDB->new(); | |
70 | my $rulecache = PMG::RuleCache->new($ruledb); | |
71 | ||
72 | $cfg->rewrite_config($rulecache, 1); | |
73 | ||
74 | print STDERR "syncing quarantine data\n"; | |
75 | PMG::Cluster::sync_master_quar($cinfo->{master}->{ip}, $cinfo->{master}->{name}); | |
76 | print STDERR "syncing quarantine data finished\n"; | |
77 | }; | |
78 | my $err = $@; | |
79 | ||
80 | foreach my $service (reverse @$PMG::Utils::db_service_list) { | |
81 | eval { PVE::Tools::run_command(['systemctl', 'start', $service]); }; | |
82 | warn $@ if $@; | |
83 | } | |
84 | ||
85 | die $err if $err; | |
86 | } | |
87 | ||
88 | __PACKAGE__->register_method({ | |
89 | name => 'index', | |
90 | path => '', | |
91 | method => 'GET', | |
92 | description => "Directory index.", | |
93 | permissions => { user => 'all' }, | |
94 | parameters => { | |
95 | additionalProperties => 0, | |
96 | properties => {}, | |
97 | }, | |
98 | returns => { | |
99 | type => 'array', | |
100 | items => { | |
101 | type => "object", | |
102 | properties => {}, | |
103 | }, | |
104 | links => [ { rel => 'child', href => "{name}" } ], | |
105 | }, | |
106 | code => sub { | |
107 | my ($param) = @_; | |
108 | ||
109 | my $result = [ | |
110 | { name => 'nodes' }, | |
111 | { name => 'status' }, | |
112 | { name => 'create' }, | |
113 | { name => 'join' }, | |
114 | ]; | |
115 | ||
116 | return $result; | |
117 | }}); | |
118 | ||
119 | __PACKAGE__->register_method({ | |
120 | name => 'nodes', | |
121 | path => 'nodes', | |
122 | method => 'GET', | |
123 | description => "Cluster node index.", | |
124 | # alway read local file | |
125 | parameters => { | |
126 | additionalProperties => 0, | |
127 | properties => {}, | |
128 | }, | |
129 | permissions => { check => [ 'admin', 'qmanager', 'audit' ] }, | |
130 | returns => { | |
131 | type => 'array', | |
132 | items => { | |
133 | type => "object", | |
134 | properties => { | |
135 | type => { type => 'string' }, | |
136 | cid => { type => 'integer' }, | |
137 | ip => { type => 'string' }, | |
138 | name => { type => 'string' }, | |
139 | hostrsapubkey => { type => 'string' }, | |
140 | rootrsapubkey => { type => 'string' }, | |
141 | fingerprint => { type => 'string' }, | |
142 | }, | |
143 | }, | |
144 | }, | |
145 | code => sub { | |
146 | my ($param) = @_; | |
147 | ||
148 | my $cinfo = PMG::ClusterConfig->new(); | |
149 | ||
150 | if (scalar(keys %{$cinfo->{ids}})) { | |
151 | my $role = $cinfo->{local}->{type} // '-'; | |
152 | if ($role eq '-') { | |
153 | die "local node '$cinfo->{local}->{name}' not part of cluster\n"; | |
154 | } | |
155 | } | |
156 | ||
157 | return PVE::RESTHandler::hash_to_array($cinfo->{ids}, 'cid'); | |
158 | }}); | |
159 | ||
160 | __PACKAGE__->register_method({ | |
161 | name => 'status', | |
162 | path => 'status', | |
163 | method => 'GET', | |
164 | description => "Cluster node status.", | |
165 | # alway read local file | |
166 | parameters => { | |
167 | additionalProperties => 0, | |
168 | properties => { | |
169 | list_single_node => { | |
170 | description => "List local node if there is no cluster defined. Please note that RSA keys and fingerprint are not valid in that case.", | |
171 | type => 'boolean', | |
172 | optional => 1, | |
173 | default => 0, | |
174 | }, | |
175 | }, | |
176 | }, | |
177 | permissions => { check => [ 'admin', 'qmanager', 'audit' ] }, | |
178 | returns => { | |
179 | type => 'array', | |
180 | items => { | |
181 | type => "object", | |
182 | properties => { | |
183 | type => { type => 'string' }, | |
184 | cid => { type => 'integer' }, | |
185 | ip => { type => 'string' }, | |
186 | name => { type => 'string' }, | |
187 | hostrsapubkey => { type => 'string' }, | |
188 | rootrsapubkey => { type => 'string' }, | |
189 | fingerprint => { type => 'string' }, | |
190 | }, | |
191 | }, | |
192 | links => [ { rel => 'child', href => "{cid}" } ], | |
193 | }, | |
194 | code => sub { | |
195 | my ($param) = @_; | |
196 | ||
197 | my $cinfo = PMG::ClusterConfig->new(); | |
198 | my $nodename = PVE::INotify::nodename(); | |
199 | ||
200 | my $res = []; | |
201 | if (scalar(keys %{$cinfo->{ids}})) { | |
202 | my $role = $cinfo->{local}->{type} // '-'; | |
203 | if ($role eq '-') { | |
204 | die "local node '$cinfo->{local}->{name}' not part of cluster\n"; | |
205 | } | |
206 | $res = PVE::RESTHandler::hash_to_array($cinfo->{ids}, 'cid'); | |
207 | ||
208 | } elsif ($param->{list_single_node}) { | |
209 | my $ni = { type => '-' }; | |
210 | foreach my $k (qw(ip name cid)) { | |
211 | $ni->{$k} = $cinfo->{local}->{$k}; | |
212 | } | |
213 | foreach my $k (qw(hostrsapubkey rootrsapubkey fingerprint)) { | |
214 | $ni->{$k} = '-'; # invalid | |
215 | } | |
216 | $res = [ $ni ]; | |
217 | } | |
218 | ||
219 | my $rpcenv = PMG::RESTEnvironment->get(); | |
220 | my $authuser = $rpcenv->get_user(); | |
221 | my $ticket = $rpcenv->get_ticket(); | |
222 | ||
223 | foreach my $ni (@$res) { | |
224 | my $info; | |
225 | eval { | |
226 | if ($ni->{cid} eq $cinfo->{local}->{cid}) { | |
227 | $info = PMG::API2::NodeInfo->status({ node => $nodename }); | |
228 | } else { | |
229 | my $conn = PVE::APIClient::LWP->new( | |
230 | ticket => $ticket, | |
231 | cookie_name => 'PMGAuthCookie', | |
232 | host => $ni->{ip}, | |
233 | cached_fingerprints => { | |
234 | $ni->{fingerprint} => 1, | |
235 | }); | |
236 | ||
237 | $info = $conn->get("/nodes/localhost/status", {}); | |
238 | } | |
239 | }; | |
240 | if (my $err = $@) { | |
241 | $ni->{conn_error} = "$err"; # convert $err to string | |
242 | next; | |
243 | } | |
244 | foreach my $k (keys %$info) { | |
245 | $ni->{$k} = $info->{$k} if !defined($ni->{$k}); | |
246 | } | |
247 | } | |
248 | ||
249 | return $res; | |
250 | }}); | |
251 | ||
252 | my $add_node_schema = PMG::ClusterConfig::Node->createSchema(1); | |
253 | delete $add_node_schema->{properties}->{cid}; | |
254 | ||
255 | __PACKAGE__->register_method({ | |
256 | name => 'add_node', | |
257 | path => 'nodes', | |
258 | method => 'POST', | |
259 | description => "Add an node to the cluster config.", | |
260 | proxyto => 'master', | |
261 | protected => 1, | |
262 | parameters => $add_node_schema, | |
263 | returns => { | |
264 | description => "Returns the resulting node list.", | |
265 | type => 'array', | |
266 | items => { | |
267 | type => "object", | |
268 | properties => { | |
269 | cid => { type => 'integer' }, | |
270 | }, | |
271 | }, | |
272 | }, | |
273 | code => sub { | |
274 | my ($param) = @_; | |
275 | ||
276 | my $code = sub { | |
277 | my $cinfo = PMG::ClusterConfig->new(); | |
278 | ||
279 | die "no cluster defined\n" if !scalar(keys %{$cinfo->{ids}}); | |
280 | ||
281 | my $master = $cinfo->{master} || die "unable to lookup master node\n"; | |
282 | ||
283 | my $next_cid; | |
284 | foreach my $cid (keys %{$cinfo->{ids}}) { | |
285 | my $d = $cinfo->{ids}->{$cid}; | |
286 | ||
287 | if ($d->{type} eq 'node' && $d->{ip} eq $param->{ip} && $d->{name} eq $param->{name}) { | |
288 | $next_cid = $cid; # allow overwrite existing node data | |
289 | last; | |
290 | } | |
291 | ||
292 | if ($d->{ip} eq $param->{ip}) { | |
293 | die "ip address '$param->{ip}' is already used by existing node $d->{name}\n"; | |
294 | } | |
295 | ||
296 | if ($d->{name} eq $param->{name}) { | |
297 | die "node with name '$param->{name}' already exists\n"; | |
298 | } | |
299 | } | |
300 | ||
301 | if (!defined($next_cid)) { | |
302 | $next_cid = ++$master->{maxcid}; | |
303 | } | |
304 | ||
305 | # create spooldir for new node to prevent problems if it gets | |
306 | # delete from the cluster before being synced initially | |
307 | PMG::MailQueue::create_spooldirs($master->{maxcid}); | |
308 | ||
309 | my $node = { | |
310 | type => 'node', | |
311 | cid => $master->{maxcid}, | |
312 | }; | |
313 | ||
314 | foreach my $k (qw(ip name hostrsapubkey rootrsapubkey fingerprint)) { | |
315 | $node->{$k} = $param->{$k}; | |
316 | } | |
317 | ||
318 | $cinfo->{ids}->{$node->{cid}} = $node; | |
319 | ||
320 | $cinfo->write(); | |
321 | ||
322 | PMG::DBTools::update_master_clusterinfo($node->{cid}); | |
323 | ||
324 | PMG::Cluster::update_ssh_keys($cinfo); | |
325 | ||
326 | return PVE::RESTHandler::hash_to_array($cinfo->{ids}, 'cid'); | |
327 | }; | |
328 | ||
329 | return PMG::ClusterConfig::lock_config($code, "add node failed"); | |
330 | }}); | |
331 | ||
332 | __PACKAGE__->register_method({ | |
333 | name => 'create', | |
334 | path => 'create', | |
335 | method => 'POST', | |
336 | description => "Create initial cluster config with current node as master.", | |
337 | # alway read local file | |
338 | parameters => { | |
339 | additionalProperties => 0, | |
340 | properties => {}, | |
341 | }, | |
342 | protected => 1, | |
343 | returns => { type => 'string' }, | |
344 | code => sub { | |
345 | my ($param) = @_; | |
346 | ||
347 | my $rpcenv = PMG::RESTEnvironment->get(); | |
348 | my $authuser = $rpcenv->get_user(); | |
349 | ||
350 | my $realcmd = sub { | |
351 | my $cinfo = PMG::ClusterConfig->new(); | |
352 | ||
353 | die "cluster already defined\n" if scalar(keys %{$cinfo->{ids}}); | |
354 | ||
355 | my $info = PMG::Cluster::read_local_cluster_info(); | |
356 | ||
357 | my $cid = 1; | |
358 | ||
359 | $info->{type} = 'master'; | |
360 | ||
361 | $info->{maxcid} = $cid, | |
362 | ||
363 | $cinfo->{ids}->{$cid} = $info; | |
364 | ||
365 | eval { | |
366 | print STDERR "stop all services accessing the database\n"; | |
367 | # stop all services accessing the database | |
368 | PMG::Utils::service_wait_stopped(40, $PMG::Utils::db_service_list); | |
369 | ||
370 | print STDERR "save new cluster configuration\n"; | |
371 | $cinfo->write(); | |
372 | ||
373 | PMG::DBTools::init_masterdb($cid); | |
374 | ||
375 | PMG::MailQueue::create_spooldirs($cid); | |
376 | ||
377 | print STDERR "cluster master successfully created\n"; | |
378 | }; | |
379 | my $err = $@; | |
380 | ||
381 | foreach my $service (reverse @$PMG::Utils::db_service_list) { | |
382 | eval { PVE::Tools::run_command(['systemctl', 'start', $service]); }; | |
383 | warn $@ if $@; | |
384 | } | |
385 | ||
386 | die $err if $err; | |
387 | }; | |
388 | ||
389 | my $code = sub { | |
390 | return $rpcenv->fork_worker('clustercreate', undef, $authuser, $realcmd); | |
391 | }; | |
392 | ||
393 | return PMG::ClusterConfig::lock_config($code, "create cluster failed"); | |
394 | }}); | |
395 | ||
396 | __PACKAGE__->register_method({ | |
397 | name => 'join', | |
398 | path => 'join', | |
399 | method => 'POST', | |
400 | description => "Join local node to an existing cluster.", | |
401 | # alway read local file | |
402 | protected => 1, | |
403 | parameters => { | |
404 | additionalProperties => 0, | |
405 | properties => { | |
406 | master_ip => { | |
407 | description => "IP address.", | |
408 | type => 'string', format => 'ip', | |
409 | }, | |
410 | fingerprint => { | |
411 | description => "SSL certificate fingerprint.", | |
412 | type => 'string', | |
413 | pattern => '^(:?[A-Z0-9][A-Z0-9]:){31}[A-Z0-9][A-Z0-9]$', | |
414 | }, | |
415 | password => { | |
416 | description => "Superuser password.", | |
417 | type => 'string', | |
418 | maxLength => 128, | |
419 | }, | |
420 | }, | |
421 | }, | |
422 | returns => { type => 'string' }, | |
423 | code => sub { | |
424 | my ($param) = @_; | |
425 | ||
426 | my $rpcenv = PMG::RESTEnvironment->get(); | |
427 | my $authuser = $rpcenv->get_user(); | |
428 | ||
429 | my $realcmd = sub { | |
430 | my $cinfo = PMG::ClusterConfig->new(); | |
431 | ||
432 | die "cluster already defined\n" if scalar(keys %{$cinfo->{ids}}); | |
433 | ||
434 | my $setup = { | |
435 | username => 'root@pam', | |
436 | password => $param->{password}, | |
437 | cookie_name => 'PMGAuthCookie', | |
438 | host => $param->{master_ip}, | |
439 | cached_fingerprints => { | |
440 | $param->{fingerprint} => 1, | |
441 | } | |
442 | }; | |
443 | ||
444 | cluster_join($cinfo, $setup); | |
445 | }; | |
446 | ||
447 | my $code = sub { | |
448 | return $rpcenv->fork_worker('clusterjoin', undef, $authuser, $realcmd); | |
449 | }; | |
450 | ||
451 | return PMG::ClusterConfig::lock_config($code, "cluster join failed"); | |
452 | }}); | |
453 | ||
454 | ||
455 | 1; |