TemplateInstallProvider.php 6.7 KB

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