|
@@ -34,12 +34,12 @@ use ModulesGarden\ProxmoxAddon\App\Services\Utility;
|
|
|
*/
|
|
*/
|
|
|
class FileRepository extends AbstractRepository
|
|
class FileRepository extends AbstractRepository
|
|
|
{
|
|
{
|
|
|
- protected $content = null;
|
|
|
|
|
- protected $storages = array();
|
|
|
|
|
- protected $nodes = array();
|
|
|
|
|
|
|
+ protected $content = null;
|
|
|
|
|
+ protected $storages = array();
|
|
|
|
|
+ protected $nodes = array();
|
|
|
protected $vmid;
|
|
protected $vmid;
|
|
|
private $templateKvm;
|
|
private $templateKvm;
|
|
|
- private $vmids = array();
|
|
|
|
|
|
|
+ private $vmids = array();
|
|
|
protected $filterNotMatch = [];
|
|
protected $filterNotMatch = [];
|
|
|
protected $forceFetch = true;
|
|
protected $forceFetch = true;
|
|
|
protected $filterPattern;
|
|
protected $filterPattern;
|
|
@@ -51,7 +51,7 @@ class FileRepository extends AbstractRepository
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param VmInterface $vm
|
|
* @param VmInterface $vm
|
|
|
* @return \MGProvision\Proxmox\v2\repository\FileRepository
|
|
* @return \MGProvision\Proxmox\v2\repository\FileRepository
|
|
|
*/
|
|
*/
|
|
@@ -59,91 +59,81 @@ class FileRepository extends AbstractRepository
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
$this->findByVmid($vm->getVmid())
|
|
$this->findByVmid($vm->getVmid())
|
|
|
- ->findByNodes(array($vm->getNode()));
|
|
|
|
|
|
|
+ ->findByNodes(array($vm->getNode()));
|
|
|
|
|
|
|
|
$this->findByContent('backup');
|
|
$this->findByContent('backup');
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param VmModel[] $vservers
|
|
* @param VmModel[] $vservers
|
|
|
*/
|
|
*/
|
|
|
public function findBackupByVmModel($vservers)
|
|
public function findBackupByVmModel($vservers)
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
$nodes = array();
|
|
$nodes = array();
|
|
|
- foreach ($vservers as $vserver)
|
|
|
|
|
- {
|
|
|
|
|
- if (!$vserver instanceof VmModel)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ foreach ($vservers as $vserver) {
|
|
|
|
|
+ if (!$vserver instanceof VmModel) {
|
|
|
throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
|
|
throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
|
|
|
}
|
|
}
|
|
|
- $nodes[] = $vserver->node;
|
|
|
|
|
|
|
+ $nodes[] = $vserver->node;
|
|
|
$this->vmids[] = $vserver->vmid;
|
|
$this->vmids[] = $vserver->vmid;
|
|
|
}
|
|
}
|
|
|
$this->findByNodes($nodes)
|
|
$this->findByNodes($nodes)
|
|
|
- ->findByContent('backup');
|
|
|
|
|
|
|
+ ->findByContent('backup');
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @return File[]|TemplateKvm[]
|
|
* @return File[]|TemplateKvm[]
|
|
|
*/
|
|
*/
|
|
|
public function fetch()
|
|
public function fetch()
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
- if (!empty($this->fetch) || !$this->forceFetch)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (!empty($this->fetch) && !$this->forceFetch) {
|
|
|
return $this->fetch;
|
|
return $this->fetch;
|
|
|
}
|
|
}
|
|
|
- $request = array();
|
|
|
|
|
|
|
+ $request = array();
|
|
|
if ($this->content)
|
|
if ($this->content)
|
|
|
$request["content"] = $this->content;
|
|
$request["content"] = $this->content;
|
|
|
if ($this->vmid)
|
|
if ($this->vmid)
|
|
|
- $request["vmid"] = $this->vmid;
|
|
|
|
|
|
|
+ $request["vmid"] = $this->vmid;
|
|
|
|
|
|
|
|
- $i = 0;
|
|
|
|
|
|
|
+ $i = 0;
|
|
|
$this->fetch = array();
|
|
$this->fetch = array();
|
|
|
- foreach ($this->nodes as $node)
|
|
|
|
|
- {
|
|
|
|
|
- if (isset($this->vmids[$i]) && $this->vmids[$i])
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ foreach ($this->nodes as $node) {
|
|
|
|
|
+ if (isset($this->vmids[$i]) && $this->vmids[$i]) {
|
|
|
$request["vmid"] = $this->vmids[$i];
|
|
$request["vmid"] = $this->vmids[$i];
|
|
|
}
|
|
}
|
|
|
- foreach ($this->storages as $storage)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ foreach ($this->storages as $storage) {
|
|
|
$vmid = null;
|
|
$vmid = null;
|
|
|
- if($request["vmid"] && version_compare($this->api()->getVersion(), "5.4", '>=')){
|
|
|
|
|
|
|
+ if ($request["vmid"] && version_compare($this->api()->getVersion(), "5.4", '>=')) {
|
|
|
$vmid = $request["vmid"];
|
|
$vmid = $request["vmid"];
|
|
|
unset($request["vmid"]);
|
|
unset($request["vmid"]);
|
|
|
}
|
|
}
|
|
|
- $path = "/nodes/{$node}/storage/{$storage}/content";
|
|
|
|
|
|
|
+ $path = "/nodes/{$node}/storage/{$storage}/content";
|
|
|
$files = $this->api()->get($path, $request);
|
|
$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']))){
|
|
|
|
|
|
|
+ foreach ($files as $file) {
|
|
|
|
|
+ if (($vmid && $file['vmid'] && $file['vmid'] != $vmid) || ($vmid && !$file['vmid'] && !preg_match("/\-{$vmid}\-/", $file['volid']))) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
- if ($this->filterNotMatch && preg_match("/{$this->filterNotMatch['volid']}/", $file['volid']))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if ($this->filterNotMatch && preg_match("/{$this->filterNotMatch['volid']}/", $file['volid'])) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
- if ($this->filterPattern && !preg_match($this->filterPattern, $file['volid']))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (!empty($this->filterPattern) && !$this->matchFilterPattern($file)) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if ($this->templateKvm)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if ($this->templateKvm) {
|
|
|
$entityObj = $this->formatTemplateKvm($node, $file);
|
|
$entityObj = $this->formatTemplateKvm($node, $file);
|
|
|
if (!$entityObj instanceof TemplateKvm)
|
|
if (!$entityObj instanceof TemplateKvm)
|
|
|
continue;
|
|
continue;
|
|
|
- }else
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else {
|
|
|
$entityObj = new File();
|
|
$entityObj = new File();
|
|
|
$entityObj->setAttributes($file);
|
|
$entityObj->setAttributes($file);
|
|
|
|
|
+ $entityObj->setNode($node);
|
|
|
}
|
|
}
|
|
|
$entityObj->setPath(sprintf("{$path}/%s", $file['volid']));
|
|
$entityObj->setPath(sprintf("{$path}/%s", $file['volid']));
|
|
|
$this->fetch[] = $entityObj;
|
|
$this->fetch[] = $entityObj;
|
|
@@ -162,9 +152,8 @@ class FileRepository extends AbstractRepository
|
|
|
|
|
|
|
|
public function size()
|
|
public function size()
|
|
|
{
|
|
{
|
|
|
- $size = (float) 0;
|
|
|
|
|
- foreach ($this->fetch() as $entityObj)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ $size = (float)0;
|
|
|
|
|
+ foreach ($this->fetch() as $entityObj) {
|
|
|
$size += $entityObj->getSize();
|
|
$size += $entityObj->getSize();
|
|
|
}
|
|
}
|
|
|
return $size;
|
|
return $size;
|
|
@@ -176,53 +165,45 @@ class FileRepository extends AbstractRepository
|
|
|
*/
|
|
*/
|
|
|
public function getSizeInGb()
|
|
public function getSizeInGb()
|
|
|
{
|
|
{
|
|
|
- $size = $this->size();
|
|
|
|
|
|
|
+ $size = $this->size();
|
|
|
Utility::unitFormat($size, "bytes", "gb");
|
|
Utility::unitFormat($size, "bytes", "gb");
|
|
|
return $size;
|
|
return $size;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
private function formatTemplateKvm($node, array $file)
|
|
private function formatTemplateKvm($node, array $file)
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
- if (empty($file['vmid']))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (empty($file['vmid'])) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
|
|
|
- $vmid = $file['vmid'];
|
|
|
|
|
|
|
+ $vmid = $file['vmid'];
|
|
|
$status = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/status/current", array());
|
|
$status = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/status/current", array());
|
|
|
- if ($status['template'] != '1')
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if ($status['template'] != '1') {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
$config = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/config", array());
|
|
$config = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/config", array());
|
|
|
- if (empty($config))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (empty($config)) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- $attributes = array();
|
|
|
|
|
|
|
+ $attributes = array();
|
|
|
$attributes['node'] = $node;
|
|
$attributes['node'] = $node;
|
|
|
- $attributes = array_merge($attributes, $file);
|
|
|
|
|
- $attributes = array_merge($attributes, $config);
|
|
|
|
|
- $template = new TemplateKvm();
|
|
|
|
|
|
|
+ $attributes = array_merge($attributes, $file);
|
|
|
|
|
+ $attributes = array_merge($attributes, $config);
|
|
|
|
|
+ $template = new TemplateKvm();
|
|
|
$template->setAttributes($attributes);
|
|
$template->setAttributes($attributes);
|
|
|
return $template;
|
|
return $template;
|
|
|
- }
|
|
|
|
|
- catch (\Exception $ex)
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function fetchAsArray()
|
|
public function fetchAsArray()
|
|
|
{
|
|
{
|
|
|
$data = array();
|
|
$data = array();
|
|
|
- foreach ($this->fetch() as $entity)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ foreach ($this->fetch() as $entity) {
|
|
|
$data[$entity->getVolid()] = $entity->getFriendlyName();
|
|
$data[$entity->getVolid()] = $entity->getFriendlyName();
|
|
|
}
|
|
}
|
|
|
return $data;
|
|
return $data;
|
|
@@ -242,7 +223,7 @@ class FileRepository extends AbstractRepository
|
|
|
|
|
|
|
|
public function findKvmTemplates()
|
|
public function findKvmTemplates()
|
|
|
{
|
|
{
|
|
|
- $this->content = 'images';
|
|
|
|
|
|
|
+ $this->content = 'images';
|
|
|
$this->templateKvm = true;
|
|
$this->templateKvm = true;
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
@@ -259,7 +240,7 @@ class FileRepository extends AbstractRepository
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function findByNodes(array $nodes)
|
|
|
|
|
|
|
+ public function findByNodes(array $nodes)
|
|
|
{
|
|
{
|
|
|
$this->nodes = $nodes;
|
|
$this->nodes = $nodes;
|
|
|
return $this;
|
|
return $this;
|
|
@@ -275,60 +256,58 @@ class FileRepository extends AbstractRepository
|
|
|
{
|
|
{
|
|
|
$this->reset();
|
|
$this->reset();
|
|
|
$backups = $this->fetch();
|
|
$backups = $this->fetch();
|
|
|
- foreach ($backups as $b)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ foreach ($backups as $b) {
|
|
|
$b->delete();
|
|
$b->delete();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @return File|TemplateKvm|null
|
|
* @return File|TemplateKvm|null
|
|
|
*/
|
|
*/
|
|
|
public function fetchLast()
|
|
public function fetchLast()
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
$file = null;
|
|
$file = null;
|
|
|
- foreach ($this->fetch() as $t)
|
|
|
|
|
- {
|
|
|
|
|
- if ($file === null)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ foreach ($this->fetch() as $t) {
|
|
|
|
|
+ if ($file === null) {
|
|
|
$file = $t;
|
|
$file = $t;
|
|
|
continue;
|
|
continue;
|
|
|
- }
|
|
|
|
|
- else if ($t->getTimestamp() > $file->getTimestamp())
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else if ($t->getTimestamp() > $file->getTimestamp()) {
|
|
|
$file = $t;
|
|
$file = $t;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return $file;
|
|
return $file;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function findVolidNotIn(array $volids){
|
|
|
|
|
|
|
+ public function findVolidNotIn(array $volids)
|
|
|
|
|
+ {
|
|
|
$this->filterNotMatch['volid'] = $volids;
|
|
$this->filterNotMatch['volid'] = $volids;
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function sortByTime(){
|
|
|
|
|
|
|
+ public function sortByTime()
|
|
|
|
|
+ {
|
|
|
$keys = [];
|
|
$keys = [];
|
|
|
$orginal = $this->fetch();
|
|
$orginal = $this->fetch();
|
|
|
- foreach ($orginal as $key => $entity){
|
|
|
|
|
|
|
+ foreach ($orginal as $key => $entity) {
|
|
|
$keys[$key] = $entity->getTimestamp();
|
|
$keys[$key] = $entity->getTimestamp();
|
|
|
}
|
|
}
|
|
|
- uasort($keys, function($a, $b){
|
|
|
|
|
|
|
+ uasort($keys, function ($a, $b) {
|
|
|
if ($a == $b) {
|
|
if ($a == $b) {
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
return ($a < $b) ? -1 : 1;
|
|
return ($a < $b) ? -1 : 1;
|
|
|
});
|
|
});
|
|
|
- $this->fetch=[];
|
|
|
|
|
- foreach ($keys as $id => $entity){
|
|
|
|
|
|
|
+ $this->fetch = [];
|
|
|
|
|
+ foreach ($keys as $id => $entity) {
|
|
|
$this->fetch[$id] = $orginal[$id];
|
|
$this->fetch[$id] = $orginal[$id];
|
|
|
}
|
|
}
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function remove($id){
|
|
|
|
|
|
|
+ public function remove($id)
|
|
|
|
|
+ {
|
|
|
unset($this->fetch[$id]);
|
|
unset($this->fetch[$id]);
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
@@ -339,19 +318,62 @@ class FileRepository extends AbstractRepository
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function findSnippetsByServiceId($serviceId){
|
|
|
|
|
|
|
+ public function findSnippetsByServiceId($serviceId)
|
|
|
|
|
+ {
|
|
|
$this->findSnippets();
|
|
$this->findSnippets();
|
|
|
- $this->filterPattern = "/userconfig\-{$serviceId}\.yaml/";
|
|
|
|
|
|
|
+ $this->filterPattern = [
|
|
|
|
|
+ "/userconfig\-{$serviceId}\.yaml/",
|
|
|
|
|
+ "/meta\-{$serviceId}\.yaml/",
|
|
|
|
|
+ "/network\-{$serviceId}\.yaml/",
|
|
|
|
|
+ ];
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function findSnippetsByVmModel(VmModel $vmModel){
|
|
|
|
|
|
|
+ public function findSnippetsByVmModel(VmModel $vmModel)
|
|
|
|
|
+ {
|
|
|
$this->findSnippets();
|
|
$this->findSnippets();
|
|
|
- $this->filterPattern = "/userconfig\-{$vmModel->hosting_id}\-{$vmModel->id}\.yaml/";
|
|
|
|
|
|
|
+ $this->filterPattern = [
|
|
|
|
|
+ "/userconfig\-{$vmModel->hosting_id}\-{$vmModel->id}\.yaml/",
|
|
|
|
|
+ "/meta\-{$vmModel->hosting_id}\-{$vmModel->id}\.yaml/",
|
|
|
|
|
+ "/network\-{$vmModel->hosting_id}\-{$vmModel->id}\.yaml/",
|
|
|
|
|
+ ];
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function first(){
|
|
|
|
|
|
|
+ public function first()
|
|
|
|
|
+ {
|
|
|
return $this->fetch()[0];
|
|
return $this->fetch()[0];
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public function findBackupQemuTemplates()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->findByContent('backup');
|
|
|
|
|
+ $this->filterPattern = "/qemu/";
|
|
|
|
|
+ $this->forceFetch = true;
|
|
|
|
|
+ return $this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function findBackupLxcTemplates()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->findByContent('backup');
|
|
|
|
|
+ $this->filterPattern = "/lxc/";
|
|
|
|
|
+ $this->forceFetch = true;
|
|
|
|
|
+ return $this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function matchFilterPattern($file)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (is_string($this->filterPattern)) {
|
|
|
|
|
+ return preg_match($this->filterPattern, $file['volid']);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (is_array($this->filterPattern)) {
|
|
|
|
|
+ foreach ($this->filterPattern as $pattern) {
|
|
|
|
|
+ if (preg_match($pattern, $file['volid'])) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|