DeleteVmJob.php 2.6 KB

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