ProductService.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\Models\ServerGroupItem;
  26. use ModulesGarden\ProxmoxAddon\App\Repositories\Cloud\ProductConfigurationRepository;
  27. use ModulesGarden\ProxmoxAddon\App\Services\LoadBalancerService;
  28. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  29. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  30. trait ProductService
  31. {
  32. /**
  33. * @return FileRepository
  34. */
  35. public function isoRepository()
  36. {
  37. if (!empty($this->isoRepository))
  38. {
  39. return $this->isoRepository;
  40. }
  41. $storageRepository = new StorageRepository();
  42. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  43. $storageRepository->findByNodes([$vm->getNode()])
  44. ->findEnabed();
  45. $storages = $storageRepository->fetchAsArray();
  46. $this->isoRepository = new FileRepository();
  47. $this->isoRepository->findByNodes([$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->isWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
  111. {
  112. $diskBytes = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
  113. Utility::unitFormat($diskBytes, $this->configuration()->getStorageUnit(), 'bytes');
  114. }
  115. else
  116. {
  117. $diskBytes = $this->configuration()->getStorageSize() ? $this->configuration()->getStorageSize() : 1;
  118. Utility::unitFormat($diskBytes, "gb", 'bytes');
  119. }
  120. if ($this->configuration()->isLoadBalancer())
  121. {
  122. $loadBalancer = new LoadBalancerService($this->getWhmcsParamByKey('serverid'));
  123. $loadBalancer->setApi($this->api());
  124. if($this->configuration()->getServerGroup()){
  125. $nodes = ServerGroupItem::ofGroupIdsAndServerId($this->configuration()->getServerGroup(), $this->getWhmcsParamByKey('serverid'))
  126. ->pluck("node")
  127. ->toArray();
  128. if(empty($nodes)){
  129. throw new \InvalidArgumentException("LoadBalancer: Cannot find group for server #". $this->getWhmcsParamByKey('serverid'));
  130. }
  131. $loadBalancer->findByNodes($nodes);
  132. }
  133. $loadBalancerNodes = $loadBalancer->findByVmCreate();
  134. $nodesForUser = $loadBalancer->findNotUser($this->getWhmcsParamByKey('userid'));
  135. if (!$nodesForUser->isEmpty())
  136. {
  137. $loadBalancerNodes = $nodesForUser;
  138. }
  139. if ($this->isWhmcsConfigOption(ConfigurableOption::MEMORY))
  140. {
  141. $ram = $this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
  142. Utility::unitFormat($ram, $this->configuration()->getMemoryUnit(), 'bytes');
  143. }
  144. else
  145. {
  146. $ram = $this->configuration()->getMemory();
  147. Utility::unitFormat( $ram, "mb", 'bytes');
  148. }
  149. if ($this->configuration()->isQemu())
  150. {
  151. $socket = $this->getWhmcsConfigOption(ConfigurableOption::SOCKETS, $this->configuration()->getSockets());
  152. $cores = $this->getWhmcsConfigOption(ConfigurableOption::CORES_PER_SOCKET, $this->configuration()->getCores());
  153. $cpu = $socket * $cores;
  154. } else if ($this->configuration()->isLxc()) {
  155. $cpu = $cores = $this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores());
  156. }
  157. $node = $loadBalancerNodes->findByRam($ram)
  158. ->findByCpu($cpu)
  159. ->findByDisk($diskBytes)
  160. ->findByVms(1)
  161. ->nodeLowLoad();
  162. return $this->node = new Node($node);
  163. }
  164. $defaultNode = $this->configuration()->getDefaultNode();
  165. $nodeRepository = new NodeRepository();
  166. $nodeRepository->setApi($this->api());
  167. switch ($defaultNode)
  168. {
  169. case "autoNode":
  170. if (empty($diskBytes))
  171. {
  172. throw new \Exception("Provide disk size.");
  173. }
  174. return $this->node = $nodeRepository->findByDiskSize($diskBytes);
  175. break;
  176. case "serverNode":
  177. $servePrivateIP = $this->getServerPrivateIpAddress();
  178. $serverIp = $servePrivateIP? $servePrivateIP : $this->getWhmcsParamByKey('serverip');
  179. return $this->node = $nodeRepository->findWithHostOrIp($this->getWhmcsParamByKey('serverhostname'), $serverIp);
  180. break;
  181. default:
  182. return $this->getDefaultNode();
  183. }
  184. }
  185. protected function getDefaultNode()
  186. {
  187. $defaultNode = $this->configuration()->getDefaultNode();
  188. $nodeRepository = new NodeRepository();
  189. $nodeRepository->setApi($this->api());
  190. switch ($defaultNode)
  191. {
  192. case "autoNode":
  193. $diskBytes =1;
  194. if ($this->isWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
  195. {
  196. $diskBytes = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
  197. Utility::unitFormat($diskBytes, $this->configuration()->getStorageUnit(), 'bytes');
  198. }
  199. else
  200. {
  201. $diskBytes = $this->configuration()->getStorageSize();
  202. Utility::unitFormat($diskBytes, "gb", 'bytes');
  203. }
  204. if (empty($diskBytes))
  205. {
  206. throw new \Exception("Provide disk size.");
  207. }
  208. return $nodeRepository->findByDiskSize($diskBytes);
  209. break;
  210. case "serverNode":
  211. $servePrivateIP = $this->getServerPrivateIpAddress();
  212. $serverIp = $servePrivateIP? $servePrivateIP : $this->getWhmcsParamByKey('serverip');
  213. return $nodeRepository->findWithHostOrIp($this->getWhmcsParamByKey('serverhostname'), $serverIp);
  214. break;
  215. default:
  216. return new Node($defaultNode);
  217. }
  218. }
  219. public function getServerPrivateIpAddress(){
  220. $serverId = $this->getWhmcsParamByKey('serverid');
  221. if( $serverId && $serverId >0){
  222. $serverAssignedIps = DB::table('tblservers')->where("id", $serverId)->value("assignedips");
  223. $serverAssignedIps = preg_replace('/\s+/', '', $serverAssignedIps);
  224. $serverAssignedIps = explode(PHP_EOL, $serverAssignedIps);
  225. return current($serverAssignedIps);
  226. }
  227. }
  228. protected function getDefaultNodeIfSet(){
  229. $defaultNode = $this->configuration()->getDefaultNode();
  230. if(!in_array($defaultNode,['autoNode','serverNode'])){
  231. return $defaultNode;
  232. }
  233. return null;
  234. }
  235. }