MigrateVmJob.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. $migration = [
  35. "target" => $this->getModelData()['targetNode'],
  36. "online" => $this->vm()->isRunning() ? 1 : 0,
  37. ];
  38. if($this->configuration()->isLoadBalancerMigrationWithLocalDisks() && $this->configuration()->isQemu()){
  39. $migration['with-local-disks'] = 1;
  40. }
  41. $taskId = $this->vm()->migrate($migration);
  42. //save task id
  43. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $this->vm()->getNode(), "migrate" => true, "config" => ["delete" => $delete]]);
  44. $this->log->info(sprintf("VM %s - Migrate", $this->vm()->getVmid()));
  45. //sleep
  46. $this->sleep(20);
  47. return false;
  48. }
  49. else
  50. {
  51. if ($this->isDone())
  52. {
  53. $this->customFieldUpdate("node", $this->getModelData()['targetNode']);
  54. $this->vm()->setNode($this->getModelData()['targetNode']);
  55. $delete = (array)$this->getModelData()['config']['delete'];
  56. foreach ($delete as $k => $v)
  57. {
  58. if (preg_match('/cloudinit/', $v))
  59. {
  60. $ex = explode(":", $v);
  61. $storage = $ex[0];
  62. $this->vm()->updateConfig([$k => "{$storage}:cloudinit,format=raw"]);
  63. unset($delete[$k]);
  64. }
  65. }
  66. return true;
  67. }
  68. else
  69. {
  70. if ($this->isTaskRunning())
  71. {
  72. //sleep
  73. $this->sleep(20);
  74. return false;
  75. }
  76. }
  77. }
  78. return true;
  79. }
  80. }