MigrateVmJob.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $taskId = $this->vm()->migrate([
  36. "target" => $this->getModelData()['targetNode'],
  37. "online" => $this->getModelData()['online'],
  38. ]);
  39. //save task id
  40. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $this->vm()->getNode(), "migrate" => true, "config" => ["delete" => $delete]]);
  41. $this->log->info(sprintf("VM %s - Migrate", $this->vm()->getVmid()));
  42. //sleep
  43. $this->sleep(5);
  44. return false;
  45. } else if ($this->isDone()) {
  46. $this->customFieldUpdate("node", $this->getModelData()['targetNode']);
  47. $this->vm()->setNode($this->getModelData()['targetNode']);
  48. //restore config
  49. if($this->vm() instanceof Kvm && !$this->vm()->hasCloudInit()){
  50. $delete = (array)$this->getModelData()['config']['delete'];
  51. foreach ($delete as $k => $v) {
  52. if (preg_match('/cloudinit/', $v)) {
  53. $ex = explode(":", $v);
  54. $storage = $ex[0];
  55. $this->vm()->updateConfig([$k => "{$storage}:cloudinit,format=raw"]);
  56. unset($delete[$k]);
  57. }
  58. }
  59. }
  60. if ($this->highAvailabilityClusterService->isConfigured()) {
  61. $this->highAvailabilityClusterService->create();
  62. }
  63. return true;
  64. } else if ($this->isTaskRunning()) {
  65. //sleep
  66. $this->sleep(5);
  67. return false;
  68. }
  69. }
  70. }