NodeRepository.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-06)
  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 MGProvision\Proxmox\v2\repository;
  20. use MGProvision\Proxmox\v2\models\Node;
  21. use MGProvision\Proxmox\v2\ProxmoxApiException;
  22. /**
  23. * Description of NodeRepository
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgarden.com>
  26. * @version 1.0.0
  27. */
  28. class NodeRepository extends AbstractRepository
  29. {
  30. private $online = true;
  31. /**
  32. *
  33. * @param string $hostname
  34. * @param string $ipAddress
  35. * @return string
  36. * @throws ProxmoxApiException
  37. */
  38. public function findWithHostOrIp($hostname, $ipAddress)
  39. {
  40. if (empty($hostname) && empty($ipAddress))
  41. {
  42. throw new ProxmoxApiException('Server\'s Hostname is empty');
  43. }
  44. //Hostname
  45. if (preg_match('/\:/', $hostname))
  46. {
  47. list($host, $port) = explode(":", $hostname);
  48. $port = (int) $port;
  49. if (is_string($host))
  50. {
  51. $hostname = $host;
  52. }
  53. }
  54. //IpAddress
  55. if (preg_match('/\:/', $ipAddress))
  56. {
  57. list($host, $port) = explode(":", $ipAddress);
  58. $port = (int) $port;
  59. if (is_string($host))
  60. {
  61. $ipAddress = $host;
  62. }
  63. }
  64. //cluster nodes
  65. $clusterConfig = $this->api()->get('/cluster/config/nodes', array());
  66. foreach ($clusterConfig as $config){
  67. if ($config['node'] && $config['ring0_addr'] && $config['ring0_addr'] == $ipAddress || ( $hostname && $config['ring0_addr'] == $hostname)){
  68. return new Node($config['node']);
  69. }
  70. }
  71. //All nodes to array
  72. $nodes = array();
  73. $result = $this->api()->get("/nodes");
  74. foreach ($result as $ret)
  75. {
  76. if ($this->online && $ret['status'] && $ret['status'] != 'online' )
  77. {
  78. continue;
  79. }
  80. $nodes[] = $ret['node'];
  81. }
  82. $nodes = array_filter($nodes);
  83. //Check if only one node
  84. if(count($nodes) === 1)
  85. {
  86. return new Node($nodes[0]);
  87. }
  88. //Find by hostname
  89. if ($hostname && !filter_var($hostname, FILTER_VALIDATE_IP))
  90. {
  91. $temp = explode(".", $hostname);
  92. $hostNode = current($temp);
  93. if ($hostNode)
  94. {
  95. foreach ($nodes as $node)
  96. {
  97. if (strtolower($hostNode) == strtolower($node))
  98. {
  99. return new Node($node);
  100. }
  101. }
  102. }
  103. }
  104. //Find by Ip Address
  105. foreach ($nodes as $node) {
  106. $networks = $this->api()->get('/nodes/' . $node . '/network', array());
  107. foreach ($networks as $n) {
  108. try {
  109. if (!$n['iface']) {
  110. continue;
  111. }
  112. $res = $this->api()->get('/nodes/' . $node . '/network/' . $n['iface'], array());
  113. if(!$res['address']){
  114. continue;
  115. }
  116. if ($res['address'] && preg_match('/\//', $res['address'])) {
  117. $res['address'] = preg_replace('/\/(.*)*/', '', $res['address']);
  118. }
  119. if ($res['address'] && $res['address'] == $ipAddress || ( $hostname && $res['address'] == $hostname))
  120. return new Node($node);
  121. } catch (\Exception $ex) {//interface does not exist
  122. if(!preg_match('/interface does not exist/',$ex->getMessage())){
  123. throw $ex;
  124. }
  125. }
  126. }
  127. }
  128. if (empty($ipAddress))
  129. {
  130. throw new ProxmoxApiException('Server\'s IP Address is empty');
  131. }
  132. throw new ProxmoxApiException('Node for IP Address "' . $ipAddress . '" not found');
  133. }
  134. /**
  135. *
  136. * @param float $bytes
  137. * @return Node
  138. * @throws ProxmoxApiException
  139. */
  140. public function findByDiskSize($bytes)
  141. {
  142. $nodes_arr = array();
  143. $nodes = $this->api()->get('/nodes');
  144. foreach ($nodes as $nodeValues)
  145. {
  146. if ($this->online && $nodeValues['status'] && $nodeValues['status'] != 'online' )
  147. {
  148. continue;
  149. }
  150. $node = $nodeValues['node'];
  151. $nodeStatus = $this->api()->get("/nodes/{$node}/status");
  152. $nodeStorages = $this->api()->get("/nodes/{$node}/storage");
  153. $storages = array();
  154. foreach ($nodeStorages as $ns)
  155. {
  156. $storages[] = array(
  157. "storage" => $ns['storage'],
  158. "avail" => $ns['avail'],
  159. "content" => $ns['content']
  160. );
  161. }
  162. if (empty($storages))
  163. continue;
  164. $nodes_arr[] = array(
  165. 'node' => $node,
  166. "memory" => $nodeStatus['memory']['free'], //order by "memory":"free"
  167. "storages" => $storages// re-order by "avail
  168. );
  169. }
  170. foreach ($nodes_arr as $nk => $row)
  171. {
  172. $memory[$nk] = $row['memory'];
  173. foreach ($row['storages'] as $sk => $storage)
  174. {
  175. $storages[$sk] = $storage['avail'];
  176. }
  177. $r = array_multisort($storages, SORT_DESC, $row['storages']);
  178. $nodes_arr[$nk]['storages'] = $row['storages'];
  179. }
  180. $r = array_multisort($memory, SORT_DESC, $nodes_arr);
  181. $storage = null;
  182. $backup_storage = null;
  183. foreach ($nodes_arr[0]["storages"] as $s)
  184. {
  185. if ($storage == null && ( strpos($s['content'], 'rootdir') !== false || strpos($s['content'], 'images') !== false ))
  186. $storage = $s['storage'];
  187. if ($backup_storage == null && strpos($s['content'], 'backup') !== false)
  188. $backup_storage = $s['storage'];
  189. }
  190. $selectedNode = array();
  191. foreach ($nodes_arr as $n)
  192. {
  193. if ($n["storages"]["0"]["avail"] > $bytes)
  194. {
  195. return new Node($n['node']);
  196. break;
  197. }
  198. }
  199. throw new ProxmoxApiException(sprintf("Storage with free space \"%s\" bytes not found", $bytes));
  200. }
  201. /**
  202. *
  203. * @return Node[]
  204. */
  205. public function fetch()
  206. {
  207. $data = array();
  208. $result = $this->api()->get("/nodes");
  209. foreach ($result as $ret)
  210. {
  211. if ($this->online && $ret['status'] && $ret['status'] != 'online' )
  212. {
  213. continue;
  214. }
  215. $data[] = new Node($ret['node']);
  216. }
  217. return $data;
  218. }
  219. public function findOnline($online)
  220. {
  221. $this->online = (boolean) $online;
  222. return $this;
  223. }
  224. }