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-10-09 11:00:18 +02:00
|
|
|
public bool $isValidConnection = false;
|
|
|
|
public bool $isValidDocker = false;
|
|
|
|
public ?string $wildcard_domain = null;
|
2023-07-07 21:07:42 +02:00
|
|
|
public int $cleanup_after_percentage;
|
2023-09-18 11:49:26 +02:00
|
|
|
public bool $dockerInstallationStarted = false;
|
2023-10-09 11:00:18 +02:00
|
|
|
protected $listeners = ['serverRefresh'];
|
2023-04-25 10:47:13 +02:00
|
|
|
|
|
|
|
protected $rules = [
|
|
|
|
'server.name' => 'required|min:6',
|
|
|
|
'server.description' => 'nullable',
|
2023-10-13 09:31:44 +02:00
|
|
|
'server.ip' => 'required',
|
2023-04-25 10:47:13 +02:00
|
|
|
'server.user' => 'required',
|
|
|
|
'server.port' => 'required',
|
2023-09-23 13:34:40 +02:00
|
|
|
'server.settings.is_cloudflare_tunnel' => '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 = [
|
2023-10-12 11:41:37 +02:00
|
|
|
'server.name' => 'Name',
|
|
|
|
'server.description' => 'Description',
|
|
|
|
'server.ip' => 'IP address',
|
|
|
|
'server.user' => 'User',
|
|
|
|
'server.port' => 'Port',
|
2023-09-23 13:34:40 +02:00
|
|
|
'server.settings.is_cloudflare_tunnel' => 'Cloudflare Tunnel',
|
2023-06-16 12:35:40 +02:00
|
|
|
'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-10-13 09:31:44 +02:00
|
|
|
public function serverRefresh()
|
|
|
|
{
|
2023-10-09 11:00:18 +02:00
|
|
|
$this->validateServer();
|
|
|
|
}
|
|
|
|
public function instantSave()
|
|
|
|
{
|
2023-09-24 10:48:54 +02:00
|
|
|
refresh_server_connection($this->server->privateKey);
|
|
|
|
$this->validateServer();
|
2023-09-23 13:34:40 +02:00
|
|
|
$this->server->settings->save();
|
|
|
|
}
|
2023-05-02 15:20:45 +02:00
|
|
|
public function installDocker()
|
|
|
|
{
|
2023-10-09 11:00:18 +02:00
|
|
|
$this->emit('installDocker');
|
2023-09-18 11:49:26 +02:00
|
|
|
$this->dockerInstallationStarted = true;
|
2023-10-09 11:00:18 +02:00
|
|
|
$activity = InstallDocker::run($this->server);
|
2023-05-04 09:11:11 +02:00
|
|
|
$this->emit('newMonitorActivity', $activity->id);
|
2023-05-02 15:20:45 +02:00
|
|
|
}
|
2023-10-13 09:31:44 +02:00
|
|
|
public function checkLocalhostConnection()
|
|
|
|
{
|
2023-10-11 13:30:36 +02:00
|
|
|
$uptime = $this->server->validateConnection();
|
|
|
|
if ($uptime) {
|
|
|
|
$this->emit('success', 'Server is reachable.');
|
|
|
|
$this->server->settings->is_reachable = true;
|
2023-10-11 13:34:51 +02:00
|
|
|
$this->server->settings->is_usable = true;
|
2023-10-11 13:30:36 +02:00
|
|
|
$this->server->settings->save();
|
|
|
|
} else {
|
|
|
|
$this->emit('error', 'Server is not reachable. Please check your connection and configuration.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-10-09 11:00:18 +02:00
|
|
|
public function validateServer($install = true)
|
2023-04-26 15:38:50 +02:00
|
|
|
{
|
2023-05-03 09:38:53 +02:00
|
|
|
try {
|
2023-10-09 11:00:18 +02:00
|
|
|
$uptime = $this->server->validateConnection();
|
2023-07-25 14:43:49 +02:00
|
|
|
if ($uptime) {
|
2023-10-09 11:00:18 +02:00
|
|
|
$install && $this->emit('success', 'Server is reachable.');
|
2023-09-09 15:30:46 +02:00
|
|
|
} else {
|
2023-10-13 09:31:44 +02:00
|
|
|
$install && $this->emit('error', 'Server is not reachable. Please check your connection and configuration.');
|
2023-09-11 17:19:30 +02:00
|
|
|
return;
|
2023-05-03 09:43:01 +02:00
|
|
|
}
|
2023-10-09 11:00:18 +02:00
|
|
|
$dockerInstalled = $this->server->validateDockerEngine();
|
|
|
|
if ($dockerInstalled) {
|
|
|
|
$install && $this->emit('success', 'Docker Engine is installed.<br> Checking version.');
|
|
|
|
} else {
|
|
|
|
$install && $this->installDocker();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$dockerVersion = $this->server->validateDockerEngineVersion();
|
2023-07-25 14:43:49 +02:00
|
|
|
if ($dockerVersion) {
|
2023-10-09 11:00:18 +02:00
|
|
|
$install && $this->emit('success', 'Docker Engine version is 23+.');
|
2023-09-09 15:30:46 +02:00
|
|
|
} else {
|
2023-10-09 11:00:18 +02:00
|
|
|
$install && $this->installDocker();
|
|
|
|
return;
|
2023-05-03 09:43:01 +02:00
|
|
|
}
|
2023-09-11 17:36:30 +02:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-09 11:00:18 +02:00
|
|
|
return handleError($e, $this);
|
2023-09-24 10:48:54 +02:00
|
|
|
} finally {
|
|
|
|
$this->emit('proxyStatusUpdated');
|
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');
|
2023-09-11 17:36:30 +02:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 15:34:25 +02:00
|
|
|
return handleError($e, $this);
|
2023-06-15 15:15:27 +02:00
|
|
|
}
|
2023-08-29 15:51:30 +02:00
|
|
|
}
|
2023-04-25 10:47:13 +02:00
|
|
|
public function submit()
|
|
|
|
{
|
2023-10-13 09:34:11 +02:00
|
|
|
if(isCloud() && !isDev()) {
|
2023-10-13 09:31:44 +02:00
|
|
|
$this->validate();
|
|
|
|
$this->validate([
|
|
|
|
'server.ip' => 'required|ip',
|
|
|
|
]);
|
2023-10-13 09:34:11 +02:00
|
|
|
} else {
|
|
|
|
$this->validate();
|
2023-10-13 09:31:44 +02:00
|
|
|
}
|
2023-09-14 18:10:13 +02:00
|
|
|
$uniqueIPs = Server::all()->reject(function (Server $server) {
|
|
|
|
return $server->id === $this->server->id;
|
|
|
|
})->pluck('ip')->toArray();
|
|
|
|
if (in_array($this->server->ip, $uniqueIPs)) {
|
|
|
|
$this->emit('error', 'IP address is already in use by another team.');
|
|
|
|
return;
|
|
|
|
}
|
2023-10-11 13:34:51 +02:00
|
|
|
refresh_server_connection($this->server->privateKey);
|
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
|
|
|
}
|