MigrateVmJob.php 3.6 KB

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