| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /* * ********************************************************************
- * ProxmoxAddon product developed. (Apr 12, 2018)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- namespace MGProvision\Proxmox\v2\repository;
- use MGProvision\Proxmox\v2\models\HardDisk;
- use \MGProvision\Proxmox\v2\ProxmoxApiException;
- use \MGProvision\Proxmox\v2\models\MountPoint;
- /**
- * Description of MounPointRepostiory
- *
- * @author Pawel Kopec <pawelk@modulesgardne.com>
- */
- class HardDiskRepostiory extends AbstractRepository
- {
- protected $path;
- private $excludeIds=[];
- public function findByPath($path)
- {
- if (!preg_match('/config/', $path))
- {
- throw new ProxmoxApiException(sprintf("Hard Disk path ('%s') is not valid", $path));
- }
- $this->path = $path;
- return $this;
- }
- /**
- *
- * @return HardDisk[]
- * @throws ProxmoxApiException
- */
- public function fetch()
- {
- if ($this->fetch)
- {
- return $this->fetch;
- }
- if (empty($this->path))
- {
- throw new ProxmoxApiException("Hard Disk path is empty");
- }
- $this->fetch = [];
- $config = $this->api()->get($this->path);
- $boot = (array) $this->getBootOrder();
- $bootDisk = reset($boot );
- foreach ($config as $id => $value)
- {
- if (!HardDisk::isConfigValid($id, $value))
- {
- continue;
- }
- $hdd = (new HardDisk($id, $value))->setPath($this->path);
- if (isset($config['bootdisk']))
- {
- $hdd->setMaster($config ['bootdisk'] == $id);
- }elseif ($bootDisk ){
- $hdd->setMaster($bootDisk == $id);
- }
- $this->fetch[$id] = $hdd;
- }
- krsort($this->fetch);
- return $this->fetch;
- }
- public function size()
- {
- $size = 0;
- foreach ($this->fetch() as $hdd)
- {
- $size += (int) $hdd->getSize();
- }
- return $size;
- }
- public function additionalSize()
- {
- $size = 0;
- foreach ($this->fetch() as $hd)
- {
- if ($hd->isMaster())
- {
- continue;
- }
- else if($this->excludeIds && in_array($hd->getId(), $this->excludeIds)){
- continue;
- }
- $size += (int) $hd->getGb();
- }
- return $size;
- }
- public function findById($id){
- foreach($this->fetch() as $entity){
- if($entity->getId()==$id){
- return $entity;
- }
- }
- throw new ProxmoxApiException("Hard Disk {$id} not found");
- }
- public function whereNotIn(array $excludeIds){
- $this->excludeIds = $excludeIds;
- return $this;
- }
- public function deleteUnused(){
- $this->reset();
- foreach ($this->fetch() as $disk){
- if(preg_match('/unused/', $disk->getId())){
- $disk->delete();
- }
- }
- }
- public function getBootOrder()
- {
- $config = $this->api()->get($this->path);
- //order=scsi0;ide1;scsi2
- if ($config['boot'] && preg_match("/order/", $config['boot'])) {
- list($key, $order) = explode("=", $config['boot']);
- return explode(";", $order);
- }
- $boot = array("c", "d", "n");
- if (isset($config['boot'])) {
- $boot = str_split($config['boot']);
- }
- return $boot;
- }
- }
|