ResourceGuard.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;
  3. use MGProvision\Proxmox\v2\repository\BackupScheduleRepository;
  4. use MGProvision\Proxmox\v2\repository\FileRepository;
  5. use MGProvision\Proxmox\v2\repository\FirewallRulesRepository;
  6. use MGProvision\Proxmox\v2\repository\HardDiskRepostiory;
  7. use MGProvision\Proxmox\v2\repository\MountPointRepostiory;
  8. use MGProvision\Proxmox\v2\repository\SnapshotRepository;
  9. use ModulesGarden\ProxmoxAddon\App\Models\SnapshotJob;
  10. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  11. use ModulesGarden\ProxmoxAddon\App\Services\CloudService;
  12. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  13. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  14. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  15. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  16. /**
  17. * Class ResourceGuard
  18. * @package ModulesGarden\ProxmoxAddon\App\Services\Cloud
  19. * @todo
  20. */
  21. class ResourceGuard
  22. {
  23. use WhmcsParams;
  24. use ProductService;
  25. use ApiService;
  26. public function backupLimit()
  27. {
  28. $cloudSerice = new CloudService();
  29. //Backup repository
  30. $backupRepository = new FileRepository();
  31. $backupRepository->setApi($this->api());
  32. $backupRepository->findBackupByVmModel($cloudSerice->getVmModelWithVmid())
  33. ->findByStorages([$this->configuration()->getBackupStorage()]);
  34. //Files limit
  35. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES, $this->configuration()->getBackupMaxFiles()) ;
  36. if ($maxFiles != "-1" && $this->configuration()->isBackupRouting())
  37. {
  38. $maxFiles++;
  39. }
  40. if ($maxFiles != "-1" && $backupRepository->count() >= $maxFiles)
  41. {
  42. throw new \Exception(sl("lang")->abtr("The maximum number of backup files has been exceeded. Please remove the old backup files."));
  43. }
  44. //Size limit
  45. $maxSize = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_SIZE, $this->configuration()->getBackupMaxSize()) ;
  46. $used = $backupRepository->size();
  47. Utility::unitFormat($used, "bytes", "gb");
  48. if ($maxSize != "-1" && $used >= $maxSize)
  49. {
  50. throw new \Exception(sl("lang")->abtr("The maximum size set for a backup has been exceeded. Please remove the old backup files."));
  51. }
  52. }
  53. /**
  54. * @return bool
  55. * @todo
  56. */
  57. public function hasBackupLimit()
  58. {
  59. try
  60. {
  61. $this->backupLimit();
  62. return true;
  63. }
  64. catch (\Exception $ex)
  65. {
  66. }
  67. return false;
  68. }
  69. public function backupJobLimit()
  70. {
  71. $this->backupLimit();
  72. $cloudSerice = new CloudService();
  73. //Backup repository
  74. $backupRepository = new BackupScheduleRepository();
  75. $backupRepository->setApi($this->api());
  76. $backupRepository->findByVmModel($cloudSerice->getVmModelWithVmid());
  77. //Files limit
  78. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES, $this->configuration()->getBackupMaxFiles()) ;
  79. if ($maxFiles != "-1" && $backupRepository->count() >= $maxFiles)
  80. {
  81. throw new \Exception(sl("lang")->tr("The maximum number of backup files has been exceeded. Please remove the old backup files."));
  82. }
  83. }
  84. public function hasBackupJobLimit()
  85. {
  86. try
  87. {
  88. $this->backupJobLimit();
  89. return true;
  90. }
  91. catch (\Exception $ex)
  92. {
  93. }
  94. return false;
  95. }
  96. public function snapshotLimit()
  97. {
  98. $cloudSerice = new CloudService();
  99. $snapshotRepository = new SnapshotRepository();
  100. $snapshotRepository->setApi($this->api());
  101. $snapshotRepository->findByVmModel($cloudSerice->getVmModels());
  102. $snapshotRepository->ignoreCurrent(true);
  103. //Files limit
  104. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOTS, $this->configuration()->getSnapshotMaxFiles());
  105. $testRepository = new SnapshotRepository();
  106. $testRepository->setApi($this->api());
  107. $testRepository->findByVmModel($cloudSerice->getVmModelWithVmid());
  108. $testRepository->ignoreCurrent(true);
  109. logModuleCall(
  110. 'ProxmoxCloudVps',
  111. __FUNCTION__,
  112. $testRepository,
  113. 'Debug',
  114. $snapshotRepository->count()
  115. );
  116. if ($maxFiles != "-1" && $snapshotRepository->count() >= $maxFiles)
  117. {
  118. throw new \Exception(sl("lang")->tr("The maximum number of snapshots has been exceeded. Please remove the old snapshots."));
  119. }
  120. }
  121. public function hasSnapshotLimit()
  122. {
  123. try
  124. {
  125. $this->snapshotLimit();
  126. return true;
  127. }
  128. catch (\Exception $ex)
  129. {
  130. }
  131. return false;
  132. }
  133. /**
  134. * @return bool
  135. * @to do implement snapshot jobs
  136. */
  137. public function snapshotJobLimit()
  138. {
  139. $max = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOT_JOBS, $this->configuration()->getSnapshotJobs());
  140. if($max == "-1"){
  141. return;
  142. }
  143. $query = SnapshotJob::ofHostingId($this->getWhmcsParamByKey("serviceid"));
  144. if ($max != "-1" && $query->count() >= $max)
  145. {
  146. throw new \Exception(sl("lang")->tr("The maximum number of snapshot jobs has been exceeded. Please remove the old snapshot jobs."));
  147. }
  148. return true;
  149. }
  150. public function hasSnapshotJobLimit()
  151. {
  152. try
  153. {
  154. $this->snapshotJobLimit();
  155. return true;
  156. }
  157. catch (\Exception $ex)
  158. {
  159. }
  160. return false;
  161. }
  162. public function firewallLimit($new=0)
  163. {
  164. $cloudSerice = new CloudService();
  165. $repository = new FirewallRulesRepository();
  166. $repository->setApi($this->api());
  167. $paths=[];
  168. $repository->findByVmModel($cloudSerice->getVmModelWithVmid());
  169. //Files limit
  170. $max = (int)$this->configuration()->getFirewallMaxRules();
  171. if ($max != "-1" && $repository->count()+$new > $max)
  172. {
  173. throw new \Exception(sl("lang")->tr("The maximum number of firewall rules has been exceeded. Please remove the old firewall rules."));
  174. }
  175. }
  176. public function hasfirewallLimit()
  177. {
  178. try
  179. {
  180. $this->firewallLimit();
  181. return true;
  182. }
  183. catch (\Exception $ex)
  184. {
  185. }
  186. return false;
  187. }
  188. }