SnapshotProvider.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. {
  32. if ($this->actionElementId)
  33. {
  34. $this->data = json_decode(base64_decode($this->actionElementId), true);
  35. }
  36. }
  37. public function create()
  38. {
  39. $this->acl()->snapshot();
  40. $this->resourceGuard()->snapshotLimit();
  41. $snapshot = new Snapshot();
  42. $snapshot->setApi($this->api());
  43. $snapshot->setPath($this->vm()->getPath() . "/snapshot");
  44. $snapshot->setAttributes([
  45. "name" => $this->formData['name'],
  46. "description" => $this->formData['description'],
  47. ]);
  48. if ($this->formData['vmstate'])
  49. {
  50. $snapshot->setVmstate($this->formData['vmstate'] == "on" ? 1 : 0);
  51. }
  52. $taskId = $snapshot->create();
  53. sleep(1);
  54. $task = $this->vm()->node()->task($taskId);
  55. if($task->isFalied()){
  56. return (new HtmlDataJsonResponse())
  57. ->setStatusError()
  58. ->setMessageAndTranslate($task->getExitstatus());
  59. }
  60. return (new HtmlDataJsonResponse())
  61. ->setStatusSuccess()
  62. ->setMessageAndTranslate('The snapshot has been created successfully')
  63. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  64. ->setCallBackFunction('pmToggleButton');
  65. }
  66. public function update()
  67. {
  68. $this->acl()->snapshot();
  69. $snapshot = new Snapshot();
  70. $snapshot->setApi($this->api());
  71. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $this->formData['name']);
  72. $snapshot->setAttributes([
  73. "name" => $this->formData['name'],
  74. "description" => $this->formData['description'],
  75. ]);
  76. $snapshot->update();
  77. return (new HtmlDataJsonResponse())
  78. ->setStatusSuccess()
  79. ->setMessageAndTranslate('The snapshot has been updated successfully');
  80. }
  81. public function delete()
  82. {
  83. $this->acl()->snapshot();
  84. $snapshot = new Snapshot();
  85. $snapshot->setApi($this->api());
  86. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $this->formData['name']);
  87. $snapshot->delete();
  88. sleep(1);
  89. return (new HtmlDataJsonResponse())
  90. ->setStatusSuccess()
  91. ->setMessageAndTranslate('The snapshot has been deleted successfully')
  92. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  93. ->setCallBackFunction('pmToggleButton');
  94. }
  95. public function deleteMass()
  96. {
  97. $this->acl()->snapshot();
  98. $snapshot = new Snapshot();
  99. $snapshot->setApi($this->api());
  100. foreach ($this->request->get('massActions') as $id)
  101. {
  102. $data = json_decode(base64_decode($id), true);
  103. $name = $data['name'];
  104. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $name);
  105. $snapshot->delete();
  106. sleep(1);
  107. }
  108. return (new HtmlDataJsonResponse())
  109. ->setStatusSuccess()
  110. ->setMessageAndTranslate('The snapshots have been deleted successfully')
  111. ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
  112. ->setCallBackFunction('pmToggleButton');
  113. }
  114. public function rollback()
  115. {
  116. $this->acl()->snapshot();
  117. $snapshot = new Snapshot();
  118. $snapshot->setApi($this->api());
  119. $snapshot->setPath($this->vm()->getPath() . "/snapshot/" . $this->formData['name']);
  120. $snapshot->rollback();
  121. return (new HtmlDataJsonResponse())
  122. ->setStatusSuccess()
  123. ->setMessageAndTranslate('The snapshot has been rolled back successfully');
  124. }
  125. }