TemplateProvider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $vm = $this->getVm($this->formData['node'], $this->formData['vmid'], $this->formData['type']);
  89. $vm->updateConfig(["ciuser" => $this->formData['ciuser'], "description" => $this->formData['description'] ]);
  90. //Update ciuser
  91. if ($this->formData['ciuser'])
  92. {
  93. $vm->updateConfig(["ciuser" => $this->formData['ciuser'], "description" => $this->formData['description'] ]);
  94. }
  95. else
  96. {
  97. //Delete ciuser
  98. $vm->deleteConfig('ciuser');
  99. }
  100. main\Core\ServiceLocator::call('lang')->addReplacementConstant('name', $this->formData['name']);
  101. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Template :name: has been updated.');
  102. }
  103. public function create()
  104. {
  105. $this->setServerId($this->formData['serverId']);
  106. $this->getApi()->setInstance();
  107. $resurcesRepostiory = new proxmox\repository\ClusterResourcesRepository();
  108. $resurcesRepostiory->findVm();
  109. foreach ($resurcesRepostiory->fetch() as $resurce)
  110. {
  111. if ($resurce->getVmid() == $this->formData['vmid'])
  112. {
  113. $vm = $resurce->getVm();
  114. break;
  115. }
  116. }
  117. if (!$vm)
  118. {
  119. main\Core\ServiceLocator::call('lang')->addReplacementConstant('vmid', $this->formData['vmid']);
  120. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Virtual Machine :vmid: has not been found')->setStatusError();
  121. }
  122. /* @var $vm proxmox\models\Kvm */
  123. if ($vm->isRunning())
  124. {
  125. $vm->stop();
  126. sleep(20);
  127. }
  128. $vm->updateConfig(["description" => $this->formData['description'], 'ciuser' => $this->formData['ciuser']]);
  129. sleep(2);
  130. $vm->convertToTemplate();
  131. sleep(1);
  132. main\Core\ServiceLocator::call('lang')->addReplacementConstant('name', $resurce->getName());
  133. return (new HtmlDataJsonResponse())->setMessageAndTranslate('Virtual Machine :name: has been converted to template successfully.');
  134. }
  135. }