MigrateProvider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Providers;
  20. use MGProvision\Proxmox\v2\repository\NodeRepository;
  21. use MGProvision\Proxmox\v2\VmFactory;
  22. use ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\MigrateVmJob;
  23. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  24. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  25. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  26. use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
  27. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AdminArea;
  28. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  29. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  30. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  31. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  32. class MigrateProvider extends BaseDataProvider implements AdminArea
  33. {
  34. use ProductService;
  35. use ApiService;
  36. public function read()
  37. {
  38. if (!$this->actionElementId )
  39. {
  40. return;
  41. }
  42. $this->api();
  43. $vmModel = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  44. ->ofId($this->actionElementId)
  45. ->firstOrFail();
  46. $this->data['vmId'] = $vmModel->id;
  47. //VM
  48. $vm = (new VmFactory())->fromVmModel($vmModel);
  49. if ($vm->isRunning())
  50. {
  51. $this->data['online'] = "on";
  52. }
  53. $nodeRepository = new NodeRepository();
  54. $nodeRepository->setApi($this->api());
  55. $nodeRepository->findOnline(true);
  56. foreach ($nodeRepository->fetch() as $item)
  57. {
  58. if ($item->getNode() == $vm->getNode())
  59. {
  60. continue;
  61. }
  62. $this->availableValues['target'][$item->getNode()] = $item->getNode();
  63. }
  64. }
  65. public function update()
  66. {
  67. $vmModel = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  68. ->ofId($this->getFormDataValues()['vmId'])
  69. ->firstOrFail();
  70. //VM
  71. $vm = (new VmFactory())->fromVmModel($vmModel);
  72. $attributes = [
  73. "targetNode" => $this->formData['target'],
  74. "online" => $this->formData['online'] == "on" ? 1 : 0
  75. ];
  76. if (Job::where("job", MigrateVmJob::class. '@handle')->whereIn("status", ['waiting', "running", ""])
  77. ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
  78. ->where("rel_type", "hosting")
  79. ->where("custom_id", $vmModel->id)
  80. ->count())
  81. {
  82. return (new HtmlDataJsonResponse())
  83. ->setStatusError()
  84. ->setMessageAndTranslate("Task 'MigrateVm' already exist");
  85. }
  86. queue(MigrateVmJob::class, $attributes, null, "hosting", $this->getWhmcsParamByKey("serviceid"),$vmModel->id);
  87. sl("lang")-> addReplacementConstant("vmid",$vm->getVmid())-> addReplacementConstant("node", $this->formData['target']);
  88. return (new HtmlDataJsonResponse())
  89. ->setStatusSuccess()
  90. ->setMessageAndTranslate("Starting migration of VM :vmid: to :node:");
  91. }
  92. }