SnapshotProvider.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  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 ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Providers;
  20. use MGProvision\Proxmox\v2\models\Snapshot;
  21. use MGProvision\Proxmox\v2\repository\SnapshotRepository;
  22. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  24. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  25. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  26. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  27. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  28. class SnapshotProvider extends BaseDataProvider implements ClientArea
  29. {
  30. use ProductService;
  31. use ApiService;
  32. public function read()
  33. {
  34. if ($this->actionElementId)
  35. {
  36. $this->data = json_decode(base64_decode($this->actionElementId), true);
  37. }
  38. }
  39. public function create()
  40. {
  41. $this->acl()->snapshot();
  42. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  43. $snapRepo = new SnapshotRepository();
  44. $snapRepo->setApi($this->api());
  45. $snapRepo->findByVm($vm);
  46. $maxSnap = $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOTS, $this->configuration()->getSnapshotMaxFiles());
  47. if ($maxSnap != "-1" && $snapRepo->count() >= $maxSnap)
  48. {
  49. throw new \Exception(sl("lang")->tr("The maximum number of snapshots has been exceeded. Please remove the old snapshots."));
  50. }
  51. $snapshot = new Snapshot();
  52. $snapshot->setApi($this->api());
  53. $snapshot->setPath($vm->getPath() . "/snapshot");
  54. $snapshot->setAttributes([
  55. "name" => $this->formData['name'],
  56. "description" => $this->formData['description'],
  57. ]);
  58. if ($this->formData['vmstate'])
  59. {
  60. $snapshot->setVmstate($this->formData['vmstate'] == "on" ? 1 : 0);
  61. }
  62. $taskId = $snapshot->create();
  63. sleep(1);
  64. $task = $vm->node()->task($taskId);
  65. if($task->isFalied()){
  66. return (new HtmlDataJsonResponse())
  67. ->setStatusError()
  68. ->setMessageAndTranslate($task->getExitstatus());
  69. }
  70. return (new HtmlDataJsonResponse())
  71. ->setStatusSuccess()
  72. ->setMessageAndTranslate('The snapshot has been created successfully')
  73. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  74. ->setCallBackFunction('pmToggleSnapshotButton');
  75. }
  76. public function update()
  77. {
  78. $this->acl()->snapshot();
  79. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  80. $snapshot = new Snapshot();
  81. $snapshot->setApi($this->api());
  82. $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
  83. $snapshot->setAttributes([
  84. "name" => $this->formData['name'],
  85. "description" => $this->formData['description'],
  86. ]);
  87. $snapshot->update();
  88. return (new HtmlDataJsonResponse())
  89. ->setStatusSuccess()
  90. ->setMessageAndTranslate('The snapshot has been updated successfully');
  91. }
  92. public function delete()
  93. {
  94. $this->acl()->snapshot();
  95. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  96. $snapshot = new Snapshot();
  97. $snapshot->setApi($this->api());
  98. $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
  99. $snapshot->delete();
  100. sleep(1);
  101. return (new HtmlDataJsonResponse())
  102. ->setStatusSuccess()
  103. ->setMessageAndTranslate('The snapshot has been deleted successfully')
  104. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  105. ->setCallBackFunction('pmToggleSnapshotButton');
  106. }
  107. public function deleteMass()
  108. {
  109. $this->acl()->snapshot();
  110. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  111. $snapshot = new Snapshot();
  112. $snapshot->setApi($this->api());
  113. foreach ($this->request->get('massActions') as $id)
  114. {
  115. $data = json_decode(base64_decode($id), true);
  116. $name = $data['name'];
  117. $snapshot->setPath($vm->getPath() . "/snapshot/" . $name);
  118. $snapshot->delete();
  119. sleep(1);
  120. }
  121. return (new HtmlDataJsonResponse())
  122. ->setStatusSuccess()
  123. ->setMessageAndTranslate('The snapshots have been deleted successfully')
  124. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  125. ->setCallBackFunction('pmToggleSnapshotButton');
  126. }
  127. public function rollback()
  128. {
  129. $this->acl()->snapshot();
  130. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  131. $snapshot = new Snapshot();
  132. $snapshot->setApi($this->api());
  133. $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
  134. $snapshot->rollback();
  135. return (new HtmlDataJsonResponse())
  136. ->setStatusSuccess()
  137. ->setMessageAndTranslate('The snapshot has been rolled back successfully');
  138. }
  139. }