]>
git.proxmox.com Git - pve-cluster.git/blob - data/PVE/API2/ClusterConfig.pm
1 package PVE
::API2
::ClusterConfig
;
9 use PVE
::RPCEnvironment
;
10 use PVE
::JSONSchema
qw(get_standard_option);
12 use PVE
::APIClient
::LWP
;
15 use base
qw(PVE::RESTHandler);
17 my $clusterconf = "/etc/pve/corosync.conf";
18 my $authfile = "/etc/corosync/authkey";
19 my $local_cluster_lock = "/var/lock/pvecm.lock";
22 type
=> 'string', format
=> 'address',
23 description
=> "Hostname (or IP) of the corosync ring0 address of this node.",
24 default => "Hostname of the node",
27 PVE
::JSONSchema
::register_standard_option
("corosync-ring0-addr", $ring0_desc);
30 type
=> 'string', format
=> 'address',
31 description
=> "Hostname (or IP) of the corosync ring1 address of this node.".
32 " Requires a valid configured ring 1 (bindnet1_addr) in the cluster.",
35 PVE
::JSONSchema
::register_standard_option
("corosync-ring1-addr", $ring1_desc);
39 description
=> "Node id for this node.",
43 PVE
::JSONSchema
::register_standard_option
("corosync-nodeid", $nodeid_desc);
45 __PACKAGE__-
>register_method({
49 description
=> "Directory index.",
51 check
=> ['perm', '/', [ 'Sys.Audit' ]],
54 additionalProperties
=> 0,
63 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
77 __PACKAGE__-
>register_method ({
82 description
=> "Generate new cluster configuration.",
84 additionalProperties
=> 0,
87 description
=> "The name of the cluster.",
88 type
=> 'string', format
=> 'pve-node',
91 nodeid
=> get_standard_option
('corosync-nodeid'),
94 description
=> "Number of votes for this node.",
99 type
=> 'string', format
=> 'ip',
100 description
=> "This specifies the network address the corosync ring 0".
101 " executive should bind to and defaults to the local IP address of the node.",
104 ring0_addr
=> get_standard_option
('corosync-ring0-addr'),
106 type
=> 'string', format
=> 'ip',
107 description
=> "This specifies the network address the corosync ring 1".
108 " executive should bind to and is optional.",
111 ring1_addr
=> get_standard_option
('corosync-ring1-addr'),
114 returns
=> { type
=> 'string' },
118 -f
$clusterconf && die "cluster config '$clusterconf' already exists\n";
120 my $rpcenv = PVE
::RPCEnvironment
::get
();
121 my $authuser = $rpcenv->get_user();
125 PVE
::Cluster
::setup_sshd_config
(1);
126 PVE
::Cluster
::setup_rootsshconfig
();
127 PVE
::Cluster
::setup_ssh_keys
();
129 PVE
::Tools
::run_command
(['/usr/sbin/corosync-keygen', '-lk', $authfile])
131 die "no authentication key available\n" if -f
!$authfile;
133 my $nodename = PVE
::INotify
::nodename
();
135 # get the corosync basis config for the new cluster
136 my $config = PVE
::Corosync
::create_conf
($nodename, %$param);
138 print "Writing corosync config to /etc/pve/corosync.conf\n";
139 PVE
::Corosync
::atomic_write_conf
($config);
141 my $local_ip_address = PVE
::Cluster
::remote_node_ip
($nodename);
142 PVE
::Cluster
::ssh_merge_keys
();
143 PVE
::Cluster
::gen_pve_node_files
($nodename, $local_ip_address);
144 PVE
::Cluster
::ssh_merge_known_hosts
($nodename, $local_ip_address, 1);
146 print "Restart corosync and cluster filesystem\n";
147 PVE
::Tools
::run_command
('systemctl restart corosync pve-cluster');
151 PVE
::Tools
::lock_file
($local_cluster_lock, 10, $code);
155 return $rpcenv->fork_worker('clustercreate', $param->{clustername
}, $authuser, $worker);
158 __PACKAGE__-
>register_method({
162 description
=> "Corosync node list.",
164 check
=> ['perm', '/', [ 'Sys.Audit' ]],
167 additionalProperties
=> 0,
175 node
=> { type
=> 'string' },
178 links
=> [ { rel
=> 'child', href
=> "{node}" } ],
184 my $conf = PVE
::Cluster
::cfs_read_file
('corosync.conf');
185 my $nodelist = PVE
::Corosync
::nodelist
($conf);
187 return PVE
::RESTHandler
::hash_to_array
($nodelist, 'node');
190 # lock method to ensure local and cluster wide atomicity
191 # if we're a single node cluster just lock locally, we have no other cluster
192 # node which we could contend with, else also acquire a cluster wide lock
193 my $config_change_lock = sub {
196 PVE
::Tools
::lock_file
($local_cluster_lock, 10, sub {
197 PVE
::Cluster
::cfs_update
(1);
198 my $members = PVE
::Cluster
::get_members
();
199 if (scalar(keys %$members) > 1) {
200 return PVE
::Cluster
::cfs_lock_file
('corosync.conf', 10, $code);
207 __PACKAGE__-
>register_method ({
209 path
=> 'nodes/{node}',
212 description
=> "Adds a node to the cluster configuration. This call is for internal use.",
214 additionalProperties
=> 0,
216 node
=> get_standard_option
('pve-node'),
217 nodeid
=> get_standard_option
('corosync-nodeid'),
220 description
=> "Number of votes for this node",
226 description
=> "Do not throw error if node already exists.",
229 ring0_addr
=> get_standard_option
('corosync-ring0-addr'),
230 ring1_addr
=> get_standard_option
('corosync-ring1-addr'),
236 corosync_authkey
=> {
247 PVE
::Cluster
::check_cfs_quorum
();
250 my $conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
251 my $nodelist = PVE
::Corosync
::nodelist
($conf);
252 my $totem_cfg = PVE
::Corosync
::totem_config
($conf);
254 my $name = $param->{node
};
256 # ensure we do not reuse an address, that can crash the whole cluster!
257 my $check_duplicate_addr = sub {
259 return if !defined($addr);
261 while (my ($k, $v) = each %$nodelist) {
262 next if $k eq $name; # allows re-adding a node if force is set
263 if ($v->{ring0_addr
} eq $addr || ($v->{ring1_addr
} && $v->{ring1_addr
} eq $addr)) {
264 die "corosync: address '$addr' already defined by node '$k'\n";
269 &$check_duplicate_addr($param->{ring0_addr
});
270 &$check_duplicate_addr($param->{ring1_addr
});
272 $param->{ring0_addr
} = $name if !$param->{ring0_addr
};
274 die "corosync: using 'ring1_addr' parameter needs a configured ring 1 interface!\n"
275 if $param->{ring1_addr
} && !defined($totem_cfg->{interface
}->{1});
277 die "corosync: ring 1 interface configured but 'ring1_addr' parameter not defined!\n"
278 if defined($totem_cfg->{interface
}->{1}) && !defined($param->{ring1_addr
});
280 if (defined(my $res = $nodelist->{$name})) {
281 $param->{nodeid
} = $res->{nodeid
} if !$param->{nodeid
};
282 $param->{votes
} = $res->{quorum_votes
} if !defined($param->{votes
});
284 if ($res->{quorum_votes
} == $param->{votes
} &&
285 $res->{nodeid
} == $param->{nodeid
} && $param->{force
}) {
286 print "forcing overwrite of configured node '$name'\n";
288 die "can't add existing node '$name'\n";
290 } elsif (!$param->{nodeid
}) {
295 foreach my $v (values %$nodelist) {
296 if ($v->{nodeid
} eq $nodeid) {
305 $param->{nodeid
} = $nodeid;
308 $param->{votes
} = 1 if !defined($param->{votes
});
310 PVE
::Cluster
::gen_local_dirs
($name);
312 eval { PVE
::Cluster
::ssh_merge_keys
(); };
315 $nodelist->{$name} = {
316 ring0_addr
=> $param->{ring0_addr
},
317 nodeid
=> $param->{nodeid
},
320 $nodelist->{$name}->{ring1_addr
} = $param->{ring1_addr
} if $param->{ring1_addr
};
321 $nodelist->{$name}->{quorum_votes
} = $param->{votes
} if $param->{votes
};
323 PVE
::Cluster
::log_msg
('notice', 'root@pam', "adding node $name to cluster");
325 PVE
::Corosync
::update_nodelist
($conf, $nodelist);
328 $config_change_lock->($code);
332 corosync_authkey
=> PVE
::Tools
::file_get_contents
($authfile),
333 corosync_conf
=> PVE
::Tools
::file_get_contents
($clusterconf),
340 __PACKAGE__-
>register_method ({
342 path
=> 'nodes/{node}',
345 description
=> "Removes a node from the cluster configuration.",
347 additionalProperties
=> 0,
349 node
=> get_standard_option
('pve-node'),
352 returns
=> { type
=> 'null' },
356 my $local_node = PVE
::INotify
::nodename
();
357 die "Cannot delete myself from cluster!\n" if $param->{node
} eq $local_node;
359 PVE
::Cluster
::check_cfs_quorum
();
362 my $conf = PVE
::Cluster
::cfs_read_file
("corosync.conf");
363 my $nodelist = PVE
::Corosync
::nodelist
($conf);
368 foreach my $tmp_node (keys %$nodelist) {
369 my $d = $nodelist->{$tmp_node};
370 my $ring0_addr = $d->{ring0_addr
};
371 my $ring1_addr = $d->{ring1_addr
};
372 if (($tmp_node eq $param->{node
}) ||
373 (defined($ring0_addr) && ($ring0_addr eq $param->{node
})) ||
374 (defined($ring1_addr) && ($ring1_addr eq $param->{node
}))) {
376 $nodeid = $d->{nodeid
};
381 die "Node/IP: $param->{node} is not a known host of the cluster.\n"
384 PVE
::Cluster
::log_msg
('notice', 'root@pam', "deleting node $node from cluster");
386 delete $nodelist->{$node};
388 PVE
::Corosync
::update_nodelist
($conf, $nodelist);
390 PVE
::Tools
::run_command
(['corosync-cfgtool','-k', $nodeid]) if defined($nodeid);
393 $config_change_lock->($code);
399 __PACKAGE__-
>register_method ({
403 check
=> ['perm', '/', [ 'Sys.Audit' ]],
406 description
=> "Get information needed to join this cluster over the connected node.",
408 additionalProperties
=> 0,
410 node
=> get_standard_option
('pve-node', {
411 description
=> "The node for which the joinee gets the nodeinfo. ",
412 default => "current connected node",
419 additionalProperties
=> 0,
425 additionalProperties
=> 1,
427 name
=> get_standard_option
('pve-node'),
428 nodeid
=> get_standard_option
('corosync-nodeid'),
429 ring0_addr
=> get_standard_option
('corosync-ring0-addr'),
430 quorum_votes
=> { type
=> 'integer', minimum
=> 0 },
431 pve_addr
=> { type
=> 'string', format
=> 'ip' },
432 pve_fp
=> get_standard_option
('fingerprint-sha256'),
436 preferred_node
=> get_standard_option
('pve-node'),
437 totem
=> { type
=> 'object' },
438 config_digest
=> { type
=> 'string' },
444 my $nodename = $param->{node
} // PVE
::INotify
::nodename
();
446 PVE
::Cluster
::cfs_update
(1);
447 my $conf = PVE
::Cluster
::cfs_read_file
('corosync.conf');
449 die "node is not in a cluster, no join info available!\n"
450 if !($conf && $conf->{main
});
452 my $totem_cfg = $conf->{main
}->{totem
} // {};
453 my $nodelist = $conf->{main
}->{nodelist
}->{node
} // {};
454 my $corosync_config_digest = $conf->{digest
};
456 die "unknown node '$nodename'\n" if ! $nodelist->{$nodename};
458 foreach my $name (keys %$nodelist) {
459 my $node = $nodelist->{$name};
460 $node->{pve_fp
} = PVE
::Cluster
::get_node_fingerprint
($name);
461 $node->{pve_addr
} = scalar(PVE
::Cluster
::remote_node_ip
($name));
465 nodelist
=> [ values %$nodelist ],
466 preferred_node
=> $nodename,
468 config_digest
=> $corosync_config_digest,
474 __PACKAGE__-
>register_method ({
479 description
=> "Joins this node into an existing cluster.",
481 additionalProperties
=> 0,
485 description
=> "Hostname (or IP) of an existing cluster member."
487 nodeid
=> get_standard_option
('corosync-nodeid'),
490 description
=> "Number of votes for this node",
496 description
=> "Do not throw error if node already exists.",
499 ring0_addr
=> get_standard_option
('corosync-ring0-addr', {
500 default => "IP resolved by node's hostname",
502 ring1_addr
=> get_standard_option
('corosync-ring1-addr'),
503 fingerprint
=> get_standard_option
('fingerprint-sha256'),
505 description
=> "Superuser (root) password of peer node.",
511 returns
=> { type
=> 'string' },
515 my $rpcenv = PVE
::RPCEnvironment
::get
();
516 my $authuser = $rpcenv->get_user();
520 PVE
::Tools
::lock_file
($local_cluster_lock, 10, \
&PVE
::Cluster
::join, $param);
524 return $rpcenv->fork_worker('clusterjoin', undef, $authuser, $worker);
528 __PACKAGE__-
>register_method({
532 description
=> "Get corosync totem protocol settings.",
534 check
=> ['perm', '/', [ 'Sys.Audit' ]],
537 additionalProperties
=> 0,
548 my $conf = PVE
::Cluster
::cfs_read_file
('corosync.conf');
550 my $totem_cfg = $conf->{main
}->{totem
};