VirtualInterfaceProvider.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\VirtualInterface\Providers;
  20. use MGProvision\Proxmox\v2\Api;
  21. use MGProvision\Proxmox\v2\models\Kvm;
  22. use MGProvision\Proxmox\v2\ProxmoxApiException;
  23. use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
  24. use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
  25. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  26. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  27. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\IpSetIpFilterService;
  28. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
  29. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  30. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\VirtualInterfaceConverter;
  31. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  32. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  33. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
  34. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  35. use Illuminate\Database\Capsule\Manager as DB;
  36. class VirtualInterfaceProvider extends BaseModelDataProvider implements ClientArea
  37. {
  38. use ProductService;
  39. use ApiService;
  40. /**
  41. * @var NetworkService
  42. */
  43. private $networkService;
  44. /**
  45. * VmUpdateProvider constructor.
  46. */
  47. public function __construct()
  48. {
  49. $this->model = new VirtualInterface();
  50. $this->networkService = new NetworkService();
  51. }
  52. public function read()
  53. {
  54. $this->availableValues['vn_id']['public'] = sl('lang')->abtr('Public');
  55. foreach (VirtualNetwork::ofHostingId($this->getWhmcsParamByKey('serviceid'))->select('id', 'name')->get() as $vn) {
  56. $this->availableValues['vn_id'][$vn->id] = $vn->name;
  57. }
  58. if ($this->getActionElementIdValue()) {
  59. $this->data['id'] = $this->getActionElementIdValue();
  60. }
  61. }
  62. public function create()
  63. {
  64. $this->model->fill($this->getFormDataValues());
  65. $this->model->hosting_id = $this->getWhmcsParamByKey('serviceid');
  66. $this->model->vm_id = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id;
  67. $this->model->ip_long = ip2long($this->model->ip);
  68. $virtualNetworkId = $this->getRequestValue('formData')['vn_id'];
  69. $this->model->vn_id = $virtualNetworkId == "public" ? 0 : $virtualNetworkId;
  70. try {
  71. DB::beginTransaction();
  72. Api::beginTransaction();
  73. if($virtualNetworkId == "public" ){
  74. VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  75. ->ofIp($this->model->ip)
  76. ->update(['vm_id' => $this->model->vm_id]);
  77. }
  78. $this->model->save();
  79. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  80. $conveter = new VirtualInterfaceConverter([$this->model], $vm);
  81. if($vm instanceof Kvm && $vm->isRunning()){
  82. $conveter->convert();
  83. $cdrom = $vm->getCdRomRepository();
  84. if($cdrom->hasCloudInit() ){
  85. $cloudInitDrive = $cdrom->getCloudInitDrive();
  86. }
  87. try {
  88. $vm->updateConfig($conveter->getNetworkDevicesAsConfig());
  89. $vm->updateConfig($conveter->getIpConfigsAsConfig());
  90. //Regenerate image
  91. if($cloudInitDrive ){
  92. $cloudInitDrive->unmount();
  93. $cloudInitDrive->mount();
  94. }
  95. }catch (ProxmoxApiException $ex) {
  96. if(preg_match("/hotplug problem/", $ex->getMessage())){
  97. $vm->updateConfig($conveter->getIpConfigsAsConfig());
  98. //Regenerate image
  99. if($cloudInitDrive ){
  100. $cloudInitDrive->unmount();
  101. $cloudInitDrive->mount();
  102. }
  103. }else{
  104. throw $ex;
  105. }
  106. }
  107. }else{
  108. //Update container
  109. $vm->updateConfig($conveter->asConfig());
  110. }
  111. DB::commit();
  112. if ($this->configuration()->isIpsetIpFilter()){
  113. $ipSetFilterService = new IpSetIpFilterService();
  114. $ipSetFilterService->create();
  115. }
  116. } catch (\Exception $ex) {
  117. DB::rollBack();
  118. Api::commit();
  119. throw $ex;
  120. }
  121. return (new HtmlDataJsonResponse())
  122. ->setStatusSuccess()
  123. ->setMessageAndTranslate('The Virtual interface has been created successfully')
  124. ->addData('createButtonStatus', false)
  125. ->addRefreshTargetId('virtualInterfaceDataTable')
  126. ->addRefreshTargetId('networkDataTable')
  127. ->setCallBackFunction('pmToggleButton');
  128. }
  129. public function update()
  130. {
  131. }
  132. public function delete()
  133. {
  134. $vi = $this->model->where('id', $this->getFormDataValues()['id'])
  135. ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
  136. ->firstOrFail();
  137. $this->networkService->deleteByIpAddresses([$vi]);
  138. if ($this->configuration()->isIpsetIpFilter()){
  139. $ipSetFilterService = new IpSetIpFilterService();
  140. $ipSetFilterService->create();
  141. }
  142. return (new HtmlDataJsonResponse())
  143. ->setStatusSuccess()
  144. ->setMessageAndTranslate('The Virtual :name: network has been deleted successfully')
  145. ->addData('createButtonStatus', true)
  146. ->addRefreshTargetId('virtualInterfaceDataTable')
  147. ->addRefreshTargetId('networkDataTable')
  148. ->setCallBackFunction('pmToggleButton');
  149. }
  150. }