From f06b8f7d207e4ea3e2403284a3b517d880e50f58 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com> Date: Fri, 5 Jan 2024 22:07:45 +0100 Subject: [PATCH] update vhost and bug fix (#87) --- app/Contracts/Webserver.php | 4 +- app/Http/Livewire/Sites/UpdateVHost.php | 41 +++++++++++++++++++ .../Nginx/GetNginxVHostCommand.php | 26 ++++++++++++ app/ServiceHandlers/Webserver/Nginx.php | 16 +++++++- app/SiteTypes/PHPSite.php | 2 +- .../commands/webserver/nginx/get-vhost.sh | 1 + .../livewire/sites/update-v-host.blade.php | 34 +++++++++++++++ resources/views/sites/settings.blade.php | 2 + tests/Feature/Http/SitesTest.php | 18 ++++++++ 9 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 app/Http/Livewire/Sites/UpdateVHost.php create mode 100755 app/SSHCommands/Nginx/GetNginxVHostCommand.php create mode 100755 resources/commands/webserver/nginx/get-vhost.sh create mode 100644 resources/views/livewire/sites/update-v-host.blade.php diff --git a/app/Contracts/Webserver.php b/app/Contracts/Webserver.php index 3a7d62a..39698e8 100755 --- a/app/Contracts/Webserver.php +++ b/app/Contracts/Webserver.php @@ -9,7 +9,9 @@ interface Webserver { public function createVHost(Site $site): void; - public function updateVHost(Site $site): void; + public function updateVHost(Site $site, bool $noSSL = false, ?string $vhost = null): void; + + public function getVHost(Site $site): string; public function deleteSite(Site $site): void; diff --git a/app/Http/Livewire/Sites/UpdateVHost.php b/app/Http/Livewire/Sites/UpdateVHost.php new file mode 100644 index 0000000..26ec759 --- /dev/null +++ b/app/Http/Livewire/Sites/UpdateVHost.php @@ -0,0 +1,41 @@ +vHost = $this->site->server->webserver()->handler()->getVHost($this->site); + } + + public function update(): void + { + try { + $this->site->server->webserver()->handler()->updateVHost($this->site, false, $this->vHost); + + $this->toast()->success('VHost updated successfully!'); + } catch (Throwable $e) { + $this->toast()->error($e->getMessage()); + } + } + + public function render(): View + { + return view('livewire.sites.update-v-host'); + } +} diff --git a/app/SSHCommands/Nginx/GetNginxVHostCommand.php b/app/SSHCommands/Nginx/GetNginxVHostCommand.php new file mode 100755 index 0000000..28dc68b --- /dev/null +++ b/app/SSHCommands/Nginx/GetNginxVHostCommand.php @@ -0,0 +1,26 @@ +file()) + ->replace('__domain__', $this->domain) + ->toString(); + } +} diff --git a/app/ServiceHandlers/Webserver/Nginx.php b/app/ServiceHandlers/Webserver/Nginx.php index 7cfef04..c0d93f8 100755 --- a/app/ServiceHandlers/Webserver/Nginx.php +++ b/app/ServiceHandlers/Webserver/Nginx.php @@ -9,6 +9,7 @@ use App\SSHCommands\Nginx\ChangeNginxPHPVersionCommand; use App\SSHCommands\Nginx\CreateNginxVHostCommand; use App\SSHCommands\Nginx\DeleteNginxSiteCommand; +use App\SSHCommands\Nginx\GetNginxVHostCommand; use App\SSHCommands\Nginx\UpdateNginxRedirectsCommand; use App\SSHCommands\Nginx\UpdateNginxVHostCommand; use App\SSHCommands\SSL\CreateCustomSSLCommand; @@ -39,19 +40,30 @@ public function createVHost(Site $site): void /** * @throws Throwable */ - public function updateVHost(Site $site, bool $noSSL = false): void + public function updateVHost(Site $site, bool $noSSL = false, ?string $vhost = null): void { $this->service->server->ssh()->exec( new UpdateNginxVHostCommand( $site->domain, $site->path, - $this->generateVhost($site, $noSSL) + $vhost ?? $this->generateVhost($site, $noSSL) ), 'update-vhost', $site->id ); } + public function getVHost(Site $site): string + { + return $this->service->server->ssh()->exec( + new GetNginxVHostCommand( + $site->domain + ), + 'get-vhost', + $site->id + ); + } + /** * @throws Throwable */ diff --git a/app/SiteTypes/PHPSite.php b/app/SiteTypes/PHPSite.php index e6a2580..ec676d4 100755 --- a/app/SiteTypes/PHPSite.php +++ b/app/SiteTypes/PHPSite.php @@ -55,6 +55,7 @@ public function createFields(array $input): array 'source_control_id' => $input['source_control'] ?? '', 'repository' => $input['repository'] ?? '', 'branch' => $input['branch'] ?? '', + 'php_version' => $input['php_version'] ?? '', ]; } @@ -62,7 +63,6 @@ public function data(array $input): array { return [ 'composer' => isset($input['composer']) && $input['composer'], - 'php_version' => $input['php_version'] ?? '', ]; } diff --git a/resources/commands/webserver/nginx/get-vhost.sh b/resources/commands/webserver/nginx/get-vhost.sh new file mode 100755 index 0000000..8cd571f --- /dev/null +++ b/resources/commands/webserver/nginx/get-vhost.sh @@ -0,0 +1 @@ +cat /etc/nginx/sites-available/__domain__ diff --git a/resources/views/livewire/sites/update-v-host.blade.php b/resources/views/livewire/sites/update-v-host.blade.php new file mode 100644 index 0000000..02eea2c --- /dev/null +++ b/resources/views/livewire/sites/update-v-host.blade.php @@ -0,0 +1,34 @@ + + {{ __('Update VHost') }} + + {{ __("You can change your site's PHP version here") }} + +
+
+ + @error('vHost') + + @enderror +
+
+ + + {{ __('Save') }} + +
diff --git a/resources/views/sites/settings.blade.php b/resources/views/sites/settings.blade.php index b21c663..0798566 100644 --- a/resources/views/sites/settings.blade.php +++ b/resources/views/sites/settings.blade.php @@ -5,6 +5,8 @@ + + {{ __("Delete Site") }} {{ __("Permanently delete the site from server") }} diff --git a/tests/Feature/Http/SitesTest.php b/tests/Feature/Http/SitesTest.php index 69ff80e..7772720 100644 --- a/tests/Feature/Http/SitesTest.php +++ b/tests/Feature/Http/SitesTest.php @@ -5,11 +5,13 @@ use App\Enums\SiteStatus; use App\Enums\SiteType; use App\Enums\SourceControl; +use App\Facades\SSH; use App\Http\Livewire\Sites\ChangePhpVersion; use App\Http\Livewire\Sites\CreateSite; use App\Http\Livewire\Sites\DeleteSite; use App\Http\Livewire\Sites\SitesList; use App\Http\Livewire\Sites\UpdateSourceControlProvider; +use App\Http\Livewire\Sites\UpdateVHost; use App\Jobs\Site\CreateVHost; use App\Models\Site; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -107,6 +109,22 @@ public function test_change_php_version(): void Bus::assertDispatched(\App\Jobs\Site\ChangePHPVersion::class); } + public function test_update_v_host(): void + { + SSH::fake(); + + $this->actingAs($this->user); + + $site = Site::factory()->create([ + 'server_id' => $this->server->id, + ]); + + Livewire::test(UpdateVHost::class, ['site' => $site]) + ->set('vHost', 'test-vhost') + ->call('update') + ->assertSuccessful(); + } + public function test_update_source_control(): void { $this->actingAs($this->user);