VmUpdateProvider.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmUpdate\Providers;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use MGProvision\Proxmox\v2\Api;
  5. use MGProvision\Proxmox\v2\models\Kvm;
  6. use MGProvision\Proxmox\v2\models\MountPoint;
  7. use MGProvision\Proxmox\v2\models\NetworkDeviceKvm;
  8. use MGProvision\Proxmox\v2\repository\FileRepository;
  9. use MGProvision\Proxmox\v2\repository\StorageRepository;
  10. use ModulesGarden\ProxmoxAddon\App\Events\Cloud\LxcUpdateEvent;
  11. use ModulesGarden\ProxmoxAddon\App\Events\Cloud\QemuUpdateEvent;
  12. use ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\Agent\ChangePasswordJob;
  13. use ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\Agent\ConfigureNetworkJob;
  14. use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
  15. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  16. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  17. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  18. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AdditionalDiskService;
  19. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AdditionalMountPointService;
  20. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AgentService;
  21. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\IpSetIpFilterService;
  22. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\VirtualInterfaceConverter;
  25. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Providers\VmCreateProvider;
  26. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  27. use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;
  28. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  29. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  30. class VmUpdateProvider extends VmCreateProvider
  31. {
  32. use ApiService;
  33. use ProductService;
  34. /**
  35. * VmUpdateProvider constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = VmModel::class;
  40. }
  41. public function read()
  42. {
  43. $this->vmModel = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel();
  44. $this->data = $this->vmModel->toArray();
  45. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  46. $config = $vm->config();
  47. $this->data['password'] = $this->vmModel->getPassword();
  48. $this->data['description'] = html_entity_decode($config['description']);
  49. $this->data['searchdomain'] = $config['searchdomain'];
  50. $ns = explode(" ",$config['nameserver']);
  51. $this->data['nameserver[0]'] = $ns[0];
  52. $this->data['nameserver[1]'] = $ns[1];
  53. /**
  54. * @deprecated
  55. $this->data['ipv4'] = $this->vmModel->ipv4Addresses->count();
  56. $this->data['ipv6'] = $this->vmModel->ipv6Addresses->count();
  57. */
  58. //Boot order
  59. if($vm instanceof Kvm){
  60. $bootOrder = $vm->getBootOrder();
  61. $this->data['bootDevice'] = $bootOrder[0];
  62. $options = [
  63. 0 => sl("lang")->tr("None"),
  64. "scsi0" => sl("lang")->tr("Disk"),
  65. "ide2" => sl("lang")->tr("CD-ROM"),
  66. "net0" => sl("lang")->tr("Network"),
  67. ];
  68. $this->availableValues['bootDevice'] = $options;
  69. }
  70. //sshkeys
  71. $this->data['sshkeys'] = rawurldecode($vm->config()['sshkeys']);
  72. //cpu Priority
  73. if($this->configuration()->hasCpuPriority()){
  74. for($i=1; $i<=5; $i++){
  75. $cpuunits = $this->configuration()->get('cpuunitsPriority'.$i);
  76. $cpulimit = $this->configuration()->get('cpulimitPriority'.$i);
  77. if($this->vmModel->cpulimit >= $cpulimit){
  78. $this->data['cpuPriority'] = $i;
  79. }
  80. if($this->vmModel->cpuunits >= $cpuunits){
  81. $this->data['cpuPriority'] = $i;
  82. }
  83. }
  84. }
  85. //iso
  86. if ($this->configuration()->isQemu() && $this->configuration()->isPermissionIsoImage()) {
  87. $this->availableValues['iso'] = ["none" => sl('lang')->abtr("None")];
  88. $storageRepository = new StorageRepository();
  89. $storageRepository->findByNodes([$vm->getNode()])
  90. ->findEnabed();
  91. $storages = $storageRepository->fetchAsArray();
  92. $isoRepository = new FileRepository();
  93. $isoRepository->findByNodes([$vm->getNode()])
  94. ->findByStorages($storages);
  95. $isoRepository->findIso();
  96. foreach ($isoRepository->fetch() as $entity) {
  97. if ($this->configuration()->isPermissionIsoImages() && !in_array($entity->getVolid(), $this->configuration()->getPermissionIsoImages())) {
  98. continue;
  99. }
  100. $this->availableValues['iso'][$entity->getVolid()] = $entity->getFriendlyName();
  101. }
  102. if( $vm->getCdRomRepository()->first()){
  103. $this->data['iso']= $vm->getCdRomRepository()->first()->getLocation();
  104. }
  105. }
  106. }
  107. public function update()
  108. {
  109. $this->vmModel = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel();
  110. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  111. $this->networkService = new NetworkService();
  112. $networkService = new NetworkService();
  113. /**
  114. * @deprecated
  115. * $ipv4 = (int) $this->formData['ipv4'] - (int) $this->vmModel->ipv4Addresses->count();
  116. * $ipv6 = (int) $this->formData['ipv6'] - (int) $this->vmModel->ipv6Addresses->count();
  117. * $networkService->hasIp($ipv4, $ipv6, $this->vmModel->node);
  118. * $networkService->addIp($ipv4, $ipv6, $this->vmModel->node);
  119. * $networkService->unassignIpAddressesAndDeleteNetwork($ipv4, $ipv6);
  120. */
  121. //load data
  122. $this->getFormDataValues();
  123. if ($vm instanceof Kvm)
  124. {
  125. //iso
  126. if ($vm->cdrom())
  127. {
  128. $vm->updateCdrom($this->formData['iso']);
  129. }
  130. //Boot Device
  131. //order=scsi0;ide0;ide1;net0
  132. logModuleCall(
  133. 'proxmoxCloud',
  134. __FUNCTION__,
  135. $this->formData,
  136. 'Debug',
  137. $this->data
  138. );
  139. $vm->updateConfig(['boot' => 'order=' . $this->formData['bootDevice']]);
  140. //sshkeys
  141. if($this->configuration()->isPermissionSshkeys()){
  142. if ($this->formData['sshkeys'])
  143. {
  144. $vm->updateConfig(["sshkeys" => rawurlencode($this->formData['sshkeys'])]);
  145. } else if($vm->config()['sshkeys'])
  146. {
  147. $vm->deleteConfig('sshkeys');
  148. }
  149. }
  150. }
  151. try{
  152. Api::beginTransaction();
  153. DB::beginTransaction();
  154. $this->fillVmModel();
  155. if($this->getFormDataValues()['password']){
  156. $this->vmModel->setPassword($this->getFormDataValues()['password']);
  157. }
  158. if($this->configuration()->isCalculateSocketsAndCores()){
  159. $this->calculateSocketsAndCores();
  160. }
  161. $this->vmModel->save();
  162. if($this->configuration()->isQemu()){
  163. fire(new QemuUpdateEvent($this->vmModel, $this->formData));
  164. }
  165. if($this->configuration()->isLxc()){
  166. fire(new LxcUpdateEvent($this->vmModel, $this->formData));
  167. }
  168. $agentEnabled = (new AgentService())->isEnabled();
  169. if($agentEnabled && $this->configuration()->isAgentConfigureNetwork()){
  170. queue(ConfigureNetworkJob::class,
  171. ['hostingId' => $this->getWhmcsParamByKey('serviceid')],
  172. null,
  173. "hosting",
  174. $this->getWhmcsParamByKey("serviceid"),
  175. $this->vmModel->id
  176. );
  177. }
  178. if($agentEnabled && $this->configuration()->isAgentPassword()){
  179. queue(ChangePasswordJob::class,
  180. ['hostingId' => $this->getWhmcsParamByKey('serviceid')],
  181. null,
  182. "hosting",
  183. $this->getWhmcsParamByKey("serviceid"),
  184. $this->vmModel->id
  185. );
  186. }
  187. DB::commit();
  188. return (new HtmlDataJsonResponse())
  189. ->setMessageAndTranslate('Server has been updated')
  190. ->setCallBackFunction('pcVmUpdatedAjaxDone')
  191. ->addRefreshTargetId('serviceInformationDataTable')
  192. ->setCallBackFunction('mgLocationReload');
  193. }catch (\Exception $ex){
  194. DB::rollBack();
  195. Api::commit();
  196. return (new HtmlDataJsonResponse())
  197. ->setStatusError()
  198. ->setMessageAndTranslate($ex->getMessage());
  199. }
  200. }
  201. }