SnapshotProvider.php 5.3 KB

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