SnapshotProvider.php 5.8 KB

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