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

116 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Server\New;
2023-09-06 15:00:56 +02:00
use App\Enums\ProxyStatus;
use App\Enums\ProxyTypes;
use App\Models\Server;
2024-02-23 15:45:53 +01:00
use App\Models\Team;
use Livewire\Component;
class ByIp extends Component
{
public $private_keys;
2023-08-14 15:22:29 +02:00
public $limit_reached;
2023-10-11 09:57:35 +02:00
public ?int $private_key_id = null;
public $new_private_key_name;
public $new_private_key_description;
public $new_private_key_value;
2023-05-02 09:20:08 +02:00
public string $name;
2023-10-11 09:57:35 +02:00
public ?string $description = null;
public string $ip;
public string $user = 'root';
public int $port = 22;
2023-11-29 10:06:52 +01:00
public bool $is_swarm_manager = false;
2023-12-18 14:01:25 +01:00
public bool $is_swarm_worker = false;
public $selected_swarm_cluster = null;
2023-12-18 14:01:25 +01:00
public bool $is_build_server = false;
public $swarm_managers = [];
2023-05-12 15:39:07 +02:00
protected $rules = [
2023-06-16 12:35:40 +02:00
'name' => 'required|string',
'description' => 'nullable|string',
'ip' => 'required',
2023-06-16 12:35:40 +02:00
'user' => 'required|string',
2023-05-12 15:39:07 +02:00
'port' => 'required|integer',
2023-11-29 10:06:52 +01:00
'is_swarm_manager' => 'required|boolean',
2023-12-18 14:01:25 +01:00
'is_swarm_worker' => 'required|boolean',
'is_build_server' => 'required|boolean',
2023-06-16 12:35:40 +02:00
];
protected $validationAttributes = [
2023-10-11 09:57:35 +02:00
'name' => 'Name',
'description' => 'Description',
'ip' => 'IP Address/Domain',
2023-10-11 09:57:35 +02:00
'user' => 'User',
'port' => 'Port',
2023-11-29 10:06:52 +01:00
'is_swarm_manager' => 'Swarm Manager',
2023-12-18 14:01:25 +01:00
'is_swarm_worker' => 'Swarm Worker',
'is_build_server' => 'Build Server',
2023-05-12 15:39:07 +02:00
];
public function mount()
{
2023-05-24 14:26:50 +02:00
$this->name = generate_random_name();
2023-05-16 11:02:51 +02: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;
}
}
2023-05-12 15:39:07 +02:00
public function setPrivateKey(string $private_key_id)
{
$this->private_key_id = $private_key_id;
}
2023-05-16 11:02:51 +02:00
public function instantSave()
{
// $this->dispatch('success', 'Application settings updated!');
2023-05-16 11:02:51 +02:00
}
public function submit()
{
2023-06-16 12:35:40 +02:00
$this->validate();
2023-05-12 15:39:07 +02:00
try {
2023-09-12 13:14:01 +02:00
if (is_null($this->private_key_id)) {
2023-12-07 19:06:32 +01:00
return $this->dispatch('error', 'You must select a private key');
2023-05-12 15:39:07 +02:00
}
2024-02-23 15:45:53 +01:00
if (Team::serverLimitReached()) {
return $this->dispatch('error', 'You have reached the server limit for your subscription.');
}
$payload = [
2023-05-12 15:39:07 +02:00
'name' => $this->name,
'description' => $this->description,
'ip' => $this->ip,
'user' => $this->user,
'port' => $this->port,
2023-08-22 17:44:49 +02:00
'team_id' => currentTeam()->id,
2023-05-16 11:02:51 +02:00
'private_key_id' => $this->private_key_id,
2023-09-06 15:00:56 +02:00
'proxy' => [
"type" => ProxyTypes::TRAEFIK_V2->value,
"status" => ProxyStatus::EXITED->value,
2023-11-28 15:49:24 +01:00
],
];
if ($this->is_swarm_worker) {
$payload['swarm_cluster'] = $this->selected_swarm_cluster;
}
$server = Server::create($payload);
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 11:02:51 +02:00
$server->settings->save();
2023-11-28 15:49:24 +01:00
$server->addInitialNetwork();
2023-12-27 16:45:01 +01:00
return redirect()->route('server.show', $server->uuid);
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-03 10:25:44 +02:00
}
}
}