Node.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\models;
  20. use \MGProvision\Proxmox\v2\ProxmoxApiException;
  21. /**
  22. * Description of Node
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. class Node extends AbstractObject
  28. {
  29. protected $node;
  30. function __construct($node)
  31. {
  32. $this->node = $node;
  33. }
  34. function getNode()
  35. {
  36. return $this->node;
  37. }
  38. function setNode($node)
  39. {
  40. $this->node = $node;
  41. }
  42. /**
  43. *
  44. * @param string $upid
  45. * @return \MGProvision\Proxmox\v2\models\Task
  46. * @throws \MGProvision\Proxmox\v2\ProxmoxApiException
  47. */
  48. public function task($upid)
  49. {
  50. if (empty($upid))
  51. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Task Id is empty");
  52. if (!is_string($upid))
  53. {
  54. throw new \MGProvision\Proxmox\v2\ProxmoxApiException(sprintf("Task Id is invalid '%s'", $upid));
  55. }
  56. $attributes = $this->api()->get("/nodes/{$this->node}/tasks/{$upid}/status");
  57. $task = new Task();
  58. $task->setAttributes($attributes);
  59. return $task;
  60. }
  61. public function hasKvm($vmid)
  62. {
  63. if (empty($vmid))
  64. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Parameter ('vmid') is empty");
  65. try
  66. {
  67. $res = $this->api()->get("/nodes/{$this->node}/qemu/{$vmid}");
  68. return true;
  69. }
  70. catch (\MGProvision\Proxmox\v2\ProxmoxApiException $ex)
  71. {
  72. return false;
  73. }
  74. }
  75. public function getFreeSpace($storage = null)
  76. {
  77. $result = $res = $this->api()->get("/nodes/{$this->node}/status");
  78. $limits = array(
  79. "memory" => $result['memory']['free'],
  80. "swap" => $result['swap']['free'],
  81. );
  82. if ($storage)
  83. {
  84. $result2 = $this->api()->get("/nodes/{$this->node}/storage/{$storage}/status", array());
  85. $limits['storage'] = $result2['avail'];
  86. }
  87. return $limits;
  88. }
  89. /**
  90. *
  91. * @return \MGProvision\Proxmox\v2\models\Kvm
  92. */
  93. public function kvm()
  94. {
  95. return new Kvm($this->node);
  96. }
  97. /**
  98. *
  99. * @return \MGProvision\Proxmox\v2\models\Lxc
  100. */
  101. public function lxc()
  102. {
  103. return new Lxc($this->node);
  104. }
  105. public function getPath()
  106. {
  107. return "/nodes/{$this->node}";
  108. }
  109. public function getMainIpAddress()
  110. {
  111. $repository = new \MGProvision\Proxmox\v2\repository\NetworkRepository();
  112. $repository->findByPath($this->getPath() . "/network")
  113. ->findTypeBridge();
  114. foreach ($repository->fetch() as $n)
  115. {
  116. if ($n->getIface() == 'vmbr0')
  117. {
  118. if ($n->getAddress())
  119. {
  120. return $n->getAddress();
  121. }
  122. }
  123. }
  124. throw new ProxmoxApiException(sprintf("IP Addres for node '%s' not found", $this->getNode()));
  125. }
  126. public function getStatus()
  127. {
  128. return $this->api()->get("/nodes/{$this->node}/status", array());
  129. }
  130. public function getSubscription()
  131. {
  132. return $this->api()->get("/nodes/{$this->node}/subscription");
  133. }
  134. public function getDns()
  135. {
  136. return $this->api()->get("/nodes/{$this->node}/dns");
  137. }
  138. public function rrdData($parameters)
  139. {
  140. return $this->api()->get("/nodes/{$this->node}/rrddata", $parameters);
  141. }
  142. }