| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace ModulesGarden\ProxmoxAddon\App\Services;
- use ModulesGarden\ProxmoxAddon\App\Models\CloudInitScript;
- use ModulesGarden\ProxmoxAddon\App\Models\Snippet;
- use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
- use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;
- use ModulesGarden\ProxmoxAddon\Core\Traits\Smarty;
- use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
- class CloudInitScriptConveter
- {
- use Smarty;
- use WhmcsParams;
- use ApiService;
- /**
- * @var CloudInitScript
- */
- protected $cloudInitScript;
- /**
- * @var ServerConfigurationRepository
- */
- protected $serverConfiguration;
- /**
- * @var VmModel|null
- */
- protected $vmModel;
- /**
- * CloudInitScriptConveter constructor.
- * @param CloudInitScript $cloudInitScript
- */
- public function __construct(CloudInitScript $cloudInitScript, ServerConfigurationRepository $serverConfiguration, $vmModel =null)
- {
- $this->cloudInitScript = $cloudInitScript;
- $this->serverConfiguration = $serverConfiguration;
- $this->vmModel = $vmModel;
- }
- /**
- * @return Snippet
- */
- public function convert(){
- if(!$this->serverConfiguration->snippetDirectory){
- throw new \InvalidArgumentException("Snippet Directory is empty");
- }
- $script = html_entity_decode($this->cloudInitScript->script, ENT_QUOTES);
- $params = sl('whmcsParams')->getWhmcsParams();
- $params['passwordHash'] = Utility::passwordHash($params['password']);
- if($this->vmModel instanceof VmModel){
- //load ip addresses
- $this->vmModel->ipv4Addresses;
- $this->vmModel->ipv6Addresses;
- $params['vm'] = $this->vmModel->toArray();
- //password decode
- $params['vm']['password'] = $this->vmModel->getPassword();
- $params['vm']['passwordHash'] = Utility::passwordHash($params['vm']['password']);
- $filename = sprintf("userconfig-%s-%s.yaml",$this->getWhmcsParamByKey('serviceid'), $this->vmModel->id);
- }else{//vps
- $filename = sprintf("userconfig-%s.yaml",$this->getWhmcsParamByKey('serviceid'));
- }
- $content = $this->getSmarty()->fetchString($script, $params);
- return new Snippet($this->serverConfiguration->snippetDirectory,$filename, $content);
- }
- }
|