HardDiskRepostiory.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\models\HardDisk;
  21. use \MGProvision\Proxmox\v2\ProxmoxApiException;
  22. use \MGProvision\Proxmox\v2\models\MountPoint;
  23. /**
  24. * Description of MounPointRepostiory
  25. *
  26. * @author Pawel Kopec <pawelk@modulesgardne.com>
  27. */
  28. class HardDiskRepostiory extends AbstractRepository
  29. {
  30. protected $path;
  31. private $excludeIds=[];
  32. public function findByPath($path)
  33. {
  34. if (!preg_match('/config/', $path))
  35. {
  36. throw new ProxmoxApiException(sprintf("Hard Disk path ('%s') is not valid", $path));
  37. }
  38. $this->path = $path;
  39. return $this;
  40. }
  41. /**
  42. *
  43. * @return HardDisk[]
  44. * @throws ProxmoxApiException
  45. */
  46. public function fetch()
  47. {
  48. if ($this->fetch)
  49. {
  50. return $this->fetch;
  51. }
  52. if (empty($this->path))
  53. {
  54. throw new ProxmoxApiException("Hard Disk path is empty");
  55. }
  56. $this->fetch = [];
  57. $config = $this->api()->get($this->path);
  58. $boot = (array) $this->getBootOrder();
  59. $bootDisk = reset($boot );
  60. foreach ($config as $id => $value)
  61. {
  62. if (!HardDisk::isConfigValid($id, $value))
  63. {
  64. continue;
  65. }
  66. $hdd = (new HardDisk($id, $value))->setPath($this->path);
  67. if (isset($config['bootdisk']))
  68. {
  69. $hdd->setMaster($config ['bootdisk'] == $id);
  70. }elseif ($bootDisk ){
  71. $hdd->setMaster($bootDisk == $id);
  72. }
  73. $this->fetch[$id] = $hdd;
  74. }
  75. krsort($this->fetch);
  76. return $this->fetch;
  77. }
  78. public function size()
  79. {
  80. $size = 0;
  81. foreach ($this->fetch() as $hdd)
  82. {
  83. $size += (int) $hdd->getSize();
  84. }
  85. return $size;
  86. }
  87. public function additionalSize()
  88. {
  89. $size = 0;
  90. foreach ($this->fetch() as $hd)
  91. {
  92. if ($hd->isMaster())
  93. {
  94. continue;
  95. }
  96. else if($this->excludeIds && in_array($hd->getId(), $this->excludeIds)){
  97. continue;
  98. }
  99. $size += (int) $hd->getGb();
  100. }
  101. return $size;
  102. }
  103. public function findById($id){
  104. foreach($this->fetch() as $entity){
  105. if($entity->getId()==$id){
  106. return $entity;
  107. }
  108. }
  109. throw new ProxmoxApiException("Hard Disk {$id} not found");
  110. }
  111. public function whereNotIn(array $excludeIds){
  112. $this->excludeIds = $excludeIds;
  113. return $this;
  114. }
  115. public function deleteUnused(){
  116. $this->reset();
  117. foreach ($this->fetch() as $disk){
  118. if(preg_match('/unused/', $disk->getId())){
  119. $disk->delete();
  120. }
  121. }
  122. }
  123. public function getBootOrder()
  124. {
  125. $config = $this->api()->get($this->path);
  126. //order=scsi0;ide1;scsi2
  127. if ($config['boot'] && preg_match("/order/", $config['boot'])) {
  128. list($key, $order) = explode("=", $config['boot']);
  129. return explode(";", $order);
  130. }
  131. $boot = array("c", "d", "n");
  132. if (isset($config['boot'])) {
  133. $boot = str_split($config['boot']);
  134. }
  135. return $boot;
  136. }
  137. }