UpdateProvider.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. $map =['c' => $this->vm()->getMasterHardDisk()->getId()];
  74. if($cdrom['bus']){
  75. $options[$cdrom['bus']] = sl("lang")->tr("CD-ROM");
  76. $map['d'] = $cdrom['bus'];
  77. }
  78. if($nd[0] instanceof NetworkDeviceKvm){
  79. $options[$nd[0]->getId()] = sl("lang")->tr("Network");
  80. $map['n'] = $nd[0]->getId();
  81. }
  82. //d -cdrom c- hard disk n - network
  83. foreach ($bootOrder as $k => $boot){
  84. if($map[$boot]){
  85. $this->data['bootOrder'.$k] =$map[$boot];
  86. }
  87. }
  88. }
  89. $this->availableValues['bootOrder0'] = $options;
  90. $this->availableValues['bootOrder1'] = $options;
  91. $this->availableValues['bootOrder2'] = $options;
  92. //sshkeys
  93. $this->data['sshkeys'] = rawurldecode($this->vm()->config()['sshkeys']);
  94. }
  95. else
  96. {
  97. if ($this->vm() instanceof Lxc)
  98. {
  99. //hostname
  100. $this->data['hostname'] = $this->vm()->config()['hostname'];
  101. }
  102. }
  103. }
  104. public function update()
  105. {
  106. if ($this->vm() instanceof Kvm)
  107. {
  108. //Hostname
  109. if(isAdmin() || $this->configuration()->hasPermissionChangeHostname()){
  110. $this->vm()->updateConfig(["name" => $this->formData['hostname']]);
  111. }
  112. //iso
  113. if ($this->vm()->cdrom())
  114. {
  115. $this->vm()->updateCdrom($this->formData['iso']);
  116. }
  117. //Boot order
  118. $bootOrder = null;
  119. $order = [];
  120. for ($i = 0; $i <= 2; $i++)
  121. {
  122. if ($this->formData['bootOrder' . $i])
  123. {
  124. $bootOrder .= $this->formData['bootOrder' . $i];
  125. $order[] = $this->formData['bootOrder' . $i];
  126. }
  127. }
  128. if(version_compare($this->api()->getVersion(), "6.3", '>=') && !empty($order)){
  129. //order=scsi0;ide0;ide1;net0
  130. $this->vm()->updateConfig(['boot' => "order=".implode(";", $order)]);
  131. }else{
  132. $this->vm()->changeBootOrder($bootOrder);
  133. }
  134. //sshkeys
  135. if($this->configuration()->isPermissionSshkeys()){
  136. if ($this->formData['sshkeys'])
  137. {
  138. $this->vm()->updateConfig(["sshkeys" => rawurlencode($this->formData['sshkeys'])]);
  139. } else if($this->vm()->config()['sshkeys'])
  140. {
  141. $this->vm()->deleteConfig('sshkeys');
  142. }
  143. if (isset($this->getWhmcsParamByKey("customfields")['sshkeys']))
  144. {
  145. $this->customFieldUpdate('sshkeys', $this->formData['sshkeys']);
  146. }
  147. }
  148. }
  149. else
  150. {
  151. if ($this->vm() instanceof Lxc)
  152. {
  153. //Hostname
  154. if(isAdmin() || $this->configuration()->hasPermissionChangeHostname()){
  155. $this->vm()->updateConfig(["hostname" => $this->formData['hostname']]);
  156. }
  157. }
  158. }
  159. if($this->formData['hostname'] && $this->hosting()->domain != $this->formData['hostname'] ){
  160. $this->hosting()->update(['domain' => $this->formData['hostname'] ]);
  161. }
  162. return (new HtmlDataJsonResponse())
  163. ->setStatusSuccess()
  164. ->setMessageAndTranslate('Service information has been changed')
  165. ->addRefreshTargetId('serviceInformationDataTable');
  166. }
  167. }