SnapshotProvider.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $this->resourceGuard()->snapshotLimit();
  43. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  44. $testRepo = new SnapshotRepository();
  45. $testRepo->setApi($this->api());
  46. $testRepo->findByVm($vm);
  47. logModuleCall(
  48. 'ProxmoxCloudVps',
  49. __FUNCTION__,
  50. $this->getWhmcsConfigOption(ConfigurableOption::SNAPSHOTS, $this->configuration()->getSnapshotMaxFiles()),
  51. 'Debug',
  52. $testRepo->count()
  53. );
  54. $snapshot = new Snapshot();
  55. $snapshot->setApi($this->api());
  56. $snapshot->setPath($vm->getPath() . "/snapshot");
  57. $snapshot->setAttributes([
  58. "name" => $this->formData['name'],
  59. "description" => $this->formData['description'],
  60. ]);
  61. if ($this->formData['vmstate'])
  62. {
  63. $snapshot->setVmstate($this->formData['vmstate'] == "on" ? 1 : 0);
  64. }
  65. $taskId = $snapshot->create();
  66. sleep(1);
  67. $task = $vm->node()->task($taskId);
  68. if($task->isFalied()){
  69. return (new HtmlDataJsonResponse())
  70. ->setStatusError()
  71. ->setMessageAndTranslate($task->getExitstatus());
  72. }
  73. return (new HtmlDataJsonResponse())
  74. ->setStatusSuccess()
  75. ->setMessageAndTranslate('The snapshot has been created successfully')
  76. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  77. ->setCallBackFunction('pmToggleSnapshotButton');
  78. }
  79. public function update()
  80. {
  81. $this->acl()->snapshot();
  82. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  83. $snapshot = new Snapshot();
  84. $snapshot->setApi($this->api());
  85. $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
  86. $snapshot->setAttributes([
  87. "name" => $this->formData['name'],
  88. "description" => $this->formData['description'],
  89. ]);
  90. $snapshot->update();
  91. return (new HtmlDataJsonResponse())
  92. ->setStatusSuccess()
  93. ->setMessageAndTranslate('The snapshot has been updated successfully');
  94. }
  95. public function delete()
  96. {
  97. $this->acl()->snapshot();
  98. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  99. $snapshot = new Snapshot();
  100. $snapshot->setApi($this->api());
  101. $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
  102. $snapshot->delete();
  103. sleep(1);
  104. return (new HtmlDataJsonResponse())
  105. ->setStatusSuccess()
  106. ->setMessageAndTranslate('The snapshot has been deleted successfully')
  107. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  108. ->setCallBackFunction('pmToggleSnapshotButton');
  109. }
  110. public function deleteMass()
  111. {
  112. $this->acl()->snapshot();
  113. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  114. $snapshot = new Snapshot();
  115. $snapshot->setApi($this->api());
  116. foreach ($this->request->get('massActions') as $id)
  117. {
  118. $data = json_decode(base64_decode($id), true);
  119. $name = $data['name'];
  120. $snapshot->setPath($vm->getPath() . "/snapshot/" . $name);
  121. $snapshot->delete();
  122. sleep(1);
  123. }
  124. return (new HtmlDataJsonResponse())
  125. ->setStatusSuccess()
  126. ->setMessageAndTranslate('The snapshots have been deleted successfully')
  127. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  128. ->setCallBackFunction('pmToggleSnapshotButton');
  129. }
  130. public function rollback()
  131. {
  132. $this->acl()->snapshot();
  133. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  134. $snapshot = new Snapshot();
  135. $snapshot->setApi($this->api());
  136. $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
  137. $snapshot->rollback();
  138. return (new HtmlDataJsonResponse())
  139. ->setStatusSuccess()
  140. ->setMessageAndTranslate('The snapshot has been rolled back successfully');
  141. }
  142. }