]> git.proxmox.com Git - pve-cluster.git/blame - data/PVE/CLI/pvecm.pm
factor out common parameter definitions
[pve-cluster.git] / data / PVE / CLI / pvecm.pm
CommitLineData
13d44dc5
DM
1package PVE::CLI::pvecm;
2
3use strict;
4use warnings;
95e7bcac 5
13d44dc5
DM
6use File::Path;
7use File::Basename;
294f76c4 8use PVE::Tools qw(run_command);
13d44dc5
DM
9use PVE::Cluster;
10use PVE::INotify;
10c6810e 11use PVE::JSONSchema qw(get_standard_option);
03b23bd0 12use PVE::RPCEnvironment;
13d44dc5 13use PVE::CLIHandler;
03b23bd0 14use PVE::PTY;
1d26c202 15use PVE::API2::ClusterConfig;
b6973a89 16use PVE::Corosync;
13d44dc5
DM
17
18use base qw(PVE::CLIHandler);
19
20$ENV{HOME} = '/root'; # for ssh-copy-id
21
22my $basedir = "/etc/pve";
23my $clusterconf = "$basedir/corosync.conf";
24my $libdir = "/var/lib/pve-cluster";
13d44dc5
DM
25my $authfile = "/etc/corosync/authkey";
26
9db3f0c0 27
03b23bd0
TL
28sub 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
13d44dc5 66__PACKAGE__->register_method ({
c53b111f 67 name => 'add',
13d44dc5
DM
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 },
10c6810e 78 nodeid => get_standard_option('corosync-nodeid'),
13d44dc5
DM
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 },
10c6810e
TL
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', {
03b23bd0
TL
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 },
13d44dc5
DM
100 },
101 },
102 returns => { type => 'null' },
c53b111f 103
13d44dc5
DM
104 code => sub {
105 my ($param) = @_;
106
107 my $nodename = PVE::INotify::nodename();
108
03b23bd0 109 my $host = $param->{hostname};
13d44dc5 110
68491de3 111 PVE::Cluster::assert_joinable($param->{ring0_addr}, $param->{ring1_addr}, $param->{force});
f566b424 112
03b23bd0
TL
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();
5a630d8f 137
13d44dc5
DM
138 # make sure known_hosts is on local filesystem
139 PVE::Cluster::ssh_unmerge_known_hosts();
140
de4b4155 141 my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
294f76c4 142 run_command($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
de4b4155 143 'errmsg' => "unable to copy ssh ID");
13d44dc5
DM
144
145 $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
146 'pvecm', 'addnode', $nodename, '--force', 1];
147
148 push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid};
13d44dc5 149 push @$cmd, '--votes', $param->{votes} if defined($param->{votes});
14d0000a 150 push @$cmd, '--ring0_addr', $param->{ring0_addr} if defined($param->{ring0_addr});
14d0000a
TL
151 push @$cmd, '--ring1_addr', $param->{ring1_addr} if defined($param->{ring1_addr});
152
13d44dc5
DM
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";
c53b111f 163 $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
13d44dc5
DM
164 "[$host]:$authfile $clusterconf", $tmpdir];
165
166 system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
167
e02bdaae
TL
168 my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf");
169 my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey");
13d44dc5 170
e02bdaae 171 PVE::Cluster::finish_join($host, $corosync_conf, $corosync_authkey);
13d44dc5
DM
172 };
173 my $err = $@;
174
175 rmtree $tmpdir;
176
177 die $err if $err;
178
179 return undef;
180 }});
181
182__PACKAGE__->register_method ({
c53b111f 183 name => 'status',
13d44dc5
DM
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' },
c53b111f 192
13d44dc5
DM
193 code => sub {
194 my ($param) = @_;
195
b6973a89 196 PVE::Corosync::check_conf_exists();
eb51b829 197
13d44dc5
DM
198 my $cmd = ['corosync-quorumtool', '-siH'];
199
200 exec (@$cmd);
201
202 exit (-1); # should not be reached
203 }});
204
205__PACKAGE__->register_method ({
c53b111f 206 name => 'nodes',
13d44dc5
DM
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' },
c53b111f 215
13d44dc5
DM
216 code => sub {
217 my ($param) = @_;
218
b6973a89 219 PVE::Corosync::check_conf_exists();
eb51b829 220
13d44dc5
DM
221 my $cmd = ['corosync-quorumtool', '-l'];
222
223 exec (@$cmd);
224
225 exit (-1); # should not be reached
226 }});
227
228__PACKAGE__->register_method ({
c53b111f 229 name => 'expected',
13d44dc5
DM
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' },
c53b111f 244
13d44dc5
DM
245 code => sub {
246 my ($param) = @_;
247
b6973a89 248 PVE::Corosync::check_conf_exists();
eb51b829 249
13d44dc5
DM
250 my $cmd = ['corosync-quorumtool', '-e', $param->{expected}];
251
252 exec (@$cmd);
253
254 exit (-1); # should not be reached
255
256 }});
257
13d44dc5 258__PACKAGE__->register_method ({
c53b111f 259 name => 'updatecerts',
13d44dc5
DM
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
3a966e22
TL
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,
f83d8153
TL
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 },
d56a1ff3
WB
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'),
f83d8153 333 },
3a966e22
TL
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
d56a1ff3 344 my $network = $param->{migration_network};
f83d8153 345 if ($param->{get_migration_ip}) {
d56a1ff3
WB
346 die "cannot use --run-command with --get_migration_ip\n"
347 if $param->{'run-command'};
43f4c5f6 348 if (my $ip = PVE::Cluster::get_local_migration_ip($network)) {
f83d8153
TL
349 print "ip: '$ip'\n";
350 } else {
43f4c5f6 351 print "no ip\n";
f83d8153
TL
352 }
353 # do not keep tunnel open when asked for migration ip
354 return undef;
355 }
356
d56a1ff3
WB
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";
b69a6e70 367 $family = PVE::Tools::get_host_address_family($ip);
d56a1ff3
WB
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
48178573 374 PVE::Tools::pipe_socket_to_command($cmd, $ip, $port);
d56a1ff3
WB
375 return undef;
376 }
377
3a966e22
TL
378 print "tunnel online\n";
379 *STDOUT->flush();
380
3bec79e5 381 while (my $line = <STDIN>) {
3a966e22
TL
382 chomp $line;
383 last if $line =~ m/^quit$/;
384 }
385
386 return undef;
387 }});
388
13d44dc5
DM
389
390our $cmddef = {
391 keygen => [ __PACKAGE__, 'keygen', ['filename']],
74e09a93 392 create => [ 'PVE::API2::ClusterConfig', 'create', ['clustername']],
13d44dc5 393 add => [ __PACKAGE__, 'add', ['hostname']],
1d26c202
TL
394 addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']],
395 delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']],
13d44dc5
DM
396 status => [ __PACKAGE__, 'status' ],
397 nodes => [ __PACKAGE__, 'nodes' ],
398 expected => [ __PACKAGE__, 'expected', ['expected']],
399 updatecerts => [ __PACKAGE__, 'updatecerts', []],
d56a1ff3 400 mtunnel => [ __PACKAGE__, 'mtunnel', ['extra-args']],
13d44dc5
DM
401};
402
4031;