]>
Commit | Line | Data |
---|---|---|
fb5f2d1e | 1 | package PMG::API2::Cluster; |
5ac6bd0e DM |
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; | |
e16a9efc | 14 | use PVE::APIClient::LWP; |
5ac6bd0e | 15 | |
cfdf6608 | 16 | use PMG::RESTEnvironment; |
5ac6bd0e | 17 | use PMG::ClusterConfig; |
e16a9efc | 18 | use PMG::Cluster; |
cfdf6608 | 19 | use PMG::DBTools; |
f607fc2f | 20 | use PMG::MailQueue; |
5ac6bd0e | 21 | |
ba11e2d3 DM |
22 | use PMG::API2::Nodes; |
23 | ||
5ac6bd0e DM |
24 | use base qw(PVE::RESTHandler); |
25 | ||
e16a9efc | 26 | sub cluster_join { |
245b527c | 27 | my ($cinfo, $conn_setup) = @_; |
e16a9efc DM |
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); | |
275d6d6d DM |
34 | |
35 | foreach my $node (@$res) { | |
245b527c | 36 | $cinfo->{ids}->{$node->{cid}} = $node; |
275d6d6d DM |
37 | } |
38 | ||
5a312cf1 DM |
39 | eval { |
40 | print STDERR "stop all services accessing the database\n"; | |
41 | # stop all services accessing the database | |
00d8b7f7 | 42 | PMG::Utils::service_wait_stopped(40, $PMG::Utils::db_service_list); |
5a312cf1 DM |
43 | |
44 | print STDERR "save new cluster configuration\n"; | |
245b527c | 45 | $cinfo->write(); |
5a312cf1 | 46 | |
245b527c | 47 | PMG::Cluster::update_ssh_keys($cinfo); |
5a312cf1 DM |
48 | |
49 | print STDERR "cluster node successfully joined\n"; | |
50 | ||
245b527c | 51 | $cinfo = PMG::ClusterConfig->new(); # reload |
5a312cf1 | 52 | |
245b527c DM |
53 | my $role = $cinfo->{'local'}->{type} // '-'; |
54 | die "local node '$cinfo->{local}->{name}' not part of cluster\n" | |
5a312cf1 DM |
55 | if $role eq '-'; |
56 | ||
245b527c | 57 | die "got unexpected role '$role' for local node '$cinfo->{local}->{name}'\n" |
809ae8f4 DM |
58 | if $role ne 'node'; |
59 | ||
245b527c | 60 | my $cid = $cinfo->{'local'}->{cid}; |
5a312cf1 | 61 | |
8293a783 | 62 | PMG::MailQueue::create_spooldirs($cid); |
5a312cf1 | 63 | |
245b527c | 64 | PMG::Cluster::sync_config_from_master($cinfo->{master}->{name}, $cinfo->{master}->{ip}); |
5a312cf1 | 65 | |
245b527c | 66 | PMG::DBTools::init_nodedb($cinfo); |
5a312cf1 | 67 | |
00d8b7f7 | 68 | my $cfg = PMG::Config->new(); |
245b527c DM |
69 | my $ruledb = PMG::RuleDB->new(); |
70 | my $rulecache = PMG::RuleCache->new($ruledb); | |
71 | ||
72 | $cfg->rewrite_config($rulecache, 1); | |
5a312cf1 DM |
73 | |
74 | print STDERR "syncing quarantine data\n"; | |
245b527c | 75 | PMG::Cluster::sync_master_quar($cinfo->{master}->{ip}, $cinfo->{master}->{name}); |
5a312cf1 DM |
76 | print STDERR "syncing quarantine data finished\n"; |
77 | }; | |
78 | my $err = $@; | |
79 | ||
00d8b7f7 | 80 | foreach my $service (reverse @$PMG::Utils::db_service_list) { |
5a312cf1 DM |
81 | eval { PVE::Tools::run_command(['systemctl', 'start', $service]); }; |
82 | warn $@ if $@; | |
83 | } | |
84 | ||
85 | die $err if $err; | |
e16a9efc DM |
86 | } |
87 | ||
5ac6bd0e DM |
88 | __PACKAGE__->register_method({ |
89 | name => 'index', | |
90 | path => '', | |
91 | method => 'GET', | |
fb5f2d1e DM |
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' }, | |
150e8421 | 111 | { name => 'status' }, |
fb5f2d1e DM |
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', | |
5ac6bd0e DM |
123 | description => "Cluster node index.", |
124 | # alway read local file | |
125 | parameters => { | |
126 | additionalProperties => 0, | |
127 | properties => {}, | |
128 | }, | |
f4302160 | 129 | permissions => { check => [ 'admin', 'qmanager', 'audit' ] }, |
5ac6bd0e DM |
130 | returns => { |
131 | type => 'array', | |
132 | items => { | |
133 | type => "object", | |
134 | properties => { | |
150e8421 | 135 | type => { type => 'string' }, |
5ac6bd0e | 136 | cid => { type => 'integer' }, |
150e8421 DM |
137 | ip => { type => 'string' }, |
138 | name => { type => 'string' }, | |
139 | hostrsapubkey => { type => 'string' }, | |
140 | rootrsapubkey => { type => 'string' }, | |
141 | fingerprint => { type => 'string' }, | |
142 | }, | |
143 | }, | |
150e8421 DM |
144 | }, |
145 | code => sub { | |
146 | my ($param) = @_; | |
147 | ||
245b527c | 148 | my $cinfo = PMG::ClusterConfig->new(); |
150e8421 | 149 | |
245b527c DM |
150 | if (scalar(keys %{$cinfo->{ids}})) { |
151 | my $role = $cinfo->{local}->{type} // '-'; | |
150e8421 | 152 | if ($role eq '-') { |
245b527c | 153 | die "local node '$cinfo->{local}->{name}' not part of cluster\n"; |
150e8421 DM |
154 | } |
155 | } | |
156 | ||
245b527c | 157 | return PVE::RESTHandler::hash_to_array($cinfo->{ids}, 'cid'); |
150e8421 DM |
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, | |
64118ca4 DM |
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 | }, | |
150e8421 | 176 | }, |
f4302160 | 177 | permissions => { check => [ 'admin', 'qmanager', 'audit' ] }, |
150e8421 DM |
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' }, | |
5ac6bd0e DM |
190 | }, |
191 | }, | |
192 | links => [ { rel => 'child', href => "{cid}" } ], | |
193 | }, | |
194 | code => sub { | |
195 | my ($param) = @_; | |
196 | ||
245b527c | 197 | my $cinfo = PMG::ClusterConfig->new(); |
64118ca4 | 198 | my $nodename = PVE::INotify::nodename(); |
5ac6bd0e | 199 | |
64118ca4 | 200 | my $res = []; |
245b527c DM |
201 | if (scalar(keys %{$cinfo->{ids}})) { |
202 | my $role = $cinfo->{local}->{type} // '-'; | |
3862c23d | 203 | if ($role eq '-') { |
245b527c | 204 | die "local node '$cinfo->{local}->{name}' not part of cluster\n"; |
3862c23d | 205 | } |
64118ca4 | 206 | $res = PVE::RESTHandler::hash_to_array($cinfo->{ids}, 'cid'); |
3862c23d | 207 | |
64118ca4 DM |
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 | } | |
ba11e2d3 DM |
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 { | |
245b527c | 226 | if ($ni->{cid} eq $cinfo->{local}->{cid}) { |
64118ca4 | 227 | $info = PMG::API2::NodeInfo->status({ node => $nodename }); |
ba11e2d3 DM |
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 = $@) { | |
15eac67d | 241 | $ni->{conn_error} = "$err"; # convert $err to string |
ba11e2d3 DM |
242 | next; |
243 | } | |
244 | foreach my $k (keys %$info) { | |
245 | $ni->{$k} = $info->{$k} if !defined($ni->{$k}); | |
246 | } | |
247 | } | |
248 | ||
249 | return $res; | |
5ac6bd0e DM |
250 | }}); |
251 | ||
e16a9efc DM |
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, | |
275d6d6d DM |
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 | }, | |
e16a9efc DM |
273 | code => sub { |
274 | my ($param) = @_; | |
275 | ||
276 | my $code = sub { | |
245b527c | 277 | my $cinfo = PMG::ClusterConfig->new(); |
e16a9efc | 278 | |
245b527c | 279 | die "no cluster defined\n" if !scalar(keys %{$cinfo->{ids}}); |
e16a9efc | 280 | |
245b527c | 281 | my $master = $cinfo->{master} || die "unable to lookup master node\n"; |
e16a9efc | 282 | |
92c560f7 | 283 | my $next_cid; |
245b527c DM |
284 | foreach my $cid (keys %{$cinfo->{ids}}) { |
285 | my $d = $cinfo->{ids}->{$cid}; | |
92c560f7 DM |
286 | |
287 | if ($d->{type} eq 'node' && $d->{ip} eq $param->{ip} && $d->{name} eq $param->{name}) { | |
275d6d6d | 288 | $next_cid = $cid; # allow overwrite existing node data |
92c560f7 DM |
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 | } | |
e16a9efc DM |
304 | |
305 | my $node = { | |
306 | type => 'node', | |
307 | cid => $master->{maxcid}, | |
308 | }; | |
309 | ||
310 | foreach my $k (qw(ip name hostrsapubkey rootrsapubkey fingerprint)) { | |
311 | $node->{$k} = $param->{$k}; | |
312 | } | |
313 | ||
245b527c | 314 | $cinfo->{ids}->{$node->{cid}} = $node; |
e16a9efc | 315 | |
245b527c | 316 | $cinfo->write(); |
e16a9efc | 317 | |
5a312cf1 DM |
318 | PMG::DBTools::update_master_clusterinfo($node->{cid}); |
319 | ||
245b527c | 320 | PMG::Cluster::update_ssh_keys($cinfo); |
5a312cf1 | 321 | |
245b527c | 322 | return PVE::RESTHandler::hash_to_array($cinfo->{ids}, 'cid'); |
275d6d6d | 323 | }; |
e16a9efc | 324 | |
275d6d6d | 325 | return PMG::ClusterConfig::lock_config($code, "add node failed"); |
e16a9efc DM |
326 | }}); |
327 | ||
cba17aeb | 328 | __PACKAGE__->register_method({ |
fb5f2d1e DM |
329 | name => 'create', |
330 | path => 'create', | |
cba17aeb DM |
331 | method => 'POST', |
332 | description => "Create initial cluster config with current node as master.", | |
333 | # alway read local file | |
334 | parameters => { | |
335 | additionalProperties => 0, | |
336 | properties => {}, | |
337 | }, | |
cfdf6608 DM |
338 | protected => 1, |
339 | returns => { type => 'string' }, | |
cba17aeb DM |
340 | code => sub { |
341 | my ($param) = @_; | |
342 | ||
cfdf6608 DM |
343 | my $rpcenv = PMG::RESTEnvironment->get(); |
344 | my $authuser = $rpcenv->get_user(); | |
345 | ||
346 | my $realcmd = sub { | |
245b527c | 347 | my $cinfo = PMG::ClusterConfig->new(); |
cba17aeb | 348 | |
56001f4f | 349 | die "cluster already defined\n" if scalar(keys %{$cinfo->{ids}}); |
cba17aeb DM |
350 | |
351 | my $info = PMG::Cluster::read_local_cluster_info(); | |
352 | ||
cfdf6608 DM |
353 | my $cid = 1; |
354 | ||
cba17aeb | 355 | $info->{type} = 'master'; |
cba17aeb | 356 | |
cfdf6608 | 357 | $info->{maxcid} = $cid, |
cba17aeb | 358 | |
245b527c | 359 | $cinfo->{ids}->{$cid} = $info; |
cfdf6608 | 360 | |
cfdf6608 DM |
361 | eval { |
362 | print STDERR "stop all services accessing the database\n"; | |
363 | # stop all services accessing the database | |
00d8b7f7 | 364 | PMG::Utils::service_wait_stopped(40, $PMG::Utils::db_service_list); |
cfdf6608 DM |
365 | |
366 | print STDERR "save new cluster configuration\n"; | |
245b527c | 367 | $cinfo->write(); |
cfdf6608 DM |
368 | |
369 | PMG::DBTools::init_masterdb($cid); | |
370 | ||
8293a783 | 371 | PMG::MailQueue::create_spooldirs($cid); |
cfdf6608 DM |
372 | |
373 | print STDERR "cluster master successfully created\n"; | |
374 | }; | |
375 | my $err = $@; | |
376 | ||
00d8b7f7 | 377 | foreach my $service (reverse @$PMG::Utils::db_service_list) { |
cfdf6608 DM |
378 | eval { PVE::Tools::run_command(['systemctl', 'start', $service]); }; |
379 | warn $@ if $@; | |
380 | } | |
381 | ||
382 | die $err if $err; | |
cba17aeb DM |
383 | }; |
384 | ||
cfdf6608 DM |
385 | my $code = sub { |
386 | return $rpcenv->fork_worker('clustercreate', undef, $authuser, $realcmd); | |
387 | }; | |
cba17aeb | 388 | |
cfdf6608 | 389 | return PMG::ClusterConfig::lock_config($code, "create cluster failed"); |
cba17aeb DM |
390 | }}); |
391 | ||
fb5f2d1e DM |
392 | __PACKAGE__->register_method({ |
393 | name => 'join', | |
394 | path => 'join', | |
395 | method => 'POST', | |
396 | description => "Join local node to an existing cluster.", | |
397 | # alway read local file | |
4884557f | 398 | protected => 1, |
fb5f2d1e DM |
399 | parameters => { |
400 | additionalProperties => 0, | |
401 | properties => { | |
402 | master_ip => { | |
403 | description => "IP address.", | |
404 | type => 'string', format => 'ip', | |
405 | }, | |
406 | fingerprint => { | |
407 | description => "SSL certificate fingerprint.", | |
408 | type => 'string', | |
409 | pattern => '^(:?[A-Z0-9][A-Z0-9]:){31}[A-Z0-9][A-Z0-9]$', | |
410 | }, | |
e16a9efc DM |
411 | password => { |
412 | description => "Superuser password.", | |
413 | type => 'string', | |
414 | maxLength => 128, | |
415 | }, | |
fb5f2d1e DM |
416 | }, |
417 | }, | |
5a312cf1 | 418 | returns => { type => 'string' }, |
fb5f2d1e DM |
419 | code => sub { |
420 | my ($param) = @_; | |
421 | ||
5a312cf1 DM |
422 | my $rpcenv = PMG::RESTEnvironment->get(); |
423 | my $authuser = $rpcenv->get_user(); | |
424 | ||
425 | my $realcmd = sub { | |
245b527c | 426 | my $cinfo = PMG::ClusterConfig->new(); |
fb5f2d1e | 427 | |
56001f4f | 428 | die "cluster already defined\n" if scalar(keys %{$cinfo->{ids}}); |
fb5f2d1e | 429 | |
e16a9efc DM |
430 | my $setup = { |
431 | username => 'root@pam', | |
432 | password => $param->{password}, | |
433 | cookie_name => 'PMGAuthCookie', | |
434 | host => $param->{master_ip}, | |
435 | cached_fingerprints => { | |
436 | $param->{fingerprint} => 1, | |
437 | } | |
438 | }; | |
439 | ||
245b527c | 440 | cluster_join($cinfo, $setup); |
fb5f2d1e DM |
441 | }; |
442 | ||
5a312cf1 DM |
443 | my $code = sub { |
444 | return $rpcenv->fork_worker('clusterjoin', undef, $authuser, $realcmd); | |
445 | }; | |
fb5f2d1e | 446 | |
5a312cf1 | 447 | return PMG::ClusterConfig::lock_config($code, "cluster join failed"); |
fb5f2d1e DM |
448 | }}); |
449 | ||
cba17aeb | 450 | |
5ac6bd0e | 451 | 1; |