CloudInitScriptConveter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services;
  3. use ModulesGarden\ProxmoxAddon\App\Models\CloudInitScript;
  4. use ModulesGarden\ProxmoxAddon\App\Models\Snippet;
  5. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  6. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  7. use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;
  8. use ModulesGarden\ProxmoxAddon\Core\Traits\Smarty;
  9. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  10. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  11. class CloudInitScriptConveter
  12. {
  13. use Smarty;
  14. use WhmcsParams;
  15. use ApiService;
  16. /**
  17. * @var CloudInitScript
  18. */
  19. protected $cloudInitScript;
  20. /**
  21. * @var ServerConfigurationRepository
  22. */
  23. protected $serverConfiguration;
  24. /**
  25. * @var VmModel|null
  26. */
  27. protected $vmModel;
  28. /**
  29. * CloudInitScriptConveter constructor.
  30. * @param CloudInitScript $cloudInitScript
  31. */
  32. public function __construct(CloudInitScript $cloudInitScript, ServerConfigurationRepository $serverConfiguration, $vmModel =null)
  33. {
  34. $this->cloudInitScript = $cloudInitScript;
  35. $this->serverConfiguration = $serverConfiguration;
  36. $this->vmModel = $vmModel;
  37. }
  38. /**
  39. * @return Snippet
  40. */
  41. public function convert($params=[]){
  42. if(!$this->serverConfiguration->snippetDirectory){
  43. throw new \InvalidArgumentException("Snippet Directory is empty");
  44. }
  45. $data=['script','meta','network'];
  46. if(empty($params)){
  47. $params = sl('whmcsParams')->getWhmcsParams();
  48. }
  49. $params['passwordHash'] = Utility::passwordHash($params['password']);
  50. if($this->vmModel instanceof VmModel){
  51. //load ip addresses
  52. $this->vmModel->ipv4Addresses;
  53. $this->vmModel->ipv6Addresses;
  54. $params['vm'] = $this->vmModel->toArray();
  55. //password decode
  56. $params['vm']['password'] = $this->vmModel->getPassword();
  57. $params['vm']['passwordHash'] = Utility::passwordHash($params['vm']['password']);
  58. $params['ipv4Addresses'] = $params['vm']['ipv4Addresses'];
  59. $params['ipv6Addresses'] = $params['vm']['ipv6Addresses'];
  60. }else{
  61. $params['ipv4Addresses'] = VmIpAddress::ofHostingId($params['serviceid'])->ofIp4()->get()->toArray();
  62. $params['ipv6Addresses'] = VmIpAddress::ofHostingId($params['serviceid'])->ofIp6()->get()->toArray();
  63. }
  64. $snippets=[];
  65. foreach ($data as $key ){
  66. if(empty($this->cloudInitScript->{$key})){
  67. continue;
  68. }
  69. $script = html_entity_decode($this->cloudInitScript->{$key}, ENT_QUOTES);
  70. $name = str_replace('script','userconfig',$key);
  71. if($this->vmModel instanceof VmModel){
  72. $filename = sprintf($name."-%s-%s.yaml",$this->getWhmcsParamByKey('serviceid'), $this->vmModel->id);
  73. }else{//vps
  74. $filename = sprintf($name."-%s.yaml",$this->getWhmcsParamByKey('serviceid'));
  75. }
  76. $content = $this->getSmarty()->fetchString($script, $params);
  77. $snippets[] = new Snippet($this->serverConfiguration->snippetDirectory,$filename, $content);
  78. }
  79. return $snippets;
  80. }
  81. }