TemplateInstallProvider.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 ModulesGarden\ProxmoxAddon\App\Jobs\Vps\MigrateVmJob;
  22. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\BackupVmJob;
  23. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\CreateVmJob;
  24. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\DeleteVmJob;
  25. use ModulesGarden\ProxmoxAddon\App\Libs\Format;
  26. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  27. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
  28. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  29. use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
  30. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
  31. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  32. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  33. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  34. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  35. use function ModulesGarden\Servers\ProxmoxVps\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'] = $this->getWhmcsCustomField('cipassword') ? $this->getWhmcsCustomField('cipassword') : htmlspecialchars($this->getWhmcsParamByKey("password"));
  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. $arguments = [
  60. 'hostingId' => $this->getWhmcsParamByKey('serviceid'),
  61. "osTemplate" => $this->formData['id'],
  62. "password" => encrypt(html_entity_decode($this->formData['password'],ENT_QUOTES)),
  63. ];
  64. if ($this->configuration()->isBackupVmBeforeReinstall())
  65. {
  66. $job = queue(BackupVmJob::class, $arguments, null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  67. }
  68. $job = queue(DeleteVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  69. $job = queue(CreateVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  70. return (new HtmlDataJsonResponse())
  71. ->setStatusSuccess()
  72. ->setMessageAndTranslate('The virtual server is being reinstalled')
  73. ->setCallBackFunction('mgLocationReload');
  74. }
  75. private function jobExist(){
  76. return Job::where("job", CreateVmJob::class. '@handle')->whereIn("status", ['waiting', "running", ""])
  77. ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
  78. ->where("rel_type", "hosting")->count() > 0;
  79. }
  80. public function processQemu()
  81. {
  82. if($this->jobExist()){
  83. return (new HtmlDataJsonResponse())
  84. ->setStatusError()
  85. ->setMessageAndTranslate("Task 'Reinstall' already exist");
  86. }
  87. list($node, $vmid) = explode("/", $this->formData['id']);
  88. $vm = new Kvm($node, $vmid);
  89. $vm->setApi($this->api());
  90. $templateSize = $vm->getMasterHardDisk()->getBytes();
  91. if ($templateSize > $this->vm()->getMasterHardDisk()->getBytes())
  92. {
  93. sl("lang")->addReplacementConstant("size", Format::convertBytes($templateSize));
  94. return (new HtmlDataJsonResponse())
  95. ->setStatusError()
  96. ->setMessageAndTranslate('OS Template require disk size :size:');
  97. }
  98. //automaticaly configure administartor username for window
  99. $this->setHostingId($this->getWhmcsParamByKey('serviceid'));
  100. if( $this->configuration()->isAgent() &&
  101. $this->configuration()->isAgentServicePassword() &&
  102. !$this->configuration()->isAgentTemplateUser() &&
  103. preg_match('/w/', $vm ->config()['ostype'])){
  104. $ciuser = $vm->config()['ciuser'];
  105. $this->hosting()->update(['username' => $ciuser ?: 'Administrator' ]);
  106. }
  107. elseif($this->configuration()->isQemu() && $this->configuration()->getCiuser()){
  108. $this->hosting()->update(['username' =>$this->configuration()->getCiuser() ]);
  109. }
  110. $arguments = [
  111. 'hostingId' => $this->getWhmcsParamByKey('serviceid'),
  112. "osTemplate" => $this->formData['id'],
  113. "password" => encrypt(html_entity_decode($this->formData['password'],ENT_QUOTES)),
  114. ];
  115. if ($this->configuration()->isBackupVmBeforeReinstall())
  116. {
  117. $job = queue(BackupVmJob::class, $arguments, null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  118. }
  119. $job = queue(DeleteVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  120. $job = queue(CreateVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
  121. return (new HtmlDataJsonResponse())
  122. ->setStatusSuccess()
  123. ->setMessageAndTranslate('The virtual server is being reinstalled')
  124. ->setCallBackFunction('mgLocationReload');
  125. }
  126. public function iso()
  127. {
  128. }
  129. }