coolify/app/Livewire/Project/Application/Swarm.php

76 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2023-12-18 13:01:25 +00:00
<?php
namespace App\Livewire\Project\Application;
use App\Models\Application;
use Livewire\Attributes\Validate;
2023-12-18 13:01:25 +00:00
use Livewire\Component;
class Swarm extends Component
{
public Application $application;
2024-06-10 20:43:34 +00:00
#[Validate('required')]
2024-11-04 10:25:45 +00:00
public int $swarmReplicas;
2023-12-18 13:01:25 +00:00
#[Validate(['nullable'])]
2024-11-04 10:25:45 +00:00
public ?string $swarmPlacementConstraints = null;
#[Validate('required')]
2024-11-04 10:25:45 +00:00
public bool $isSwarmOnlyWorkerNodes;
2024-06-10 20:43:34 +00:00
public function mount()
{
2024-11-04 10:25:45 +00:00
try {
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
2023-12-18 13:01:25 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-11-04 10:25:45 +00:00
public function syncData(bool $toModel = false)
2024-06-10 20:43:34 +00:00
{
2024-11-04 10:25:45 +00:00
if ($toModel) {
2023-12-18 13:01:25 +00:00
$this->validate();
2024-11-04 10:25:45 +00:00
$this->application->swarm_replicas = $this->swarmReplicas;
$this->application->swarm_placement_constraints = $this->swarmPlacementConstraints ? base64_encode($this->swarmPlacementConstraints) : null;
$this->application->settings->is_swarm_only_worker_nodes = $this->isSwarmOnlyWorkerNodes;
$this->application->save();
2023-12-18 13:01:25 +00:00
$this->application->settings->save();
2024-11-04 10:25:45 +00:00
} else {
$this->swarmReplicas = $this->application->swarm_replicas;
if ($this->application->swarm_placement_constraints) {
$this->swarmPlacementConstraints = base64_decode($this->application->swarm_placement_constraints);
} else {
$this->swarmPlacementConstraints = null;
}
$this->isSwarmOnlyWorkerNodes = $this->application->settings->is_swarm_only_worker_nodes;
}
}
public function instantSave()
{
try {
$this->syncData(true);
2023-12-18 13:01:25 +00:00
$this->dispatch('success', 'Swarm settings updated.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function submit()
{
2023-12-18 13:01:25 +00:00
try {
2024-11-04 10:25:45 +00:00
$this->syncData(true);
2023-12-18 13:01:25 +00:00
$this->dispatch('success', 'Swarm settings updated.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2023-12-18 13:01:25 +00:00
public function render()
{
return view('livewire.project.application.swarm');
}
}