VmProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Aug 23, 2018)
  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\ProxmoxAddon\App\UI\VmCleaner\Providers;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  23. use ModulesGarden\ProxmoxAddon\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  24. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  25. /**
  26. *
  27. * Description of RangeVmProvider
  28. *
  29. * @author Pawel Kopec <pawelk@modulesgardne.com>
  30. */
  31. class VmProvider extends BaseDataProvider implements AdminArea
  32. {
  33. use main\App\Services\BaseService;
  34. public function read()
  35. {
  36. if (!$this->actionElementId)
  37. {
  38. return false;
  39. }
  40. $this->data = json_decode(base64_decode($this->actionElementId), true);
  41. $this->data['serverId'] = $this->getRequestValue('id');
  42. }
  43. public function delete()
  44. {
  45. $this->setServerId($this->formData['serverId']);
  46. $this->getApi()->setInstance();
  47. $vm = $this->getVm($this->formData['node'], $this->formData['vmid'], $this->formData['type']);
  48. $haResurce = new proxmox\models\HaResource();
  49. $haResurce->setSid($vm->getVmid())
  50. ->setType($vm->getVirtualization() == "lxc" ? "ct" : "vm");
  51. if ($haResurce->exist())
  52. {
  53. $haResurce->delete();
  54. }
  55. $vm->delete();
  56. main\Core\ServiceLocator::call('lang')->addReplacementConstant('vmid', $this->formData['vmid']);
  57. sleep(1);
  58. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Deleting Virtual Machine :vmid: in progress');
  59. }
  60. private function getVm($node, $vmid, $type)
  61. {
  62. switch ($type)
  63. {
  64. case 'qemu' :
  65. $vm = new proxmox\models\Kvm($node, $vmid);
  66. break;
  67. case 'lxc':
  68. $vm = new proxmox\models\Lxc($node, $vmid);
  69. break;
  70. default :
  71. throw new \Exception(sprintf("Unkown virtualization %s type", $type));
  72. }
  73. return $vm;
  74. }
  75. public function deleteMass()
  76. {
  77. if (!$this->getRequestValue('massActions'))
  78. {
  79. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Bug no massActions data!')->setStatusError();
  80. return;
  81. }
  82. $this->setServerId($this->getRequestValue('id'));
  83. $this->getApi()->setInstance();
  84. foreach ($this->getRequestValue('massActions') as $id)
  85. {
  86. $data = json_decode(base64_decode($id), true);
  87. $this->getVm($data['node'], $data['vmid'], $data['type'])->delete();
  88. }
  89. sleep(1);
  90. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Deleting the selected Virtual Machines in progress');
  91. }
  92. public function update()
  93. {
  94. }
  95. }