]>
Commit | Line | Data |
---|---|---|
13d44dc5 DM |
1 | package PVE::CLI::pvecm; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
95e7bcac | 5 | |
91c25936 | 6 | use Cwd qw(getcwd); |
13d44dc5 DM |
7 | use File::Path; |
8 | use File::Basename; | |
294f76c4 | 9 | use PVE::Tools qw(run_command); |
13d44dc5 DM |
10 | use PVE::Cluster; |
11 | use PVE::INotify; | |
10c6810e | 12 | use PVE::JSONSchema qw(get_standard_option); |
03b23bd0 | 13 | use PVE::RPCEnvironment; |
13d44dc5 | 14 | use PVE::CLIHandler; |
03b23bd0 | 15 | use PVE::PTY; |
1d26c202 | 16 | use PVE::API2::ClusterConfig; |
b6973a89 | 17 | use PVE::Corosync; |
c5204e14 | 18 | use PVE::Cluster::Setup; |
13d44dc5 DM |
19 | |
20 | use base qw(PVE::CLIHandler); | |
21 | ||
22 | $ENV{HOME} = '/root'; # for ssh-copy-id | |
23 | ||
24 | my $basedir = "/etc/pve"; | |
25 | my $clusterconf = "$basedir/corosync.conf"; | |
26 | my $libdir = "/var/lib/pve-cluster"; | |
13d44dc5 DM |
27 | my $authfile = "/etc/corosync/authkey"; |
28 | ||
9db3f0c0 | 29 | |
03b23bd0 TL |
30 | sub setup_environment { |
31 | PVE::RPCEnvironment->setup_default_cli_env(); | |
32 | } | |
33 | ||
13d44dc5 | 34 | __PACKAGE__->register_method ({ |
c53b111f | 35 | name => 'keygen', |
13d44dc5 DM |
36 | path => 'keygen', |
37 | method => 'PUT', | |
38 | description => "Generate new cryptographic key for corosync.", | |
39 | parameters => { | |
40 | additionalProperties => 0, | |
41 | properties => { | |
42 | filename => { | |
43 | type => 'string', | |
44 | description => "Output file name" | |
45 | } | |
46 | }, | |
47 | }, | |
48 | returns => { type => 'null' }, | |
c53b111f | 49 | |
13d44dc5 DM |
50 | code => sub { |
51 | my ($param) = @_; | |
52 | ||
53 | my $filename = $param->{filename}; | |
54 | ||
55 | # test EUID | |
56 | $> == 0 || die "Error: Authorization key must be generated as root user.\n"; | |
57 | my $dirname = dirname($filename); | |
13d44dc5 DM |
58 | |
59 | die "key file '$filename' already exists\n" if -e $filename; | |
60 | ||
61 | File::Path::make_path($dirname) if $dirname; | |
62 | ||
294f76c4 | 63 | run_command(['corosync-keygen', '-l', '-k', $filename]); |
13d44dc5 DM |
64 | |
65 | return undef; | |
66 | }}); | |
67 | ||
63afd01d OB |
68 | my $foreach_member = sub { |
69 | my ($code, $noerr) = @_; | |
70 | ||
71 | my $members = PVE::Cluster::get_members(); | |
72 | foreach my $node (sort keys %$members) { | |
73 | if (my $ip = $members->{$node}->{ip}) { | |
74 | $code->($node, $ip); | |
75 | } else { | |
76 | die "cannot get the cluster IP for node '$node'.\n" if !$noerr; | |
77 | warn "cannot get the cluster IP for node '$node'.\n"; | |
78 | return undef; | |
79 | } | |
80 | } | |
81 | }; | |
82 | ||
83 | __PACKAGE__->register_method ({ | |
84 | name => 'setup_qdevice', | |
85 | path => 'setup_qdevice', | |
86 | method => 'PUT', | |
87 | description => "Setup the use of a QDevice", | |
88 | parameters => { | |
89 | additionalProperties => 0, | |
90 | properties => { | |
91 | address => { | |
92 | type => 'string', format => 'ip', | |
93 | description => "Specifies the network address of an external corosync QDevice" , | |
94 | }, | |
95 | network => { | |
96 | type => 'string', | |
97 | format => 'CIDR', | |
98 | description => 'The network which should be used to connect to the external qdevice', | |
99 | optional => 1, | |
100 | }, | |
101 | force => { | |
102 | type => 'boolean', | |
103 | description => "Do not throw error on possible dangerous operations.", | |
104 | optional => 1, | |
105 | }, | |
106 | }, | |
107 | }, | |
108 | returns => { type => 'null' }, | |
109 | ||
110 | code => sub { | |
111 | my ($param) = @_; | |
112 | ||
75a3d341 | 113 | PVE::Corosync::check_conf_exists(); |
63afd01d OB |
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'; | |
9e7e46ae | 130 | if (scalar(%{$members}) & 1) { |
63afd01d OB |
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 | ||
75a3d341 | 269 | PVE::Corosync::check_conf_exists(); |
63afd01d OB |
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, | |
8ef581e4 | 320 | properties => PVE::Corosync::add_corosync_link_properties({ |
13d44dc5 DM |
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 | 337 | fingerprint => get_standard_option('fingerprint-sha256', { |
03b23bd0 TL |
338 | optional => 1, |
339 | }), | |
340 | 'use_ssh' => { | |
341 | type => 'boolean', | |
342 | description => "Always use SSH to join, even if peer may do it over API.", | |
343 | optional => 1, | |
344 | }, | |
8ef581e4 | 345 | }), |
13d44dc5 DM |
346 | }, |
347 | returns => { type => 'null' }, | |
c53b111f | 348 | |
13d44dc5 DM |
349 | code => sub { |
350 | my ($param) = @_; | |
351 | ||
91c25936 FW |
352 | # avoid "transport endpoint not connected" errors that occur if |
353 | # restarting pmxcfs while in fuse-mounted /etc/pve | |
354 | die "Navigate out of $basedir before running 'pvecm add', for example by running 'cd'.\n" | |
355 | if getcwd() =~ m!^$basedir(/.*)?$!; | |
356 | ||
13d44dc5 | 357 | my $nodename = PVE::INotify::nodename(); |
03b23bd0 | 358 | my $host = $param->{hostname}; |
f566b424 | 359 | |
83e5e7b7 | 360 | my $worker = sub { |
03b23bd0 | 361 | |
83e5e7b7 | 362 | if (!$param->{use_ssh}) { |
f55fe6b3 | 363 | my $password = PVE::PTY::read_password("Please enter superuser (root) password for '$host': "); |
03b23bd0 | 364 | |
83e5e7b7 TL |
365 | delete $param->{use_ssh}; |
366 | $param->{password} = $password; | |
03b23bd0 | 367 | |
83e5e7b7 | 368 | my $local_cluster_lock = "/var/lock/pvecm.lock"; |
c5204e14 | 369 | PVE::Tools::lock_file($local_cluster_lock, 10, \&PVE::Cluster::Setup::join, $param); |
83e5e7b7 TL |
370 | |
371 | if (my $err = $@) { | |
efe59a84 | 372 | if (ref($err) eq 'PVE::APIClient::Exception' && defined($err->{code}) && $err->{code} == 501) { |
83e5e7b7 TL |
373 | $err = "Remote side is not able to use API for Cluster join!\n" . |
374 | "Pass the 'use_ssh' switch or update the remote side.\n"; | |
375 | } | |
376 | die $err; | |
03b23bd0 | 377 | } |
83e5e7b7 | 378 | return; # all OK, the API join endpoint successfully set us up |
03b23bd0 | 379 | } |
03b23bd0 | 380 | |
83e5e7b7 TL |
381 | # allow fallback to old ssh only join if wished or needed |
382 | ||
1e3e951c | 383 | my $local_ip_address = PVE::Cluster::remote_node_ip($nodename); |
8ef581e4 | 384 | my $links = PVE::Corosync::extract_corosync_link_args($param); |
1e3e951c | 385 | |
8ef581e4 | 386 | PVE::Cluster::Setup::assert_joinable($local_ip_address, $links, $param->{force}); |
1e3e951c | 387 | |
c5204e14 FG |
388 | PVE::Cluster::Setup::setup_sshd_config(); |
389 | PVE::Cluster::Setup::setup_rootsshconfig(); | |
390 | PVE::Cluster::Setup::setup_ssh_keys(); | |
03b23bd0 | 391 | |
83e5e7b7 | 392 | # make sure known_hosts is on local filesystem |
c5204e14 | 393 | PVE::Cluster::Setup::ssh_unmerge_known_hosts(); |
5a630d8f | 394 | |
83e5e7b7 | 395 | my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"]; |
6ae43599 TL |
396 | run_command( |
397 | $cmd, 'outfunc' => sub {}, 'errfunc' => sub {}, 'errmsg' => "unable to copy ssh ID"); | |
13d44dc5 | 398 | |
a755ff54 SR |
399 | $cmd = ['ssh', $host, '-o', 'BatchMode=yes', 'pvecm', 'apiver']; |
400 | my $remote_apiver = 0; | |
401 | run_command($cmd, 'outfunc' => sub { | |
402 | $remote_apiver = shift; | |
403 | chomp $remote_apiver; | |
404 | }, 'noerr' => 1); | |
405 | ||
498e355e | 406 | PVE::Cluster::Setup::assert_we_can_join_cluster_version($remote_apiver); |
a755ff54 | 407 | |
6ae43599 | 408 | $cmd = ['ssh', $host, '-o', 'BatchMode=yes', 'pvecm', 'addnode', $nodename, '--force', 1]; |
13d44dc5 | 409 | |
83e5e7b7 TL |
410 | push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid}; |
411 | push @$cmd, '--votes', $param->{votes} if defined($param->{votes}); | |
8ef581e4 | 412 | |
f017b47f FG |
413 | my $link_desc = get_standard_option('corosync-link'); |
414 | ||
8ef581e4 SR |
415 | foreach my $link (keys %$links) { |
416 | push @$cmd, "--link$link", PVE::JSONSchema::print_property_string( | |
f017b47f | 417 | $links->{$link}, $link_desc->{format}); |
8ef581e4 | 418 | } |
83e5e7b7 | 419 | |
88b4cb13 SR |
420 | # this will be used as fallback if no links are specified |
421 | if (!%$links) { | |
a755ff54 SR |
422 | push @$cmd, '--link0', $local_ip_address if $remote_apiver == 0; |
423 | push @$cmd, '--new_node_ip', $local_ip_address if $remote_apiver >= 1; | |
424 | ||
88b4cb13 SR |
425 | print "No cluster network links passed explicitly, fallback to local node" |
426 | . " IP '$local_ip_address'\n"; | |
427 | } | |
428 | ||
83e5e7b7 TL |
429 | if (system (@$cmd) != 0) { |
430 | my $cmdtxt = join (' ', @$cmd); | |
431 | die "unable to add node: command failed ($cmdtxt)\n"; | |
432 | } | |
13d44dc5 | 433 | |
83e5e7b7 TL |
434 | my $tmpdir = "$libdir/.pvecm_add.tmp.$$"; |
435 | mkdir $tmpdir; | |
14d0000a | 436 | |
83e5e7b7 TL |
437 | eval { |
438 | print "copy corosync auth key\n"; | |
439 | $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq', | |
96979975 | 440 | "[$host]:$authfile", "[$host]:$clusterconf", $tmpdir]; |
13d44dc5 | 441 | |
83e5e7b7 | 442 | system(@$cmd) == 0 || die "can't rsync data from host '$host'\n"; |
13d44dc5 | 443 | |
83e5e7b7 TL |
444 | my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf"); |
445 | my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey"); | |
13d44dc5 | 446 | |
1e0c6aff | 447 | PVE::Cluster::Setup::finish_join($nodename, $corosync_conf, $corosync_authkey); |
83e5e7b7 TL |
448 | }; |
449 | my $err = $@; | |
13d44dc5 | 450 | |
83e5e7b7 | 451 | rmtree $tmpdir; |
13d44dc5 | 452 | |
83e5e7b7 | 453 | die $err if $err; |
13d44dc5 | 454 | }; |
13d44dc5 | 455 | |
83e5e7b7 TL |
456 | # use a synced worker so we get a nice task log when joining through CLI |
457 | my $rpcenv = PVE::RPCEnvironment::get(); | |
458 | my $authuser = $rpcenv->get_user(); | |
13d44dc5 | 459 | |
83e5e7b7 | 460 | $rpcenv->fork_worker('clusterjoin', '', $authuser, $worker); |
13d44dc5 DM |
461 | |
462 | return undef; | |
463 | }}); | |
464 | ||
465 | __PACKAGE__->register_method ({ | |
c53b111f | 466 | name => 'status', |
13d44dc5 DM |
467 | path => 'status', |
468 | method => 'GET', | |
469 | description => "Displays the local view of the cluster status.", | |
470 | parameters => { | |
471 | additionalProperties => 0, | |
472 | properties => {}, | |
473 | }, | |
474 | returns => { type => 'null' }, | |
c53b111f | 475 | |
13d44dc5 DM |
476 | code => sub { |
477 | my ($param) = @_; | |
478 | ||
b6973a89 | 479 | PVE::Corosync::check_conf_exists(); |
3df092f9 TL |
480 | my $conf = eval { PVE::Cluster::cfs_read_file("corosync.conf") } // {}; |
481 | warn "$@" if $@; | |
482 | my $totem = PVE::Corosync::totem_config($conf); | |
483 | ||
484 | if (scalar(%$totem)) { | |
485 | my $print_info = sub { | |
486 | my ($label, $key, $default) = @_; | |
487 | my $val = $totem->{$key} // $default; | |
488 | printf "%-17s %s\n", "$label:", "$val"; | |
489 | }; | |
490 | ||
491 | printf "Cluster information\n"; | |
492 | printf "-------------------\n"; | |
493 | $print_info->('Name', 'cluster_name', 'UNKOWN?'); | |
494 | $print_info->('Config Version', 'config_version', -1); | |
495 | $print_info->('Transport', 'transport', 'knet'); | |
496 | $print_info->('Secure auth', 'secauth', 'off'); | |
497 | printf "\n"; | |
498 | } | |
eb51b829 | 499 | |
28d5105b | 500 | exec ('corosync-quorumtool', '-siH'); |
13d44dc5 DM |
501 | exit (-1); # should not be reached |
502 | }}); | |
503 | ||
504 | __PACKAGE__->register_method ({ | |
c53b111f | 505 | name => 'nodes', |
13d44dc5 DM |
506 | path => 'nodes', |
507 | method => 'GET', | |
508 | description => "Displays the local view of the cluster nodes.", | |
509 | parameters => { | |
510 | additionalProperties => 0, | |
511 | properties => {}, | |
512 | }, | |
513 | returns => { type => 'null' }, | |
c53b111f | 514 | |
13d44dc5 DM |
515 | code => sub { |
516 | my ($param) = @_; | |
517 | ||
b6973a89 | 518 | PVE::Corosync::check_conf_exists(); |
eb51b829 | 519 | |
28d5105b | 520 | exec ('corosync-quorumtool', '-l'); |
13d44dc5 DM |
521 | exit (-1); # should not be reached |
522 | }}); | |
523 | ||
524 | __PACKAGE__->register_method ({ | |
c53b111f | 525 | name => 'expected', |
13d44dc5 DM |
526 | path => 'expected', |
527 | method => 'PUT', | |
528 | description => "Tells corosync a new value of expected votes.", | |
529 | parameters => { | |
530 | additionalProperties => 0, | |
531 | properties => { | |
532 | expected => { | |
533 | type => 'integer', | |
534 | description => "Expected votes", | |
535 | minimum => 1, | |
536 | }, | |
537 | }, | |
538 | }, | |
539 | returns => { type => 'null' }, | |
c53b111f | 540 | |
13d44dc5 DM |
541 | code => sub { |
542 | my ($param) = @_; | |
543 | ||
b6973a89 | 544 | PVE::Corosync::check_conf_exists(); |
eb51b829 | 545 | |
28d5105b | 546 | exec ('corosync-quorumtool', '-e', $param->{expected}); |
13d44dc5 | 547 | exit (-1); # should not be reached |
13d44dc5 DM |
548 | }}); |
549 | ||
13d44dc5 | 550 | __PACKAGE__->register_method ({ |
c53b111f | 551 | name => 'updatecerts', |
13d44dc5 DM |
552 | path => 'updatecerts', |
553 | method => 'PUT', | |
554 | description => "Update node certificates (and generate all needed files/directories).", | |
555 | parameters => { | |
556 | additionalProperties => 0, | |
557 | properties => { | |
558 | force => { | |
80d19645 | 559 | description => "Force generation of new SSL certificate.", |
13d44dc5 DM |
560 | type => 'boolean', |
561 | optional => 1, | |
562 | }, | |
563 | silent => { | |
564 | description => "Ignore errors (i.e. when cluster has no quorum).", | |
565 | type => 'boolean', | |
566 | optional => 1, | |
567 | }, | |
568 | }, | |
569 | }, | |
570 | returns => { type => 'null' }, | |
571 | code => sub { | |
572 | my ($param) = @_; | |
573 | ||
0ddbd320 | 574 | # we get called by the pveproxy.service ExecStartPre and as we do |
4e8cc352 | 575 | # IO (on /etc/pve) which can hang (uninterruptedly D state). That'd be |
0ddbd320 | 576 | # no-good for ExecStartPre as it fails the whole service in this case |
4e8cc352 | 577 | PVE::Tools::run_fork_with_timeout(30, sub { |
c5204e14 | 578 | PVE::Cluster::Setup::updatecerts_and_ssh($param->@{qw(force silent)}); |
462c16b7 | 579 | PVE::Cluster::prepare_observed_file_basedirs(); |
50f74e31 | 580 | }); |
13d44dc5 DM |
581 | |
582 | return undef; | |
583 | }}); | |
584 | ||
ac7a8cf1 TL |
585 | __PACKAGE__->register_method ({ |
586 | name => 'mtunnel', | |
587 | path => 'mtunnel', | |
588 | method => 'POST', | |
589 | description => "Used by VM/CT migration - do not use manually.", | |
590 | parameters => { | |
591 | additionalProperties => 0, | |
592 | properties => { | |
593 | get_migration_ip => { | |
594 | type => 'boolean', | |
595 | default => 0, | |
596 | description => 'return the migration IP, if configured', | |
597 | optional => 1, | |
598 | }, | |
599 | migration_network => { | |
600 | type => 'string', | |
601 | format => 'CIDR', | |
602 | description => 'the migration network used to detect the local migration IP', | |
603 | optional => 1, | |
604 | }, | |
605 | 'run-command' => { | |
606 | type => 'boolean', | |
607 | description => 'Run a command with a tcp socket as standard input.' | |
608 | .' The IP address and port are printed via this' | |
609 | ." command's stdandard output first, each on a separate line.", | |
610 | optional => 1, | |
611 | }, | |
612 | 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args'), | |
613 | }, | |
614 | }, | |
615 | returns => { type => 'null'}, | |
616 | code => sub { | |
617 | my ($param) = @_; | |
618 | ||
619 | if (!PVE::Cluster::check_cfs_quorum(1)) { | |
620 | print "no quorum\n"; | |
621 | return undef; | |
622 | } | |
623 | ||
ab966729 FG |
624 | my $get_local_migration_ip = sub { |
625 | my ($cidr) = @_; | |
626 | ||
627 | if (!defined($cidr)) { | |
628 | my $dc_conf = cfs_read_file('datacenter.cfg'); | |
629 | $cidr = $dc_conf->{migration}->{network} | |
630 | if defined($dc_conf->{migration}->{network}); | |
631 | } | |
632 | ||
633 | if (defined($cidr)) { | |
634 | my $ips = PVE::Network::get_local_ip_from_cidr($cidr); | |
635 | ||
636 | die "could not get migration ip: no IP address configured on local " . | |
637 | "node for network '$cidr'\n" if scalar(@$ips) == 0; | |
638 | ||
26604116 TL |
639 | die "could not get migration ip: multiple, different, IP address configured for " . |
640 | "network '$cidr'\n" if scalar(@$ips) > 1 && grep { @$ips[0] ne $_ } @$ips; | |
ab966729 FG |
641 | |
642 | return @$ips[0]; | |
643 | } | |
644 | ||
645 | return undef; | |
646 | }; | |
647 | ||
ac7a8cf1 TL |
648 | my $network = $param->{migration_network}; |
649 | if ($param->{get_migration_ip}) { | |
650 | die "cannot use --run-command with --get_migration_ip\n" | |
651 | if $param->{'run-command'}; | |
ab966729 FG |
652 | |
653 | if (my $ip = $get_local_migration_ip->($network)) { | |
ac7a8cf1 TL |
654 | print "ip: '$ip'\n"; |
655 | } else { | |
656 | print "no ip\n"; | |
657 | } | |
658 | # do not keep tunnel open when asked for migration ip | |
659 | return undef; | |
660 | } | |
661 | ||
662 | if ($param->{'run-command'}) { | |
663 | my $cmd = $param->{'extra-args'}; | |
664 | die "missing command\n" | |
665 | if !$cmd || !scalar(@$cmd); | |
666 | ||
667 | # Get an ip address to listen on, and find a free migration port | |
668 | my ($ip, $family); | |
669 | if (defined($network)) { | |
ab966729 | 670 | $ip = $get_local_migration_ip->($network) |
ac7a8cf1 TL |
671 | or die "failed to get migration IP address to listen on\n"; |
672 | $family = PVE::Tools::get_host_address_family($ip); | |
673 | } else { | |
674 | my $nodename = PVE::INotify::nodename(); | |
675 | ($ip, $family) = PVE::Network::get_ip_from_hostname($nodename, 0); | |
676 | } | |
677 | my $port = PVE::Tools::next_migrate_port($family, $ip); | |
678 | ||
679 | PVE::Tools::pipe_socket_to_command($cmd, $ip, $port); | |
680 | return undef; | |
681 | } | |
682 | ||
683 | print "tunnel online\n"; | |
684 | *STDOUT->flush(); | |
685 | ||
686 | while (my $line = <STDIN>) { | |
687 | chomp $line; | |
688 | last if $line =~ m/^quit$/; | |
689 | } | |
690 | ||
691 | return undef; | |
692 | }}); | |
693 | ||
694 | ||
13d44dc5 | 695 | our $cmddef = { |
a755ff54 SR |
696 | apiver => [ 'PVE::API2::ClusterConfig', 'join_api_version', [], {}, sub { |
697 | my $apiver = shift; | |
698 | print "$apiver\n"; | |
699 | }], | |
13d44dc5 | 700 | keygen => [ __PACKAGE__, 'keygen', ['filename']], |
74e09a93 | 701 | create => [ 'PVE::API2::ClusterConfig', 'create', ['clustername']], |
13d44dc5 | 702 | add => [ __PACKAGE__, 'add', ['hostname']], |
1d26c202 TL |
703 | addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']], |
704 | delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']], | |
13d44dc5 DM |
705 | status => [ __PACKAGE__, 'status' ], |
706 | nodes => [ __PACKAGE__, 'nodes' ], | |
707 | expected => [ __PACKAGE__, 'expected', ['expected']], | |
708 | updatecerts => [ __PACKAGE__, 'updatecerts', []], | |
ac7a8cf1 | 709 | mtunnel => [ __PACKAGE__, 'mtunnel', ['extra-args']], |
63afd01d OB |
710 | qdevice => { |
711 | setup => [ __PACKAGE__, 'setup_qdevice', ['address']], | |
712 | remove => [ __PACKAGE__, 'remove_qdevice', []], | |
713 | } | |
13d44dc5 DM |
714 | }; |
715 | ||
716 | 1; |