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\interfaces\VmInterface; Use MGProvision\Proxmox\v2\models\File; use MGProvision\Proxmox\v2\models\TemplateKvm; use ModulesGarden\ProxmoxAddon\App\Models\VmModel; use ModulesGarden\ProxmoxAddon\App\Services\Utility; /** * Description of FileRepository * * @author Pawel Kopec * @version 1.0.0 */ class FileRepository extends AbstractRepository { protected $content = null; protected $storages = array(); protected $nodes = array(); protected $vmid; private $templateKvm; private $vmids = array(); protected $filterNotMatch = []; protected $forceFetch = true; protected $filterPattern; public function findByVmid($vmid) { $this->vmid = $vmid; return $this; } /** * * @param VmInterface $vm * @return \MGProvision\Proxmox\v2\repository\FileRepository */ public function findBackup(VmInterface $vm) { $this->findByVmid($vm->getVmid()) ->findByNodes(array($vm->getNode())); $this->findByContent('backup'); return $this; } /** * * @param VmModel[] $vservers */ public function findBackupByVmModel($vservers) { $nodes = array(); foreach ($vservers as $vserver) { if (!$vserver instanceof VmModel) { throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver)); } $nodes[] = $vserver->node; $this->vmids[] = $vserver->vmid; } $this->findByNodes($nodes) ->findByContent('backup'); return $this; } /** * * @return File[]|TemplateKvm[] */ public function fetch() { if (!empty($this->fetch) || !$this->forceFetch) { return $this->fetch; } $request = array(); if ($this->content) $request["content"] = $this->content; if ($this->vmid) $request["vmid"] = $this->vmid; $i = 0; $this->fetch = array(); foreach ($this->nodes as $node) { if (isset($this->vmids[$i]) && $this->vmids[$i]) { $request["vmid"] = $this->vmids[$i]; } foreach ($this->storages as $storage) { $vmid = null; if($request["vmid"] && version_compare($this->api()->getVersion(), "5.4", '>=')){ $vmid = $request["vmid"]; unset($request["vmid"]); } $path = "/nodes/{$node}/storage/{$storage}/content"; $files = $this->api()->get($path, $request); foreach ($files as $file) { if(($vmid && $file['vmid'] && $file['vmid'] != $vmid ) || ($vmid && !$file['vmid'] && !preg_match("/\-{$vmid}\-/", $file['volid']))){ continue; } if ($this->filterNotMatch && preg_match("/{$this->filterNotMatch['volid']}/", $file['volid'])) { continue; } if ($this->filterPattern && !preg_match($this->filterPattern, $file['volid'])) { continue; } if ($this->templateKvm) { $entityObj = $this->formatTemplateKvm($node, $file); if (!$entityObj instanceof TemplateKvm) continue; }else { $entityObj = new File(); $entityObj->setAttributes($file); } $entityObj->setPath(sprintf("{$path}/%s", $file['volid'])); $this->fetch[] = $entityObj; } } $i++; } $this->forceFetch = false; return $this->fetch; } public function count() { return count($this->fetch()); } public function size() { $size = (float) 0; foreach ($this->fetch() as $entityObj) { $size += $entityObj->getSize(); } return $size; } /** * @return float * @throws \Exception */ public function getSizeInGb() { $size = $this->size(); Utility::unitFormat($size, "bytes", "gb"); return $size; } private function formatTemplateKvm($node, array $file) { if (empty($file['vmid'])) { return; } try { $vmid = $file['vmid']; $status = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/status/current", array()); if ($status['template'] != '1') { return; } $config = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/config", array()); if (empty($config)) { return; } $attributes = array(); $attributes['node'] = $node; $attributes = array_merge($attributes, $file); $attributes = array_merge($attributes, $config); $template = new TemplateKvm(); $template->setAttributes($attributes); return $template; } catch (\Exception $ex) { } } public function fetchAsArray() { $data = array(); foreach ($this->fetch() as $entity) { $data[$entity->getVolid()] = $entity->getFriendlyName(); } return $data; } public function findByContent($content) { $this->content = $content; return $this; } public function findLxcTemplates() { $this->content = 'vztmpl'; return $this; } public function findKvmTemplates() { $this->content = 'images'; $this->templateKvm = true; return $this; } public function findIso() { $this->content = 'iso'; return $this; } public function findByStorages($storages) { $this->storages = $storages; return $this; } public function findByNodes(array $nodes) { $this->nodes = $nodes; return $this; } public function findByNode(\MGProvision\Proxmox\v2\models\Node $node) { $this->nodes = array($node->getNode()); return $this; } public function delete() { $this->reset(); $backups = $this->fetch(); foreach ($backups as $b) { $b->delete(); } } /** * * @return File|TemplateKvm|null */ public function fetchLast() { $file = null; foreach ($this->fetch() as $t) { if ($file === null) { $file = $t; continue; } else if ($t->getTimestamp() > $file->getTimestamp()) { $file = $t; } } return $file; } public function findVolidNotIn(array $volids){ $this->filterNotMatch['volid'] = $volids; return $this; } public function sortByTime(){ $keys = []; $orginal = $this->fetch(); foreach ($orginal as $key => $entity){ $keys[$key] = $entity->getTimestamp(); } uasort($keys, function($a, $b){ if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); $this->fetch=[]; foreach ($keys as $id => $entity){ $this->fetch[$id] = $orginal[$id]; } return $this; } public function remove($id){ unset($this->fetch[$id]); return $this; } public function findSnippets() { $this->content = 'snippets'; return $this; } public function findSnippetsByServiceId($serviceId){ $this->findSnippets(); $this->filterPattern = "/userconfig\-{$serviceId}\.yaml/"; return $this; } public function findSnippetsByVmModel(VmModel $vmModel){ $this->findSnippets(); $this->filterPattern = "/userconfig\-{$vmModel->hosting_id}\-{$vmModel->id}\.yaml/"; return $this; } public function first(){ return $this->fetch()[0]; } }