Factor out the code which checks if the node can join another cluster. It will be used by the new API endpoint to join a cluster but stays also in the CLIHandler as we keep the old legacy SSH method for a bit.
This is not a completely 1:1 move, I changed: * &$error(...) to $error->(...) * removing a few empty lines, where code was so spread out that those lines resulted in the opposite of what they intended, i.e., less readability Signed-off-by: Thomas Lamprecht <[email protected]> --- data/PVE/CLI/pvecm.pm | 64 ++------------------------------------------------ data/PVE/Cluster.pm | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 62 deletions(-) diff --git a/data/PVE/CLI/pvecm.pm b/data/PVE/CLI/pvecm.pm index 1fbb58d..91bd469 100755 --- a/data/PVE/CLI/pvecm.pm +++ b/data/PVE/CLI/pvecm.pm @@ -300,67 +300,10 @@ __PACKAGE__->register_method ({ PVE::Cluster::setup_rootsshconfig(); PVE::Cluster::setup_ssh_keys(); + PVE::Cluster::assert_joinable($param->{ring0_addr}, $param->{ring1_addr}, $param->{force}); + my $host = $param->{hostname}; - my ($errors, $warnings) = ('', ''); - - my $error = sub { - my ($msg, $suppress) = @_; - - if ($suppress) { - $warnings .= "* $msg\n"; - } else { - $errors .= "* $msg\n"; - } - }; - - if (!$param->{force}) { - - if (-f $authfile) { - &$error("authentication key '$authfile' already exists", $param->{force}); - } - - if (-f $clusterconf) { - &$error("cluster config '$clusterconf' already exists", $param->{force}); - } - - my $vmlist = PVE::Cluster::get_vmlist(); - if ($vmlist && $vmlist->{ids} && scalar(keys %{$vmlist->{ids}})) { - &$error("this host already contains virtual guests", $param->{force}); - } - - if (system("corosync-quorumtool -l >/dev/null 2>&1") == 0) { - &$error("corosync is already running, is this node already in a cluster?!", $param->{force}); - } - } - - # check if corosync ring IPs are configured on the current nodes interfaces - my $check_ip = sub { - my $ip = shift; - if (defined($ip)) { - if (!PVE::JSONSchema::pve_verify_ip($ip, 1)) { - my $host = $ip; - eval { $ip = PVE::Network::get_ip_from_hostname($host); }; - if ($@) { - &$error("cannot use '$host': $@\n") ; - return; - } - } - - my $cidr = (Net::IP::ip_is_ipv6($ip)) ? "$ip/128" : "$ip/32"; - my $configured_ips = PVE::Network::get_local_ip_from_cidr($cidr); - - &$error("cannot use IP '$ip', it must be configured exactly once on local node!\n") - if (scalar(@$configured_ips) != 1); - } - }; - - &$check_ip($param->{ring0_addr}); - &$check_ip($param->{ring1_addr}); - - warn "warning, ignore the following errors:\n$warnings" if $warnings; - die "detected the following error(s):\n$errors" if $errors; - # make sure known_hosts is on local filesystem PVE::Cluster::ssh_unmerge_known_hosts(); @@ -372,11 +315,8 @@ __PACKAGE__->register_method ({ 'pvecm', 'addnode', $nodename, '--force', 1]; push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid}; - push @$cmd, '--votes', $param->{votes} if defined($param->{votes}); - push @$cmd, '--ring0_addr', $param->{ring0_addr} if defined($param->{ring0_addr}); - push @$cmd, '--ring1_addr', $param->{ring1_addr} if defined($param->{ring1_addr}); if (system (@$cmd) != 0) { diff --git a/data/PVE/Cluster.pm b/data/PVE/Cluster.pm index 5fc7d86..d6e9f37 100644 --- a/data/PVE/Cluster.pm +++ b/data/PVE/Cluster.pm @@ -37,6 +37,11 @@ my $basedir = "/etc/pve"; my $authdir = "$basedir/priv"; my $lockdir = "/etc/pve/priv/lock"; +# cfs and corosync files +my $localclusterdir = "/etc/corosync"; +my $authfile = "$localclusterdir/authkey"; +my $clusterconf = "$basedir/corosync.conf"; + my $authprivkeyfn = "$authdir/authkey.key"; my $authpubkeyfn = "$basedir/authkey.pub"; my $pveca_key_fn = "$authdir/pve-root-ca.key"; @@ -1683,4 +1688,64 @@ sub ssh_info_to_command { return $cmd; } +sub assert_joinable { + my ($ring0_addr, $ring1_addr, $force) = @_; + + my ($errors, $warnings) = ('', ''); + my $error = sub { + my ($msg, $suppress) = @_; + + if ($suppress) { + $warnings .= "* $msg\n"; + } else { + $errors .= "* $msg\n"; + } + }; + + if (!$force) { + + if (-f $authfile) { + $error->("authentication key '$authfile' already exists", $force); + } + + if (-f $clusterconf) { + $error->("cluster config '$clusterconf' already exists", $force); + } + + my $vmlist = PVE::Cluster::get_vmlist(); + if ($vmlist && $vmlist->{ids} && scalar(keys %{$vmlist->{ids}})) { + $error->("this host already contains virtual guests", $force); + } + + if (system("corosync-quorumtool -l >/dev/null 2>&1") == 0) { + $error->("corosync is already running, is this node already in a cluster?!", $force); + } + } + + # check if corosync ring IPs are configured on the current nodes interfaces + my $check_ip = sub { + my $ip = shift // return; + if (!PVE::JSONSchema::pve_verify_ip($ip, 1)) { + my $host = $ip; + eval { $ip = PVE::Network::get_ip_from_hostname($host); }; + if ($@) { + $error->("cannot use '$host': $@\n", 1) ; + return; + } + } + + my $cidr = (Net::IP::ip_is_ipv6($ip)) ? "$ip/128" : "$ip/32"; + my $configured_ips = PVE::Network::get_local_ip_from_cidr($cidr); + + $error->("cannot use IP '$ip', it must be configured exactly once on local node!\n") + if (scalar(@$configured_ips) != 1); + }; + + $check_ip->($ring0_addr); + $check_ip->($ring1_addr); + + warn "warning, ignore the following errors:\n$warnings" if $warnings; + die "detected the following error(s):\n$errors" if $errors; +} + 1; -- 2.14.2 _______________________________________________ pve-devel mailing list [email protected] https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
