IsoInstallProvider.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\ProxmoxVps\App\UI\Reinstall\Providers;
  20. use MGProvision\Proxmox\v2\repository\SnapshotRepository;
  21. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  22. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HighAvailabilityClusterService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Vps\IpSetIpFilterService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  26. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  27. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  28. class IsoInstallProvider extends BaseDataProvider implements ClientArea
  29. {
  30. use ProductService;
  31. use ApiService;
  32. /**
  33. * @var HighAvailabilityClusterService
  34. */
  35. private $highAvailabilityClusterService;
  36. /**
  37. * @var IpSetIpFilterService
  38. */
  39. private $ipSetIpFilterService;
  40. public function read()
  41. {
  42. if ($this->actionElementId)
  43. {
  44. $this->data['id'] = $this->actionElementId;
  45. }
  46. }
  47. public function update()
  48. {
  49. }
  50. private function initServices()
  51. {
  52. $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
  53. $this->ipSetIpFilterService = new IpSetIpFilterService();
  54. }
  55. public function processQemu()
  56. {
  57. $this->initServices();
  58. //Empyty template
  59. if (!$this->formData['id'])
  60. {
  61. throw new \Exception ("Provide OS Template");
  62. }
  63. $spanshootRepostiory = new SnapshotRepository();
  64. $spanshootRepostiory->findByVm($this->vm())
  65. ->ignoreCurrent(true);
  66. if ($spanshootRepostiory->count())
  67. {
  68. return (new HtmlDataJsonResponse())
  69. ->setStatusError()
  70. ->setMessageAndTranslate('Remove the snapshots before reinstallation.');
  71. }
  72. //ha
  73. if ($this->highAvailabilityClusterService->exist())
  74. {
  75. $this->highAvailabilityClusterService->delete();
  76. }
  77. //stop
  78. if ($this->vm()->isRunning())
  79. {
  80. $this->vm()->stop();
  81. }
  82. //Remove cloud-init drive
  83. foreach ($this->vm()->config() as $id => $v)
  84. {
  85. if (preg_match('/cloudinit/', $v))
  86. {
  87. $this->vm()->deleteConfig($id);
  88. }
  89. }
  90. //insert unattended iso
  91. $this->vm()->changeIso($this->formData['id']);
  92. //remove hard drive and recreate a new one, (the drive have to be empty)
  93. $masterHdd = $this->vm()->getMasterHardDisk();
  94. $masterHdd->delete();
  95. sleep(1);
  96. //Delete Unused Disk
  97. foreach ($this->vm()->config(true) as $id => $v)
  98. {
  99. if (preg_match('/unused/', $id))
  100. {
  101. $this->vm()->deleteConfig($id);
  102. }
  103. }
  104. //create new disk
  105. $bytes = $masterHdd->getBytes();
  106. $size = (int)$bytes / pow(1024, 3);
  107. $masterHdd->setSize($size);
  108. $masterHdd->create();
  109. sleep(1);
  110. //Boot disk
  111. $this->vm()->updateConfig(['bootdisk' => $masterHdd->getId()]);
  112. //set bootorder to disk, cdrom, network (if cdrom would be first, the installer will run on every boot -> bootloop)
  113. $this->vm()->changeBootOrder('cdn');
  114. //ipset
  115. if ($this->configuration()->isIpsetIpFilter())
  116. {
  117. $this->ipSetIpFilterService->create();
  118. }
  119. //HA
  120. if ($this->configuration()->getClusterState())
  121. {
  122. $this->highAvailabilityClusterService->create();
  123. }
  124. //start vps
  125. $this->vm()->start();
  126. //show message that installation is now processing. The login information will be available in the noVNC console after installation.
  127. return (new HtmlDataJsonResponse())
  128. ->setStatusSuccess()
  129. ->setMessageAndTranslate('Installation is now processing. The login information will be available in the noVNC console.');
  130. }
  131. }