TerminateAccount.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**********************************************************************
  3. * ProxmoxVPS developed. (26.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. **********************************************************************/
  19. namespace ModulesGarden\Servers\ProxmoxVps\App\Http\Actions;
  20. use MGProvision\Proxmox\v2\models\Lxc;
  21. use MGProvision\Proxmox\v2\repository\BackupScheduleRepository;
  22. use MGProvision\Proxmox\v2\repository\FileRepository;
  23. use MGProvision\Proxmox\v2\repository\FirewallRulesRepository;
  24. use ModulesGarden\ProxmoxAddon\App\Models\KeyPair;
  25. use ModulesGarden\ProxmoxAddon\App\Models\SnapshotJob;
  26. use ModulesGarden\ProxmoxAddon\App\Models\TaskHistory;
  27. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  28. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HighAvailabilityClusterService;
  29. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
  30. use ModulesGarden\ProxmoxAddon\App\Services\Vps\NetworkService;
  31. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  32. use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
  33. use ModulesGarden\ProxmoxAddon\App\Traits\Vps\SnippetTrait;
  34. use ModulesGarden\ProxmoxAddon\Core\Models\ModuleSettings\Model as ModuleSettings;
  35. use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
  36. use ModulesGarden\Servers\ProxmoxVps\App\Helpers\AppParams;
  37. use ModulesGarden\Servers\ProxmoxVps\App\Helpers\ProxmoxAddonValidator;
  38. use ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\Instances\AddonController;
  39. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\WhmcsParams;
  40. class TerminateAccount extends AddonController
  41. {
  42. use WhmcsParams;
  43. use ProductService;
  44. use ApiService;
  45. use UserService;
  46. use HostingService;
  47. use SnippetTrait;
  48. /**
  49. * @var HighAvailabilityClusterService
  50. */
  51. private $highAvailabilityClusterService;
  52. public function __construct()
  53. {
  54. $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
  55. }
  56. public function execute($params = null)
  57. {
  58. if(!ProxmoxAddonValidator::isInstalled()){
  59. return ProxmoxAddonValidator::failAsString();
  60. }
  61. (new AppParams())->initFromWhmcsParams();
  62. try
  63. {
  64. if (!$this->getWhmcsParamByKey('serviceid'))
  65. {
  66. throw new \InvalidArgumentException("The Service Id cannot be empty");
  67. }
  68. //Cron delete active jobs
  69. Job::whereIn("status", ['waiting', "running", "", "error"])
  70. ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
  71. ->where("rel_type", "hosting")
  72. ->update(["status" => 'cancelled']);
  73. //snapshot jobs
  74. SnapshotJob::ofHostingId($this->getWhmcsParamByKey("serviceid"))->delete();
  75. //User delete
  76. if ($this->isUser())
  77. {
  78. $this->deleteUser();
  79. }
  80. //Unassing IP
  81. $networkService = new NetworkService();
  82. $networkService->deleteIpAddresses();
  83. //vmid validation
  84. if (!$this->getWhmcsParamByKey('customfields')['vmid'])
  85. {
  86. throw new \InvalidArgumentException("Custom Field \"VMID\" cannot be empty");
  87. }
  88. //node
  89. if (!$this->getWhmcsParamByKey('customfields')['node'])
  90. {
  91. throw new \InvalidArgumentException("Custom Field \"VMID\" cannot be empty");
  92. }
  93. //Delete HA
  94. if ($this->highAvailabilityClusterService->exist())
  95. {
  96. $this->highAvailabilityClusterService->delete();
  97. }
  98. //Stop VM
  99. if ($this->vm()->isRunning())
  100. {
  101. $this->vm()->stop();
  102. sleep(4);
  103. }
  104. //VM Protection LXC
  105. if ($this->vm() instanceof Lxc && $this->vm()->config()['protection'] == "1")
  106. {
  107. $this->vm()->protectionOff();
  108. KeyPair::ofHostingId($this->getWhmcsParamByKey("serviceid"))->delete();
  109. }
  110. //Destroy firewall rules
  111. if (version_compare($this->api()->getVersion(), "3.2", '>'))
  112. {
  113. $rules = new FirewallRulesRepository();
  114. $rules->findByVm($this->vm())
  115. ->delete();
  116. }
  117. //Destroy Backups
  118. if ($this->configuration()->isDeleteBackups())
  119. {
  120. $storage = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : 'local';
  121. $fileRepository = new FileRepository();
  122. $fileRepository->findBackup($this->vm())
  123. ->findByStorages([$storage])
  124. ->delete();
  125. }
  126. //Destroy Backup Jobs
  127. $schedule = new BackupScheduleRepository();
  128. $schedule->findByVm($this->vm());
  129. $schedule->delete();
  130. //Destroy VM
  131. $upid = $this->vm()->delete();
  132. //Create Task History
  133. $type = str_replace(["qemu", "lxc"], ["VM", "CT"], $this->vm()->getVirtualization());
  134. $task = new TaskHistory();
  135. $task->fill([
  136. 'hosting_id' => $this->getWhmcsParamByKey("serviceid"),
  137. 'upid' => $upid,
  138. 'name' => sprintf("%s %s - %s", $type, $this->vm()->getVmid(), "Delete"),
  139. 'vmid' => $this->vm()->getVmid(),
  140. 'node' => $this->vm()->getNode(),
  141. 'status' => 0
  142. ])->save();
  143. //snippets
  144. if($this->hasSnippet() && $snippets = $this->getSnippetFiles()){
  145. foreach ($snippets as $snippet){
  146. $snippet->delete();
  147. }
  148. }
  149. //Reset Custom Fields
  150. $this->customFieldUpdate("vmid", "");
  151. $this->customFieldUpdate("node", "");
  152. //Reset VLAN Tag
  153. if ($this->getWhmcsParamByKey('customfields')['VLAN Tag'])
  154. {
  155. $this->customFieldUpdate("VLAN Tag", "");
  156. }
  157. //Cache
  158. $serviceId = $this->getWhmcsParamByKey('serviceid');
  159. ModuleSettings::where('setting', "LIKE", "%{$serviceId}_cacheData%")->delete();
  160. return "success";
  161. }
  162. catch (\Exception $ex)
  163. {
  164. if (\ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings::isDebug())
  165. {
  166. logModuleCall(
  167. 'ProxmoxVps',
  168. __CLASS__,
  169. [],
  170. null,
  171. $ex->getMessage() . " " . $ex->getTraceAsString()
  172. );
  173. }
  174. return $ex->getMessage();
  175. }
  176. }
  177. }