]>
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 | ||
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}; |
8db9997d | 110 | my $local_ip_address = PVE::Cluster::remote_node_ip($nodename); |
13d44dc5 | 111 | |
68491de3 | 112 | PVE::Cluster::assert_joinable($param->{ring0_addr}, $param->{ring1_addr}, $param->{force}); |
f566b424 | 113 | |
83e5e7b7 | 114 | my $worker = sub { |
03b23bd0 | 115 | |
83e5e7b7 TL |
116 | if (!$param->{use_ssh}) { |
117 | print "Please enter superuser (root) password for '$host':\n"; | |
118 | my $password = PVE::PTY::read_password("Password for root\@$host: "); | |
03b23bd0 | 119 | |
83e5e7b7 TL |
120 | delete $param->{use_ssh}; |
121 | $param->{password} = $password; | |
03b23bd0 | 122 | |
83e5e7b7 TL |
123 | my $local_cluster_lock = "/var/lock/pvecm.lock"; |
124 | PVE::Tools::lock_file($local_cluster_lock, 10, \&PVE::Cluster::join, $param); | |
125 | ||
126 | if (my $err = $@) { | |
efe59a84 | 127 | if (ref($err) eq 'PVE::APIClient::Exception' && defined($err->{code}) && $err->{code} == 501) { |
83e5e7b7 TL |
128 | $err = "Remote side is not able to use API for Cluster join!\n" . |
129 | "Pass the 'use_ssh' switch or update the remote side.\n"; | |
130 | } | |
131 | die $err; | |
03b23bd0 | 132 | } |
83e5e7b7 | 133 | return; # all OK, the API join endpoint successfully set us up |
03b23bd0 | 134 | } |
03b23bd0 | 135 | |
83e5e7b7 TL |
136 | # allow fallback to old ssh only join if wished or needed |
137 | ||
138 | PVE::Cluster::setup_sshd_config(); | |
139 | PVE::Cluster::setup_rootsshconfig(); | |
140 | PVE::Cluster::setup_ssh_keys(); | |
03b23bd0 | 141 | |
83e5e7b7 TL |
142 | # make sure known_hosts is on local filesystem |
143 | PVE::Cluster::ssh_unmerge_known_hosts(); | |
5a630d8f | 144 | |
83e5e7b7 TL |
145 | my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"]; |
146 | run_command($cmd, 'outfunc' => sub {}, 'errfunc' => sub {}, | |
147 | 'errmsg' => "unable to copy ssh ID"); | |
13d44dc5 | 148 | |
83e5e7b7 TL |
149 | $cmd = ['ssh', $host, '-o', 'BatchMode=yes', |
150 | 'pvecm', 'addnode', $nodename, '--force', 1]; | |
13d44dc5 | 151 | |
83e5e7b7 TL |
152 | push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid}; |
153 | push @$cmd, '--votes', $param->{votes} if defined($param->{votes}); | |
e423668d | 154 | push @$cmd, '--ring0_addr', $param->{ring0_addr} // $local_ip_address; |
83e5e7b7 TL |
155 | push @$cmd, '--ring1_addr', $param->{ring1_addr} if defined($param->{ring1_addr}); |
156 | ||
157 | if (system (@$cmd) != 0) { | |
158 | my $cmdtxt = join (' ', @$cmd); | |
159 | die "unable to add node: command failed ($cmdtxt)\n"; | |
160 | } | |
13d44dc5 | 161 | |
83e5e7b7 TL |
162 | my $tmpdir = "$libdir/.pvecm_add.tmp.$$"; |
163 | mkdir $tmpdir; | |
14d0000a | 164 | |
83e5e7b7 TL |
165 | eval { |
166 | print "copy corosync auth key\n"; | |
167 | $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq', | |
168 | "[$host]:$authfile $clusterconf", $tmpdir]; | |
13d44dc5 | 169 | |
83e5e7b7 | 170 | system(@$cmd) == 0 || die "can't rsync data from host '$host'\n"; |
13d44dc5 | 171 | |
83e5e7b7 TL |
172 | my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf"); |
173 | my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey"); | |
13d44dc5 | 174 | |
83e5e7b7 TL |
175 | PVE::Cluster::finish_join($host, $corosync_conf, $corosync_authkey); |
176 | }; | |
177 | my $err = $@; | |
13d44dc5 | 178 | |
83e5e7b7 | 179 | rmtree $tmpdir; |
13d44dc5 | 180 | |
83e5e7b7 | 181 | die $err if $err; |
13d44dc5 | 182 | }; |
13d44dc5 | 183 | |
83e5e7b7 TL |
184 | # use a synced worker so we get a nice task log when joining through CLI |
185 | my $rpcenv = PVE::RPCEnvironment::get(); | |
186 | my $authuser = $rpcenv->get_user(); | |
13d44dc5 | 187 | |
83e5e7b7 | 188 | $rpcenv->fork_worker('clusterjoin', '', $authuser, $worker); |
13d44dc5 DM |
189 | |
190 | return undef; | |
191 | }}); | |
192 | ||
193 | __PACKAGE__->register_method ({ | |
c53b111f | 194 | name => 'status', |
13d44dc5 DM |
195 | path => 'status', |
196 | method => 'GET', | |
197 | description => "Displays the local view of the cluster status.", | |
198 | parameters => { | |
199 | additionalProperties => 0, | |
200 | properties => {}, | |
201 | }, | |
202 | returns => { type => 'null' }, | |
c53b111f | 203 | |
13d44dc5 DM |
204 | code => sub { |
205 | my ($param) = @_; | |
206 | ||
b6973a89 | 207 | PVE::Corosync::check_conf_exists(); |
eb51b829 | 208 | |
13d44dc5 DM |
209 | my $cmd = ['corosync-quorumtool', '-siH']; |
210 | ||
211 | exec (@$cmd); | |
212 | ||
213 | exit (-1); # should not be reached | |
214 | }}); | |
215 | ||
216 | __PACKAGE__->register_method ({ | |
c53b111f | 217 | name => 'nodes', |
13d44dc5 DM |
218 | path => 'nodes', |
219 | method => 'GET', | |
220 | description => "Displays the local view of the cluster nodes.", | |
221 | parameters => { | |
222 | additionalProperties => 0, | |
223 | properties => {}, | |
224 | }, | |
225 | returns => { type => 'null' }, | |
c53b111f | 226 | |
13d44dc5 DM |
227 | code => sub { |
228 | my ($param) = @_; | |
229 | ||
b6973a89 | 230 | PVE::Corosync::check_conf_exists(); |
eb51b829 | 231 | |
13d44dc5 DM |
232 | my $cmd = ['corosync-quorumtool', '-l']; |
233 | ||
234 | exec (@$cmd); | |
235 | ||
236 | exit (-1); # should not be reached | |
237 | }}); | |
238 | ||
239 | __PACKAGE__->register_method ({ | |
c53b111f | 240 | name => 'expected', |
13d44dc5 DM |
241 | path => 'expected', |
242 | method => 'PUT', | |
243 | description => "Tells corosync a new value of expected votes.", | |
244 | parameters => { | |
245 | additionalProperties => 0, | |
246 | properties => { | |
247 | expected => { | |
248 | type => 'integer', | |
249 | description => "Expected votes", | |
250 | minimum => 1, | |
251 | }, | |
252 | }, | |
253 | }, | |
254 | returns => { type => 'null' }, | |
c53b111f | 255 | |
13d44dc5 DM |
256 | code => sub { |
257 | my ($param) = @_; | |
258 | ||
b6973a89 | 259 | PVE::Corosync::check_conf_exists(); |
eb51b829 | 260 | |
13d44dc5 DM |
261 | my $cmd = ['corosync-quorumtool', '-e', $param->{expected}]; |
262 | ||
263 | exec (@$cmd); | |
264 | ||
265 | exit (-1); # should not be reached | |
266 | ||
267 | }}); | |
268 | ||
13d44dc5 | 269 | __PACKAGE__->register_method ({ |
c53b111f | 270 | name => 'updatecerts', |
13d44dc5 DM |
271 | path => 'updatecerts', |
272 | method => 'PUT', | |
273 | description => "Update node certificates (and generate all needed files/directories).", | |
274 | parameters => { | |
275 | additionalProperties => 0, | |
276 | properties => { | |
277 | force => { | |
278 | description => "Force generation of new SSL certifate.", | |
279 | type => 'boolean', | |
280 | optional => 1, | |
281 | }, | |
282 | silent => { | |
283 | description => "Ignore errors (i.e. when cluster has no quorum).", | |
284 | type => 'boolean', | |
285 | optional => 1, | |
286 | }, | |
287 | }, | |
288 | }, | |
289 | returns => { type => 'null' }, | |
290 | code => sub { | |
291 | my ($param) = @_; | |
292 | ||
50f74e31 TL |
293 | PVE::Tools::run_with_timeout(30, sub { |
294 | PVE::Cluster::updatecerts_and_ssh($param->@{qw(force silent)}); | |
295 | }); | |
13d44dc5 DM |
296 | |
297 | return undef; | |
298 | }}); | |
299 | ||
3a966e22 TL |
300 | __PACKAGE__->register_method ({ |
301 | name => 'mtunnel', | |
302 | path => 'mtunnel', | |
303 | method => 'POST', | |
304 | description => "Used by VM/CT migration - do not use manually.", | |
305 | parameters => { | |
306 | additionalProperties => 0, | |
f83d8153 TL |
307 | properties => { |
308 | get_migration_ip => { | |
309 | type => 'boolean', | |
310 | default => 0, | |
311 | description => 'return the migration IP, if configured', | |
312 | optional => 1, | |
313 | }, | |
314 | migration_network => { | |
315 | type => 'string', | |
316 | format => 'CIDR', | |
317 | description => 'the migration network used to detect the local migration IP', | |
318 | optional => 1, | |
319 | }, | |
d56a1ff3 WB |
320 | 'run-command' => { |
321 | type => 'boolean', | |
322 | description => 'Run a command with a tcp socket as standard input.' | |
323 | .' The IP address and port are printed via this' | |
324 | ." command's stdandard output first, each on a separate line.", | |
325 | optional => 1, | |
326 | }, | |
327 | 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args'), | |
f83d8153 | 328 | }, |
3a966e22 TL |
329 | }, |
330 | returns => { type => 'null'}, | |
331 | code => sub { | |
332 | my ($param) = @_; | |
333 | ||
334 | if (!PVE::Cluster::check_cfs_quorum(1)) { | |
335 | print "no quorum\n"; | |
336 | return undef; | |
337 | } | |
338 | ||
d56a1ff3 | 339 | my $network = $param->{migration_network}; |
f83d8153 | 340 | if ($param->{get_migration_ip}) { |
d56a1ff3 WB |
341 | die "cannot use --run-command with --get_migration_ip\n" |
342 | if $param->{'run-command'}; | |
43f4c5f6 | 343 | if (my $ip = PVE::Cluster::get_local_migration_ip($network)) { |
f83d8153 TL |
344 | print "ip: '$ip'\n"; |
345 | } else { | |
43f4c5f6 | 346 | print "no ip\n"; |
f83d8153 TL |
347 | } |
348 | # do not keep tunnel open when asked for migration ip | |
349 | return undef; | |
350 | } | |
351 | ||
d56a1ff3 WB |
352 | if ($param->{'run-command'}) { |
353 | my $cmd = $param->{'extra-args'}; | |
354 | die "missing command\n" | |
355 | if !$cmd || !scalar(@$cmd); | |
356 | ||
357 | # Get an ip address to listen on, and find a free migration port | |
358 | my ($ip, $family); | |
359 | if (defined($network)) { | |
360 | $ip = PVE::Cluster::get_local_migration_ip($network) | |
361 | or die "failed to get migration IP address to listen on\n"; | |
b69a6e70 | 362 | $family = PVE::Tools::get_host_address_family($ip); |
d56a1ff3 WB |
363 | } else { |
364 | my $nodename = PVE::INotify::nodename(); | |
365 | ($ip, $family) = PVE::Network::get_ip_from_hostname($nodename, 0); | |
366 | } | |
367 | my $port = PVE::Tools::next_migrate_port($family, $ip); | |
368 | ||
48178573 | 369 | PVE::Tools::pipe_socket_to_command($cmd, $ip, $port); |
d56a1ff3 WB |
370 | return undef; |
371 | } | |
372 | ||
3a966e22 TL |
373 | print "tunnel online\n"; |
374 | *STDOUT->flush(); | |
375 | ||
3bec79e5 | 376 | while (my $line = <STDIN>) { |
3a966e22 TL |
377 | chomp $line; |
378 | last if $line =~ m/^quit$/; | |
379 | } | |
380 | ||
381 | return undef; | |
382 | }}); | |
383 | ||
13d44dc5 DM |
384 | |
385 | our $cmddef = { | |
386 | keygen => [ __PACKAGE__, 'keygen', ['filename']], | |
74e09a93 | 387 | create => [ 'PVE::API2::ClusterConfig', 'create', ['clustername']], |
13d44dc5 | 388 | add => [ __PACKAGE__, 'add', ['hostname']], |
1d26c202 TL |
389 | addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']], |
390 | delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']], | |
13d44dc5 DM |
391 | status => [ __PACKAGE__, 'status' ], |
392 | nodes => [ __PACKAGE__, 'nodes' ], | |
393 | expected => [ __PACKAGE__, 'expected', ['expected']], | |
394 | updatecerts => [ __PACKAGE__, 'updatecerts', []], | |
d56a1ff3 | 395 | mtunnel => [ __PACKAGE__, 'mtunnel', ['extra-args']], |
13d44dc5 DM |
396 | }; |
397 | ||
398 | 1; |