UpdateProvider.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\ServiceInformation\Providers;
  20. use MGProvision\Proxmox\v2\models\Kvm;
  21. use MGProvision\Proxmox\v2\models\Lxc;
  22. use MGProvision\Proxmox\v2\models\NetworkDeviceKvm;
  23. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
  25. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  26. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\isAdmin;
  27. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  28. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  29. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  30. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  31. class UpdateProvider extends BaseDataProvider implements ClientArea
  32. {
  33. use ApiService;
  34. use ProductService;
  35. use HostingService;
  36. public function read()
  37. {
  38. if ($this->vm() instanceof Kvm)
  39. {
  40. //hostname
  41. $this->data['hostname'] = $this->vm()->config()['name'];
  42. //ISO
  43. if(isAdmin() || $this->configuration()->isPermissionIsoImage()){
  44. $this->data['iso'] = $this->vm()->cdrom()['isoRaw'];
  45. $this->availableValues['iso'] = ["none" => sl("lang")->abtr("template", "None")];
  46. foreach ($this->isoRepository()->fetch() as $file)
  47. {
  48. if ($this->configuration()->isPermissionIsoImages() && !in_array($file->getVolid(), $this->configuration()->getPermissionIsoImages()))
  49. {
  50. continue;
  51. }
  52. $this->availableValues['iso'][$file->getVolid()] = sl("lang")->abtr("template", $file->getFriendlyName());
  53. }
  54. }
  55. //Boot order
  56. $bootOrder = $this->vm()->getBootOrder();
  57. $this->data['bootOrder0'] = $bootOrder[0];
  58. $this->data['bootOrder1'] = $bootOrder[1];
  59. $this->data['bootOrder2'] = $bootOrder[2];
  60. $options = [
  61. 0 => sl("lang")->tr("None"),
  62. "c" => sl("lang")->tr("Disk"),
  63. "d" => sl("lang")->tr("CD-ROM"),
  64. "n" => sl("lang")->tr("Network"),
  65. ];
  66. if(version_compare($this->api()->getVersion(), "6.3", '>=')){
  67. $cdrom = $this->vm()->cdrom();
  68. $nd = $this->vm()->getNetworkDevices($this->configuration()->getBridge());
  69. $options = [
  70. 0 => sl("lang")->tr("None"),
  71. $this->vm()->getMasterHardDisk()->getId() => sl("lang")->tr("Disk"),
  72. ];
  73. if($cdrom['bus']){
  74. $options[$cdrom['bus']] = sl("lang")->tr("CD-ROM");
  75. }
  76. if($nd[0] instanceof NetworkDeviceKvm){
  77. $options[$nd[0]->getId()] = sl("lang")->tr("Network");
  78. }
  79. }
  80. $this->availableValues['bootOrder0'] = $options;
  81. $this->availableValues['bootOrder1'] = $options;
  82. $this->availableValues['bootOrder2'] = $options;
  83. //sshkeys
  84. $this->data['sshkeys'] = rawurldecode($this->vm()->config()['sshkeys']);
  85. }
  86. else
  87. {
  88. if ($this->vm() instanceof Lxc)
  89. {
  90. //hostname
  91. $this->data['hostname'] = $this->vm()->config()['hostname'];
  92. }
  93. }
  94. }
  95. public function update()
  96. {
  97. if ($this->vm() instanceof Kvm)
  98. {
  99. //Hostname
  100. $this->vm()->updateConfig(["name" => $this->formData['hostname']]);
  101. //iso
  102. if ($this->vm()->cdrom())
  103. {
  104. $this->vm()->updateCdrom($this->formData['iso']);
  105. }
  106. //Boot order
  107. $bootOrder = null;
  108. $order = [];
  109. for ($i = 0; $i <= 2; $i++)
  110. {
  111. if ($this->formData['bootOrder' . $i])
  112. {
  113. $bootOrder .= $this->formData['bootOrder' . $i];
  114. $order[] = $this->formData['bootOrder' . $i];
  115. }
  116. }
  117. if(version_compare($this->api()->getVersion(), "6.3", '>=') && !empty($order)){
  118. //order=scsi0;ide0;ide1;net0
  119. $this->vm()->updateConfig(['boot' => "order=".implode(";", $order)]);
  120. }else{
  121. $this->vm()->changeBootOrder($bootOrder);
  122. }
  123. //sshkeys
  124. if($this->configuration()->isPermissionSshkeys()){
  125. if ($this->formData['sshkeys'])
  126. {
  127. $this->vm()->updateConfig(["sshkeys" => rawurlencode($this->formData['sshkeys'])]);
  128. } else if($this->vm()->config()['sshkeys'])
  129. {
  130. $this->vm()->deleteConfig('sshkeys');
  131. }
  132. if (isset($this->getWhmcsParamByKey("customfields")['sshkeys']))
  133. {
  134. $this->customFieldUpdate('sshkeys', $this->formData['sshkeys']);
  135. }
  136. }
  137. }
  138. else
  139. {
  140. if ($this->vm() instanceof Lxc)
  141. {
  142. //Hostname
  143. $this->vm()->updateConfig(["hostname" => $this->formData['hostname']]);
  144. }
  145. }
  146. if( $this->hosting()->domain != $this->formData['hostname'] ){
  147. $this->hosting()->update(['domain' => $this->formData['hostname'] ]);
  148. }
  149. return (new HtmlDataJsonResponse())
  150. ->setStatusSuccess()
  151. ->setMessageAndTranslate('Service information has been changed')
  152. ->addRefreshTargetId('serviceInformationDataTable');
  153. }
  154. }