DeleteVmJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\Reinstall;
  3. use ModulesGarden\ProxmoxAddon\App\Jobs\BaseJob;
  4. use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
  5. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  6. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ContainerService;
  7. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\HighAvailabilityClusterService;
  8. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  9. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  10. class DeleteVmJob extends BaseJob
  11. {
  12. use ProductService;
  13. /**
  14. * @var HighAvailabilityClusterService
  15. */
  16. protected $highAvailabilityClusterService;
  17. protected function initServices()
  18. {
  19. $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
  20. $this->containerService = new ContainerService();
  21. }
  22. public function handle($text = null)
  23. {
  24. $this->initParams();
  25. $this->initServices();
  26. $this->initVm();
  27. if ($this->isDone())
  28. {
  29. //Reset IP addresses usage
  30. if($this->configuration()->isQemu()){
  31. VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  32. ->ofVmId(sl('Vm')->getVmModel()->id)
  33. ->update(['net' => "" ]);
  34. VirtualInterface::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  35. ->ofVmId(sl('Vm')->getVmModel()->id)
  36. ->update(['net' => "" ]);
  37. }
  38. return true;
  39. }
  40. else
  41. {
  42. if ($this->isTaskRunning())
  43. {
  44. $this->sleep(20);
  45. return false;
  46. }
  47. }
  48. //Delete HA
  49. if ($this->highAvailabilityClusterService->exist())
  50. {
  51. $this->highAvailabilityClusterService->delete();
  52. }
  53. if (sl('Vm')->getVm()->isRunning())
  54. {
  55. sl('Vm')->getVm()->stop();
  56. sleep(15);
  57. }
  58. //vm protection
  59. if (sl('Vm')->getVm()->config()['protection'] == "1")
  60. {
  61. sl('Vm')->getVm()->protectionOff();
  62. sleep(1);
  63. }
  64. //vm container
  65. $this->saveVmContainer();
  66. //delete vm
  67. $taskId = sl('Vm')->getVm()->delete();
  68. //save task id
  69. $this->putModelDataAndSave(["taskId" => $taskId, "node" => sl('Vm')->getVm()->getNode()]);
  70. //task history
  71. $this->createTaskHistory($taskId, "Destroy");
  72. $this->sleep(10);
  73. return false;
  74. }
  75. private function saveVmContainer()
  76. {
  77. if ($this->getModelData()['container'])
  78. {
  79. return;
  80. }
  81. if ($this->configuration()->isLxc())
  82. {
  83. $container = sl('Vm')->getVm()->getReinstallConfig();
  84. $container['vmid'] = sl('Vm')->getVm()->getVmid();
  85. $container['ostemplate'] = $this->getModelData()['osTemplate'];
  86. $container['password'] = decrypt($this->getModelData()['password']);
  87. //SSH Public key.
  88. if ($this->configuration()->isSshKeyPairs())
  89. {
  90. $container['ssh-public-keys'] = $this->containerService->makeKeyPairs()->getPublic();
  91. }
  92. $this->putModelDataAndSave(['container' => $container]);
  93. }
  94. }
  95. }