| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace ModulesGarden\ProxmoxAddon\App\Jobs\Cloud;
- use Illuminate\Database\Capsule\Manager as DB;
- use MGProvision\Proxmox\v2\Api;
- use MGProvision\Proxmox\v2\models\Kvm;
- use MGProvision\Proxmox\v2\models\Lxc;
- use MGProvision\Proxmox\v2\ProxmoxApiException;
- use ModulesGarden\ProxmoxAddon\App\Events\Cloud\QemuUpdateEvent;
- use ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmCreatedEvent;
- use ModulesGarden\ProxmoxAddon\App\Jobs\BaseJob;
- use ModulesGarden\ProxmoxAddon\App\Models\TaskHistory;
- use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AdditionalDiskService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AgentService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ContainerService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\UserService;
- use ModulesGarden\ProxmoxAddon\App\Services\EmailService;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
- class CloneQemuJob extends BaseJob
- {
- use ProductService;
- use UserService;
- /**
- * @var \ModulesGarden\ProxmoxAddon\App\Services\Cloud\ContainerService
- */
- protected $containerService;
- /**
- * @var Kvm|Lxc
- */
- protected $vm;
- /**
- * @var AdditionalDiskService
- */
- protected $additionalDiskService;
- protected function initServices()
- {
- $this->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));
- }
- }
- }
|