MigrateProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\ProxmoxVps\App\UI\Home\Providers;
  20. use MGProvision\Proxmox\v2\repository\NodeRepository;
  21. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\MigrateVmJob;
  22. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  24. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
  26. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  27. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  28. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  29. use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
  30. class MigrateProvider extends BaseDataProvider implements AdminArea
  31. {
  32. use ProductService;
  33. use ApiService;
  34. public function read()
  35. {
  36. $nodeRepository = new NodeRepository();
  37. $nodeRepository->setApi($this->api());
  38. $nodeRepository->findOnline(true);
  39. foreach ($nodeRepository->fetch() as $item)
  40. {
  41. if ($item->getNode() == $this->vm()->getNode())
  42. {
  43. continue;
  44. }
  45. $this->availableValues['target'][$item->getNode()] = $item->getNode();
  46. }
  47. if ($this->vm()->isRunning())
  48. {
  49. $this->data['online'] = "on";
  50. }
  51. }
  52. public function update()
  53. {
  54. $attributes = [
  55. "targetNode" => $this->formData['target'],
  56. "online" => $this->formData['online'] == "on" ? 1 : 0
  57. ];
  58. if (Job::where("job", MigrateVmJob::class. '@handle')->whereIn("status", ['waiting', "running", ""])
  59. ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
  60. ->where("rel_type", "hosting")->count())
  61. {
  62. return (new HtmlDataJsonResponse())
  63. ->setStatusError()
  64. ->setMessageAndTranslate("Task 'MigrateVm' already exist");
  65. }
  66. queue(MigrateVmJob::class, $attributes, null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  67. sl("lang")-> addReplacementConstant("vmid",$this->vm()->getVmid())-> addReplacementConstant("node", $this->formData['target']);
  68. return (new HtmlDataJsonResponse())
  69. ->setStatusSuccess()
  70. ->setMessageAndTranslate("Starting migration of VM :vmid: to :node:")
  71. ->addData('refreshState', 'serverinformationTable')
  72. ->setCallBackFunction('refreshTable');
  73. }
  74. }