[pve-devel] [PATCH cluster v5 13/17] api/cluster: create cluster in forked worker

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Jan 26 14:25:23 CET 2018


Creating a cluster may need a bit longer, we need to gather random
data for the corosync authkey, restart services and such.
As we're now exposed in the API the 30 second response limit from
pveproxy is a big reason to do this. But we also get a nice task log
entry with this, which is nice.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 data/PVE/API2/ClusterConfig.pm | 44 +++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/data/PVE/API2/ClusterConfig.pm b/data/PVE/API2/ClusterConfig.pm
index 954c293..8c88a17 100644
--- a/data/PVE/API2/ClusterConfig.pm
+++ b/data/PVE/API2/ClusterConfig.pm
@@ -100,38 +100,42 @@ __PACKAGE__->register_method ({
 	    },
 	},
     },
-    returns => { type => 'null' },
-
+    returns => { type => 'string' },
     code => sub {
 	my ($param) = @_;
 
 	-f $clusterconf && die "cluster config '$clusterconf' already exists\n";
 
-	PVE::Cluster::setup_sshd_config(1);
-	PVE::Cluster::setup_rootsshconfig();
-	PVE::Cluster::setup_ssh_keys();
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $authuser = $rpcenv->get_user();
 
-	PVE::Tools::run_command(['/usr/sbin/corosync-keygen', '-lk', $authfile])
-	    if !-f $authfile;
-	die "no authentication key available\n" if -f !$authfile;
+	my $worker = sub {
+	    PVE::Cluster::setup_sshd_config(1);
+	    PVE::Cluster::setup_rootsshconfig();
+	    PVE::Cluster::setup_ssh_keys();
 
-	my $nodename = PVE::INotify::nodename();
+	    PVE::Tools::run_command(['/usr/sbin/corosync-keygen', '-lk', $authfile])
+		if !-f $authfile;
+	    die "no authentication key available\n" if -f !$authfile;
 
-	# get the corosync basis config for the new cluster
-	my $config = PVE::Corosync::create_conf($nodename, %$param);
+	    my $nodename = PVE::INotify::nodename();
 
-	print "Writing corosync config to /etc/pve/corosync.conf\n";
-	PVE::Corosync::atomic_write_conf($config);
+	    # get the corosync basis config for the new cluster
+	    my $config = PVE::Corosync::create_conf($nodename, %$param);
 
-	my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
-	PVE::Cluster::ssh_merge_keys();
-	PVE::Cluster::gen_pve_node_files($nodename, $local_ip_address);
-	PVE::Cluster::ssh_merge_known_hosts($nodename, $local_ip_address, 1);
+	    print "Writing corosync config to /etc/pve/corosync.conf\n";
+	    PVE::Corosync::atomic_write_conf($config);
 
-	print "Restart corosync and cluster filesystem\n";
-	PVE::Tools::run_command('systemctl restart corosync pve-cluster');
+	    my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
+	    PVE::Cluster::ssh_merge_keys();
+	    PVE::Cluster::gen_pve_node_files($nodename, $local_ip_address);
+	    PVE::Cluster::ssh_merge_known_hosts($nodename, $local_ip_address, 1);
 
-	return undef;
+	    print "Restart corosync and cluster filesystem\n";
+	    PVE::Tools::run_command('systemctl restart corosync pve-cluster');
+	};
+
+	return $rpcenv->fork_worker('clustercreate', '',  $authuser, $worker);
 }});
 
 __PACKAGE__->register_method({
-- 
2.14.2





More information about the pve-devel mailing list