FileRepository.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-11)
  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\repository;
  20. use MGProvision\Proxmox\v2\interfaces\VmInterface;
  21. Use MGProvision\Proxmox\v2\models\File;
  22. use MGProvision\Proxmox\v2\models\TemplateKvm;
  23. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  25. /**
  26. * Description of FileRepository
  27. *
  28. * @author Pawel Kopec <pawelk@modulesgarden.com>
  29. * @version 1.0.0
  30. */
  31. class FileRepository extends AbstractRepository
  32. {
  33. protected $content = null;
  34. protected $storages = array();
  35. protected $nodes = array();
  36. protected $vmid;
  37. private $templateKvm;
  38. private $vmids = array();
  39. protected $filterNotMatch = [];
  40. protected $forceFetch = true;
  41. protected $filterPattern;
  42. public function findByVmid($vmid)
  43. {
  44. $this->vmid = $vmid;
  45. return $this;
  46. }
  47. /**
  48. *
  49. * @param VmInterface $vm
  50. * @return \MGProvision\Proxmox\v2\repository\FileRepository
  51. */
  52. public function findBackup(VmInterface $vm)
  53. {
  54. $this->findByVmid($vm->getVmid())
  55. ->findByNodes(array($vm->getNode()));
  56. $this->findByContent('backup');
  57. return $this;
  58. }
  59. /**
  60. *
  61. * @param VmModel[] $vservers
  62. */
  63. public function findBackupByVmModel($vservers)
  64. {
  65. $nodes = array();
  66. foreach ($vservers as $vserver)
  67. {
  68. if (!$vserver instanceof VmModel)
  69. {
  70. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
  71. }
  72. $nodes[] = $vserver->node;
  73. $this->vmids[] = $vserver->vmid;
  74. }
  75. $this->findByNodes($nodes)
  76. ->findByContent('backup');
  77. return $this;
  78. }
  79. /**
  80. *
  81. * @return File[]|TemplateKvm[]
  82. */
  83. public function fetch()
  84. {
  85. if (!empty($this->fetch) || !$this->forceFetch)
  86. {
  87. return $this->fetch;
  88. }
  89. $request = array();
  90. if ($this->content)
  91. $request["content"] = $this->content;
  92. if ($this->vmid)
  93. $request["vmid"] = $this->vmid;
  94. $i = 0;
  95. $this->fetch = array();
  96. foreach ($this->nodes as $node)
  97. {
  98. if (isset($this->vmids[$i]) && $this->vmids[$i])
  99. {
  100. $request["vmid"] = $this->vmids[$i];
  101. }
  102. foreach ($this->storages as $storage)
  103. {
  104. $vmid = null;
  105. if($request["vmid"] && version_compare($this->api()->getVersion(), "5.4", '>=')){
  106. $vmid = $request["vmid"];
  107. unset($request["vmid"]);
  108. }
  109. $path = "/nodes/{$node}/storage/{$storage}/content";
  110. $files = $this->api()->get($path, $request);
  111. foreach ($files as $file)
  112. {
  113. if(($vmid && $file['vmid'] && $file['vmid'] != $vmid ) || ($vmid && !$file['vmid'] && !preg_match("/\-{$vmid}\-/", $file['volid']))){
  114. continue;
  115. }
  116. if ($this->filterNotMatch && preg_match("/{$this->filterNotMatch['volid']}/", $file['volid']))
  117. {
  118. continue;
  119. }
  120. if ($this->filterPattern && !preg_match($this->filterPattern, $file['volid']))
  121. {
  122. continue;
  123. }
  124. if ($this->templateKvm)
  125. {
  126. $entityObj = $this->formatTemplateKvm($node, $file);
  127. if (!$entityObj instanceof TemplateKvm)
  128. continue;
  129. }else
  130. {
  131. $entityObj = new File();
  132. $entityObj->setAttributes($file);
  133. }
  134. $entityObj->setPath(sprintf("{$path}/%s", $file['volid']));
  135. $this->fetch[] = $entityObj;
  136. }
  137. }
  138. $i++;
  139. }
  140. $this->forceFetch = false;
  141. return $this->fetch;
  142. }
  143. public function count()
  144. {
  145. return count($this->fetch());
  146. }
  147. public function size()
  148. {
  149. $size = (float) 0;
  150. foreach ($this->fetch() as $entityObj)
  151. {
  152. $size += $entityObj->getSize();
  153. }
  154. return $size;
  155. }
  156. /**
  157. * @return float
  158. * @throws \Exception
  159. */
  160. public function getSizeInGb()
  161. {
  162. $size = $this->size();
  163. Utility::unitFormat($size, "bytes", "gb");
  164. return $size;
  165. }
  166. private function formatTemplateKvm($node, array $file)
  167. {
  168. if (empty($file['vmid']))
  169. {
  170. return;
  171. }
  172. try
  173. {
  174. $vmid = $file['vmid'];
  175. $status = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/status/current", array());
  176. if ($status['template'] != '1')
  177. {
  178. return;
  179. }
  180. $config = $this->api()->get("/nodes/{$node}/qemu/{$vmid}/config", array());
  181. if (empty($config))
  182. {
  183. return;
  184. }
  185. $attributes = array();
  186. $attributes['node'] = $node;
  187. $attributes = array_merge($attributes, $file);
  188. $attributes = array_merge($attributes, $config);
  189. $template = new TemplateKvm();
  190. $template->setAttributes($attributes);
  191. return $template;
  192. }
  193. catch (\Exception $ex)
  194. {
  195. }
  196. }
  197. public function fetchAsArray()
  198. {
  199. $data = array();
  200. foreach ($this->fetch() as $entity)
  201. {
  202. $data[$entity->getVolid()] = $entity->getFriendlyName();
  203. }
  204. return $data;
  205. }
  206. public function findByContent($content)
  207. {
  208. $this->content = $content;
  209. return $this;
  210. }
  211. public function findLxcTemplates()
  212. {
  213. $this->content = 'vztmpl';
  214. return $this;
  215. }
  216. public function findKvmTemplates()
  217. {
  218. $this->content = 'images';
  219. $this->templateKvm = true;
  220. return $this;
  221. }
  222. public function findIso()
  223. {
  224. $this->content = 'iso';
  225. return $this;
  226. }
  227. public function findByStorages($storages)
  228. {
  229. $this->storages = $storages;
  230. return $this;
  231. }
  232. public function findByNodes(array $nodes)
  233. {
  234. $this->nodes = $nodes;
  235. return $this;
  236. }
  237. public function findByNode(\MGProvision\Proxmox\v2\models\Node $node)
  238. {
  239. $this->nodes = array($node->getNode());
  240. return $this;
  241. }
  242. public function delete()
  243. {
  244. $this->reset();
  245. $backups = $this->fetch();
  246. foreach ($backups as $b)
  247. {
  248. $b->delete();
  249. }
  250. }
  251. /**
  252. *
  253. * @return File|TemplateKvm|null
  254. */
  255. public function fetchLast()
  256. {
  257. $file = null;
  258. foreach ($this->fetch() as $t)
  259. {
  260. if ($file === null)
  261. {
  262. $file = $t;
  263. continue;
  264. }
  265. else if ($t->getTimestamp() > $file->getTimestamp())
  266. {
  267. $file = $t;
  268. }
  269. }
  270. return $file;
  271. }
  272. public function findVolidNotIn(array $volids){
  273. $this->filterNotMatch['volid'] = $volids;
  274. return $this;
  275. }
  276. public function sortByTime(){
  277. $keys = [];
  278. $orginal = $this->fetch();
  279. foreach ($orginal as $key => $entity){
  280. $keys[$key] = $entity->getTimestamp();
  281. }
  282. uasort($keys, function($a, $b){
  283. if ($a == $b) {
  284. return 0;
  285. }
  286. return ($a < $b) ? -1 : 1;
  287. });
  288. $this->fetch=[];
  289. foreach ($keys as $id => $entity){
  290. $this->fetch[$id] = $orginal[$id];
  291. }
  292. return $this;
  293. }
  294. public function remove($id){
  295. unset($this->fetch[$id]);
  296. return $this;
  297. }
  298. public function findSnippets()
  299. {
  300. $this->content = 'snippets';
  301. return $this;
  302. }
  303. public function findSnippetsByServiceId($serviceId){
  304. $this->findSnippets();
  305. $this->filterPattern = "/userconfig\-{$serviceId}\.yaml/";
  306. return $this;
  307. }
  308. public function findSnippetsByVmModel(VmModel $vmModel){
  309. $this->findSnippets();
  310. $this->filterPattern = "/userconfig\-{$vmModel->hosting_id}\-{$vmModel->id}\.yaml/";
  311. return $this;
  312. }
  313. public function first(){
  314. return $this->fetch()[0];
  315. }
  316. }