CloudService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. $ipunassigned = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))->pluck('ip')->toArray();
  65. if(!empty( $ipunassigned)){
  66. (new IpLogService())->unassigned($ipunassigned);
  67. }
  68. VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))->delete();
  69. IpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  70. ->update(["hosting_id" => 0]);
  71. }
  72. public function delete(VmModel $vmModel){
  73. //Cron delete active jobs
  74. $this->cancellJobs($vmModel->id);
  75. sl('Vm')->setVmModel($vmModel);
  76. //Unassing IP
  77. $this->networkService->deleteIpAddresses();
  78. if($vmModel->vmid == 0){
  79. $vmModel->delete();
  80. return;
  81. }
  82. //init api
  83. $this->api();
  84. //init vm
  85. sl('Vm')->setVm((new VmFactory())->fromVmModel($vmModel));
  86. //Delete HA
  87. if ($this->highAvailabilityClusterService->exist())
  88. {
  89. $this->highAvailabilityClusterService->delete();
  90. }
  91. try{
  92. //Stop VM
  93. if (sl('Vm')->getVm()->isRunning())
  94. {
  95. sl('Vm')->getVm()->stop();
  96. sleep(4);
  97. }
  98. //VM Protection LXC
  99. if (sl('Vm')->getVm() instanceof Lxc && sl('Vm')->getVm()->config()['protection'] == "1")
  100. {
  101. sl('Vm')->getVm()->protectionOff();
  102. }
  103. } catch (ProxmoxApiException $ex) {
  104. //Configuration file 'nodes/proxmox4/qemu-server/105.conf' does not exist
  105. if (!preg_match("/Configuration file/", $ex->getMessage()) && !preg_match("/does not exist/", $ex->getMessage())) {
  106. throw $ex;
  107. }
  108. }
  109. //virtual interfaces delete
  110. VirtualInterface::ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofVmId($vmModel->id)->delete();
  111. //key pair delete
  112. KeyPair::ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofVmId($vmModel->id)->delete();
  113. //Destroy firewall rules
  114. if (version_compare($this->api()->getVersion(), "3.2", '>'))
  115. {
  116. $rules = new FirewallRulesRepository();
  117. $rules->findByVm(sl('Vm')->getVm())
  118. ->delete();
  119. }
  120. //Destroy Backups
  121. $storage = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : 'local';
  122. $fileRepository = new FileRepository();
  123. $fileRepository->findBackup(sl('Vm')->getVm())
  124. ->findByStorages([$storage])
  125. ->delete();
  126. //Destroy Backup Jobs
  127. $schedule = new BackupScheduleRepository();
  128. $schedule->findByVm(sl('Vm')->getVm());
  129. $schedule->delete();
  130. //Destroy VM
  131. try {
  132. $upid = sl('Vm')->getVm()->delete();
  133. //Create Task History
  134. $type = str_replace(["qemu", "lxc"], ["VM", "CT"], sl('Vm')->getVm()->getVirtualization());
  135. $task = new TaskHistory();
  136. $task->fill([
  137. 'hosting_id' => $this->getWhmcsParamByKey("serviceid"),
  138. 'upid' => $upid,
  139. 'name' => sprintf("%s %s - %s", $type, sl('Vm')->getVm()->getVmid(), "Delete"),
  140. 'vmid' => sl('Vm')->getVm()->getVmid(),
  141. 'node' => sl('Vm')->getVm()->getNode(),
  142. 'status' => 0
  143. ])->save();
  144. } catch (ProxmoxApiException $ex) {
  145. //Configuration file 'nodes/proxmox4/qemu-server/105.conf' does not exist
  146. if (!preg_match("/Configuration file/", $ex->getMessage()) && !preg_match("/does not exist/", $ex->getMessage())) {
  147. throw $ex;
  148. }
  149. }
  150. //delete vmModel
  151. $vmModel->delete();
  152. if($this->hasSnippet() && $files = $this->getSnippetFiles($vmModel)){
  153. foreach ($files as $file){
  154. $file->delete();
  155. }
  156. }
  157. }
  158. public function cancellJobs($customId){
  159. Job::whereIn("status", ['waiting', "running", "", "error"])
  160. ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
  161. ->where("rel_type", "hosting")
  162. ->where("custom_id", $customId)
  163. ->update(["status" => 'cancelled']);
  164. }
  165. }