]> git.proxmox.com Git - pve-cluster.git/blob - data/PVE/CLI/pvecm.pm
48c110bef77a03f0fb25d6ded41c31d5978b2092
[pve-cluster.git] / data / PVE / CLI / pvecm.pm
1 package PVE::CLI::pvecm;
2
3 use strict;
4 use warnings;
5
6 use File::Path;
7 use File::Basename;
8 use PVE::Tools qw(run_command);
9 use PVE::Cluster;
10 use PVE::INotify;
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::RPCEnvironment;
13 use PVE::CLIHandler;
14 use PVE::PTY;
15 use PVE::API2::ClusterConfig;
16 use PVE::Corosync;
17 use PVE::Cluster::Setup;
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";
26 my $authfile = "/etc/corosync/authkey";
27
28
29 sub setup_environment {
30 PVE::RPCEnvironment->setup_default_cli_env();
31 }
32
33 __PACKAGE__->register_method ({
34 name => 'keygen',
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' },
48
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);
57
58 die "key file '$filename' already exists\n" if -e $filename;
59
60 File::Path::make_path($dirname) if $dirname;
61
62 run_command(['corosync-keygen', '-l', '-k', $filename]);
63
64 return undef;
65 }});
66
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
314 __PACKAGE__->register_method ({
315 name => 'add',
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 },
326 nodeid => get_standard_option('corosync-nodeid'),
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 },
338 link0 => get_standard_option('corosync-link'),
339 link1 => get_standard_option('corosync-link'),
340 fingerprint => get_standard_option('fingerprint-sha256', {
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 },
348 },
349 },
350 returns => { type => 'null' },
351
352 code => sub {
353 my ($param) = @_;
354
355 my $nodename = PVE::INotify::nodename();
356
357 my $host = $param->{hostname};
358 my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
359
360 my $link0 = PVE::Cluster::parse_corosync_link($param->{link0});
361 my $link1 = PVE::Cluster::parse_corosync_link($param->{link1});
362
363 PVE::Cluster::Setup::assert_joinable($local_ip_address, $link0, $link1, $param->{force});
364
365 my $worker = sub {
366
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: ");
370
371 delete $param->{use_ssh};
372 $param->{password} = $password;
373
374 my $local_cluster_lock = "/var/lock/pvecm.lock";
375 PVE::Tools::lock_file($local_cluster_lock, 10, \&PVE::Cluster::Setup::join, $param);
376
377 if (my $err = $@) {
378 if (ref($err) eq 'PVE::APIClient::Exception' && defined($err->{code}) && $err->{code} == 501) {
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;
383 }
384 return; # all OK, the API join endpoint successfully set us up
385 }
386
387 # allow fallback to old ssh only join if wished or needed
388
389 PVE::Cluster::Setup::setup_sshd_config();
390 PVE::Cluster::Setup::setup_rootsshconfig();
391 PVE::Cluster::Setup::setup_ssh_keys();
392
393 # make sure known_hosts is on local filesystem
394 PVE::Cluster::Setup::ssh_unmerge_known_hosts();
395
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");
399
400 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
401 'pvecm', 'addnode', $nodename, '--force', 1];
402
403 push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid};
404 push @$cmd, '--votes', $param->{votes} if defined($param->{votes});
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});
409
410 if (system (@$cmd) != 0) {
411 my $cmdtxt = join (' ', @$cmd);
412 die "unable to add node: command failed ($cmdtxt)\n";
413 }
414
415 my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
416 mkdir $tmpdir;
417
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];
422
423 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
424
425 my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf");
426 my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey");
427
428 PVE::Cluster::Setup::finish_join($host, $corosync_conf, $corosync_authkey);
429 };
430 my $err = $@;
431
432 rmtree $tmpdir;
433
434 die $err if $err;
435 };
436
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();
440
441 $rpcenv->fork_worker('clusterjoin', '', $authuser, $worker);
442
443 return undef;
444 }});
445
446 __PACKAGE__->register_method ({
447 name => 'status',
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' },
456
457 code => sub {
458 my ($param) = @_;
459
460 PVE::Corosync::check_conf_exists();
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 }
480
481 my $cmd = ['corosync-quorumtool', '-siH'];
482
483 exec (@$cmd);
484
485 exit (-1); # should not be reached
486 }});
487
488 __PACKAGE__->register_method ({
489 name => 'nodes',
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' },
498
499 code => sub {
500 my ($param) = @_;
501
502 PVE::Corosync::check_conf_exists();
503
504 my $cmd = ['corosync-quorumtool', '-l'];
505
506 exec (@$cmd);
507
508 exit (-1); # should not be reached
509 }});
510
511 __PACKAGE__->register_method ({
512 name => 'expected',
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' },
527
528 code => sub {
529 my ($param) = @_;
530
531 PVE::Corosync::check_conf_exists();
532
533 my $cmd = ['corosync-quorumtool', '-e', $param->{expected}];
534
535 exec (@$cmd);
536
537 exit (-1); # should not be reached
538
539 }});
540
541 __PACKAGE__->register_method ({
542 name => 'updatecerts',
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
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 {
569 PVE::Cluster::Setup::updatecerts_and_ssh($param->@{qw(force silent)});
570 });
571
572 return undef;
573 }});
574
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
614 my $network = $param->{migration_network};
615 if ($param->{get_migration_ip}) {
616 die "cannot use --run-command with --get_migration_ip\n"
617 if $param->{'run-command'};
618 if (my $ip = PVE::Cluster::get_local_migration_ip($network)) {
619 print "ip: '$ip'\n";
620 } else {
621 print "no ip\n";
622 }
623 # do not keep tunnel open when asked for migration ip
624 return undef;
625 }
626
627 if ($param->{'run-command'}) {
628 my $cmd = $param->{'extra-args'};
629 die "missing command\n"
630 if !$cmd || !scalar(@$cmd);
631
632 # Get an ip address to listen on, and find a free migration port
633 my ($ip, $family);
634 if (defined($network)) {
635 $ip = PVE::Cluster::get_local_migration_ip($network)
636 or die "failed to get migration IP address to listen on\n";
637 $family = PVE::Tools::get_host_address_family($ip);
638 } else {
639 my $nodename = PVE::INotify::nodename();
640 ($ip, $family) = PVE::Network::get_ip_from_hostname($nodename, 0);
641 }
642 my $port = PVE::Tools::next_migrate_port($family, $ip);
643
644 PVE::Tools::pipe_socket_to_command($cmd, $ip, $port);
645 return undef;
646 }
647
648 print "tunnel online\n";
649 *STDOUT->flush();
650
651 while (my $line = <STDIN>) {
652 chomp $line;
653 last if $line =~ m/^quit$/;
654 }
655
656 return undef;
657 }});
658
659
660 our $cmddef = {
661 keygen => [ __PACKAGE__, 'keygen', ['filename']],
662 create => [ 'PVE::API2::ClusterConfig', 'create', ['clustername']],
663 add => [ __PACKAGE__, 'add', ['hostname']],
664 addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']],
665 delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']],
666 status => [ __PACKAGE__, 'status' ],
667 nodes => [ __PACKAGE__, 'nodes' ],
668 expected => [ __PACKAGE__, 'expected', ['expected']],
669 updatecerts => [ __PACKAGE__, 'updatecerts', []],
670 mtunnel => [ __PACKAGE__, 'mtunnel', ['extra-args']],
671 qdevice => {
672 setup => [ __PACKAGE__, 'setup_qdevice', ['address']],
673 remove => [ __PACKAGE__, 'remove_qdevice', []],
674 }
675 };
676
677 1;