RestoreArchiveJob.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Jobs\Vps;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use MGProvision\Proxmox\v2\Api;
  5. use MGProvision\Proxmox\v2\models\Kvm;
  6. use MGProvision\Proxmox\v2\models\Node;
  7. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\CustomField;
  8. use ModulesGarden\ProxmoxAddon\App\Events\Vps\VmCreatedEvent;
  9. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  10. use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
  11. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  12. use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;
  13. class RestoreArchiveJob extends CloneQemuJob
  14. {
  15. public function handle()
  16. {
  17. $this->initParams();
  18. $this->initServices();
  19. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  20. if ($this->isDone()) {
  21. $this->qemuUpdate();
  22. if ($this->agentUpdate()) {
  23. fire(new VmCreatedEvent($this->vm()));
  24. return true;
  25. }
  26. return false;
  27. }elseif ($this->isTaskRunning())
  28. {
  29. //sleep
  30. $this->sleep(5);
  31. return false;
  32. }
  33. try {
  34. Api::beginTransaction();
  35. DB::beginTransaction();
  36. if($this->isWhmcsConfigOption(ConfigurableOption::ARCHIVE) && $this->getWhmcsConfigOption(ConfigurableOption::ARCHIVE) !="0"){
  37. $archive = $this->getWhmcsConfigOption(ConfigurableOption::ARCHIVE);
  38. }else{
  39. $archive = $this->configuration()->archive;
  40. }
  41. list($backupNode, $volid) = explode(":", $archive, 2);
  42. //vmid
  43. $vmid = $this->nextVmid();
  44. $this->customFieldUpdate("vmid", $vmid);
  45. $vm = new Kvm($backupNode,$vmid);
  46. $vm->setApi($this->api());
  47. $container = [
  48. 'vmid' => $vmid,
  49. 'archive' => $volid
  50. ];
  51. $this->setVm($vm);
  52. $taskId = $vm->create($container);
  53. $this->customFieldUpdate("node", $backupNode);
  54. DB::commit();
  55. }catch (\Exception $ex)
  56. {
  57. echo $ex->getMessage();
  58. DB::rollBack();
  59. Api::commit();
  60. throw $ex;
  61. }
  62. //task history
  63. $this->createTaskHistory($taskId, "Create");
  64. //save task id
  65. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $backupNode]);
  66. //sleep
  67. $this->sleep();
  68. return false;
  69. }
  70. }