]>
Commit | Line | Data |
---|---|---|
13d44dc5 DM |
1 | package PVE::CLI::pvecm; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
95e7bcac | 5 | |
13d44dc5 DM |
6 | use File::Path; |
7 | use File::Basename; | |
294f76c4 | 8 | use PVE::Tools qw(run_command); |
13d44dc5 DM |
9 | use PVE::Cluster; |
10 | use PVE::INotify; | |
10c6810e | 11 | use PVE::JSONSchema qw(get_standard_option); |
03b23bd0 | 12 | use PVE::RPCEnvironment; |
13d44dc5 | 13 | use PVE::CLIHandler; |
03b23bd0 | 14 | use PVE::PTY; |
1d26c202 | 15 | use PVE::API2::ClusterConfig; |
b6973a89 | 16 | use PVE::Corosync; |
c5204e14 | 17 | use PVE::Cluster::Setup; |
13d44dc5 DM |
18 | |
19 | use base qw(PVE::CLIHandler); | |
20 | ||
21 | $ENV{HOME} = '/root'; # for ssh-copy-id | |
22 | ||
23 | my $basedir = "/etc/pve"; | |
24 | my $clusterconf = "$basedir/corosync.conf"; | |
25 | my $libdir = "/var/lib/pve-cluster"; | |
13d44dc5 DM |
26 | my $authfile = "/etc/corosync/authkey"; |
27 | ||
9db3f0c0 | 28 | |
03b23bd0 TL |
29 | sub setup_environment { |
30 | PVE::RPCEnvironment->setup_default_cli_env(); | |
31 | } | |
32 | ||
13d44dc5 | 33 | __PACKAGE__->register_method ({ |
c53b111f | 34 | name => 'keygen', |
13d44dc5 DM |
35 | path => 'keygen', |
36 | method => 'PUT', | |
37 | description => "Generate new cryptographic key for corosync.", | |
38 | parameters => { | |
39 | additionalProperties => 0, | |
40 | properties => { | |
41 | filename => { | |
42 | type => 'string', | |
43 | description => "Output file name" | |
44 | } | |
45 | }, | |
46 | }, | |
47 | returns => { type => 'null' }, | |
c53b111f | 48 | |
13d44dc5 DM |
49 | code => sub { |
50 | my ($param) = @_; | |
51 | ||
52 | my $filename = $param->{filename}; | |
53 | ||
54 | # test EUID | |
55 | $> == 0 || die "Error: Authorization key must be generated as root user.\n"; | |
56 | my $dirname = dirname($filename); | |
13d44dc5 DM |
57 | |
58 | die "key file '$filename' already exists\n" if -e $filename; | |
59 | ||
60 | File::Path::make_path($dirname) if $dirname; | |
61 | ||
294f76c4 | 62 | run_command(['corosync-keygen', '-l', '-k', $filename]); |
13d44dc5 DM |
63 | |
64 | return undef; | |
65 | }}); | |
66 | ||
63afd01d OB |
67 | my $foreach_member = sub { |
68 | my ($code, $noerr) = @_; | |
69 | ||
70 | my $members = PVE::Cluster::get_members(); | |
71 | foreach my $node (sort keys %$members) { | |
72 | if (my $ip = $members->{$node}->{ip}) { | |
73 | $code->($node, $ip); | |
74 | } else { | |
75 | die "cannot get the cluster IP for node '$node'.\n" if !$noerr; | |
76 | warn "cannot get the cluster IP for node '$node'.\n"; | |
77 | return undef; | |
78 | } | |
79 | } | |
80 | }; | |
81 | ||
82 | __PACKAGE__->register_method ({ | |
83 | name => 'setup_qdevice', | |
84 | path => 'setup_qdevice', | |
85 | method => 'PUT', | |
86 | description => "Setup the use of a QDevice", | |
87 | parameters => { | |
88 | additionalProperties => 0, | |
89 | properties => { | |
90 | address => { | |
91 | type => 'string', format => 'ip', | |
92 | description => "Specifies the network address of an external corosync QDevice" , | |
93 | }, | |
94 | network => { | |
95 | type => 'string', | |
96 | format => 'CIDR', | |
97 | description => 'The network which should be used to connect to the external qdevice', | |
98 | optional => 1, | |
99 | }, | |
100 | force => { | |
101 | type => 'boolean', | |
102 | description => "Do not throw error on possible dangerous operations.", | |
103 | optional => 1, | |
104 | }, | |
105 | }, | |
106 | }, | |
107 | returns => { type => 'null' }, | |
108 | ||
109 | code => sub { | |
110 | my ($param) = @_; | |
111 | ||
112 | die "Node not in a cluster. Aborting.\n" | |
113 | if !PVE::Corosync::check_conf_exists(1); | |
114 | ||
115 | my $members = PVE::Cluster::get_members(); | |
116 | foreach my $node (sort keys %$members) { | |
117 | die "All nodes must be online! Node $node is offline, aborting.\n" | |
118 | if !$members->{$node}->{online}; | |
119 | } | |
120 | ||
121 | my $conf = PVE::Cluster::cfs_read_file("corosync.conf"); | |
122 | ||
123 | die "QDevice already configured!\n" | |
124 | if defined($conf->{main}->{quorum}->{device}) && !$param->{force}; | |
125 | ||
126 | my $network = $param->{network}; | |
127 | ||
128 | my $model = "net"; | |
129 | my $algorithm = 'ffsplit'; | |
130 | if (scalar($members) & 1) { | |
131 | if ($param->{force}) { | |
132 | $algorithm = 'lms'; | |
133 | } else { | |
134 | die "Clusters with an odd node count are not officially supported!\n"; | |
135 | } | |
136 | } | |
137 | ||
138 | my $qnetd_addr = $param->{address}; | |
139 | my $base_dir = "/etc/corosync/qdevice/net"; | |
140 | my $db_dir_qnetd = "/etc/corosync/qnetd/nssdb"; | |
141 | my $db_dir_node = "$base_dir/nssdb"; | |
142 | my $ca_export_base = "qnetd-cacert.crt"; | |
143 | my $ca_export_file = "$db_dir_qnetd/$ca_export_base"; | |
144 | my $crq_file_base = "qdevice-net-node.crq"; | |
145 | my $p12_file_base = "qdevice-net-node.p12"; | |
146 | my $qdevice_certutil = "corosync-qdevice-net-certutil"; | |
147 | my $qnetd_certutil= "corosync-qnetd-certutil"; | |
148 | my $clustername = $conf->{main}->{totem}->{cluster_name}; | |
149 | ||
150 | run_command(['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$qnetd_addr"]); | |
151 | ||
152 | if (-d $db_dir_node) { | |
153 | # FIXME: check on all nodes?! | |
154 | if ($param->{force}) { | |
155 | rmtree $db_dir_node; | |
156 | } else { | |
157 | die "QDevice certificate store already initialised, set force to delete!\n"; | |
158 | } | |
159 | } | |
160 | ||
161 | my $ssh_cmd = ['ssh', '-o', 'BatchMode=yes', '-lroot']; | |
162 | my $scp_cmd = ['scp', '-o', 'BatchMode=yes']; | |
163 | ||
164 | print "\nINFO: initializing qnetd server\n"; | |
165 | run_command( | |
166 | [@$ssh_cmd, $qnetd_addr, $qnetd_certutil, "-i"], | |
167 | noerr => 1 | |
168 | ); | |
169 | ||
170 | print "\nINFO: copying CA cert and initializing on all nodes\n"; | |
171 | run_command([@$scp_cmd, "root\@\[$qnetd_addr\]:$ca_export_file", "/etc/pve/$ca_export_base"]); | |
172 | $foreach_member->(sub { | |
173 | my ($node, $ip) = @_; | |
174 | my $outsub = sub { print "\nnode '$node': " . shift }; | |
175 | run_command( | |
176 | [@$ssh_cmd, $ip, $qdevice_certutil, "-i", "-c", "/etc/pve/$ca_export_base"], | |
177 | noerr => 1, outfunc => \&$outsub | |
178 | ); | |
179 | }); | |
180 | unlink "/etc/pve/$ca_export_base"; | |
181 | ||
182 | print "\nINFO: generating cert request\n"; | |
183 | run_command([$qdevice_certutil, "-r", "-n", $clustername]); | |
184 | ||
185 | print "\nINFO: copying exported cert request to qnetd server\n"; | |
186 | run_command([@$scp_cmd, "$db_dir_node/$crq_file_base", "root\@\[$qnetd_addr\]:/tmp"]); | |
187 | ||
188 | print "\nINFO: sign and export cluster cert\n"; | |
189 | run_command([ | |
190 | @$ssh_cmd, $qnetd_addr, $qnetd_certutil, "-s", "-c", | |
191 | "/tmp/$crq_file_base", "-n", "$clustername" | |
192 | ]); | |
193 | ||
194 | print "\nINFO: copy exported CRT\n"; | |
195 | run_command([ | |
196 | @$scp_cmd, "root\@\[$qnetd_addr\]:$db_dir_qnetd/cluster-$clustername.crt", | |
197 | "$db_dir_node" | |
198 | ]); | |
199 | ||
200 | print "\nINFO: import certificate\n"; | |
201 | run_command(["$qdevice_certutil", "-M", "-c", "$db_dir_node/cluster-$clustername.crt"]); | |
202 | ||
203 | print "\nINFO: copy and import pk12 cert to all nodes\n"; | |
204 | run_command([@$scp_cmd, "$db_dir_node/$p12_file_base", "/etc/pve/"]); | |
205 | $foreach_member->(sub { | |
206 | my ($node, $ip) = @_; | |
207 | my $outsub = sub { print "\nnode '$node': " . shift }; | |
208 | run_command([ | |
209 | @$ssh_cmd, $ip, "$qdevice_certutil", "-m", "-c", | |
210 | "/etc/pve/$p12_file_base"], outfunc => \&$outsub | |
211 | ); | |
212 | }); | |
213 | unlink "/etc/pve/$p12_file_base"; | |
214 | ||
215 | ||
216 | my $code = sub { | |
217 | my $conf = PVE::Cluster::cfs_read_file("corosync.conf"); | |
218 | my $quorum_section = $conf->{main}->{quorum}; | |
219 | ||
220 | die "Qdevice already configured, must be removed before setting up new one!\n" | |
221 | if defined($quorum_section->{device}); # must not be forced! | |
222 | ||
223 | my $qdev_section = { | |
224 | model => $model, | |
225 | "$model" => { | |
226 | tls => 'on', | |
227 | host => $qnetd_addr, | |
228 | algorithm => $algorithm, | |
229 | } | |
230 | }; | |
231 | $qdev_section->{votes} = 1 if $algorithm eq 'ffsplit'; | |
232 | ||
233 | $quorum_section->{device} = $qdev_section; | |
234 | ||
235 | PVE::Corosync::atomic_write_conf($conf); | |
236 | }; | |
237 | ||
238 | print "\nINFO: add QDevice to cluster configuration\n"; | |
239 | PVE::Cluster::cfs_lock_file('corosync.conf', 10, $code); | |
240 | die $@ if $@; | |
241 | ||
242 | $foreach_member->(sub { | |
243 | my ($node, $ip) = @_; | |
244 | my $outsub = sub { print "\nnode '$node': " . shift }; | |
245 | print "\nINFO: start and enable corosync qdevice daemon on node '$node'...\n"; | |
246 | run_command([@$ssh_cmd, $ip, 'systemctl', 'start', 'corosync-qdevice'], outfunc => \&$outsub); | |
247 | run_command([@$ssh_cmd, $ip, 'systemctl', 'enable', 'corosync-qdevice'], outfunc => \&$outsub); | |
248 | }); | |
249 | ||
250 | run_command(['corosync-cfgtool', '-R']); # do cluster wide config reload | |
251 | ||
252 | return undef; | |
253 | }}); | |
254 | ||
255 | __PACKAGE__->register_method ({ | |
256 | name => 'remove_qdevice', | |
257 | path => 'remove_qdevice', | |
258 | method => 'DELETE', | |
259 | description => "Remove a configured QDevice", | |
260 | parameters => { | |
261 | additionalProperties => 0, | |
262 | properties => {}, | |
263 | }, | |
264 | returns => { type => 'null' }, | |
265 | ||
266 | code => sub { | |
267 | my ($param) = @_; | |
268 | ||
269 | die "Node not in a cluster. Aborting.\n" | |
270 | if !PVE::Corosync::check_conf_exists(1); | |
271 | ||
272 | my $members = PVE::Cluster::get_members(); | |
273 | foreach my $node (sort keys %$members) { | |
274 | die "All nodes must be online! Node $node is offline, aborting.\n" | |
275 | if !$members->{$node}->{online}; | |
276 | } | |
277 | ||
278 | my $ssh_cmd = ['ssh', '-o', 'BatchMode=yes', '-lroot']; | |
279 | ||
280 | my $code = sub { | |
281 | my $conf = PVE::Cluster::cfs_read_file("corosync.conf"); | |
282 | my $quorum_section = $conf->{main}->{quorum}; | |
283 | ||
284 | die "No QDevice configured!\n" if !defined($quorum_section->{device}); | |
285 | ||
286 | delete $quorum_section->{device}; | |
287 | ||
288 | PVE::Corosync::atomic_write_conf($conf); | |
289 | ||
290 | # cleanup qdev state (cert storage) | |
291 | my $qdev_state_dir = "/etc/corosync/qdevice"; | |
292 | $foreach_member->(sub { | |
293 | my (undef, $ip) = @_; | |
294 | run_command([@$ssh_cmd, $ip, '--', 'rm', '-rf', $qdev_state_dir]); | |
295 | }); | |
296 | }; | |
297 | ||
298 | PVE::Cluster::cfs_lock_file('corosync.conf', 10, $code); | |
299 | die $@ if $@; | |
300 | ||
301 | $foreach_member->(sub { | |
302 | my (undef, $ip) = @_; | |
303 | run_command([@$ssh_cmd, $ip, 'systemctl', 'stop', 'corosync-qdevice']); | |
304 | run_command([@$ssh_cmd, $ip, 'systemctl', 'disable', 'corosync-qdevice']); | |
305 | }); | |
306 | ||
307 | run_command(['corosync-cfgtool', '-R']); | |
308 | ||
309 | print "\nRemoved Qdevice.\n"; | |
310 | ||
311 | return undef; | |
312 | }}); | |
313 | ||
13d44dc5 | 314 | __PACKAGE__->register_method ({ |
c53b111f | 315 | name => 'add', |
13d44dc5 DM |
316 | path => 'add', |
317 | method => 'PUT', | |
318 | description => "Adds the current node to an existing cluster.", | |
319 | parameters => { | |
320 | additionalProperties => 0, | |
321 | properties => { | |
322 | hostname => { | |
323 | type => 'string', | |
324 | description => "Hostname (or IP) of an existing cluster member." | |
325 | }, | |
10c6810e | 326 | nodeid => get_standard_option('corosync-nodeid'), |
13d44dc5 DM |
327 | votes => { |
328 | type => 'integer', | |
329 | description => "Number of votes for this node", | |
330 | minimum => 0, | |
331 | optional => 1, | |
332 | }, | |
333 | force => { | |
334 | type => 'boolean', | |
335 | description => "Do not throw error if node already exists.", | |
336 | optional => 1, | |
337 | }, | |
1584e3a1 TL |
338 | link0 => get_standard_option('corosync-link'), |
339 | link1 => get_standard_option('corosync-link'), | |
10c6810e | 340 | fingerprint => get_standard_option('fingerprint-sha256', { |
03b23bd0 TL |
341 | optional => 1, |
342 | }), | |
343 | 'use_ssh' => { | |
344 | type => 'boolean', | |
345 | description => "Always use SSH to join, even if peer may do it over API.", | |
346 | optional => 1, | |
347 | }, | |
13d44dc5 DM |
348 | }, |
349 | }, | |
350 | returns => { type => 'null' }, | |
c53b111f | 351 | |
13d44dc5 DM |
352 | code => sub { |
353 | my ($param) = @_; | |
354 | ||
355 | my $nodename = PVE::INotify::nodename(); | |
356 | ||
03b23bd0 | 357 | my $host = $param->{hostname}; |
8db9997d | 358 | my $local_ip_address = PVE::Cluster::remote_node_ip($nodename); |
13d44dc5 | 359 | |
cde60d30 FG |
360 | my $link0 = PVE::Corosync::parse_corosync_link($param->{link0}); |
361 | my $link1 = PVE::Corosync::parse_corosync_link($param->{link1}); | |
1584e3a1 | 362 | |
c5204e14 | 363 | PVE::Cluster::Setup::assert_joinable($local_ip_address, $link0, $link1, $param->{force}); |
f566b424 | 364 | |
83e5e7b7 | 365 | my $worker = sub { |
03b23bd0 | 366 | |
83e5e7b7 TL |
367 | if (!$param->{use_ssh}) { |
368 | print "Please enter superuser (root) password for '$host':\n"; | |
369 | my $password = PVE::PTY::read_password("Password for root\@$host: "); | |
03b23bd0 | 370 | |
83e5e7b7 TL |
371 | delete $param->{use_ssh}; |
372 | $param->{password} = $password; | |
03b23bd0 | 373 | |
83e5e7b7 | 374 | my $local_cluster_lock = "/var/lock/pvecm.lock"; |
c5204e14 | 375 | PVE::Tools::lock_file($local_cluster_lock, 10, \&PVE::Cluster::Setup::join, $param); |
83e5e7b7 TL |
376 | |
377 | if (my $err = $@) { | |
efe59a84 | 378 | if (ref($err) eq 'PVE::APIClient::Exception' && defined($err->{code}) && $err->{code} == 501) { |
83e5e7b7 TL |
379 | $err = "Remote side is not able to use API for Cluster join!\n" . |
380 | "Pass the 'use_ssh' switch or update the remote side.\n"; | |
381 | } | |
382 | die $err; | |
03b23bd0 | 383 | } |
83e5e7b7 | 384 | return; # all OK, the API join endpoint successfully set us up |
03b23bd0 | 385 | } |
03b23bd0 | 386 | |
83e5e7b7 TL |
387 | # allow fallback to old ssh only join if wished or needed |
388 | ||
c5204e14 FG |
389 | PVE::Cluster::Setup::setup_sshd_config(); |
390 | PVE::Cluster::Setup::setup_rootsshconfig(); | |
391 | PVE::Cluster::Setup::setup_ssh_keys(); | |
03b23bd0 | 392 | |
83e5e7b7 | 393 | # make sure known_hosts is on local filesystem |
c5204e14 | 394 | PVE::Cluster::Setup::ssh_unmerge_known_hosts(); |
5a630d8f | 395 | |
83e5e7b7 TL |
396 | my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"]; |
397 | run_command($cmd, 'outfunc' => sub {}, 'errfunc' => sub {}, | |
398 | 'errmsg' => "unable to copy ssh ID"); | |
13d44dc5 | 399 | |
83e5e7b7 TL |
400 | $cmd = ['ssh', $host, '-o', 'BatchMode=yes', |
401 | 'pvecm', 'addnode', $nodename, '--force', 1]; | |
13d44dc5 | 402 | |
83e5e7b7 TL |
403 | push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid}; |
404 | push @$cmd, '--votes', $param->{votes} if defined($param->{votes}); | |
1584e3a1 TL |
405 | # just pass the un-parsed string through, or as we've address as |
406 | # the default_key, we can just pass the fallback directly too | |
407 | push @$cmd, '--link0', $param->{link0} // $local_ip_address; | |
408 | push @$cmd, '--link1', $param->{link1} if defined($param->{link1}); | |
83e5e7b7 TL |
409 | |
410 | if (system (@$cmd) != 0) { | |
411 | my $cmdtxt = join (' ', @$cmd); | |
412 | die "unable to add node: command failed ($cmdtxt)\n"; | |
413 | } | |
13d44dc5 | 414 | |
83e5e7b7 TL |
415 | my $tmpdir = "$libdir/.pvecm_add.tmp.$$"; |
416 | mkdir $tmpdir; | |
14d0000a | 417 | |
83e5e7b7 TL |
418 | eval { |
419 | print "copy corosync auth key\n"; | |
420 | $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq', | |
421 | "[$host]:$authfile $clusterconf", $tmpdir]; | |
13d44dc5 | 422 | |
83e5e7b7 | 423 | system(@$cmd) == 0 || die "can't rsync data from host '$host'\n"; |
13d44dc5 | 424 | |
83e5e7b7 TL |
425 | my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf"); |
426 | my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey"); | |
13d44dc5 | 427 | |
c5204e14 | 428 | PVE::Cluster::Setup::finish_join($host, $corosync_conf, $corosync_authkey); |
83e5e7b7 TL |
429 | }; |
430 | my $err = $@; | |
13d44dc5 | 431 | |
83e5e7b7 | 432 | rmtree $tmpdir; |
13d44dc5 | 433 | |
83e5e7b7 | 434 | die $err if $err; |
13d44dc5 | 435 | }; |
13d44dc5 | 436 | |
83e5e7b7 TL |
437 | # use a synced worker so we get a nice task log when joining through CLI |
438 | my $rpcenv = PVE::RPCEnvironment::get(); | |
439 | my $authuser = $rpcenv->get_user(); | |
13d44dc5 | 440 | |
83e5e7b7 | 441 | $rpcenv->fork_worker('clusterjoin', '', $authuser, $worker); |
13d44dc5 DM |
442 | |
443 | return undef; | |
444 | }}); | |
445 | ||
446 | __PACKAGE__->register_method ({ | |
c53b111f | 447 | name => 'status', |
13d44dc5 DM |
448 | path => 'status', |
449 | method => 'GET', | |
450 | description => "Displays the local view of the cluster status.", | |
451 | parameters => { | |
452 | additionalProperties => 0, | |
453 | properties => {}, | |
454 | }, | |
455 | returns => { type => 'null' }, | |
c53b111f | 456 | |
13d44dc5 DM |
457 | code => sub { |
458 | my ($param) = @_; | |
459 | ||
b6973a89 | 460 | PVE::Corosync::check_conf_exists(); |
3df092f9 TL |
461 | my $conf = eval { PVE::Cluster::cfs_read_file("corosync.conf") } // {}; |
462 | warn "$@" if $@; | |
463 | my $totem = PVE::Corosync::totem_config($conf); | |
464 | ||
465 | if (scalar(%$totem)) { | |
466 | my $print_info = sub { | |
467 | my ($label, $key, $default) = @_; | |
468 | my $val = $totem->{$key} // $default; | |
469 | printf "%-17s %s\n", "$label:", "$val"; | |
470 | }; | |
471 | ||
472 | printf "Cluster information\n"; | |
473 | printf "-------------------\n"; | |
474 | $print_info->('Name', 'cluster_name', 'UNKOWN?'); | |
475 | $print_info->('Config Version', 'config_version', -1); | |
476 | $print_info->('Transport', 'transport', 'knet'); | |
477 | $print_info->('Secure auth', 'secauth', 'off'); | |
478 | printf "\n"; | |
479 | } | |
eb51b829 | 480 | |
13d44dc5 DM |
481 | my $cmd = ['corosync-quorumtool', '-siH']; |
482 | ||
483 | exec (@$cmd); | |
484 | ||
485 | exit (-1); # should not be reached | |
486 | }}); | |
487 | ||
488 | __PACKAGE__->register_method ({ | |
c53b111f | 489 | name => 'nodes', |
13d44dc5 DM |
490 | path => 'nodes', |
491 | method => 'GET', | |
492 | description => "Displays the local view of the cluster nodes.", | |
493 | parameters => { | |
494 | additionalProperties => 0, | |
495 | properties => {}, | |
496 | }, | |
497 | returns => { type => 'null' }, | |
c53b111f | 498 | |
13d44dc5 DM |
499 | code => sub { |
500 | my ($param) = @_; | |
501 | ||
b6973a89 | 502 | PVE::Corosync::check_conf_exists(); |
eb51b829 | 503 | |
13d44dc5 DM |
504 | my $cmd = ['corosync-quorumtool', '-l']; |
505 | ||
506 | exec (@$cmd); | |
507 | ||
508 | exit (-1); # should not be reached | |
509 | }}); | |
510 | ||
511 | __PACKAGE__->register_method ({ | |
c53b111f | 512 | name => 'expected', |
13d44dc5 DM |
513 | path => 'expected', |
514 | method => 'PUT', | |
515 | description => "Tells corosync a new value of expected votes.", | |
516 | parameters => { | |
517 | additionalProperties => 0, | |
518 | properties => { | |
519 | expected => { | |
520 | type => 'integer', | |
521 | description => "Expected votes", | |
522 | minimum => 1, | |
523 | }, | |
524 | }, | |
525 | }, | |
526 | returns => { type => 'null' }, | |
c53b111f | 527 | |
13d44dc5 DM |
528 | code => sub { |
529 | my ($param) = @_; | |
530 | ||
b6973a89 | 531 | PVE::Corosync::check_conf_exists(); |
eb51b829 | 532 | |
13d44dc5 DM |
533 | my $cmd = ['corosync-quorumtool', '-e', $param->{expected}]; |
534 | ||
535 | exec (@$cmd); | |
536 | ||
537 | exit (-1); # should not be reached | |
538 | ||
539 | }}); | |
540 | ||
13d44dc5 | 541 | __PACKAGE__->register_method ({ |
c53b111f | 542 | name => 'updatecerts', |
13d44dc5 DM |
543 | path => 'updatecerts', |
544 | method => 'PUT', | |
545 | description => "Update node certificates (and generate all needed files/directories).", | |
546 | parameters => { | |
547 | additionalProperties => 0, | |
548 | properties => { | |
549 | force => { | |
550 | description => "Force generation of new SSL certifate.", | |
551 | type => 'boolean', | |
552 | optional => 1, | |
553 | }, | |
554 | silent => { | |
555 | description => "Ignore errors (i.e. when cluster has no quorum).", | |
556 | type => 'boolean', | |
557 | optional => 1, | |
558 | }, | |
559 | }, | |
560 | }, | |
561 | returns => { type => 'null' }, | |
562 | code => sub { | |
563 | my ($param) = @_; | |
564 | ||
4e8cc352 TL |
565 | # we get called by the pve-cluster.service ExecStartPost and as we do |
566 | # IO (on /etc/pve) which can hang (uninterruptedly D state). That'd be | |
567 | # no-good for ExecStartPost as it fails the whole service in this case | |
568 | PVE::Tools::run_fork_with_timeout(30, sub { | |
c5204e14 | 569 | PVE::Cluster::Setup::updatecerts_and_ssh($param->@{qw(force silent)}); |
50f74e31 | 570 | }); |
13d44dc5 DM |
571 | |
572 | return undef; | |
573 | }}); | |
574 | ||
ac7a8cf1 TL |
575 | __PACKAGE__->register_method ({ |
576 | name => 'mtunnel', | |
577 | path => 'mtunnel', | |
578 | method => 'POST', | |
579 | description => "Used by VM/CT migration - do not use manually.", | |
580 | parameters => { | |
581 | additionalProperties => 0, | |
582 | properties => { | |
583 | get_migration_ip => { | |
584 | type => 'boolean', | |
585 | default => 0, | |
586 | description => 'return the migration IP, if configured', | |
587 | optional => 1, | |
588 | }, | |
589 | migration_network => { | |
590 | type => 'string', | |
591 | format => 'CIDR', | |
592 | description => 'the migration network used to detect the local migration IP', | |
593 | optional => 1, | |
594 | }, | |
595 | 'run-command' => { | |
596 | type => 'boolean', | |
597 | description => 'Run a command with a tcp socket as standard input.' | |
598 | .' The IP address and port are printed via this' | |
599 | ." command's stdandard output first, each on a separate line.", | |
600 | optional => 1, | |
601 | }, | |
602 | 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args'), | |
603 | }, | |
604 | }, | |
605 | returns => { type => 'null'}, | |
606 | code => sub { | |
607 | my ($param) = @_; | |
608 | ||
609 | if (!PVE::Cluster::check_cfs_quorum(1)) { | |
610 | print "no quorum\n"; | |
611 | return undef; | |
612 | } | |
613 | ||
ab966729 FG |
614 | my $get_local_migration_ip = sub { |
615 | my ($cidr) = @_; | |
616 | ||
617 | if (!defined($cidr)) { | |
618 | my $dc_conf = cfs_read_file('datacenter.cfg'); | |
619 | $cidr = $dc_conf->{migration}->{network} | |
620 | if defined($dc_conf->{migration}->{network}); | |
621 | } | |
622 | ||
623 | if (defined($cidr)) { | |
624 | my $ips = PVE::Network::get_local_ip_from_cidr($cidr); | |
625 | ||
626 | die "could not get migration ip: no IP address configured on local " . | |
627 | "node for network '$cidr'\n" if scalar(@$ips) == 0; | |
628 | ||
629 | die "could not get migration ip: multiple IP address configured for " . | |
630 | "network '$cidr'\n" if scalar(@$ips) > 1; | |
631 | ||
632 | return @$ips[0]; | |
633 | } | |
634 | ||
635 | return undef; | |
636 | }; | |
637 | ||
ac7a8cf1 TL |
638 | my $network = $param->{migration_network}; |
639 | if ($param->{get_migration_ip}) { | |
640 | die "cannot use --run-command with --get_migration_ip\n" | |
641 | if $param->{'run-command'}; | |
ab966729 FG |
642 | |
643 | if (my $ip = $get_local_migration_ip->($network)) { | |
ac7a8cf1 TL |
644 | print "ip: '$ip'\n"; |
645 | } else { | |
646 | print "no ip\n"; | |
647 | } | |
648 | # do not keep tunnel open when asked for migration ip | |
649 | return undef; | |
650 | } | |
651 | ||
652 | if ($param->{'run-command'}) { | |
653 | my $cmd = $param->{'extra-args'}; | |
654 | die "missing command\n" | |
655 | if !$cmd || !scalar(@$cmd); | |
656 | ||
657 | # Get an ip address to listen on, and find a free migration port | |
658 | my ($ip, $family); | |
659 | if (defined($network)) { | |
ab966729 | 660 | $ip = $get_local_migration_ip->($network) |
ac7a8cf1 TL |
661 | or die "failed to get migration IP address to listen on\n"; |
662 | $family = PVE::Tools::get_host_address_family($ip); | |
663 | } else { | |
664 | my $nodename = PVE::INotify::nodename(); | |
665 | ($ip, $family) = PVE::Network::get_ip_from_hostname($nodename, 0); | |
666 | } | |
667 | my $port = PVE::Tools::next_migrate_port($family, $ip); | |
668 | ||
669 | PVE::Tools::pipe_socket_to_command($cmd, $ip, $port); | |
670 | return undef; | |
671 | } | |
672 | ||
673 | print "tunnel online\n"; | |
674 | *STDOUT->flush(); | |
675 | ||
676 | while (my $line = <STDIN>) { | |
677 | chomp $line; | |
678 | last if $line =~ m/^quit$/; | |
679 | } | |
680 | ||
681 | return undef; | |
682 | }}); | |
683 | ||
684 | ||
13d44dc5 DM |
685 | our $cmddef = { |
686 | keygen => [ __PACKAGE__, 'keygen', ['filename']], | |
74e09a93 | 687 | create => [ 'PVE::API2::ClusterConfig', 'create', ['clustername']], |
13d44dc5 | 688 | add => [ __PACKAGE__, 'add', ['hostname']], |
1d26c202 TL |
689 | addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']], |
690 | delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']], | |
13d44dc5 DM |
691 | status => [ __PACKAGE__, 'status' ], |
692 | nodes => [ __PACKAGE__, 'nodes' ], | |
693 | expected => [ __PACKAGE__, 'expected', ['expected']], | |
694 | updatecerts => [ __PACKAGE__, 'updatecerts', []], | |
ac7a8cf1 | 695 | mtunnel => [ __PACKAGE__, 'mtunnel', ['extra-args']], |
63afd01d OB |
696 | qdevice => { |
697 | setup => [ __PACKAGE__, 'setup_qdevice', ['address']], | |
698 | remove => [ __PACKAGE__, 'remove_qdevice', []], | |
699 | } | |
13d44dc5 DM |
700 | }; |
701 | ||
702 | 1; |