coolify/app/Livewire/Server/New/ByIp.php

169 lines
6 KiB
PHP
Raw Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Server\New;
2023-09-06 13:00:56 +00:00
use App\Enums\ProxyTypes;
use App\Models\Server;
2024-02-23 14:45:53 +00:00
use App\Models\Team;
use App\Support\ValidationPatterns;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Collection;
2024-11-14 16:38:20 +00:00
use Livewire\Attributes\Locked;
use Livewire\Component;
class ByIp extends Component
{
use AuthorizesRequests;
2024-11-14 16:38:20 +00:00
#[Locked]
public $private_keys;
2024-06-10 20:43:34 +00:00
2024-11-14 16:38:20 +00:00
#[Locked]
2023-08-14 13:22:29 +00:00
public $limit_reached;
2024-06-10 20:43:34 +00:00
2023-10-11 07:57:35 +00:00
public ?int $private_key_id = null;
2024-06-10 20:43:34 +00:00
public $new_private_key_name;
2024-06-10 20:43:34 +00:00
public $new_private_key_description;
2024-06-10 20:43:34 +00:00
public $new_private_key_value;
2023-05-02 07:20:08 +00:00
public string $name;
2024-06-10 20:43:34 +00:00
2023-10-11 07:57:35 +00:00
public ?string $description = null;
2024-06-10 20:43:34 +00:00
public string $ip;
2024-06-10 20:43:34 +00:00
public string $user = 'root';
2024-06-10 20:43:34 +00:00
public int $port = 22;
2024-06-10 20:43:34 +00:00
2023-11-29 09:06:52 +00:00
public bool $is_swarm_manager = false;
2024-06-10 20:43:34 +00:00
2023-12-18 13:01:25 +00:00
public bool $is_swarm_worker = false;
2024-06-10 20:43:34 +00:00
public $selected_swarm_cluster = null;
2023-12-18 13:01:25 +00:00
public bool $is_build_server = false;
2024-11-14 16:38:20 +00:00
#[Locked]
public Collection $swarm_managers;
2024-06-10 20:43:34 +00:00
public function mount()
{
2023-05-24 12:26:50 +00:00
$this->name = generate_random_name();
2024-03-22 10:34:15 +00:00
$this->private_key_id = $this->private_keys->first()?->id;
$this->swarm_managers = Server::isUsable()->get()->where('settings.is_swarm_manager', true);
if ($this->swarm_managers->count() > 0) {
$this->selected_swarm_cluster = $this->swarm_managers->first()->id;
}
}
protected function rules(): array
{
return [
'private_key_id' => 'nullable|integer',
'new_private_key_name' => 'nullable|string',
'new_private_key_description' => 'nullable|string',
'new_private_key_value' => 'nullable|string',
'name' => ValidationPatterns::nameRules(),
'description' => ValidationPatterns::descriptionRules(),
'ip' => 'required|string',
'user' => 'required|string',
'port' => 'required|integer|between:1,65535',
'is_swarm_manager' => 'required|boolean',
'is_swarm_worker' => 'required|boolean',
'selected_swarm_cluster' => 'nullable|integer',
'is_build_server' => 'required|boolean',
];
}
protected function messages(): array
{
return array_merge(ValidationPatterns::combinedMessages(), [
'private_key_id.integer' => 'The Private Key field must be an integer.',
'private_key_id.nullable' => 'The Private Key field is optional.',
'new_private_key_name.string' => 'The Private Key Name must be a string.',
'new_private_key_description.string' => 'The Private Key Description must be a string.',
'new_private_key_value.string' => 'The Private Key Value must be a string.',
'ip.required' => 'The IP Address/Domain is required.',
'ip.string' => 'The IP Address/Domain must be a string.',
'user.required' => 'The User field is required.',
'user.string' => 'The User field must be a string.',
'port.required' => 'The Port field is required.',
'port.integer' => 'The Port field must be an integer.',
'port.between' => 'The Port field must be between 1 and 65535.',
'is_swarm_manager.required' => 'The Swarm Manager field is required.',
'is_swarm_manager.boolean' => 'The Swarm Manager field must be true or false.',
'is_swarm_worker.required' => 'The Swarm Worker field is required.',
'is_swarm_worker.boolean' => 'The Swarm Worker field must be true or false.',
'selected_swarm_cluster.integer' => 'The Swarm Cluster field must be an integer.',
'is_build_server.required' => 'The Build Server field is required.',
'is_build_server.boolean' => 'The Build Server field must be true or false.',
]);
}
2023-05-12 13:39:07 +00:00
public function setPrivateKey(string $private_key_id)
{
$this->private_key_id = $private_key_id;
}
2023-05-16 09:02:51 +00:00
public function instantSave()
{
// $this->dispatch('success', 'Application settings updated!');
2023-05-16 09:02:51 +00:00
}
public function submit()
{
2023-06-16 10:35:40 +00:00
$this->validate();
2023-05-12 13:39:07 +00:00
try {
$this->authorize('create', Server::class);
2024-11-14 16:38:20 +00:00
if (Server::where('team_id', currentTeam()->id)
->where('ip', $this->ip)
->exists()) {
return $this->dispatch('error', 'This IP/Domain is already in use by another server in your team.');
}
2023-09-12 11:14:01 +00:00
if (is_null($this->private_key_id)) {
2023-12-07 18:06:32 +00:00
return $this->dispatch('error', 'You must select a private key');
2023-05-12 13:39:07 +00:00
}
2024-02-23 14:45:53 +00:00
if (Team::serverLimitReached()) {
return $this->dispatch('error', 'You have reached the server limit for your subscription.');
}
$payload = [
2023-05-12 13:39:07 +00:00
'name' => $this->name,
'description' => $this->description,
'ip' => $this->ip,
'user' => $this->user,
'port' => $this->port,
2023-08-22 15:44:49 +00:00
'team_id' => currentTeam()->id,
2023-05-16 09:02:51 +00:00
'private_key_id' => $this->private_key_id,
];
if ($this->is_swarm_worker) {
$payload['swarm_cluster'] = $this->selected_swarm_cluster;
}
if ($this->is_build_server) {
data_forget($payload, 'proxy');
}
$server = Server::create($payload);
$server->proxy->set('status', 'exited');
$server->proxy->set('type', ProxyTypes::TRAEFIK->value);
$server->save();
if ($this->is_build_server) {
$this->is_swarm_manager = false;
$this->is_swarm_worker = false;
} else {
$server->settings->is_swarm_manager = $this->is_swarm_manager;
$server->settings->is_swarm_worker = $this->is_swarm_worker;
}
$server->settings->is_build_server = $this->is_build_server;
2023-05-16 09:02:51 +00:00
$server->settings->save();
2024-06-10 20:43:34 +00:00
2023-12-27 15:45:01 +00:00
return redirect()->route('server.show', $server->uuid);
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-03 08:25:44 +00:00
}
}
}