| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2016-10-11)
- * *
- *
- * 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\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 <pawelk@modulesgarden.com>
- * @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];
- }
- }
|