2023-04-25 10:47:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Server;
|
|
|
|
|
2023-05-04 09:11:11 +02:00
|
|
|
use App\Actions\Server\InstallDocker;
|
2023-04-25 10:47:13 +02:00
|
|
|
use App\Models\Server;
|
2023-08-29 15:51:30 +02:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2023-04-25 10:47:13 +02:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Form extends Component
|
|
|
|
{
|
2023-08-29 15:51:30 +02:00
|
|
|
use AuthorizesRequests;
|
2023-04-25 10:47:13 +02:00
|
|
|
public Server $server;
|
2023-04-26 15:38:50 +02:00
|
|
|
public $uptime;
|
2023-05-02 19:25:47 +02:00
|
|
|
public $dockerVersion;
|
2023-06-22 15:25:57 +02:00
|
|
|
public string|null $wildcard_domain = null;
|
2023-07-07 21:07:42 +02:00
|
|
|
public int $cleanup_after_percentage;
|
2023-04-25 10:47:13 +02:00
|
|
|
|
|
|
|
protected $rules = [
|
|
|
|
'server.name' => 'required|min:6',
|
|
|
|
'server.description' => 'nullable',
|
|
|
|
'server.ip' => 'required',
|
|
|
|
'server.user' => 'required',
|
|
|
|
'server.port' => 'required',
|
2023-06-15 13:51:31 +02:00
|
|
|
'server.settings.is_reachable' => 'required',
|
2023-06-22 15:25:57 +02:00
|
|
|
'server.settings.is_part_of_swarm' => 'required',
|
2023-06-23 14:20:47 +02:00
|
|
|
'wildcard_domain' => 'nullable|url',
|
2023-04-25 10:47:13 +02:00
|
|
|
];
|
2023-06-16 12:35:40 +02:00
|
|
|
protected $validationAttributes = [
|
|
|
|
'server.name' => 'name',
|
|
|
|
'server.description' => 'description',
|
|
|
|
'server.ip' => 'ip',
|
|
|
|
'server.user' => 'user',
|
|
|
|
'server.port' => 'port',
|
|
|
|
'server.settings.is_reachable' => 'is reachable',
|
|
|
|
'server.settings.is_part_of_swarm' => 'is part of swarm'
|
|
|
|
];
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-06-22 15:25:57 +02:00
|
|
|
public function mount()
|
|
|
|
{
|
|
|
|
$this->wildcard_domain = $this->server->settings->wildcard_domain;
|
2023-07-07 21:07:42 +02:00
|
|
|
$this->cleanup_after_percentage = $this->server->settings->cleanup_after_percentage;
|
2023-06-22 15:25:57 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-05-02 15:20:45 +02:00
|
|
|
public function installDocker()
|
|
|
|
{
|
2023-08-22 17:44:49 +02:00
|
|
|
$activity = resolve(InstallDocker::class)($this->server, currentTeam());
|
2023-05-04 09:11:11 +02:00
|
|
|
$this->emit('newMonitorActivity', $activity->id);
|
2023-05-02 15:20:45 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-05-14 14:43:49 +02:00
|
|
|
public function validateServer()
|
2023-04-26 15:38:50 +02:00
|
|
|
{
|
2023-05-03 09:38:53 +02:00
|
|
|
try {
|
2023-07-25 14:43:49 +02:00
|
|
|
['uptime' => $uptime, 'dockerVersion' => $dockerVersion] = validateServer($this->server);
|
|
|
|
if ($uptime) {
|
|
|
|
$this->uptime = $uptime;
|
2023-05-03 09:43:01 +02:00
|
|
|
}
|
2023-07-25 14:43:49 +02:00
|
|
|
if ($dockerVersion) {
|
|
|
|
$this->dockerVersion = $dockerVersion;
|
2023-07-14 13:01:55 +02:00
|
|
|
$this->emit('proxyStatusUpdated');
|
2023-05-03 09:43:01 +02:00
|
|
|
}
|
2023-05-03 09:38:53 +02:00
|
|
|
} catch (\Exception $e) {
|
2023-06-22 21:17:53 +02:00
|
|
|
return general_error_handler(customErrorMessage: "Server is not reachable. Reason: {$e->getMessage()}", that: $this);
|
2023-05-03 09:38:53 +02:00
|
|
|
}
|
2023-04-26 15:38:50 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-05-03 10:25:44 +02:00
|
|
|
public function delete()
|
|
|
|
{
|
2023-08-29 15:51:30 +02:00
|
|
|
try {
|
|
|
|
$this->authorize('delete', $this->server);
|
|
|
|
if (!$this->server->isEmpty()) {
|
|
|
|
$this->emit('error', 'Server has defined resources. Please delete them first.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->server->delete();
|
|
|
|
return redirect()->route('server.all');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return general_error_handler(err: $e, that: $this);
|
2023-06-15 15:15:27 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-08-29 15:51:30 +02:00
|
|
|
|
|
|
|
}
|
2023-04-25 10:47:13 +02:00
|
|
|
public function submit()
|
|
|
|
{
|
|
|
|
$this->validate();
|
|
|
|
// $validation = Validator::make($this->server->toArray(), [
|
|
|
|
// 'ip' => [
|
|
|
|
// 'ip'
|
|
|
|
// ],
|
|
|
|
// ]);
|
|
|
|
// if ($validation->fails()) {
|
|
|
|
// foreach ($validation->errors()->getMessages() as $key => $value) {
|
|
|
|
// $this->addError("server.{$key}", $value[0]);
|
|
|
|
// }
|
|
|
|
// return;
|
|
|
|
// }
|
2023-06-22 15:25:57 +02:00
|
|
|
$this->server->settings->wildcard_domain = $this->wildcard_domain;
|
2023-07-07 21:07:42 +02:00
|
|
|
$this->server->settings->cleanup_after_percentage = $this->cleanup_after_percentage;
|
2023-06-22 15:25:57 +02:00
|
|
|
$this->server->settings->save();
|
2023-04-25 10:47:13 +02:00
|
|
|
$this->server->save();
|
2023-06-22 10:04:39 +02:00
|
|
|
$this->emit('success', 'Server updated successfully.');
|
2023-04-25 10:47:13 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
}
|