DiskProvider.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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['additionalDiskSize'])
  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. //ssd
  75. $hdd->setSsd($this->configuration()->isAdditionalDiskSsd() ? 1 : null);
  76. //replicate
  77. if ($this->configuration()->isAdditionalDiskReplicate())
  78. {
  79. $hdd->setReplicate(0);
  80. }
  81. //discard
  82. if ($this->configuration()->isAdditionalDiskDiscard())
  83. {
  84. $hdd->setDiscard('on');
  85. }
  86. //iothread for VirtlO & SCSI 'VIRTIO', 'SCSI'
  87. if ($this->configuration()->isAdditionalDiskIoThread() && in_array($this->formData['bus'], ['virtio', 'scsi']))
  88. {
  89. $hdd->setIothread(1);
  90. }
  91. $vmModel->disks += $this->formData['additionalDiskSize'];
  92. $hdd->create();
  93. $vmModel->save();
  94. $resourceManager = new ResourceManager();
  95. return (new HtmlDataJsonResponse())
  96. ->setStatusSuccess()
  97. ->setMessageAndTranslate('The hard disk has been created successfully')
  98. ->addData('createButtonStatus', $resourceManager->disk()->hasFreeTotal())
  99. ->setCallBackFunction('pmToggleDiskButton');
  100. }
  101. public function update()
  102. {
  103. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  104. $vmModel = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel();
  105. $vmId = $vmModel->id;
  106. $hdd = $vm->findHardDiskById($this->formData['id']);
  107. if ((int)$hdd->getGb() > (int)$this->formData['additionalDiskSize'])
  108. {
  109. return (new HtmlDataJsonResponse())
  110. ->setStatusError()
  111. ->setMessageAndTranslate('Downgrading the disk size is restricted');
  112. }
  113. //resize
  114. if ((int)$hdd->getGb() < (int)$this->formData['additionalDiskSize'])
  115. {
  116. $size = "+" . abs((int)$this->formData['additionalDiskSize'] - (int)$hdd->getGb()) . "G";
  117. $hdd->resize($size);
  118. }
  119. if($hdd->getName() == 'disk-0'){
  120. $vm->config(true);
  121. $vmModel->disk = $vm->getMasterHddSize();
  122. }else{
  123. $vmModel->disks = $vm->getHardDiskRepostiory()->additionalSize();
  124. }
  125. $vmModel->save();
  126. //backup
  127. $backup = $this->formData['backup'] == "on" ? null : 0;
  128. $hdd->setBackup($backup)->update();
  129. /* thurdata no auto reboot
  130. if ($vm->isRunning() &&
  131. !Job::waiting()->ofJob(RebootVmJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofCustomId($vmId)->count())
  132. {
  133. queue(RebootVmJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"),$vmId);
  134. } */
  135. $resourceManager = new ResourceManager();
  136. return (new HtmlDataJsonResponse())
  137. ->setStatusSuccess()
  138. ->setMessageAndTranslate('The hard disk has been updated successfully')
  139. ->addData('createButtonStatus',$resourceManager->disk()->hasFreeTotal())
  140. ->setCallBackFunction('pmToggleDiskButton');
  141. }
  142. public function delete()
  143. {
  144. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  145. $vmModel = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel();
  146. $hdd = $vm->findHardDiskById($this->formData['id']);
  147. if ($hdd->getName() == 'disk-0') {
  148. return (new HtmlDataJsonResponse())
  149. ->setStatusError()
  150. ->setMessageAndTranslate('The system disk cannot be deleted');
  151. }
  152. if($hdd->isMaster()){
  153. return (new HtmlDataJsonResponse())
  154. ->setStatusError()
  155. ->setMessageAndTranslate('The master hard disk cannot be deleted');
  156. }
  157. $hdd->delete();
  158. unset($this->vm);
  159. foreach($vm->getUnusedHardDisks() as $hd){
  160. if($hd->isMaster()){
  161. continue;
  162. }
  163. if($hd->getName() == 'disk-0'){
  164. continue;
  165. }
  166. if(preg_match('/unused/', $hd->getId())){
  167. $hd->delete();
  168. break;
  169. }
  170. }
  171. $vmModel->disks = $vm->getHardDiskRepostiory()->additionalSize();
  172. $vmModel->save();
  173. $resourceManager = new ResourceManager();
  174. return (new HtmlDataJsonResponse())
  175. ->setStatusSuccess()
  176. ->setMessageAndTranslate('The hard disk has been deleted successfully')
  177. ->addData('createButtonStatus', $resourceManager->disk()->hasFreeTotal())
  178. ->setCallBackFunction('pmToggleDiskButton');
  179. }
  180. }