DiskProvider.php 8.2 KB

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