DiskProvider.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Disk\Providers;
  20. use MGProvision\Proxmox\v2\models\HardDisk;
  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 DiskProvider extends BaseDataProvider implements ClientArea
  30. {
  31. use ProductService;
  32. use ApiService;
  33. public function read()
  34. {
  35. if ($this->actionElementId && $this->actionElementId != "diskDataTable")
  36. {
  37. $disk = $this->vm()->findHardDiskById($this->actionElementId);
  38. $this->data = $disk->getAttributes();
  39. $this->data['id'] = $this->actionElementId;
  40. //backup
  41. $backup = $this->data['backup'];
  42. $this->data['backup'] = $backup == "0" ? "off" : "on";
  43. //size
  44. $this->data['size'] = $disk->getGb();
  45. }
  46. }
  47. public function create()
  48. {
  49. $this->resourceGuard()->diskLimit($this->formData['size']);
  50. $storage = $this->configuration()->getAdditionalDiskStorage();
  51. $ide = $this->vm()->findFreeIde($this->formData['bus']);
  52. $id = $this->formData['bus'] . $ide;
  53. $hdd = new HardDisk($id);
  54. $hdd->setApi($this->api());
  55. $hdd->setPath($this->vm()->getPath() . "/config");
  56. $hdd->setStorage($storage)
  57. ->setSize($this->formData['size'])
  58. ->setMedia("disk")
  59. ->setFormat($this->formData['format'])
  60. ->setBackup($this->configuration()->isPermissionAdditionalDiskBackup() && $this->formData['backup'] == "on" ? null : 0);
  61. //disk speed
  62. $hdd->setMbps_rd($this->configuration()->getAdditionalDiskMbpsRd())
  63. ->setMbps_wr($this->configuration()->getAdditionalDiskMbpsWr());
  64. //IOPS
  65. $hdd->setIops_rd($this->configuration()->getAdditionalDiskIopsRd())
  66. ->setIops_rd_max($this->configuration()->getAdditionalDiskIopsRdMax())
  67. ->setIops_wr($this->configuration()->getAdditionalDiskIopsWr())
  68. ->setIops_wr_max($this->configuration()->getAdditionalDiskIopsWrMax());
  69. //cach
  70. $hdd->setCache($this->configuration()->getAdditionalDiskCache());
  71. //replicate
  72. if ($this->configuration()->isAdditionalDiskReplicate())
  73. {
  74. $hdd->setReplicate(0);
  75. }
  76. //discard
  77. if ($this->configuration()->isAdditionalDiskDiscard())
  78. {
  79. $hdd->setDiscard('on');
  80. }
  81. //iothread for VirtlO & SCSI 'VIRTIO', 'SCSI'
  82. if ($this->configuration()->isAdditionalDiskIoThread() && in_array($this->formData['bus'], ['virtio', 'scsi']))
  83. {
  84. $hdd->setIothread(1);
  85. }
  86. $hdd->create();
  87. return (new HtmlDataJsonResponse())
  88. ->setStatusSuccess()
  89. ->setMessageAndTranslate('The hard disk has been created successfully')
  90. ->addData('createButtonStatus', $this->resourceGuard()->hasDiskLimit())
  91. ->setCallBackFunction('pmToggleButton');
  92. }
  93. public function update()
  94. {
  95. $this->resourceGuard()->diskLimit($this->formData['size'], $this->formData['id']);
  96. $hdd = $this->vm()->findHardDiskById($this->formData['id']);
  97. if($hdd->isMaster()){
  98. return (new HtmlDataJsonResponse())
  99. ->setStatusError()
  100. ->setMessageAndTranslate('Update the master disk size is restricted');
  101. }
  102. if ((int)$hdd->getGb() > (int)$this->formData['size'])
  103. {
  104. return (new HtmlDataJsonResponse())
  105. ->setStatusError()
  106. ->setMessageAndTranslate('Downgrading the disk size is restricted');
  107. }
  108. //resize
  109. if ((int)$hdd->getGb() < (int)$this->formData['size'])
  110. {
  111. $size = "+" . abs((int)$this->formData['size'] - (int)$hdd->getGb()) . "G";
  112. $hdd->resize($size);
  113. }
  114. //backup
  115. $backup = $this->formData['backup'] == "on" ? null : 0;
  116. if ($this->configuration()->isPermissionAdditionalDiskBackup() && $backup != $hdd->getBackup())
  117. {
  118. $hdd->setBackup($backup);
  119. $hdd->update();
  120. }
  121. //reboot
  122. if ($this->vm()->isRunning() &&
  123. !Job::waiting()->ofJob(RebootVmJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->count())
  124. {
  125. queue(RebootVmJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  126. }
  127. return (new HtmlDataJsonResponse())
  128. ->setStatusSuccess()
  129. ->setMessageAndTranslate('The hard disk has been updated successfully')
  130. ->addData('createButtonStatus', $this->resourceGuard()->hasDiskLimit())
  131. ->setCallBackFunction('pmToggleButton');
  132. }
  133. public function delete()
  134. {
  135. $hdd = $this->vm()->findHardDiskById($this->formData['id']);
  136. if($hdd->isMaster()){
  137. return (new HtmlDataJsonResponse())
  138. ->setStatusError()
  139. ->setMessageAndTranslate('The master hard disk cannot be deleted');
  140. }
  141. $hdd->delete();
  142. unset($this->vm);
  143. foreach($this->vm()->getHardDisks() as $hd){
  144. if($hd->isMaster()){
  145. continue;
  146. }
  147. if(preg_match('/unused/', $hd->getId())){
  148. $hd->delete();
  149. break;
  150. }
  151. }
  152. return (new HtmlDataJsonResponse())
  153. ->setStatusSuccess()
  154. ->setMessageAndTranslate('The hard disk has been deleted successfully')
  155. ->addData('createButtonStatus', $this->resourceGuard()->hasDiskLimit())
  156. ->setCallBackFunction('pmToggleButton');
  157. }
  158. }