ResourceGuard.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;
  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\Utility;
  12. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  13. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  14. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  15. class ResourceGuard
  16. {
  17. use WhmcsParams;
  18. use ProductService;
  19. use ApiService;
  20. public function backupLimit()
  21. {
  22. //Backup repository
  23. $backupRepository = new FileRepository();
  24. $backupRepository->setApi($this->api());
  25. $backupRepository->findBackup($this->vm())
  26. ->findByStorages([$this->configuration()->getBackupStorage()]);
  27. //Files limit
  28. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES, $this->configuration()->getBackupMaxFiles()) ;
  29. if ($maxFiles != "-1" && $this->configuration()->isBackupRouting())
  30. {
  31. $maxFiles++;
  32. }
  33. if ($maxFiles != "-1" && $backupRepository->count() >= $maxFiles)
  34. {
  35. throw new \Exception(sl("lang")->abtr("The maximum number of backup files has been exceeded. Please remove the old backup files."));
  36. }
  37. //Size limit
  38. $maxSize = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_SIZE, $this->configuration()->getBackupMaxSize()) ;
  39. $used = $backupRepository->size();
  40. Utility::unitFormat($used, "bytes", "gb");
  41. if ($maxSize != "-1" && $used >= $maxSize)
  42. {
  43. throw new \Exception(sl("lang")->abtr("The maximum size set for a backup has been exceeded. Please remove the old backup files."));
  44. }
  45. }
  46. public function hasBackupLimit()
  47. {
  48. try
  49. {
  50. $this->backupLimit();
  51. return true;
  52. }
  53. catch (\Exception $ex)
  54. {
  55. return false;
  56. }
  57. }
  58. public function backupJobLimit()
  59. {
  60. $this->backupLimit();
  61. //Backup repository
  62. $backupRepository = new BackupScheduleRepository();
  63. $backupRepository->setApi($this->api());
  64. $backupRepository->findByVm($this->vm());
  65. //Files limit
  66. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES, $this->configuration()->getBackupMaxFiles()) ;
  67. if ($maxFiles != "-1" && $backupRepository->count() >= $maxFiles)
  68. {
  69. throw new \Exception(sl("lang")->tr("The maximum number of backup files has been exceeded. Please remove the old backup files."));
  70. }
  71. }
  72. public function hasBackupJobLimit()
  73. {
  74. try
  75. {
  76. $this->backupJobLimit();
  77. return true;
  78. }
  79. catch (\Exception $ex)
  80. {
  81. return false;
  82. }
  83. }
  84. public function snapshotLimit()
  85. {
  86. $snapshotRepository = new SnapshotRepository();
  87. $snapshotRepository->setApi($this->api());
  88. $snapshotRepository->findByVm($this->vm());
  89. $snapshotRepository->ignoreCurrent(true);
  90. //Files limit
  91. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOTS, $this->configuration()->getSnapshotMaxFiles());
  92. if ($maxFiles != "-1" && $snapshotRepository->count() >= $maxFiles)
  93. {
  94. throw new \Exception(sl("lang")->tr("The maximum number of snapshots has been exceeded. Please remove the old snapshots."));
  95. }
  96. }
  97. public function hasSnapshotLimit()
  98. {
  99. try
  100. {
  101. $this->snapshotLimit();
  102. return true;
  103. }
  104. catch (\Exception $ex)
  105. {
  106. return false;
  107. }
  108. }
  109. public function snapshotJobLimit()
  110. {
  111. $max = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOT_JOBS, $this->configuration()->getSnapshotJobs());
  112. if($max == "-1"){
  113. return;
  114. }
  115. $query = SnapshotJob::ofHostingId($this->getWhmcsParamByKey("serviceid"));
  116. if ($max != "-1" && $query->count() >= $max)
  117. {
  118. throw new \Exception(sl("lang")->tr("The maximum number of snapshot jobs has been exceeded. Please remove the old snapshot jobs."));
  119. }
  120. }
  121. public function hasSnapshotJobLimit()
  122. {
  123. try
  124. {
  125. $this->snapshotJobLimit();
  126. return true;
  127. }
  128. catch (\Exception $ex)
  129. {
  130. return false;
  131. }
  132. }
  133. public function firewallLimit()
  134. {
  135. $repository = new FirewallRulesRepository();
  136. $repository->setApi($this->api());
  137. $repository->findByVm($this->vm());
  138. //Files limit
  139. $max = (int)$this->configuration()->getFirewallMaxRules();
  140. if ($max != "-1" && $repository->count() >= $max)
  141. {
  142. throw new \Exception(sl("lang")->tr("The maximum number of firewall rules has been exceeded. Please remove the old firewall rules."));
  143. }
  144. }
  145. public function hasfirewallLimit()
  146. {
  147. try
  148. {
  149. $this->firewallLimit();
  150. return true;
  151. }
  152. catch (\Exception $ex)
  153. {
  154. return false;
  155. }
  156. }
  157. public function mountPointLimit($newSize = 0, $excludeId = null)
  158. {
  159. //Max size
  160. $maxSize = $this->configuration()->getAdditionalDiskSize();//GB
  161. if ($this->isWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE) )
  162. {
  163. $maxSize = $this->getWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE);
  164. Utility::unitFormat($maxSize, $this->configuration()->getAdditionalDiskUnit(), "gb");
  165. }
  166. $repository = new MountPointRepostiory();
  167. $repository->setApi($this->api());
  168. $repository->findByPath($this->vm()->getPath() . '/config');
  169. if (!is_null($excludeId))
  170. {
  171. $repository->whereNotIn([$excludeId]);
  172. }
  173. $used = $repository->additionalSize();
  174. $free = $maxSize - $used;
  175. if ($maxSize != "-1" && $newSize > $free)
  176. {
  177. throw new \Exception(sprintf(sl("lang")->tr("You are not able to set %s GB of disk size. Available disk size: %s GB"), $newSize, $free));
  178. }
  179. }
  180. public function hasMountPointLimit()
  181. {
  182. //Max size
  183. $maxSize = $this->configuration()->getAdditionalDiskSize();//GB
  184. if ($this->isWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE) )
  185. {
  186. $maxSize = $this->getWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE);
  187. Utility::unitFormat($maxSize, $this->configuration()->getAdditionalDiskUnit(), "gb");
  188. }
  189. return $this->vm()->getMounPoints()->additionalSize() < $maxSize;
  190. }
  191. public function diskLimit($newSize = 0, $excludeId = null)
  192. {
  193. //Max size
  194. $maxSize = $this->configuration()->getAdditionalDiskSize();//GB
  195. if ($this->isWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE) )
  196. {
  197. $maxSize = $this->getWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE);
  198. Utility::unitFormat($maxSize, $this->configuration()->getAdditionalDiskUnit(), "gb");
  199. }
  200. $repository = new HardDiskRepostiory();
  201. $repository->setApi($this->api());
  202. $repository->findByPath($this->vm()->getPath() . '/config');
  203. if (!is_null($excludeId))
  204. {
  205. $repository->whereNotIn([$excludeId]);
  206. }
  207. $used = $repository->additionalSize();
  208. $free = $maxSize - $used;
  209. if ($maxSize != "-1" && $newSize > $free)
  210. {
  211. throw new \Exception(sprintf(sl("lang")->tr("You are not able to set %s GB of disk size. Available disk size: %s GB"), $newSize, $free));
  212. }
  213. }
  214. public function hasDiskLimit()
  215. {
  216. //Max size
  217. $maxSize = $this->configuration()->getAdditionalDiskSize();//GB
  218. if ($this->isWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE) )
  219. {
  220. $maxSize = $this->getWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE);
  221. Utility::unitFormat($maxSize, $this->configuration()->getAdditionalDiskUnit(), "gb");
  222. }
  223. $repository = new HardDiskRepostiory();
  224. $repository->setApi($this->api());
  225. $repository->findByPath($this->vm()->getPath() . '/config');
  226. return $repository->additionalSize() < $maxSize;
  227. }
  228. }