BackupScheduleRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-12-13)
  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\BackupSchedule;
  22. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  23. /**
  24. * Description of BackupSchedule
  25. *
  26. * @author Pawel Kopec <pawelk@modulesgarden.com>
  27. * @version 1.0.0
  28. */
  29. class BackupScheduleRepository extends AbstractRepository
  30. {
  31. protected $vmids = array();
  32. protected $nodes = array();
  33. public function findByVm(VmInterface $vm)
  34. {
  35. $this->findByVmids(array($vm->getVmid()));
  36. return $this;
  37. }
  38. public function findByVmids(array $vmids)
  39. {
  40. $this->vmids = $vmids;
  41. return $this;
  42. }
  43. public function findByNodes(array $nodes)
  44. {
  45. $this->nodes = $nodes;
  46. return $this;
  47. }
  48. /**
  49. *
  50. * @return BackupSchedule[]
  51. */
  52. public function fetch()
  53. {
  54. if (!empty($this->fetch))
  55. {
  56. return $this->fetch;
  57. }
  58. $path = "/cluster/backup";
  59. $res = $this->api()->get($path);
  60. $this->fetch = array();
  61. foreach ($res as $job)
  62. {
  63. if ((!$job['vmid'] && $this->vmids) || ($job['vmid'] && $this->vmids && !in_array($job['vmid'], $this->vmids)))
  64. {
  65. continue;
  66. }
  67. if ((!$job['node'] && $this->nodes) || ( $job['node'] && $this->nodes && !in_array($job['node'], $this->nodes)))
  68. {
  69. continue;
  70. }
  71. $backupShedule = new BackupSchedule();
  72. $backupShedule->setAttributes($job);
  73. $backupShedule->setPath(sprintf("{$path}/%s", $job['id']));
  74. $this->fetch[] = $backupShedule;
  75. }
  76. return $this->fetch;
  77. }
  78. /**
  79. *
  80. * @param VmModel[] $vservers
  81. */
  82. public function findByVmModel($vservers)
  83. {
  84. foreach ($vservers as $vserver)
  85. {
  86. if (!$vserver instanceof VmModel)
  87. {
  88. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
  89. }
  90. $this->vmids[] = $vserver->vmid;
  91. }
  92. return $this;
  93. }
  94. public function count()
  95. {
  96. return count($this->fetch());
  97. }
  98. public function delete()
  99. {
  100. for ($i = 0; $i <= 100; $i++)
  101. {
  102. $this->reset();
  103. $backups = $this->fetch();
  104. if (empty($backups))
  105. break;
  106. foreach ($backups as $b)
  107. {
  108. $b->delete();
  109. break;
  110. }
  111. }
  112. }
  113. }