VmUpdateProvider.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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['ciuser'] = $config['ciuser'];
  48. $this->data['cipassword'] = $config['cipassword'];
  49. $this->data['description'] = html_entity_decode($config['description']);
  50. $this->data['searchdomain'] = $config['searchdomain'];
  51. $ns = explode(" ",$config['nameserver']);
  52. $this->data['nameserver[0]'] = $ns[0];
  53. $this->data['nameserver[1]'] = $ns[1];
  54. /**
  55. * @deprecated
  56. $this->data['ipv4'] = $this->vmModel->ipv4Addresses->count();
  57. $this->data['ipv6'] = $this->vmModel->ipv6Addresses->count();
  58. */
  59. //sshkeys
  60. $this->data['sshkeys'] = rawurldecode($vm->config()['sshkeys']);
  61. //cpu Priority
  62. if($this->configuration()->hasCpuPriority()){
  63. for($i=1; $i<=5; $i++){
  64. $cpuunits = $this->configuration()->get('cpuunitsPriority'.$i);
  65. $cpulimit = $this->configuration()->get('cpulimitPriority'.$i);
  66. if($this->vmModel->cpulimit >= $cpulimit){
  67. $this->data['cpuPriority'] = $i;
  68. }
  69. if($this->vmModel->cpuunits >= $cpuunits){
  70. $this->data['cpuPriority'] = $i;
  71. }
  72. }
  73. }
  74. //iso
  75. if ($this->configuration()->isQemu() && $this->configuration()->isPermissionIsoImage()) {
  76. $this->availableValues['iso'] = ["none" => sl('lang')->abtr("None")];
  77. $storageRepository = new StorageRepository();
  78. $storageRepository->findByNodes([$vm->getNode()])
  79. ->findEnabed();
  80. $storages = $storageRepository->fetchAsArray();
  81. $isoRepository = new FileRepository();
  82. $isoRepository->findByNodes([$vm->getNode()])
  83. ->findByStorages($storages);
  84. $isoRepository->findIso();
  85. foreach ($isoRepository->fetch() as $entity) {
  86. if ($this->configuration()->isPermissionIsoImages() && !in_array($entity->getVolid(), $this->configuration()->getPermissionIsoImages())) {
  87. continue;
  88. }
  89. $this->availableValues['iso'][$entity->getVolid()] = $entity->getFriendlyName();
  90. }
  91. if( $vm->getCdRomRepository()->first()){
  92. $this->data['iso']= $vm->getCdRomRepository()->first()->getLocation();
  93. }
  94. }
  95. }
  96. public function update()
  97. {
  98. $this->vmModel = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel();
  99. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  100. $this->networkService = new NetworkService();
  101. $networkService = new NetworkService();
  102. /**
  103. * @deprecated
  104. * $ipv4 = (int) $this->formData['ipv4'] - (int) $this->vmModel->ipv4Addresses->count();
  105. * $ipv6 = (int) $this->formData['ipv6'] - (int) $this->vmModel->ipv6Addresses->count();
  106. * $networkService->hasIp($ipv4, $ipv6, $this->vmModel->node);
  107. * $networkService->addIp($ipv4, $ipv6, $this->vmModel->node);
  108. * $networkService->unassignIpAddressesAndDeleteNetwork($ipv4, $ipv6);
  109. */
  110. //load data
  111. $this->getFormDataValues();
  112. if ($vm instanceof Kvm)
  113. {
  114. //iso
  115. if ($vm->cdrom())
  116. {
  117. $vm->updateCdrom($this->formData['iso']);
  118. }
  119. //sshkeys
  120. if($this->configuration()->isPermissionSshkeys()){
  121. if ($this->formData['sshkeys'])
  122. {
  123. $vm->updateConfig(["sshkeys" => rawurlencode($this->formData['sshkeys'])]);
  124. } else if($vm->config()['sshkeys'])
  125. {
  126. $vm->deleteConfig('sshkeys');
  127. }
  128. }
  129. }
  130. try{
  131. Api::beginTransaction();
  132. DB::beginTransaction();
  133. $this->fillVmModel();
  134. if($this->getFormDataValues()['ciuser']){
  135. $vm->updateConfig(["ciuser" => $this->formData['ciuser']]);
  136. }
  137. if($this->getFormDataValues()['cipassword']){
  138. $vm->updateConfig(["cipassword" => $this->formData['cipassword']]);
  139. }
  140. if($this->configuration()->isCalculateSocketsAndCores()){
  141. $this->calculateSocketsAndCores();
  142. }
  143. $this->vmModel->save();
  144. if($this->configuration()->isQemu()){
  145. fire(new QemuUpdateEvent($this->vmModel, $this->formData));
  146. }
  147. if($this->configuration()->isLxc()){
  148. fire(new LxcUpdateEvent($this->vmModel, $this->formData));
  149. }
  150. $agentEnabled = (new AgentService())->isEnabled();
  151. if($agentEnabled && $this->configuration()->isAgentConfigureNetwork()){
  152. queue(ConfigureNetworkJob::class,
  153. ['hostingId' => $this->getWhmcsParamByKey('serviceid')],
  154. null,
  155. "hosting",
  156. $this->getWhmcsParamByKey("serviceid"),
  157. $this->vmModel->id
  158. );
  159. }
  160. if($agentEnabled && $this->configuration()->isAgentPassword()){
  161. queue(ChangePasswordJob::class,
  162. ['hostingId' => $this->getWhmcsParamByKey('serviceid')],
  163. null,
  164. "hosting",
  165. $this->getWhmcsParamByKey("serviceid"),
  166. $this->vmModel->id
  167. );
  168. }
  169. DB::commit();
  170. return (new HtmlDataJsonResponse())
  171. ->setMessageAndTranslate('Server has been updated')
  172. ->setCallBackFunction('pcVmUpdatedAjaxDone')
  173. ->addRefreshTargetId('serviceInformationDataTable')
  174. ->setCallBackFunction('mgLocationReload');
  175. }catch (\Exception $ex){
  176. DB::rollBack();
  177. Api::commit();
  178. return (new HtmlDataJsonResponse())
  179. ->setStatusError()
  180. ->setMessageAndTranslate($ex->getMessage());
  181. }
  182. }
  183. }