TemplateInstallProvider.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\models\Kvm;
  21. use MGProvision\Proxmox\v2\repository\FirewallRulesRepository;
  22. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\MigrateVmJob;
  23. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\BackupVmJob;
  24. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\CreateVmJob;
  25. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\DeleteVmJob;
  26. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\UpdateSnippet;
  27. use ModulesGarden\ProxmoxAddon\App\Libs\Format;
  28. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  29. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
  30. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  31. use ModulesGarden\ProxmoxAddon\App\Traits\Vps\SnippetTrait;
  32. use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
  33. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
  34. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  35. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  36. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  37. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  38. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  39. class TemplateInstallProvider extends BaseDataProvider implements ClientArea, AdminArea
  40. {
  41. use ProductService;
  42. use ApiService;
  43. use HostingService;
  44. use SnippetTrait;
  45. public function read()
  46. {
  47. if ($this->actionElementId)
  48. {
  49. $this->data['id'] = $this->actionElementId;
  50. $this->data['password'] = $this->getWhmcsCustomField('cipassword') ? $this->getWhmcsCustomField('cipassword') : htmlspecialchars($this->getWhmcsParamByKey("password"));
  51. }
  52. }
  53. public function update()
  54. {
  55. }
  56. public function processLxc()
  57. {
  58. if($this->jobExist()){
  59. return (new HtmlDataJsonResponse())
  60. ->setStatusError()
  61. ->setMessageAndTranslate("Task 'Reinstall' already exist");
  62. }
  63. $firewalRules = new FirewallRulesRepository();
  64. $firewalRules->setApi($this->api());
  65. $firewalRules->findByVm($this->vm());
  66. $arguments = [
  67. 'hostingId' => $this->getWhmcsParamByKey('serviceid'),
  68. "osTemplate" => $this->formData['id'],
  69. "password" => encrypt(html_entity_decode($this->formData['password'],ENT_QUOTES)),
  70. "firewallRules" => $firewalRules->fetchAsArray()
  71. ];
  72. if ($this->configuration()->isBackupVmBeforeReinstall())
  73. {
  74. $job = queue(BackupVmJob::class, $arguments, null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  75. }
  76. $job = queue(DeleteVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  77. $job = queue(CreateVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  78. return (new HtmlDataJsonResponse())
  79. ->setStatusSuccess()
  80. ->setMessageAndTranslate('The virtual server is being reinstalled')
  81. ->setCallBackFunction('mgLocationReload');
  82. }
  83. private function jobExist(){
  84. return Job::where("job", CreateVmJob::class. '@handle')->whereIn("status", ['waiting', "running", ""])
  85. ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
  86. ->where("rel_type", "hosting")->count() > 0;
  87. }
  88. public function processQemu()
  89. {
  90. if($this->jobExist()){
  91. return (new HtmlDataJsonResponse())
  92. ->setStatusError()
  93. ->setMessageAndTranslate("Task 'Reinstall' already exist");
  94. }
  95. list($node, $vmid) = explode("/", $this->formData['id']);
  96. $vm = new Kvm($node, $vmid);
  97. $vm->setApi($this->api());
  98. if($vm->config()['template'] !=1){
  99. sl("lang")->addReplacementConstant("vmid", $vmid);
  100. return (new HtmlDataJsonResponse())
  101. ->setStatusError()
  102. ->setMessageAndTranslate('Reinstall protection, the VMID :vmid: is not KVM Template');
  103. }
  104. $templateSize = $vm->getMasterHardDisk()->getBytes();
  105. if ($templateSize > $this->vm()->getMasterHardDisk()->getBytes())
  106. {
  107. sl("lang")->addReplacementConstant("size", Format::convertBytes($templateSize));
  108. return (new HtmlDataJsonResponse())
  109. ->setStatusError()
  110. ->setMessageAndTranslate('OS Template require disk size :size:');
  111. }
  112. //automaticaly configure administartor username for window
  113. $this->setHostingId($this->getWhmcsParamByKey('serviceid'));
  114. if( $this->configuration()->isAgent() &&
  115. $this->configuration()->isAgentServicePassword() &&
  116. !$this->configuration()->isAgentTemplateUser() &&
  117. preg_match('/w/', $vm ->config()['ostype'])){
  118. $ciuser = $vm->config()['ciuser'];
  119. $this->hosting()->update(['username' => $ciuser ?: 'Administrator' ]);
  120. }
  121. elseif( $vm->config()['ciuser']){
  122. $this->hosting()->update(['username' => $vm->config()['ciuser'] ]);
  123. }
  124. elseif($this->configuration()->isQemu() && $this->configuration()->getCiuser()){
  125. $this->hosting()->update(['username' =>$this->configuration()->getCiuser() ]);
  126. }
  127. $firewalRules = new FirewallRulesRepository();
  128. $firewalRules->setApi($this->api());
  129. $firewalRules->findByVm($this->vm());
  130. $arguments = [
  131. 'hostingId' => $this->getWhmcsParamByKey('serviceid'),
  132. "osTemplate" => $this->formData['id'],
  133. "password" => encrypt(html_entity_decode($this->formData['password'],ENT_QUOTES)),
  134. "firewallRules" => $firewalRules->fetchAsArray()
  135. ];
  136. if ($this->configuration()->isBackupVmBeforeReinstall())
  137. {
  138. $job = queue(BackupVmJob::class, $arguments, null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  139. }
  140. if($this->configuration()->isQemu() && $this->hasSnippet()){
  141. $job = queue(UpdateSnippet::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  142. }
  143. $job = queue(DeleteVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  144. $job = queue(CreateVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  145. return (new HtmlDataJsonResponse())
  146. ->setStatusSuccess()
  147. ->setMessageAndTranslate('The virtual server is being reinstalled')
  148. ->setCallBackFunction('mgLocationReload');
  149. }
  150. public function iso()
  151. {
  152. }
  153. }