TemplateProvider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Templates\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 RangeTemplateProvider
  28. *
  29. * @author Pawel Kopec <pawelk@modulesgardne.com>
  30. */
  31. class TemplateProvider 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. $this->getVm($this->formData['node'], $this->formData['vmid'], $this->formData['type'])->delete();
  48. main\Core\ServiceLocator::call('lang')->addReplacementConstant('name', $this->formData['name']);
  49. sleep(1);
  50. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Deleting template :name: in progress');
  51. }
  52. private function getVm($node, $vmid, $type)
  53. {
  54. switch ($type)
  55. {
  56. case 'qemu' :
  57. $vm = new proxmox\models\Kvm($node, $vmid);
  58. break;
  59. case 'lxc':
  60. $vm = new proxmox\models\Lxc($node, $vmid);
  61. break;
  62. default :
  63. throw new \Exception(sprintf("Unkown virtualization %s type", $type));
  64. }
  65. return $vm;
  66. }
  67. public function deleteMass()
  68. {
  69. if (!$this->getRequestValue('massActions'))
  70. {
  71. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Bug no massActions data!')->setStatusError();
  72. return;
  73. }
  74. $this->setServerId($this->getRequestValue('id'));
  75. $this->getApi()->setInstance();
  76. foreach ($this->getRequestValue('massActions') as $id)
  77. {
  78. $data = json_decode(base64_decode($id), true);
  79. $this->getVm($data['node'], $data['vmid'], $data['type'])->delete();
  80. }
  81. sleep(1);
  82. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Deleting the selected templates in progress');
  83. }
  84. public function update()
  85. {
  86. $this->setServerId($this->formData['serverId']);
  87. $this->getApi()->setInstance();
  88. //Update ciuser
  89. if ($this->formData['ciuser'])
  90. {
  91. $this->getVm($this->formData['node'], $this->formData['vmid'], $this->formData['type'])->updateConfig(["ciuser" => $this->formData['ciuser']]);
  92. }
  93. else
  94. {
  95. //Delete ciuser
  96. $this->getVm($this->formData['node'], $this->formData['vmid'], $this->formData['type'])->deleteConfig('ciuser');
  97. }
  98. main\Core\ServiceLocator::call('lang')->addReplacementConstant('name', $this->formData['name']);
  99. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Template :name: has been updated.');
  100. }
  101. public function create()
  102. {
  103. $this->setServerId($this->formData['serverId']);
  104. $this->getApi()->setInstance();
  105. $resurcesRepostiory = new proxmox\repository\ClusterResourcesRepository();
  106. $resurcesRepostiory->findVm();
  107. foreach ($resurcesRepostiory->fetch() as $resurce)
  108. {
  109. if ($resurce->getVmid() == $this->formData['vmid'])
  110. {
  111. $vm = $resurce->getVm();
  112. break;
  113. }
  114. }
  115. if (!$vm)
  116. {
  117. main\Core\ServiceLocator::call('lang')->addReplacementConstant('vmid', $this->formData['vmid']);
  118. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Virtual Machine :vmid: has not been found')->setStatusError();
  119. }
  120. /* @var $vm proxmox\models\Kvm */
  121. if ($vm->isRunning())
  122. {
  123. $vm->stop();
  124. sleep(20);
  125. }
  126. if ($this->formData['description'])
  127. {
  128. $vm->updateConfig(["description" => $this->formData['description']]);
  129. sleep(2);
  130. }
  131. $vm->convertToTemplate();
  132. sleep(1);
  133. main\Core\ServiceLocator::call('lang')->addReplacementConstant('name', $resurce->getName());
  134. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Virtual Machine :name: has been converted to template successfully.');
  135. }
  136. }