MountPointProvider.php 6.8 KB

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