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

68 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Livewire\Server\New;
use App\Models\PrivateKey;
use App\Models\Server;
use Livewire\Component;
class ByIp extends Component
{
public $private_keys;
2023-05-12 15:39:07 +02:00
public int|null $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-05-03 12:38:57 +02:00
public string|null $description = null;
public string $ip;
public string $user = 'root';
public int $port = 22;
2023-05-16 11:02:51 +02:00
public bool $is_part_of_swarm = false;
2023-05-12 15:39:07 +02:00
protected $rules = [
'name' => 'required',
'ip' => 'required',
'user' => 'required',
'port' => 'required|integer',
2023-05-16 11:02:51 +02:00
'is_part_of_swarm' => 'required|boolean',
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;
}
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->emit('saved', 'Application settings updated!');
}
public function submit()
{
2023-05-12 15:39:07 +02:00
try {
if (!$this->private_key_id) {
return $this->emit('error', 'You must select a private key');
}
$this->validate();
$server = Server::create([
'name' => $this->name,
'description' => $this->description,
'ip' => $this->ip,
'user' => $this->user,
'port' => $this->port,
'team_id' => session('currentTeam')->id,
2023-05-16 11:02:51 +02:00
'private_key_id' => $this->private_key_id,
2023-05-12 15:39:07 +02:00
]);
2023-05-16 11:02:51 +02:00
$server->settings->is_part_of_swarm = $this->is_part_of_swarm;
$server->settings->save();
2023-05-12 15:39:07 +02:00
return redirect()->route('server.show', $server->uuid);
} catch (\Exception $e) {
2023-06-09 15:55:21 +02:00
return general_error_handler(err: $e);
2023-05-03 10:25:44 +02:00
}
}
}