SnapshotProvider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\ProxmoxVps\App\UI\Snapshot\Providers;
  20. use MGProvision\Proxmox\v2\models\Snapshot;
  21. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  22. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  23. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  24. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  26. class SnapshotProvider extends BaseDataProvider implements ClientArea
  27. {
  28. use ProductService;
  29. use ApiService;
  30. public function read() {
  31. if ($this->actionElementId) {
  32. $this->data = json_decode(base64_decode($this->actionElementId), true);
  33. }
  34. }
  35. public function create() {
  36. $this->acl()->snapshot();
  37. $this->resourceGuard()->snapshotLimit();
  38. $snapshot = new Snapshot();
  39. $snapshot->setApi($this->api());
  40. $snapshot->setPath($this->vm()->getPath() . "/snapshot");
  41. $snapshot->setAttributes([
  42. "name" => $this->formData['name'],
  43. "description" => $this->formData['description'],
  44. ]);
  45. if ($this->formData['vmstate']) {
  46. $snapshot->setVmstate($this->formData['vmstate'] == "on" ? 1 : 0);
  47. }
  48. $taskId = $snapshot->create();
  49. sleep(1);
  50. $task = $this->vm()->node()->task($taskId);
  51. if($task->isFalied()) {
  52. return (new HtmlDataJsonResponse())
  53. ->setStatusError()
  54. ->setMessageAndTranslate($task->getExitstatus());
  55. }
  56. return (new HtmlDataJsonResponse())
  57. ->setStatusSuccess()
  58. ->setMessageAndTranslate('The snapshot has been created successfully')
  59. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  60. ->setCallBackFunction('pmToggleButton');
  61. }
  62. public function update() {
  63. $this->acl()->snapshot();
  64. $snapshot = new Snapshot();
  65. $snapshot->setApi($this->api());
  66. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $this->formData['name']);
  67. $snapshot->setAttributes([
  68. "name" => $this->formData['name'],
  69. "description" => $this->formData['description'],
  70. ]);
  71. $snapshot->update();
  72. return (new HtmlDataJsonResponse())
  73. ->setStatusSuccess()
  74. ->setMessageAndTranslate('The snapshot has been updated successfully');
  75. }
  76. public function delete() {
  77. $this->acl()->snapshot();
  78. $snapshot = new Snapshot();
  79. $snapshot->setApi($this->api());
  80. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $this->formData['name']);
  81. $snapshot->delete();
  82. sleep(1);
  83. return (new HtmlDataJsonResponse())
  84. ->setStatusSuccess()
  85. ->setMessageAndTranslate('The snapshot has been deleted successfully')
  86. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  87. ->setCallBackFunction('pmToggleButton');
  88. }
  89. public function deleteMass() {
  90. $this->acl()->snapshot();
  91. $snapshot = new Snapshot();
  92. $snapshot->setApi($this->api());
  93. foreach ($this->request->get('massActions') as $id) {
  94. $data = json_decode(base64_decode($id), true);
  95. $name = $data['name'];
  96. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $name);
  97. $snapshot->delete();
  98. sleep(1);
  99. }
  100. return (new HtmlDataJsonResponse())
  101. ->setStatusSuccess()
  102. ->setMessageAndTranslate('The snapshots have been deleted successfully')
  103. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  104. ->setCallBackFunction('pmToggleButton');
  105. }
  106. public function rollback() {
  107. $this->acl()->snapshot();
  108. $snapshot = new Snapshot();
  109. $snapshot->setApi($this->api());
  110. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $this->formData['name']);
  111. $snapshot->rollback();
  112. return (new HtmlDataJsonResponse())
  113. ->setStatusSuccess()
  114. ->setMessageAndTranslate('The snapshot has been rolled back successfully');
  115. }
  116. }