| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS Product developed. (27.03.19)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Providers;
- use MGProvision\Proxmox\v2\Api;
- use MGProvision\Proxmox\v2\models\Kvm;
- use MGProvision\Proxmox\v2\models\Lxc;
- use MGProvision\Proxmox\v2\models\NetworkDeviceLxc;
- use MGProvision\Proxmox\v2\ProxmoxApiException;
- use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
- use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
- use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
- use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\IpSetIpFilterService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
- use ModulesGarden\ProxmoxAddon\App\Services\Cloud\VirtualInterfaceConverter;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
- use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
- use Illuminate\Database\Capsule\Manager as DB;
- class VirtualInterfaceProvider extends BaseModelDataProvider implements ClientArea
- {
- use ProductService;
- use ApiService;
- /**
- * @var NetworkService
- */
- private $networkService;
- /**
- * VmUpdateProvider constructor.
- */
- public function __construct()
- {
- $this->model = new VirtualInterface();
- $this->networkService = new NetworkService();
- }
- public function read()
- {
- $this->availableValues['vn_id']['public'] = sl('lang')->abtr('Public');
- $resurceManager = new ResourceManager();
- $resurceManager->vmIds([\ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id]);
- if(!$resurceManager->virtualInterfaces()->getMax() || $resurceManager->virtualInterfaces()->free()){
- foreach (VirtualNetwork::ofHostingId($this->getWhmcsParamByKey('serviceid'))->select('id', 'name')->get() as $vn) {
- $this->availableValues['vn_id'][$vn->id] = $vn->name;
- }
- }
- if ($this->getActionElementIdValue()) {
- $this->data['id'] = $this->getActionElementIdValue();
- }
- }
- public function create()
- {
- $this->model->fill($this->getFormDataValues());
- $this->model->hosting_id = $this->getWhmcsParamByKey('serviceid');
- $this->model->vm_id = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id;
- $this->model->ip_long = ip2long($this->model->ip);
- $virtualNetworkId = $this->getRequestValue('formData')['vn_id'];
- $this->model->vn_id = $virtualNetworkId == "public" ? 0 : $virtualNetworkId;
- try {
- DB::beginTransaction();
- Api::beginTransaction();
- if($virtualNetworkId == "public" ){
- VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->ofIp($this->model->ip)
- ->update(['vm_id' => $this->model->vm_id]);
- }
- $this->model->save();
- $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
- $conveter = new VirtualInterfaceConverter([$this->model], $vm);
- if($vm instanceof Kvm && $vm->isRunning()){
- $conveter->convert();
- $cdrom = $vm->getCdRomRepository();
- if($cdrom->hasCloudInit() ){
- $cloudInitDrive = $cdrom->getCloudInitDrive();
- }
- try {
- $networkDevices = $conveter->getNetworkDevicesAsConfig();
- $ipConfig = $conveter->getIpConfigsAsConfig();
- if(!empty($networkDevices)){
- $vm->updateConfig($networkDevices);
- }
- if(!empty($ipConfig)){
- $vm->updateConfig($ipConfig);
- }
- //Regenerate image
- if($cloudInitDrive ){
- $cloudInitDrive->unmount();
- $cloudInitDrive->mount();
- }
- }catch (ProxmoxApiException $ex) {
- if(preg_match("/hotplug problem/", $ex->getMessage())){
- $vm->updateConfig($conveter->getIpConfigsAsConfig());
- //Regenerate image
- if($cloudInitDrive ){
- $cloudInitDrive->unmount();
- $cloudInitDrive->mount();
- }
- }else{
- throw $ex;
- }
- }
- }else{
- //Update container
- $config = $conveter->asConfig();
- if(!empty($config)){
- $vm->updateConfig($config);
- }
- }
- DB::commit();
- if ($this->configuration()->isIpsetIpFilter()){
- $ipSetFilterService = new IpSetIpFilterService();
- $ipSetFilterService->create();
- }
- } catch (\Exception $ex) {
- DB::rollBack();
- Api::commit();
- throw $ex;
- }
- return (new HtmlDataJsonResponse())
- ->setStatusSuccess()
- ->setMessageAndTranslate('The Virtual interface has been created successfully')
- ->addData('createButtonStatus', false)
- ->addRefreshTargetId('virtualInterfaceDataTable')
- ->addRefreshTargetId('networkDataTable')
- ->setCallBackFunction('pmToggleButton');
- }
- public function update()
- {
- }
- public function delete()
- {
- $vi = $this->model->where('id', $this->getFormDataValues()['id'])
- ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
- ->firstOrFail();
- if(!$this->canDelete($vi)){
- sl("lang")->addReplacementConstant("ip", $vi->ip);
- return (new HtmlDataJsonResponse())
- ->setStatusError()
- ->setMessageAndTranslate('Delete the primary Ip Address :ip: is restricted.');
- }
- $this->networkService->deleteByIpAddresses([$vi]);
- if ($this->configuration()->isIpsetIpFilter()){
- $ipSetFilterService = new IpSetIpFilterService();
- $ipSetFilterService->create();
- }
- return (new HtmlDataJsonResponse())
- ->setStatusSuccess()
- ->setMessageAndTranslate('The Virtual :name: network has been deleted successfully')
- ->addData('createButtonStatus', true)
- ->addRefreshTargetId('virtualInterfaceDataTable')
- ->addRefreshTargetId('networkDataTable')
- ->setCallBackFunction('pmToggleButton');
- }
- protected function canDelete(VirtualInterface $vi){
- if(!$this->configuration()->isOneNetworkDevice()){
- return true;
- }
- if( $vi->net != "net0" ){
- return true;
- }
- $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
- if($vm instanceof Kvm && $vm->getIpConfig()->findByNetworkId("net0")->fetch()[0]->getIp(false) == $vi->ip ){
- return false;
- }
- if($vm instanceof Lxc && $vm->findNetworkDevice($vi->ip) instanceof NetworkDeviceLxc){
- return false;
- }
- return true;
- }
- }
|