ProductService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.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\ProxmoxAddon\App\Services\Cloud;
  20. use Illuminate\Database\Capsule\Manager as DB;
  21. use MGProvision\Proxmox\v2\models\Node;
  22. use MGProvision\Proxmox\v2\repository\FileRepository;
  23. use MGProvision\Proxmox\v2\repository\NodeRepository;
  24. use MGProvision\Proxmox\v2\repository\StorageRepository;
  25. use ModulesGarden\ProxmoxAddon\App\Repositories\Cloud\ProductConfigurationRepository;
  26. use ModulesGarden\ProxmoxAddon\App\Services\LoadBalancerService;
  27. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  28. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  29. trait ProductService
  30. {
  31. /**
  32. * @return FileRepository
  33. */
  34. public function isoRepository()
  35. {
  36. if (!empty($this->isoRepository))
  37. {
  38. return $this->isoRepository;
  39. }
  40. $storageRepository = new StorageRepository();
  41. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  42. $storageRepository->findByNodes([$vm->getNode()])
  43. ->findEnabed();
  44. $storages = $storageRepository->fetchAsArray();
  45. $this->isoRepository = new FileRepository();
  46. $this->isoRepository->findByNodes([$vm->getNode()])
  47. ->findByStorages($storages);
  48. $this->isoRepository->findIso();
  49. return $this->isoRepository;
  50. }
  51. /**
  52. * @return ProductConfigurationRepository
  53. */
  54. public function configuration()
  55. {
  56. if (!empty($this->configuration))
  57. {
  58. return $this->configuration;
  59. }
  60. if (!$this->productId)
  61. {
  62. $this->setProductId($this->getWhmcsParamByKey("packageid"));
  63. }
  64. return $this->configuration = new ProductConfigurationRepository($this->productId);
  65. }
  66. /**
  67. * @return int
  68. */
  69. public function getProductId()
  70. {
  71. return $this->productId;
  72. }
  73. /**
  74. * @param int $productId
  75. */
  76. public function setProductId($productId)
  77. {
  78. $this->productId = $productId;
  79. }
  80. public function acl()
  81. {
  82. if (!empty($this->acl))
  83. {
  84. return $this->acl;
  85. }
  86. return $this->acl = new AclService();
  87. }
  88. /**
  89. * @return ResourceGuard
  90. */
  91. public function resourceGuard()
  92. {
  93. if (!empty($this->resourceGuard))
  94. {
  95. return $this->resourceGuard;
  96. }
  97. return $this->resourceGuard = new ResourceGuard();
  98. }
  99. /**
  100. * @return Node
  101. * @throws \MGProvision\Proxmox\v2\ProxmoxApiException
  102. */
  103. public function getNode()
  104. {
  105. if ($this->node)
  106. {
  107. return $this->node;
  108. }
  109. if ($this->isWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
  110. {
  111. $diskBytes = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
  112. Utility::unitFormat($diskBytes, $this->configuration()->getStorageUnit(), 'bytes');
  113. }
  114. else
  115. {
  116. $diskBytes = $this->configuration()->getStorageSize() ? $this->configuration()->getStorageSize() : 1;
  117. Utility::unitFormat($diskBytes, "gb", 'bytes');
  118. }
  119. if ($this->configuration()->isLoadBalancer())
  120. {
  121. $loadBalancer = new LoadBalancerService($this->getWhmcsParamByKey('serverid'));
  122. $loadBalancer->setApi($this->api());
  123. $loadBalancerNodes = $loadBalancer->findByVmCreate();
  124. $nodesForUser = $loadBalancer->findNotUser($this->getWhmcsParamByKey('userid'));
  125. if (!$nodesForUser->isEmpty())
  126. {
  127. $loadBalancerNodes = $nodesForUser;
  128. }
  129. if ($this->isWhmcsConfigOption(ConfigurableOption::MEMORY))
  130. {
  131. $ram = $this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
  132. Utility::unitFormat($ram, $this->configuration()->getMemoryUnit(), 'bytes');
  133. }
  134. else
  135. {
  136. $ram = $this->configuration()->getMemory();
  137. Utility::unitFormat( $ram, "mb", 'bytes');
  138. }
  139. if ($this->configuration()->isQemu())
  140. {
  141. $socket = $this->getWhmcsConfigOption(ConfigurableOption::SOCKETS, $this->configuration()->getSockets());
  142. $cores = $this->getWhmcsConfigOption(ConfigurableOption::CORES_PER_SOCKET, $this->configuration()->getCores());
  143. $cpu = $socket * $cores;
  144. } else if ($this->configuration()->isLxc()) {
  145. $cpu = $cores = $this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores());
  146. }
  147. $node = $loadBalancerNodes->findByRam($ram)
  148. ->findByCpu($cpu)
  149. ->findByDisk($diskBytes)
  150. ->findByVms(1)
  151. ->nodeLowLoad();
  152. return $this->node = new Node($node);
  153. }
  154. $defaultNode = $this->configuration()->getDefaultNode();
  155. $nodeRepository = new NodeRepository();
  156. $nodeRepository->setApi($this->api());
  157. switch ($defaultNode)
  158. {
  159. case "autoNode":
  160. if (empty($diskBytes))
  161. {
  162. throw new \Exception("Provide disk size.");
  163. }
  164. return $this->node = $nodeRepository->findByDiskSize($diskBytes);
  165. break;
  166. case "serverNode":
  167. $servePrivateIP = $this->getServerPrivateIpAddress();
  168. $serverIp = $servePrivateIP? $servePrivateIP : $this->getWhmcsParamByKey('serverip');
  169. return $this->node = $nodeRepository->findWithHostOrIp($this->getWhmcsParamByKey('serverhostname'), $serverIp);
  170. break;
  171. default:
  172. return $this->getDefaultNode();
  173. }
  174. }
  175. protected function getDefaultNode()
  176. {
  177. $defaultNode = $this->configuration()->getDefaultNode();
  178. $nodeRepository = new NodeRepository();
  179. $nodeRepository->setApi($this->api());
  180. switch ($defaultNode)
  181. {
  182. case "autoNode":
  183. $diskBytes =1;
  184. if ($this->isWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
  185. {
  186. $diskBytes = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
  187. Utility::unitFormat($diskBytes, $this->configuration()->getStorageUnit(), 'bytes');
  188. }
  189. else
  190. {
  191. $diskBytes = $this->configuration()->getStorageSize();
  192. Utility::unitFormat($diskBytes, "gb", 'bytes');
  193. }
  194. if (empty($diskBytes))
  195. {
  196. throw new \Exception("Provide disk size.");
  197. }
  198. return $nodeRepository->findByDiskSize($diskBytes);
  199. break;
  200. case "serverNode":
  201. $servePrivateIP = $this->getServerPrivateIpAddress();
  202. $serverIp = $servePrivateIP? $servePrivateIP : $this->getWhmcsParamByKey('serverip');
  203. return $nodeRepository->findWithHostOrIp($this->getWhmcsParamByKey('serverhostname'), $serverIp);
  204. break;
  205. default:
  206. return new Node($defaultNode);
  207. }
  208. }
  209. public function getServerPrivateIpAddress(){
  210. $serverId = $this->getWhmcsParamByKey('serverid');
  211. if( $serverId && $serverId >0){
  212. $serverAssignedIps = DB::table('tblservers')->where("id", $serverId)->value("assignedips");
  213. $serverAssignedIps = preg_replace('/\s+/', '', $serverAssignedIps);
  214. $serverAssignedIps = explode(PHP_EOL, $serverAssignedIps);
  215. return current($serverAssignedIps);
  216. }
  217. }
  218. protected function getDefaultNodeIfSet(){
  219. $defaultNode = $this->configuration()->getDefaultNode();
  220. if(!in_array($defaultNode,['autoNode','serverNode'])){
  221. return $defaultNode;
  222. }
  223. return null;
  224. }
  225. }