DiskProvider.php 7.4 KB

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