update vhost and bug fix (#87)

This commit is contained in:
Saeed Vaziry 2024-01-05 22:07:45 +01:00 committed by GitHub
parent f120a570e8
commit f06b8f7d20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 140 additions and 4 deletions

View File

@ -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;

View File

@ -0,0 +1,41 @@
<?php
namespace App\Http\Livewire\Sites;
use App\Models\Site;
use App\Traits\HasToast;
use App\Traits\RefreshComponentOnBroadcast;
use Illuminate\Contracts\View\View;
use Livewire\Component;
use Throwable;
class UpdateVHost extends Component
{
use HasToast;
use RefreshComponentOnBroadcast;
public Site $site;
public string $vHost = 'Loading...';
public function loadVHost(): void
{
$this->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');
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\SSHCommands\Nginx;
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
class GetNginxVHostCommand extends Command
{
public function __construct(
protected string $domain
) {
}
public function file(): string
{
return File::get(resource_path('commands/webserver/nginx/get-vhost.sh'));
}
public function content(): string
{
return str($this->file())
->replace('__domain__', $this->domain)
->toString();
}
}

View File

@ -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
*/

View File

@ -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'] ?? '',
];
}

View File

@ -0,0 +1 @@
cat /etc/nginx/sites-available/__domain__

View File

@ -0,0 +1,34 @@
<x-card>
<x-slot name="title">{{ __('Update VHost') }}</x-slot>
<x-slot name="description">{{ __("You can change your site's PHP version here") }}</x-slot>
<form
id="update-vhost"
wire:submit.prevent="update"
class="space-y-6"
>
<div>
<x-textarea
id="vHost"
wire:init="loadVHost"
wire:model.defer="vHost"
rows="10"
class="mt-1 block w-full"
></x-textarea>
@error('vHost')
<x-input-error
class="mt-2"
:messages="$message"
/>
@enderror
</div>
</form>
<x-slot name="actions">
<x-primary-button
form="update-vhost"
wire:loading.attr="disabled"
>{{ __('Save') }}</x-primary-button>
</x-slot>
</x-card>

View File

@ -5,6 +5,8 @@
<livewire:sites.update-source-control-provider :site="$site"/>
<livewire:sites.update-v-host :site="$site"/>
<x-card>
<x-slot name="title">{{ __("Delete Site") }}</x-slot>
<x-slot name="description">{{ __("Permanently delete the site from server") }}</x-slot>

View File

@ -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);