RestoreVm.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\Reinstall;
  3. use MGProvision\Proxmox\v2\repository\FileRepository;
  4. use ModulesGarden\ProxmoxAddon\App\Jobs\BaseJob;
  5. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  6. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  7. class RestoreVm extends BaseJob
  8. {
  9. use ProductService;
  10. public function handle($text = null)
  11. {
  12. $this->initParams();
  13. $this->initServices();
  14. $this->initVm();
  15. if ($this->isTaskRunning())
  16. {
  17. $this->sleep(20);
  18. return false;
  19. }
  20. //get backup
  21. $storage = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : 'local';
  22. $fileRepository = new FileRepository();
  23. $fileRepository->findBackup(sl('Vm')->getVm())
  24. ->findByStorages([$storage]);
  25. $backup = $fileRepository->fetchLast();
  26. //done process is finished
  27. if ($this->isDone())
  28. {
  29. $backup->delete();
  30. return true;
  31. }
  32. //restore
  33. $taskId = sl('Vm')->getVm()->restore($backup->getVolid());
  34. //save task id
  35. $this->putModelDataAndSave(["taskId" => $taskId, "node" => sl('Vm')->getVm()->getNode()]);
  36. $this->sleep(20);
  37. return false;
  38. }
  39. }