VirtualInterfaceProvider.php 8.1 KB

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