MountPointProvider.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\MountPoint\Providers;
  20. use MGProvision\Proxmox\v2\models\MountPoint;
  21. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RebootVmJob;
  22. use ModulesGarden\ProxmoxAddon\App\Models\Job;
  23. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  26. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  27. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  28. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  29. class MountPointProvider extends BaseDataProvider implements ClientArea
  30. {
  31. use ApiService;
  32. use ProductService;
  33. public function read()
  34. {
  35. if ($this->actionElementId && $this->actionElementId != "mountPointDataTable")
  36. {
  37. $disk = $this->vm()->getMounPoints()->findMountPointById($this->actionElementId);
  38. $this->data = $disk->toArray();
  39. $this->data['id'] = $this->actionElementId;
  40. //backup
  41. $backup = $this->data['backup'];
  42. $this->data['backup'] = $backup == "1" ? "on" : "off";
  43. //size
  44. $this->data['size'] = (int)$disk->getGb();
  45. }
  46. }
  47. public function create()
  48. {
  49. $this->resourceGuard()->mountPointLimit($this->formData['size']);
  50. //disk storage
  51. $storage = $this->configuration()->getMountPointStorage();
  52. $mountPointRepository = $this->vm()->getMounPoints();
  53. $hdd = new MountPoint($mountPointRepository->nextId());
  54. $hdd->setLocation($storage . ":" . $this->formData['size'])
  55. ->setAcl($this->configuration()->getMountPointAcl() == "default" ? null : $this->configuration()->getMountPointAcl())
  56. ->setRo($this->configuration()->isMountPointRo() ? 1 : null)
  57. ->setQuota($this->configuration()->isMountPointQuota() ? 1 : null)
  58. ->setBackup($this->configuration()->isPermissionMountPointBackup() && $this->formData['backup'] == "on" ? 1 : null)
  59. ->setReplicate($this->configuration()->isMountPointReplicate() ? '0' : null)
  60. ->setMp($this->formData['mp'])
  61. ->setPath($this->vm()->getPath() . "/config")
  62. ->setApi($this->api())
  63. ->create();
  64. return (new HtmlDataJsonResponse())
  65. ->setStatusSuccess()
  66. ->setMessageAndTranslate('The hard disk has been created successfully')
  67. ->addData('createButtonStatus', $this->resourceGuard()->hasMountPointLimit())
  68. ->setCallBackFunction('pmToggleButton');
  69. }
  70. public function update()
  71. {
  72. $this->resourceGuard()->mountPointLimit($this->formData['size'], $this->formData['id']);
  73. $hdd = $this->vm()->getMounPoints()->findMountPointById($this->formData['id']);
  74. if($hdd->isMaster()){
  75. return (new HtmlDataJsonResponse())
  76. ->setStatusError()
  77. ->setMessageAndTranslate('Update the master disk size is restricted');
  78. }
  79. if ((int)$hdd->getGb() > (int)$this->formData['size'])
  80. {
  81. return (new HtmlDataJsonResponse())
  82. ->setStatusError()
  83. ->setMessageAndTranslate('Downgrading the disk size is restricted');
  84. }
  85. //resize
  86. if ((int)$hdd->getGb() < (int)$this->formData['size'])
  87. {
  88. $size = "+" . abs((int)$this->formData['size'] - (int)$hdd->getGb()) . "G";
  89. $hdd->resize($size);
  90. }
  91. //backup
  92. $backup = $this->configuration()->isPermissionMountPointBackup() && $this->formData['backup'] == "on" ? 1 : null;
  93. if ($backup != $hdd->getBackup())
  94. {
  95. $hdd->setBackup($backup);
  96. $hdd->update();
  97. }
  98. //reboot
  99. if ($this->vm()->isRunning() &&
  100. !Job::waiting()->ofJob(RebootVmJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->count())
  101. {
  102. queue(RebootVmJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  103. }
  104. return (new HtmlDataJsonResponse())
  105. ->setStatusSuccess()
  106. ->setMessageAndTranslate('The hard disk has been updated successfully')
  107. ->addData('createButtonStatus', $this->resourceGuard()->hasMountPointLimit())
  108. ->setCallBackFunction('pmToggleButton');
  109. }
  110. public function delete()
  111. {
  112. $hdd = $this->vm()->getMounPoints()->findMountPointById($this->formData['id']);
  113. if($hdd->getId() == "rootfs"){
  114. return (new HtmlDataJsonResponse())
  115. ->setStatusError()
  116. ->setMessageAndTranslate('The master hard disk cannot be deleted');
  117. }
  118. $hdd->delete();
  119. return (new HtmlDataJsonResponse())
  120. ->setStatusSuccess()
  121. ->setMessageAndTranslate('The hard disk has been deleted successfully')
  122. ->addData('createButtonStatus', $this->resourceGuard()->hasMountPointLimit())
  123. ->setCallBackFunction('pmToggleButton');
  124. }
  125. }