Przeglądaj źródła

use nostart job

andre 4 lat temu
rodzic
commit
1fd06a8749

+ 3 - 0
app/Config/events.php

@@ -16,6 +16,9 @@ return [
     \ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmCreatedEvent::class     => [
         \ModulesGarden\ProxmoxAddon\App\Listeners\Cloud\VmCreatedListener::class
     ],
+    \ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmClonedEvent::class     => [
+        \ModulesGarden\ProxmoxAddon\App\Listeners\Cloud\VmClonedListener::class
+    ],
     \ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmReinstalledEvent::class => [
         \ModulesGarden\ProxmoxAddon\App\Listeners\Cloud\VmReinstalledListener::class
     ],

+ 192 - 0
app/Jobs/Cloud/CloneQemuJobNoStart.php

@@ -0,0 +1,192 @@
+<?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\VmClonedEvent;
+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 CloneQemuJobNoStart 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());
+            }
+
+            logModuleCall(
+                'proxmoxCloud',
+                __FUNCTION__,
+                $this->getModelData(),
+                'Debug',
+                $this->getVmModel()
+            );
+
+            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 VmClonedEvent($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));
+        }
+    }
+
+}

+ 30 - 0
app/Listeners/Cloud/VmClonedListener.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace ModulesGarden\ProxmoxAddon\App\Listeners\Cloud;
+
+use ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmCreatedEvent;
+
+class VmClonedListener extends VmListener
+{
+
+    public function handle(VmCreatedEvent $event)
+    {
+        $this->event    = $event;
+        $this->api();
+        $this->initVmAnVmModel();
+        //update user permission
+        $this->userUpdatePermission([$this->event->getVmModel()->vmid]);
+        //Update firewal Options
+        $this->firewallOptionService->update();
+        //IP Set
+        if ($this->configuration()->isIpsetIpFilter())
+        {
+            $this->ipSetFilterService->create();
+        }
+        //HA
+        if ($this->highAvailabilityClusterService->isConfigured() && !$this->highAvailabilityClusterService->exist())
+        {
+            $this->highAvailabilityClusterService->create();
+        }
+    }
+}