IsoInstallProvider.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Reinstall\Providers;
  20. use MGProvision\Proxmox\v2\repository\FileRepository;
  21. use MGProvision\Proxmox\v2\repository\SnapshotRepository;
  22. use MGProvision\Proxmox\v2\repository\StorageRepository;
  23. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\HighAvailabilityClusterService;
  25. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\IpSetIpFilterService;
  26. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  27. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  28. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  29. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  30. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  31. class IsoInstallProvider extends BaseDataProvider implements ClientArea
  32. {
  33. use ProductService;
  34. use ApiService;
  35. /**
  36. * @var HighAvailabilityClusterService
  37. */
  38. private $highAvailabilityClusterService;
  39. /**
  40. * @var IpSetIpFilterService
  41. */
  42. private $ipSetIpFilterService;
  43. public function read()
  44. {
  45. if ($this->actionElementId)
  46. {
  47. $this->data['id'] = $this->actionElementId;
  48. }
  49. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  50. $storageRepository = new StorageRepository();
  51. $storageRepository->findByNodes([$vm->getNode()])
  52. ->findEnabed();
  53. $storages = $storageRepository->fetchAsArray();
  54. $isoRepository = new FileRepository();
  55. $isoRepository->findByNodes([$vm->getNode()])
  56. ->findByStorages($storages);
  57. $isoRepository->findIso();
  58. $this->availableValues['secondaryIsoImage'][0] = sl('lang')->abtr("None");
  59. foreach ($isoRepository->fetch() as $entity)
  60. {
  61. if ($this->configuration()->isPermissionSecondaryIsoImages() && !in_array($entity->getVolid(), $this->configuration()->getPermissionSecondaryIsoImages()))
  62. {
  63. continue;
  64. }
  65. $this->availableValues['secondaryIsoImage'][$entity->getVolid()] = sl('lang')->abtr( "template", $entity->getFriendlyName());
  66. }
  67. }
  68. public function update()
  69. {
  70. }
  71. private function initServices()
  72. {
  73. $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
  74. $this->ipSetIpFilterService = new IpSetIpFilterService();
  75. }
  76. public function processQemu()
  77. {
  78. $this->initServices();
  79. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  80. //Empyty template
  81. if (!$this->formData['id'])
  82. {
  83. throw new \Exception ("Provide OS Template");
  84. }
  85. $spanshootRepostiory = new SnapshotRepository();
  86. $spanshootRepostiory->findByVm($vm)
  87. ->ignoreCurrent(true);
  88. if ($spanshootRepostiory->count())
  89. {
  90. return (new HtmlDataJsonResponse())
  91. ->setStatusError()
  92. ->setMessageAndTranslate('Remove the snapshots before reinstallation.');
  93. }
  94. \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->data['osTemplate'] = null;
  95. \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->save();
  96. //ha
  97. if ($this->highAvailabilityClusterService->exist())
  98. {
  99. $this->highAvailabilityClusterService->delete();
  100. }
  101. //stop
  102. if ($vm->isRunning())
  103. {
  104. $vm->stop();
  105. }
  106. //Remove cloud-init drive
  107. foreach ($vm->config() as $id => $v)
  108. {
  109. if (preg_match('/cloudinit/', $v))
  110. {
  111. $vm->deleteConfig($id);
  112. }
  113. }
  114. //insert unattended iso
  115. $cdroms = $vm->getCdRom()->fetch();
  116. if(!$cdroms[0] || ($this->formData['secondaryIsoImage'] && !$cdroms[1])){
  117. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("CD-ROM volume not found");
  118. }
  119. $cdroms[0]->setLocation($this->formData['id'])->update();
  120. //secondaryIsoImage
  121. if($this->formData['secondaryIsoImage'] ){
  122. $cdroms[1]->setLocation($this->formData['secondaryIsoImage'])->update();
  123. }
  124. //remove hard drive and recreate a new one, (the drive have to be empty)
  125. $masterHdd = $vm->getMasterHardDisk();
  126. $masterHdd->delete();
  127. sleep(1);
  128. //Delete Unused Disk
  129. foreach ($vm->config(true) as $id => $v)
  130. {
  131. if (preg_match('/unused/', $id))
  132. {
  133. $vm->deleteConfig($id);
  134. }
  135. }
  136. //create new disk
  137. $bytes = $masterHdd->getBytes();
  138. $size = (int)$bytes / pow(1024, 3);
  139. $masterHdd->setSize($size);
  140. $masterHdd->create();
  141. sleep(1);
  142. //Boot disk
  143. $vm->updateConfig(['bootdisk' => $masterHdd->getId()]);
  144. //set bootorder to disk, cdrom, network (if cdrom would be first, the installer will run on every boot -> bootloop)
  145. $vm->changeBootOrder('cdn');
  146. //ipset
  147. if ($this->configuration()->isIpsetIpFilter())
  148. {
  149. $this->ipSetIpFilterService->create();
  150. }
  151. //HA
  152. if ($this->configuration()->getClusterState())
  153. {
  154. $this->highAvailabilityClusterService->create();
  155. }
  156. //start vps
  157. $vm->start();
  158. //show message that installation is now processing. The login information will be available in the noVNC console after installation.
  159. return (new HtmlDataJsonResponse())
  160. ->setStatusSuccess()
  161. ->setMessageAndTranslate('Installation is now processing. The login information will be available in the noVNC console.');
  162. }
  163. }