MountPointRepostiory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Apr 12, 2018)
  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\ProxmoxApiException;
  21. use \MGProvision\Proxmox\v2\models\MountPoint;
  22. /**
  23. * Description of MounPointRepostiory
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgardne.com>
  26. */
  27. class MountPointRepostiory extends AbstractRepository
  28. {
  29. protected $path;
  30. private $excludeIds=[];
  31. public function findByPath($path)
  32. {
  33. if (!preg_match('/config/', $path))
  34. {
  35. throw new ProxmoxApiException(sprintf("MounPoint path ('%s') is not valid", $path));
  36. }
  37. $this->path = $path;
  38. return $this;
  39. }
  40. /**
  41. *
  42. * @return MountPoint[]
  43. * @throws ProxmoxApiException
  44. */
  45. public function fetch()
  46. {
  47. if ($this->fetch)
  48. {
  49. return $this->fetch;
  50. }
  51. if (empty($this->path))
  52. {
  53. throw new ProxmoxApiException("MounPoint path is empty");
  54. }
  55. $this->fetch = [];
  56. foreach ($this->api()->get($this->path) as $id => $config)
  57. {
  58. if (!preg_match('/mp/', $id) && $id != 'rootfs' && !preg_match('/disk/', $config))
  59. {
  60. continue;
  61. }
  62. $this->fetch[$id] = (new MountPoint($id, $config))->setPath($this->path);
  63. }
  64. krsort($this->fetch);
  65. return $this->fetch;
  66. }
  67. public function size()
  68. {
  69. $size = 0;
  70. foreach ($this->fetch() as $mp)
  71. {
  72. $size += (int) $mp->getSize();
  73. }
  74. return $size;
  75. }
  76. /**
  77. * Return sieze in GB
  78. * @return int
  79. * @throws ProxmoxApiException
  80. */
  81. public function additionalSize()
  82. {
  83. $size = 0;
  84. foreach ($this->fetch() as $mp)
  85. {
  86. if ($mp->getId() == "rootfs")
  87. {
  88. continue;
  89. }
  90. else if($this->excludeIds && in_array($mp->getId(), $this->excludeIds)){
  91. continue;
  92. }
  93. $size += (int) $mp->getSize();
  94. }
  95. return $size;
  96. }
  97. public function additionalSizeToBytes(){
  98. //GB => Bytes
  99. return $this->additionalSize() * pow(1024, 3);
  100. }
  101. public function nextId()
  102. {
  103. $used = [];
  104. foreach ($this->fetch() as $mp)
  105. {
  106. $used[] = preg_replace("/[a-z]+/", "", $mp->getId());
  107. }
  108. for ($n = 0; $n <= 7; $n++)
  109. {
  110. if (!in_array($n, $used))
  111. {
  112. return "mp" . $n;
  113. }
  114. }
  115. throw new ProxmoxApiException("Mount Point limit exceeded");
  116. }
  117. public function findMountPointById($id){
  118. foreach($this->fetch() as $entity){
  119. if($entity->getId()==$id){
  120. return $entity;
  121. }
  122. }
  123. throw new ProxmoxApiException("Mount Point {$id} not found");
  124. }
  125. public function findById($id){
  126. return $this->findMountPointById($id);
  127. }
  128. public function whereNotIn(array $excludeIds){
  129. $this->excludeIds = $excludeIds;
  130. return $this;
  131. }
  132. public function deleteUnused(){
  133. $this->reset();
  134. foreach ($this->fetch() as $disk){
  135. if(preg_match('/unused/', $disk->getId())){
  136. $disk->delete();
  137. }
  138. }
  139. }
  140. }