From 7ca813e6fd56142da3e2fef72caa3a4c13faf5a8 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 10 Oct 2011 09:10:17 +0200 Subject: [PATCH] run openvz create_vm as background task --- PVE/API2/OpenVZ.pm | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/PVE/API2/OpenVZ.pm b/PVE/API2/OpenVZ.pm index a5164723..0d2b07a9 100644 --- a/PVE/API2/OpenVZ.pm +++ b/PVE/API2/OpenVZ.pm @@ -108,10 +108,16 @@ __PACKAGE__->register_method({ }, }), }, - returns => { type => 'null'}, + returns => { + type => 'string', + }, code => sub { my ($param) = @_; + my $rpcenv = PVE::RPCEnvironment::get(); + + my $user = $rpcenv->get_user(); + my $node = extract_param($param, 'node'); # fixme: fork worker? @@ -164,20 +170,22 @@ __PACKAGE__->register_method({ my $rawconf = PVE::OpenVZ::generate_raw_config($pve_base_ovz_config, $conf); - PVE::Tools::file_set_contents($basecfg_fn, $rawconf); + my $realcmd = sub { + PVE::Tools::file_set_contents($basecfg_fn, $rawconf); - my $cmd = ['vzctl', '--skiplock', 'create', $vmid, '--ostemplate', $tpath ]; + my $cmd = ['vzctl', '--skiplock', 'create', $vmid, '--ostemplate', $tpath ]; - PVE::Tools::run_command($cmd); + PVE::Tools::run_command($cmd); - # hack: vzctl '--userpasswd' starts the CT, but we want - # to avoid that for create - PVE::OpenVZ::set_rootpasswd($vmid, $password) if defined($password); + # hack: vzctl '--userpasswd' starts the CT, but we want + # to avoid that for create + PVE::OpenVZ::set_rootpasswd($vmid, $password) if defined($password); + }; + + return $rpcenv->fork_worker('vzcreate', $vmid, $user, $realcmd); }; - PVE::OpenVZ::lock_container($vmid, $code); - - return undef; + return PVE::OpenVZ::lock_container($vmid, $code); }}); __PACKAGE__->register_method({