ProductService.php 8.1 KB

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