MigrateVmJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Jobs\Vps;
  3. use MGProvision\Proxmox\v2\models\Kvm;
  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. if($this->isTask() && $this->isFailed()){
  24. $this->log->error(ucfirst($this->getTask()->getExitstatus()));
  25. }
  26. $this->highAvailabilityClusterService->delete();
  27. //delete config
  28. $delete = (array)$this->getModelData()['config']['delete'];
  29. foreach ($this->vm()->config() as $k => $v) {
  30. if (preg_match('/cloudinit/', $v)) {
  31. $this->vm()->deleteConfig($k);
  32. $delete[$k] = $v;
  33. }
  34. }
  35. $migration = [
  36. "target" => $this->getModelData()['targetNode'],
  37. ];
  38. if($this->configuration()->isLxc() && is_numeric($this->getModelData()['restart'])){
  39. $migration['restart'] = $this->getModelData()['restart'];
  40. }
  41. if($this->configuration()->isQemu()){
  42. $migration['online'] = $this->getModelData()['online'];
  43. $migration['with-local-disks'] = $this->getModelData()['with-local-disks'];
  44. }
  45. $taskId = $this->vm()->migrate($migration);
  46. //save task id
  47. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $this->vm()->getNode(), "migrate" => true, "config" => ["delete" => $delete]]);
  48. $this->log->info(sprintf("VM %s - Migrate", $this->vm()->getVmid()));
  49. //sleep
  50. $this->sleep(5);
  51. return false;
  52. } else if ($this->isDone()) {
  53. $this->customFieldUpdate("node", $this->getModelData()['targetNode']);
  54. $this->vm()->setNode($this->getModelData()['targetNode']);
  55. //restore config
  56. if($this->vm() instanceof Kvm && !$this->vm()->hasCloudInit()){
  57. $delete = (array)$this->getModelData()['config']['delete'];
  58. foreach ($delete as $k => $v) {
  59. if (preg_match('/cloudinit/', $v)) {
  60. $ex = explode(":", $v);
  61. $storage = $ex[0];
  62. $this->vm()->updateConfig([$k => "{$storage}:cloudinit,format=raw"]);
  63. unset($delete[$k]);
  64. }
  65. }
  66. }
  67. if ($this->highAvailabilityClusterService->isConfigured()) {
  68. $this->highAvailabilityClusterService->create();
  69. }
  70. return true;
  71. } else if ($this->isTaskRunning()) {
  72. //sleep
  73. $this->sleep(5);
  74. return false;
  75. }
  76. }
  77. }