ResourceGuard.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. return false;
  67. }
  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. return false;
  94. }
  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. return false;
  120. }
  121. }
  122. /**
  123. * @return bool
  124. * @to do implement snapshot jobs
  125. */
  126. public function snapshotJobLimit()
  127. {
  128. return true;
  129. }
  130. public function hasSnapshotJobLimit()
  131. {
  132. try
  133. {
  134. $this->snapshotJobLimit();
  135. return true;
  136. }
  137. catch (\Exception $ex)
  138. {
  139. return false;
  140. }
  141. }
  142. public function firewallLimit()
  143. {
  144. $cloudSerice = new CloudService();
  145. $repository = new FirewallRulesRepository();
  146. $repository->setApi($this->api());
  147. $paths=[];
  148. $repository->findByVmModel($cloudSerice->getVmModelWithVmid());
  149. //Files limit
  150. $max = (int)$this->configuration()->getFirewallMaxRules();
  151. if ($max != "-1" && $repository->count() >= $max)
  152. {
  153. throw new \Exception(sl("lang")->tr("The maximum number of firewall rules has been exceeded. Please remove the old firewall rules."));
  154. }
  155. }
  156. public function hasfirewallLimit()
  157. {
  158. try
  159. {
  160. $this->firewallLimit();
  161. return true;
  162. }
  163. catch (\Exception $ex)
  164. {
  165. return false;
  166. }
  167. }
  168. }