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