CloudService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services;
  3. use MGProvision\Proxmox\v2\ProxmoxApiException;
  4. use MGProvision\Proxmox\v2\repository\BackupScheduleRepository;
  5. use MGProvision\Proxmox\v2\repository\FileRepository;
  6. use MGProvision\Proxmox\v2\repository\FirewallRulesRepository;
  7. use MGProvision\Proxmox\v2\VmFactory;
  8. use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
  9. use ModulesGarden\ProxmoxAddon\App\Models\KeyPair;
  10. use ModulesGarden\ProxmoxAddon\App\Models\TaskHistory;
  11. use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
  12. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  13. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  14. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\HighAvailabilityClusterService;
  15. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
  16. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  17. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
  18. use ModulesGarden\ProxmoxAddon\App\Traits\Cloud\SnippetTrait;
  19. use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
  20. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  21. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  22. class CloudService
  23. {
  24. use ProductService;
  25. use WhmcsParams;
  26. use ApiService;
  27. use SnippetTrait;
  28. protected $networkService;
  29. protected $highAvailabilityClusterService;
  30. /**
  31. * @var ResourceManager
  32. */
  33. protected $resourceManager;
  34. /**
  35. * CloudService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->networkService = new NetworkService();
  40. $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
  41. }
  42. /**
  43. * @return VmModel[]
  44. */
  45. public function getVmModels(){
  46. return VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))->get();
  47. }
  48. public function getVmModelWithVmid(){
  49. return VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))->notVmid(0)->get();
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getVmids(){
  55. return VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  56. ->notVmid('0')
  57. ->pluck('vmid')
  58. ->toArray();
  59. }
  60. public function destroy(){
  61. foreach ($this->getVmModels() as $vmModel){
  62. $this->delete($vmModel);
  63. }
  64. VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))->delete();
  65. IpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  66. ->update(["hosting_id" => 0]);
  67. }
  68. public function delete(VmModel $vmModel){
  69. //Cron delete active jobs
  70. $this->cancellJobs($vmModel->id);
  71. sl('Vm')->setVmModel($vmModel);
  72. //Unassing IP
  73. $this->networkService->deleteIpAddresses();
  74. if($vmModel->vmid == 0){
  75. $vmModel->delete();
  76. return;
  77. }
  78. //init api
  79. $this->api();
  80. //init vm
  81. sl('Vm')->setVm((new VmFactory())->fromVmModel($vmModel));
  82. //Delete HA
  83. if ($this->highAvailabilityClusterService->exist())
  84. {
  85. $this->highAvailabilityClusterService->delete();
  86. }
  87. try{
  88. //Stop VM
  89. if (sl('Vm')->getVm()->isRunning())
  90. {
  91. sl('Vm')->getVm()->stop();
  92. sleep(4);
  93. }
  94. //VM Protection LXC
  95. if (sl('Vm')->getVm() instanceof Lxc && sl('Vm')->getVm()->config()['protection'] == "1")
  96. {
  97. sl('Vm')->getVm()->protectionOff();
  98. }
  99. } catch (ProxmoxApiException $ex) {
  100. //Configuration file 'nodes/proxmox4/qemu-server/105.conf' does not exist
  101. if (!preg_match("/Configuration file/", $ex->getMessage()) && !preg_match("/does not exist/", $ex->getMessage())) {
  102. throw $ex;
  103. }
  104. }
  105. //virtual interfaces delete
  106. VirtualInterface::ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofVmId($vmModel->id)->delete();
  107. //key pair delete
  108. KeyPair::ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofVmId($vmModel->id)->delete();
  109. //Destroy firewall rules
  110. if (version_compare($this->api()->getVersion(), "3.2", '>'))
  111. {
  112. $rules = new FirewallRulesRepository();
  113. $rules->findByVm(sl('Vm')->getVm())
  114. ->delete();
  115. }
  116. //Destroy Backups
  117. $storage = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : 'local';
  118. $fileRepository = new FileRepository();
  119. $fileRepository->findBackup(sl('Vm')->getVm())
  120. ->findByStorages([$storage])
  121. ->delete();
  122. //Destroy Backup Jobs
  123. $schedule = new BackupScheduleRepository();
  124. $schedule->findByVm(sl('Vm')->getVm());
  125. $schedule->delete();
  126. //Destroy VM
  127. try {
  128. $upid = sl('Vm')->getVm()->delete();
  129. //Create Task History
  130. $type = str_replace(["qemu", "lxc"], ["VM", "CT"], sl('Vm')->getVm()->getVirtualization());
  131. $task = new TaskHistory();
  132. $task->fill([
  133. 'hosting_id' => $this->getWhmcsParamByKey("serviceid"),
  134. 'upid' => $upid,
  135. 'name' => sprintf("%s %s - %s", $type, sl('Vm')->getVm()->getVmid(), "Delete"),
  136. 'vmid' => sl('Vm')->getVm()->getVmid(),
  137. 'node' => sl('Vm')->getVm()->getNode(),
  138. 'status' => 0
  139. ])->save();
  140. } catch (ProxmoxApiException $ex) {
  141. //Configuration file 'nodes/proxmox4/qemu-server/105.conf' does not exist
  142. if (!preg_match("/Configuration file/", $ex->getMessage()) && !preg_match("/does not exist/", $ex->getMessage())) {
  143. throw $ex;
  144. }
  145. }
  146. //delete vmModel
  147. $vmModel->delete();
  148. if($this->hasSnippet() && $file = $this->getSnippetFile($vmModel)){
  149. $file->delete();
  150. }
  151. }
  152. public function cancellJobs($customId){
  153. Job::whereIn("status", ['waiting', "running", "", "error"])
  154. ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
  155. ->where("rel_type", "hosting")
  156. ->where("custom_id", $customId)
  157. ->update(["status" => 'cancelled']);
  158. }
  159. }