Skip to content

Commit

Permalink
update vhost and bug fix (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry authored Jan 5, 2024
1 parent f120a57 commit f06b8f7
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/Contracts/Webserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
41 changes: 41 additions & 0 deletions app/Http/Livewire/Sites/UpdateVHost.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
26 changes: 26 additions & 0 deletions app/SSHCommands/Nginx/GetNginxVHostCommand.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
16 changes: 14 additions & 2 deletions app/ServiceHandlers/Webserver/Nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion app/SiteTypes/PHPSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function createFields(array $input): array
'source_control_id' => $input['source_control'] ?? '',
'repository' => $input['repository'] ?? '',
'branch' => $input['branch'] ?? '',
'php_version' => $input['php_version'] ?? '',
];
}

public function data(array $input): array
{
return [
'composer' => isset($input['composer']) && $input['composer'],
'php_version' => $input['php_version'] ?? '',
];
}

Expand Down
1 change: 1 addition & 0 deletions resources/commands/webserver/nginx/get-vhost.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cat /etc/nginx/sites-available/__domain__
34 changes: 34 additions & 0 deletions resources/views/livewire/sites/update-v-host.blade.php
Original file line number Diff line number Diff line change
@@ -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>
2 changes: 2 additions & 0 deletions resources/views/sites/settings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/Http/SitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f06b8f7

Please sign in to comment.