MigrateVmJob.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Jobs\Vps\LoadBalancer;
  3. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\BaseJob;
  4. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HighAvailabilityClusterService;
  5. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
  6. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  7. class MigrateVmJob extends BaseJob
  8. {
  9. use ProductService;
  10. use HostingService;
  11. /**
  12. * @var HighAvailabilityClusterService
  13. */
  14. protected $highAvailabilityClusterService;
  15. public function handle($text = null)
  16. {
  17. $this->initParams();
  18. $this->initServices();
  19. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  20. $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
  21. //Migrate
  22. if (!$this->getModelData()['taskId'] || ($this->isTask() && !$this->isTaskRunning() && $this->isFailed()))
  23. {
  24. $this->highAvailabilityClusterService->delete();
  25. $delete = (array)$this->getModelData()['config']['delete'];
  26. foreach ($this->vm()->config() as $k => $v)
  27. {
  28. if (preg_match('/cloudinit/', $v))
  29. {
  30. $this->vm()->deleteConfig($k);
  31. $delete[$k] = $v;
  32. }
  33. }
  34. $taskId = $this->vm()->migrate([
  35. "target" => $this->getModelData()['targetNode'],
  36. "online" => $this->vm()->isRunning() ? 1 : 0,
  37. ]);
  38. //save task id
  39. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $this->vm()->getNode(), "migrate" => true, "config" => ["delete" => $delete]]);
  40. $this->log->info(sprintf("VM %s - Migrate", $this->vm()->getVmid()));
  41. //sleep
  42. $this->sleep(20);
  43. return false;
  44. }
  45. else
  46. {
  47. if ($this->isDone())
  48. {
  49. $this->customFieldUpdate("node", $this->getModelData()['targetNode']);
  50. $this->vm()->setNode($this->getModelData()['targetNode']);
  51. $delete = (array)$this->getModelData()['config']['delete'];
  52. foreach ($delete as $k => $v)
  53. {
  54. if (preg_match('/cloudinit/', $v))
  55. {
  56. $ex = explode(":", $v);
  57. $storage = $ex[0];
  58. $this->vm()->updateConfig([$k => "{$storage}:cloudinit,format=raw"]);
  59. unset($delete[$k]);
  60. }
  61. }
  62. return true;
  63. }
  64. else
  65. {
  66. if ($this->isTaskRunning())
  67. {
  68. //sleep
  69. $this->sleep(20);
  70. return false;
  71. }
  72. }
  73. }
  74. return true;
  75. }
  76. }