emailService = new EmailService(); $this->containerService = new ContainerService(); $this->agentService = new AgentService(); $this->additionalDiskService = new AdditionalDiskService(); } public function handle() { $this->initParams(); $this->initServices(); //create task validation if ($this->isDone()) { $this->initVm(); if(!sl('Vm')->getVm()->isRunning()){ $this->deleteNetwork(); } if($this->getModelData()['additionalDiskSize1'] && !$this->additionalDiskService->hasDisk()){ $this->additionalDiskService->create($this->getModelData()); } fire(new QemuUpdateEvent($this->getVmModel(), $this->getModelData())); try{ if($this->agentService->isEnabled()){ if(!sl('Vm')->getVm()->isRunning()){ $this->log->info(sprintf("VM %s - Start", sl('Vm')->getVm()->getVmid())); sl('Vm')->getVm()->start(); $this->sleep(5); return false; } sl('Vm')->getVm()->agent()->ping(); $this->agentService ->passwordUpdate(); $this->agentService ->configureNetwork(); } }catch (ProxmoxApiException $ex){ if(preg_match("/not running/", $ex->getMessage())){ $this->log->info($ex->getMessage()); }else{ $this->log->error($ex->getMessage()); } //sleep $this->sleep(5); return false; } fire(new VmCreatedEvent($this->getVmModel())); return true; } elseif ($this->isTaskRunning()) { //sleep $this->sleep(5); return false; } try { Api::beginTransaction(); DB::beginTransaction(); $osTemplate = $this->getModelData()['osTemplate']; //Support for configurable options i.e vmname|OS Name if (preg_match('/\//', $osTemplate)) { list($templateNode, $templateVmid) = explode("/", $osTemplate); } //vmid $vmid = $this->nextVmid(); $this->getVmModel(); $this->vmModel->vmid = $vmid; $this->vmModel->save(); //init container $container = [ "newid" => $vmid, "full" => $this->configuration()->getCloneMode(), "target" => $this->vmModel->node ]; //description $container['description'] = $this->getModelData()['description']; //hostname $container['name'] = $this->getModelData()['name']; //Storage if (!$this->configuration()->isCloneOnTheSameStorage() && $this->configuration()->getDiskStorage() && $this->configuration()->getCloneMode() == "1") { $container['storage'] = $this->configuration()->getDiskStorage(); } //pool if ($this->configuration()->getPool()) { $container['pool'] = $this->configuration()->getPool(); } //bwlimit if($this->configuration()->getBwLimit()){ $container['bwlimit'] = $this->configuration()->getBwLimit(); } //Create $template = new Kvm($templateNode, $templateVmid); $template->setApi($this->api()); $taskId = $template->cloneVm($container); DB::commit(); } catch (\Exception $ex) { DB::rollBack(); Api::commit(); $this->failed($ex->getMessage()); throw $ex; } //task history $task = new TaskHistory(); $task->fill([ 'hosting_id' => $this->getWhmcsParamByKey("serviceid"), 'upid' => $taskId, 'name' => sprintf("VM %s - %s", $vmid, "Clone"), 'vmid' => $template->getVmid(), 'node' => $template->getNode(), 'status' => 0 ])->save(); //save task id $this->putModelDataAndSave(["taskId" => $taskId, "node" => $template->getNode(), "templateNode" => $templateNode]); //sleep $this->sleep(); return false; } private function deleteNetwork(){ $deleteNetwork = []; foreach (sl('Vm')->getVm()->getNetworkDevices() as $networkDevice){ if(VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid')) ->ofNet($networkDevice->getId()) ->ofVmId(sl('Vm')->getVmModel()->id) ->count()){ continue; } $deleteNetwork[]=$networkDevice->getId(); } if(!empty($deleteNetwork)){ sl('Vm')->getVm()->deleteConfig(implode(",",$deleteNetwork)); } } }