ResourceGuard.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if ($maxFiles != "-1" && $snapshotRepository->count() >= $maxFiles)
  106. {
  107. throw new \Exception(sl("lang")->tr("The maximum number of snapshots has been exceeded. Please remove the old snapshots."));
  108. }
  109. }
  110. public function hasSnapshotLimit()
  111. {
  112. try
  113. {
  114. $this->snapshotLimit();
  115. return true;
  116. }
  117. catch (\Exception $ex)
  118. {
  119. }
  120. return false;
  121. }
  122. /**
  123. * @return bool
  124. * @to do implement snapshot jobs
  125. */
  126. public function snapshotJobLimit()
  127. {
  128. $max = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOT_JOBS, $this->configuration()->getSnapshotJobs());
  129. if($max == "-1"){
  130. return;
  131. }
  132. $query = SnapshotJob::ofHostingId($this->getWhmcsParamByKey("serviceid"));
  133. if ($max != "-1" && $query->count() >= $max)
  134. {
  135. throw new \Exception(sl("lang")->tr("The maximum number of snapshot jobs has been exceeded. Please remove the old snapshot jobs."));
  136. }
  137. return true;
  138. }
  139. public function hasSnapshotJobLimit()
  140. {
  141. try
  142. {
  143. $this->snapshotJobLimit();
  144. return true;
  145. }
  146. catch (\Exception $ex)
  147. {
  148. }
  149. return false;
  150. }
  151. public function firewallLimit($new=0)
  152. {
  153. $cloudSerice = new CloudService();
  154. $repository = new FirewallRulesRepository();
  155. $repository->setApi($this->api());
  156. $paths=[];
  157. $repository->findByVmModel($cloudSerice->getVmModelWithVmid());
  158. //Files limit
  159. $max = (int)$this->configuration()->getFirewallMaxRules();
  160. if ($max != "-1" && $repository->count()+$new > $max)
  161. {
  162. throw new \Exception(sl("lang")->tr("The maximum number of firewall rules has been exceeded. Please remove the old firewall rules."));
  163. }
  164. }
  165. public function hasfirewallLimit()
  166. {
  167. try
  168. {
  169. $this->firewallLimit();
  170. return true;
  171. }
  172. catch (\Exception $ex)
  173. {
  174. }
  175. return false;
  176. }
  177. }