]> git.proxmox.com Git - pve-cluster.git/blob - data/PVE/CLI/pvecm.pm
factor out common parameter definitions
[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
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";
25 my $authfile = "/etc/corosync/authkey";
26
27
28 sub setup_environment {
29 PVE::RPCEnvironment->setup_default_cli_env();
30 }
31
32 __PACKAGE__->register_method ({
33 name => 'keygen',
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' },
47
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);
56
57 die "key file '$filename' already exists\n" if -e $filename;
58
59 File::Path::make_path($dirname) if $dirname;
60
61 run_command(['corosync-keygen', '-l', '-k', $filename]);
62
63 return undef;
64 }});
65
66 __PACKAGE__->register_method ({
67 name => 'add',
68 path => 'add',
69 method => 'PUT',
70 description => "Adds the current node to an existing cluster.",
71 parameters => {
72 additionalProperties => 0,
73 properties => {
74 hostname => {
75 type => 'string',
76 description => "Hostname (or IP) of an existing cluster member."
77 },
78 nodeid => get_standard_option('corosync-nodeid'),
79 votes => {
80 type => 'integer',
81 description => "Number of votes for this node",
82 minimum => 0,
83 optional => 1,
84 },
85 force => {
86 type => 'boolean',
87 description => "Do not throw error if node already exists.",
88 optional => 1,
89 },
90 ring0_addr => get_standard_option('corosync-ring0-addr'),
91 ring1_addr => get_standard_option('corosync-ring1-addr'),
92 fingerprint => get_standard_option('fingerprint-sha256', {
93 optional => 1,
94 }),
95 'use_ssh' => {
96 type => 'boolean',
97 description => "Always use SSH to join, even if peer may do it over API.",
98 optional => 1,
99 },
100 },
101 },
102 returns => { type => 'null' },
103
104 code => sub {
105 my ($param) = @_;
106
107 my $nodename = PVE::INotify::nodename();
108
109 my $host = $param->{hostname};
110
111 PVE::Cluster::assert_joinable($param->{ring0_addr}, $param->{ring1_addr}, $param->{force});
112
113 if (!$param->{use_ssh}) {
114 print "Please enter superuser (root) password for '$host':\n";
115 my $password = PVE::PTY::read_password("Password for root\@$host: ");
116
117 delete $param->{use_ssh};
118 $param->{password} = $password;
119
120 eval { PVE::Cluster::join($param) };
121
122 if (my $err = $@) {
123 if (ref($err) eq 'PVE::APIClient::Exception' && $err->{code} == 501) {
124 $err = "Remote side is not able to use API for Cluster join!\n" .
125 "Pass the 'use_ssh' switch or update the remote side.\n";
126 }
127 die $err;
128 }
129 return; # all OK, the API join endpoint successfully set us up
130 }
131
132 # allow fallback to old ssh only join if wished or needed
133
134 PVE::Cluster::setup_sshd_config();
135 PVE::Cluster::setup_rootsshconfig();
136 PVE::Cluster::setup_ssh_keys();
137
138 # make sure known_hosts is on local filesystem
139 PVE::Cluster::ssh_unmerge_known_hosts();
140
141 my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
142 run_command($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
143 'errmsg' => "unable to copy ssh ID");
144
145 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
146 'pvecm', 'addnode', $nodename, '--force', 1];
147
148 push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid};
149 push @$cmd, '--votes', $param->{votes} if defined($param->{votes});
150 push @$cmd, '--ring0_addr', $param->{ring0_addr} if defined($param->{ring0_addr});
151 push @$cmd, '--ring1_addr', $param->{ring1_addr} if defined($param->{ring1_addr});
152
153 if (system (@$cmd) != 0) {
154 my $cmdtxt = join (' ', @$cmd);
155 die "unable to add node: command failed ($cmdtxt)\n";
156 }
157
158 my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
159 mkdir $tmpdir;
160
161 eval {
162 print "copy corosync auth key\n";
163 $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
164 "[$host]:$authfile $clusterconf", $tmpdir];
165
166 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
167
168 my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf");
169 my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey");
170
171 PVE::Cluster::finish_join($host, $corosync_conf, $corosync_authkey);
172 };
173 my $err = $@;
174
175 rmtree $tmpdir;
176
177 die $err if $err;
178
179 return undef;
180 }});
181
182 __PACKAGE__->register_method ({
183 name => 'status',
184 path => 'status',
185 method => 'GET',
186 description => "Displays the local view of the cluster status.",
187 parameters => {
188 additionalProperties => 0,
189 properties => {},
190 },
191 returns => { type => 'null' },
192
193 code => sub {
194 my ($param) = @_;
195
196 PVE::Corosync::check_conf_exists();
197
198 my $cmd = ['corosync-quorumtool', '-siH'];
199
200 exec (@$cmd);
201
202 exit (-1); # should not be reached
203 }});
204
205 __PACKAGE__->register_method ({
206 name => 'nodes',
207 path => 'nodes',
208 method => 'GET',
209 description => "Displays the local view of the cluster nodes.",
210 parameters => {
211 additionalProperties => 0,
212 properties => {},
213 },
214 returns => { type => 'null' },
215
216 code => sub {
217 my ($param) = @_;
218
219 PVE::Corosync::check_conf_exists();
220
221 my $cmd = ['corosync-quorumtool', '-l'];
222
223 exec (@$cmd);
224
225 exit (-1); # should not be reached
226 }});
227
228 __PACKAGE__->register_method ({
229 name => 'expected',
230 path => 'expected',
231 method => 'PUT',
232 description => "Tells corosync a new value of expected votes.",
233 parameters => {
234 additionalProperties => 0,
235 properties => {
236 expected => {
237 type => 'integer',
238 description => "Expected votes",
239 minimum => 1,
240 },
241 },
242 },
243 returns => { type => 'null' },
244
245 code => sub {
246 my ($param) = @_;
247
248 PVE::Corosync::check_conf_exists();
249
250 my $cmd = ['corosync-quorumtool', '-e', $param->{expected}];
251
252 exec (@$cmd);
253
254 exit (-1); # should not be reached
255
256 }});
257
258 __PACKAGE__->register_method ({
259 name => 'updatecerts',
260 path => 'updatecerts',
261 method => 'PUT',
262 description => "Update node certificates (and generate all needed files/directories).",
263 parameters => {
264 additionalProperties => 0,
265 properties => {
266 force => {
267 description => "Force generation of new SSL certifate.",
268 type => 'boolean',
269 optional => 1,
270 },
271 silent => {
272 description => "Ignore errors (i.e. when cluster has no quorum).",
273 type => 'boolean',
274 optional => 1,
275 },
276 },
277 },
278 returns => { type => 'null' },
279 code => sub {
280 my ($param) = @_;
281
282 PVE::Cluster::setup_rootsshconfig();
283
284 PVE::Cluster::gen_pve_vzdump_symlink();
285
286 if (!PVE::Cluster::check_cfs_quorum(1)) {
287 return undef if $param->{silent};
288 die "no quorum - unable to update files\n";
289 }
290
291 PVE::Cluster::setup_ssh_keys();
292
293 my $nodename = PVE::INotify::nodename();
294
295 my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
296
297 PVE::Cluster::gen_pve_node_files($nodename, $local_ip_address, $param->{force});
298 PVE::Cluster::ssh_merge_keys();
299 PVE::Cluster::ssh_merge_known_hosts($nodename, $local_ip_address);
300 PVE::Cluster::gen_pve_vzdump_files();
301
302 return undef;
303 }});
304
305 __PACKAGE__->register_method ({
306 name => 'mtunnel',
307 path => 'mtunnel',
308 method => 'POST',
309 description => "Used by VM/CT migration - do not use manually.",
310 parameters => {
311 additionalProperties => 0,
312 properties => {
313 get_migration_ip => {
314 type => 'boolean',
315 default => 0,
316 description => 'return the migration IP, if configured',
317 optional => 1,
318 },
319 migration_network => {
320 type => 'string',
321 format => 'CIDR',
322 description => 'the migration network used to detect the local migration IP',
323 optional => 1,
324 },
325 'run-command' => {
326 type => 'boolean',
327 description => 'Run a command with a tcp socket as standard input.'
328 .' The IP address and port are printed via this'
329 ." command's stdandard output first, each on a separate line.",
330 optional => 1,
331 },
332 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args'),
333 },
334 },
335 returns => { type => 'null'},
336 code => sub {
337 my ($param) = @_;
338
339 if (!PVE::Cluster::check_cfs_quorum(1)) {
340 print "no quorum\n";
341 return undef;
342 }
343
344 my $network = $param->{migration_network};
345 if ($param->{get_migration_ip}) {
346 die "cannot use --run-command with --get_migration_ip\n"
347 if $param->{'run-command'};
348 if (my $ip = PVE::Cluster::get_local_migration_ip($network)) {
349 print "ip: '$ip'\n";
350 } else {
351 print "no ip\n";
352 }
353 # do not keep tunnel open when asked for migration ip
354 return undef;
355 }
356
357 if ($param->{'run-command'}) {
358 my $cmd = $param->{'extra-args'};
359 die "missing command\n"
360 if !$cmd || !scalar(@$cmd);
361
362 # Get an ip address to listen on, and find a free migration port
363 my ($ip, $family);
364 if (defined($network)) {
365 $ip = PVE::Cluster::get_local_migration_ip($network)
366 or die "failed to get migration IP address to listen on\n";
367 $family = PVE::Tools::get_host_address_family($ip);
368 } else {
369 my $nodename = PVE::INotify::nodename();
370 ($ip, $family) = PVE::Network::get_ip_from_hostname($nodename, 0);
371 }
372 my $port = PVE::Tools::next_migrate_port($family, $ip);
373
374 PVE::Tools::pipe_socket_to_command($cmd, $ip, $port);
375 return undef;
376 }
377
378 print "tunnel online\n";
379 *STDOUT->flush();
380
381 while (my $line = <STDIN>) {
382 chomp $line;
383 last if $line =~ m/^quit$/;
384 }
385
386 return undef;
387 }});
388
389
390 our $cmddef = {
391 keygen => [ __PACKAGE__, 'keygen', ['filename']],
392 create => [ 'PVE::API2::ClusterConfig', 'create', ['clustername']],
393 add => [ __PACKAGE__, 'add', ['hostname']],
394 addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']],
395 delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']],
396 status => [ __PACKAGE__, 'status' ],
397 nodes => [ __PACKAGE__, 'nodes' ],
398 expected => [ __PACKAGE__, 'expected', ['expected']],
399 updatecerts => [ __PACKAGE__, 'updatecerts', []],
400 mtunnel => [ __PACKAGE__, 'mtunnel', ['extra-args']],
401 };
402
403 1;