MountPoint.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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\models;
  20. /**
  21. * Description of MounPoint
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgardne.com>
  24. */
  25. class MountPoint extends AbstractObject
  26. {
  27. protected $id;
  28. protected $path;
  29. protected $mp;
  30. protected $size;
  31. protected $acl;
  32. protected $backup;
  33. protected $quota;
  34. protected $replicate;
  35. protected $ro;
  36. protected $location;
  37. protected $name;
  38. protected $gb;
  39. protected $storage;
  40. public function __construct($id, $config = null)
  41. {
  42. $this->id = $id;
  43. if ($config)
  44. {
  45. $config = self::asArray($config);
  46. $config['location'] = key($config);
  47. $ex = explode(":", $config['location'] );
  48. $config['storage'] = $ex[0];
  49. $this->setAttributes($config);
  50. if(!$config['size']){
  51. $this->setSize($ex[1]."G");
  52. }
  53. if ($id == 'rootfs')
  54. {
  55. $this->setBackup(1);
  56. }
  57. }
  58. }
  59. public function getId()
  60. {
  61. return $this->id;
  62. }
  63. public function getPath()
  64. {
  65. return $this->path;
  66. }
  67. public function getLocation()
  68. {
  69. return $this->location;
  70. }
  71. public function getName()
  72. {
  73. if($this->getId()=="rootfs"){
  74. return $this->name = "Root Disk";
  75. }
  76. preg_match('/disk-[0-9]/', $this->getLocation(), $matches);
  77. $matches['0'] = str_replace(['-'], [" "], $matches['0']);
  78. return $this->name = ucfirst($matches['0']);
  79. }
  80. public function setLocation($location)
  81. {
  82. $this->location = $location;
  83. return $this;
  84. }
  85. public function setName($name)
  86. {
  87. $this->name = $name;
  88. return $this;
  89. }
  90. public function setPath($path)
  91. {
  92. $this->path = $path;
  93. return $this;
  94. }
  95. public function getMp()
  96. {
  97. return $this->mp;
  98. }
  99. public function getSize()
  100. {
  101. return $this->size;
  102. }
  103. public function getAcl()
  104. {
  105. return $this->acl;
  106. }
  107. public function getBackup()
  108. {
  109. return $this->backup;
  110. }
  111. public function isBackup()
  112. {
  113. return $this->backup==1;
  114. }
  115. public function getQuota()
  116. {
  117. return $this->quota;
  118. }
  119. public function getReplicate()
  120. {
  121. return $this->replicate;
  122. }
  123. public function getRo()
  124. {
  125. return $this->ro;
  126. }
  127. public function setMp($mp)
  128. {
  129. $this->mp = $mp;
  130. return $this;
  131. }
  132. public function setSize($size)
  133. {
  134. $this->size = $size;
  135. return $this;
  136. }
  137. public function setAcl($acl)
  138. {
  139. $this->acl = $acl;
  140. return $this;
  141. }
  142. public function setBackup($backup)
  143. {
  144. $this->backup = $backup;
  145. return $this;
  146. }
  147. public function setQuota($quota)
  148. {
  149. $this->quota = $quota;
  150. return $this;
  151. }
  152. public function setReplicate($replicate)
  153. {
  154. $this->replicate = $replicate;
  155. return $this;
  156. }
  157. public function setRo($ro)
  158. {
  159. $this->ro = $ro;
  160. return $this;
  161. }
  162. public function getBytes()
  163. {
  164. if(preg_match('/K/', $this->getSize())){//KB => Bytes
  165. $size = (int) $this->getSize();
  166. return $size * 1024 ;
  167. }
  168. elseif(preg_match('/M/', $this->getSize())){//MB => Bytes
  169. $size = (int) $this->getSize();
  170. return $size * pow(1024,2);
  171. }
  172. else if(preg_match('/T/', $this->getSize())){//T => Bytes
  173. $size = (int) $this->getSize();
  174. return $size * pow(1024,4);
  175. }
  176. else{//GB => Bytes
  177. $size = (int) $this->getSize();
  178. return $size * pow(1024,3);
  179. }
  180. }
  181. public function getGb()
  182. {
  183. if(preg_match('/M/', $this->getSize())){//MB => GB
  184. $size = (int) $this->getSize();
  185. return $size / 1024;
  186. }
  187. else if(preg_match('/T/', $this->getSize())){//T => GB
  188. $size = (int) $this->getSize();
  189. return $size * 1024;
  190. }
  191. else{//GB
  192. return (int) $this->getSize();
  193. }
  194. }
  195. /**
  196. * @param mixed $id
  197. */
  198. public function setId($id)
  199. {
  200. $this->id = $id;
  201. return $this;
  202. }
  203. public function resize($size)
  204. {
  205. $path = str_replace('config', 'resize', $this->path);
  206. return $this->api()->put($path, ['disk' => $this->id, 'size' => $size]);
  207. }
  208. public function asConfig()
  209. {
  210. $config = [$this->location];
  211. foreach (['mp', 'acl', 'backup', 'quota', 'replicate', 'ro'] as $k)
  212. {
  213. if (!is_null($this->{$k}))
  214. {
  215. $config[] = "{$k}=" . $this->{$k};
  216. }
  217. }
  218. return implode(",", $config);
  219. }
  220. public function update()
  221. {
  222. return $this->api()->put($this->path, [$this->id => $this->asConfig()]);
  223. }
  224. public function create()
  225. {
  226. return $this->api()->put($this->path, [$this->id => $this->asConfig()]);
  227. }
  228. public function delete()
  229. {
  230. return $this->api()->put($this->path, ["delete" => $this->id]);
  231. }
  232. public function isMaster(){
  233. return $this->getId() == "rootfs";
  234. }
  235. /**
  236. * @return mixed
  237. */
  238. public function getStorage()
  239. {
  240. return $this->storage;
  241. }
  242. /**
  243. * @param mixed $storage
  244. * @return MountPoint
  245. */
  246. public function setStorage($storage)
  247. {
  248. $this->storage = $storage;
  249. return $this;
  250. }
  251. public function move($delete=0){
  252. $config = [
  253. "volume" => $this->getId(),
  254. 'storage' => $this->getStorage(),
  255. 'delete' => $delete
  256. ];
  257. $path = str_replace("/config", "/move_volume", $this->getPath());
  258. return $this->api()->post($path ,$config );
  259. }
  260. }