HardDisk.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2017-07-18)
  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 HardDisk
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgarden.com>
  24. * @version 1.0.0
  25. */
  26. class HardDisk extends AbstractObject{
  27. protected $id;
  28. protected $location;
  29. protected $storage;
  30. protected $cache;
  31. protected $discard;
  32. protected $media;
  33. protected $replicate;
  34. protected $format;
  35. protected $backup;
  36. protected $mbps_rd;
  37. protected $mbps_rd_max;
  38. protected $mbps_wr;
  39. protected $mbps_wr_max;
  40. protected $iops_rd;
  41. protected $iops_rd_max;
  42. protected $iops_wr;
  43. protected $iops_wr_max;
  44. protected $size;
  45. protected $iothread;
  46. protected $ssd;
  47. protected $path;
  48. private $master;
  49. public function __construct($id, $config=null) {
  50. $this->id = $id;
  51. if ($config) {
  52. $config = self::asArray($config);
  53. $config['location'] = key($config);
  54. $ex = explode(":", key($config));
  55. $config['storage'] = $ex[0];
  56. $this->setAttributes($config);
  57. }
  58. }
  59. public function getId(){
  60. return $this->id;
  61. }
  62. /**
  63. * @param mixed $id
  64. */
  65. public function setId($id)
  66. {
  67. $this->id = $id;
  68. return $this;
  69. }
  70. public function setMaster($master){
  71. $this->master = $master;
  72. return $this;
  73. }
  74. public function isMaster(){
  75. if(is_bool($this->master)){
  76. return $this->master;
  77. }
  78. return stripos($this->getLocation(), 'disk-0') !== false;
  79. }
  80. public function getName(){
  81. $matches = array();
  82. preg_match('/disk-[0-9]*/', $this->getLocation(), $matches);
  83. if ($matches[0]){
  84. return $matches[0];
  85. }
  86. }
  87. public function getLocation() {
  88. return $this->location;
  89. }
  90. public function getStorage(){
  91. return $this->storage;
  92. }
  93. public function getFormat() {
  94. if(!$this->format && preg_match("/\./", $this->location)){
  95. $ex = explode(".", $this->location);
  96. return end($ex);
  97. }
  98. return $this->format;
  99. }
  100. public function getBackup() {
  101. return $this->backup;
  102. }
  103. public function isBackup() {
  104. return is_null($this->backup);
  105. }
  106. public function getCache() {
  107. return $this->cache;
  108. }
  109. public function getMbps_rd() {
  110. return $this->mbps_rd;
  111. }
  112. public function getMbps_rd_max() {
  113. return $this->mbps_rd_max;
  114. }
  115. public function getMbps_wr() {
  116. return $this->mbps_wr;
  117. }
  118. public function getMbps_wr_max() {
  119. return $this->mbps_wr_max;
  120. }
  121. public function getIops_rd() {
  122. return $this->iops_rd;
  123. }
  124. public function getIops_rd_max() {
  125. return $this->iops_rd_max;
  126. }
  127. public function getIops_wr() {
  128. return $this->iops_wr;
  129. }
  130. public function getIops_wr_max() {
  131. return $this->iops_wr_max;
  132. }
  133. public function getSize() {
  134. return $this->size;
  135. }
  136. public function setLocation($location) {
  137. $this->location = $location;
  138. return $this;
  139. }
  140. /**
  141. * @param mixed $storage
  142. */
  143. public function setStorage($storage)
  144. {
  145. $this->storage = $storage;
  146. return $this;
  147. }
  148. public function setFormat($format) {
  149. $this->format = $format;
  150. return $this;
  151. }
  152. public function setBackup($backup) {
  153. $this->backup = $backup;
  154. return $this;
  155. }
  156. public function setCache($cache) {
  157. $this->cache = $cache;
  158. return $this;
  159. }
  160. public function setMbps_rd($mbps_rd) {
  161. $this->mbps_rd = $mbps_rd;
  162. return $this;
  163. }
  164. public function setMbps_rd_max($mbps_rd_max) {
  165. $this->mbps_rd_max = $mbps_rd_max;
  166. return $this;
  167. }
  168. public function setMbps_wr($mbps_wr) {
  169. $this->mbps_wr = $mbps_wr;
  170. return $this;
  171. }
  172. public function setMbps_wr_max($mbps_wr_max) {
  173. $this->mbps_wr_max = $mbps_wr_max;
  174. return $this;
  175. }
  176. public function setIops_rd($iops_rd) {
  177. $this->iops_rd = $iops_rd;
  178. return $this;
  179. }
  180. public function setIops_rd_max($iops_rd_max) {
  181. $this->iops_rd_max = $iops_rd_max;
  182. return $this;
  183. }
  184. public function setIops_wr($iops_wr) {
  185. $this->iops_wr = $iops_wr;
  186. return $this;
  187. }
  188. public function setIops_wr_max($iops_wr_max) {
  189. $this->iops_wr_max = $iops_wr_max;
  190. return $this;
  191. }
  192. public function setSize($size) {
  193. $this->size = $size;
  194. return $this;
  195. }
  196. public function getDiscard() {
  197. return $this->discard;
  198. }
  199. public function getMedia() {
  200. return $this->media;
  201. }
  202. public function getReplicate() {
  203. return $this->replicate;
  204. }
  205. public function setDiscard($discard) {
  206. $this->discard = $discard;
  207. return $this;
  208. }
  209. public function setMedia($media) {
  210. $this->media = $media;
  211. return $this;
  212. }
  213. public function setReplicate($replicate) {
  214. $this->replicate = $replicate;
  215. return $this;
  216. }
  217. public function getBytes(){
  218. if(preg_match('/K/', $this->getSize())){//KB => Bytes
  219. $size = (int) $this->getSize();
  220. return $size * 1024 ;
  221. }
  222. elseif(preg_match('/M/', $this->getSize())){//MB => Bytes
  223. $size = (int) $this->getSize();
  224. return $size * pow(1024,2);
  225. }
  226. else if(preg_match('/T/', $this->getSize())){//T => Bytes
  227. $size = (int) $this->getSize();
  228. return $size * pow(1024,4);
  229. }
  230. else{//GB => Bytes
  231. $size = (int) $this->getSize();
  232. return $size * pow(1024,3);
  233. }
  234. }
  235. public function getGb(){
  236. if(preg_match('/M/', $this->getSize())){//MB => GB
  237. $size = (int) $this->getSize();
  238. return $size / 1024;
  239. }
  240. else if(preg_match('/T/', $this->getSize())){//T => GB
  241. $size = (int) $this->getSize();
  242. return $size * 1024;
  243. }
  244. else{//GB
  245. return (int) $this->getSize();
  246. }
  247. }
  248. public function formatBytes(){//Bytes => GB
  249. return $this->getBytes() / pow(1024,3);
  250. }
  251. public function asConfig(){
  252. $fields=['cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max',
  253. 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max','size', 'iothread','ssd'];
  254. $config = [];
  255. if($this->getLocation()){
  256. $config[]= $this->getLocation();
  257. }else{
  258. $config[]= "{$this->getStorage()}:{$this->getSize()}";
  259. unset($fields[array_search("size", $fields)]);
  260. }
  261. $this->toConfig($fields, $config);
  262. return implode(",", $config);
  263. }
  264. public function getPath()
  265. {
  266. return $this->path;
  267. }
  268. public function setPath($path)
  269. {
  270. $this->path = $path;
  271. return $this;
  272. }
  273. public function delete(){
  274. return $this->api()->put($this->getPath(), ["delete"=> $this->getId()]);
  275. }
  276. public function create(){
  277. $config = [];
  278. $config[] = $this->getStorage().":".$this->getSize();
  279. $fields=['cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max',
  280. 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max', 'iothread'];
  281. $this->toConfig($fields, $config);
  282. $prameters = implode(",", $config);
  283. return $this->api()->put($this->getPath(), [$this->getId()=> $prameters]);
  284. }
  285. public function getType(){
  286. return preg_replace('/[0-9]+/', '', $this->getId());
  287. }
  288. public function getIothread()
  289. {
  290. return $this->iothread;
  291. }
  292. public function setIothread($iothread)
  293. {
  294. $this->iothread = $iothread;
  295. return $this;
  296. }
  297. /**
  298. * @return mixed
  299. */
  300. public function getSsd()
  301. {
  302. return $this->ssd;
  303. }
  304. /**
  305. * @param mixed $ssd
  306. * @return HardDisk
  307. */
  308. public function setSsd($ssd)
  309. {
  310. $this->ssd = $ssd;
  311. return $this;
  312. }
  313. public function getAttributes(){
  314. $fields=['cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max',
  315. 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max','size', 'iothread', 'ssd'];
  316. $config=[];
  317. foreach($fields as $field)
  318. {
  319. $methodName = 'get'.ucfirst($field);
  320. if(method_exists($this,$methodName ) && isset( $this->{$field})){
  321. $config[$field]=$this->{$methodName}();
  322. }
  323. }
  324. return $config;
  325. }
  326. public function update(){
  327. $config = [];
  328. $config[]= $this->getLocation();
  329. $fields=[ 'cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max',
  330. 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max', 'iothread','ssd'];
  331. $this->toConfig($fields, $config);
  332. $prameters = implode(",", $config);
  333. return $this->api()->put($this->getPath(), [$this->getId() => $prameters]);
  334. }
  335. public function resize($size)
  336. {
  337. $setting = [
  338. "disk" => $this->getId(),
  339. "size" => $size,
  340. ];
  341. $path = str_replace('config', 'resize', $this->getPath());
  342. return $this->api()->put($path, $setting);
  343. }
  344. public function getBus(){
  345. return preg_replace("/[0-9]+/", "", $this->getId());
  346. }
  347. public static function isConfigValid($id, $config){
  348. $bus = [ "ide",'sata', 'virtio', 'scsi'];
  349. $invalid = ['hotplug' ,'agent'];
  350. if (in_array($id, $invalid))
  351. {
  352. return false;
  353. }
  354. if (preg_match('/efidisk/', $config))
  355. {
  356. return false;
  357. }
  358. if (preg_match('/unused/', $id))
  359. {
  360. return false;
  361. }
  362. if (!preg_match('/disk/', $config))
  363. {
  364. foreach ($bus as $b){
  365. if(preg_match("/{$b}\d/", $id) && !preg_match('/cdrom/', $config) ){
  366. return true;
  367. }
  368. }
  369. return false;
  370. }
  371. return true;
  372. }
  373. }