ProductService.php 9.4 KB

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