TemplateInstallProvider.php 7.2 KB

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