Add load balancer (#453)

This commit is contained in:
Saeed Vaziry
2025-01-30 23:52:51 +01:00
committed by GitHub
parent 53e20cbc2a
commit 6966f06b1a
54 changed files with 3128 additions and 1833 deletions

View File

@ -3,6 +3,7 @@
namespace App\Web\Pages\Servers\Sites;
use App\Actions\Site\CreateSite;
use App\Enums\LoadBalancerMethod;
use App\Enums\SiteType;
use App\Models\Site;
use App\Models\SourceControl;
@ -133,6 +134,17 @@ protected function getHeaderActions(): array
->rules(fn (Get $get) => CreateSite::rules($this->server, $get())['version']),
// WordPress
$this->wordpressFields(),
// Load Balancer
Select::make('method')
->label('Balancing Method')
->validationAttribute('Balancing Method')
->options(
collect(LoadBalancerMethod::all())
->mapWithKeys(fn ($method) => [$method => $method])
)
->visible(fn (Get $get) => $get('type') === SiteType::LOAD_BALANCER)
->rules(fn (Get $get) => CreateSite::rules($this->server, $get())['method'] ?? []),
// User
TextInput::make('user')
->label('User')
->placeholder('vito')

View File

@ -7,6 +7,7 @@
use App\Actions\Site\UpdateDeploymentScript;
use App\Actions\Site\UpdateEnv;
use App\Enums\SiteFeature;
use App\Enums\SiteType;
use App\Web\Fields\CodeEditorField;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
@ -58,6 +59,10 @@ public function getWidgets(): array
if (in_array(SiteFeature::DEPLOYMENT, $this->site->type()->supportedFeatures())) {
$widgets[] = [Widgets\DeploymentsList::class, ['site' => $this->site]];
}
if ($this->site->type === SiteType::LOAD_BALANCER) {
$widgets[] = [Widgets\LoadBalancerServers::class, ['site' => $this->site]];
}
}
return $widgets;

View File

@ -0,0 +1,141 @@
<?php
namespace App\Web\Pages\Servers\Sites\Widgets;
use App\Actions\Site\UpdateLoadBalancer;
use App\Enums\LoadBalancerMethod;
use App\Models\LoadBalancerServer;
use App\Models\Site;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Widgets\Widget;
use Livewire\Attributes\On;
class LoadBalancerServers extends Widget implements HasForms
{
use InteractsWithForms;
protected static string $view = 'components.form';
public Site $site;
public string $method;
public array $servers = [];
public function mount(): void
{
$this->setLoadBalancerServers();
if (empty($this->servers)) {
$this->servers = [
[
'server' => null,
'port' => 80,
'weight' => null,
'backup' => false,
],
];
}
$this->method = $this->site->type_data['method'] ?? LoadBalancerMethod::ROUND_ROBIN;
}
#[On('load-balancer-updated')]
public function setLoadBalancerServers(): void
{
$this->servers = $this->site->loadBalancerServers->map(function (LoadBalancerServer $server) {
return [
'server' => $server->ip,
'port' => $server->port,
'weight' => $server->weight,
'backup' => $server->backup,
];
})->toArray();
}
public function form(Form $form): Form
{
return $form
->schema([
Section::make()
->heading('Load Balancer Servers')
->description('You can add or remove servers from the load balancer here')
->columns(3)
->schema([
Select::make('method')
->label('Balancing Method')
->validationAttribute('Balancing Method')
->options(
collect(LoadBalancerMethod::all())
->mapWithKeys(fn ($method) => [$method => $method])
)
->rules(UpdateLoadBalancer::rules($this->site)['method']),
Repeater::make('servers')
->schema([
Select::make('server')
->placeholder('Select a server')
->searchable()
->required()
->rules(UpdateLoadBalancer::rules($this->site)['servers.*.server'])
->options(function () {
return $this->site->project->servers()
->where('id', '!=', $this->site->server_id)
->get()
->mapWithKeys(function ($server) {
return [$server->local_ip => $server->name.' ('.$server->local_ip.')'];
});
}),
TextInput::make('port')
->default(80)
->required()
->rules(UpdateLoadBalancer::rules($this->site)['servers.*.port']),
TextInput::make('weight')
->rules(UpdateLoadBalancer::rules($this->site)['servers.*.weight']),
Toggle::make('backup')
->label('Backup')
->inline(false)
->default(false),
])
->columnSpan(3)
->live()
->reorderable(false)
->columns(4)
->reorderableWithDragAndDrop(false)
->addActionLabel('Add Server'),
])
->footerActions([
Action::make('save')
->label('Save')
->action(fn () => $this->save()),
]),
]);
}
public function save(): void
{
$this->authorize('update', [$this->site, $this->site->server]);
$this->validate();
run_action($this, function () {
app(UpdateLoadBalancer::class)->update($this->site, [
'method' => $this->method,
'servers' => $this->servers,
]);
$this->dispatch('load-balancer-updated');
Notification::make()
->success()
->title('Load balancer updated!')
->send();
});
}
}

View File

@ -27,6 +27,8 @@ class UpdateServerInfo extends Widget implements HasForms
public string $ip;
public ?string $local_ip;
public string $port;
public function mount(Server $server): void
@ -34,6 +36,7 @@ public function mount(Server $server): void
$this->server = $server;
$this->name = $server->name;
$this->ip = $server->ip;
$this->local_ip = $server->local_ip;
$this->port = $server->port;
}
@ -52,6 +55,10 @@ public function form(Form $form): Form
TextInput::make('ip')
->label('IP Address')
->rules(EditServer::rules($this->server)['ip']),
TextInput::make('local_ip')
->label('Local Network IP Address')
->placeholder('10.0.0.1')
->rules(EditServer::rules($this->server)['local_ip']),
TextInput::make('port')
->label('Port')
->rules(EditServer::rules($this->server)['port']),