ChangePassword.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**********************************************************************
  3. * ProxmoxVPS developed. (26.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\Http\Actions;
  20. use MGProvision\Proxmox\v2\ProxmoxApiException;
  21. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Agent\ChangePasswordJob;
  22. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RebootVmJob;
  23. use ModulesGarden\ProxmoxAddon\App\Models\Job;
  24. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  25. use ModulesGarden\ProxmoxAddon\App\Services\Vps\AgentService;
  26. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  27. use ModulesGarden\Servers\ProxmoxVps\App\Enum\CustomField;
  28. use ModulesGarden\Servers\ProxmoxVps\App\Helpers\AppParams;
  29. use ModulesGarden\Servers\ProxmoxVps\App\Helpers\ProxmoxAddonValidator;
  30. use ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\Instances\AddonController;
  31. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  32. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\WhmcsParams;
  33. class ChangePassword extends AddonController
  34. {
  35. use WhmcsParams;
  36. use ProductService;
  37. use ApiService;
  38. public function execute($params = null)
  39. {
  40. if(!ProxmoxAddonValidator::isInstalled()){
  41. return ProxmoxAddonValidator::failAsString();
  42. }
  43. (new AppParams())->initFromWhmcsParams();
  44. try
  45. {
  46. //cloud init
  47. if($this->configuration()->isCloudInit()){
  48. //KVM only
  49. $container = [];
  50. if($this->getWhmcsCustomField(CustomField::CI_USER)){
  51. $container['ciuser'] = $this->getWhmcsCustomField(CustomField::CI_USER);
  52. }else{
  53. $container['ciuser'] = $this->configuration()->getCiuser() ? $this->configuration()->getCiuser() : $params['username'];
  54. }
  55. $container['cipassword'] = $params['password'];
  56. $this->vm()->updateConfig($container);
  57. //restart
  58. if (!Job::waiting()->ofJob(RebootVmJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->count())
  59. {
  60. queue(RebootVmJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  61. }
  62. }
  63. if ($this->configuration()->isAgent() && $this->configuration()->isAgentServicePassword()){
  64. try{
  65. if(!$this->vm()->isRunning()) {
  66. throw new ProxmoxApiException("VM not running");
  67. }
  68. $agent = new AgentService();
  69. $this->vm()->agent()->ping();
  70. $agent->passwordUpdate();
  71. }catch (ProxmoxApiException $ex){
  72. queue(ChangePasswordJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
  73. }
  74. }
  75. return "success";
  76. }
  77. catch (\Exception $ex)
  78. {
  79. return $ex->getMessage();
  80. }
  81. }
  82. }