lasthourcloud/app/Http/Livewire/Server/Form.php

105 lines
3.7 KiB
PHP
Raw Normal View History

2023-04-25 08:47:13 +00:00
<?php
namespace App\Http\Livewire\Server;
2023-05-04 07:11:11 +00:00
use App\Actions\Server\InstallDocker;
2023-04-25 08:47:13 +00:00
use App\Models\Server;
use Livewire\Component;
2023-07-13 11:16:24 +00:00
use Visus\Cuid2\Cuid2;
2023-04-25 08:47:13 +00:00
class Form extends Component
{
public Server $server;
public $uptime;
2023-05-02 17:25:47 +00:00
public $dockerVersion;
2023-06-22 13:25:57 +00:00
public string|null $wildcard_domain = null;
2023-07-07 19:07:42 +00:00
public int $cleanup_after_percentage;
2023-07-13 11:16:24 +00:00
public string|null $modalId = null;
2023-04-25 08:47:13 +00:00
protected $rules = [
'server.name' => 'required|min:6',
'server.description' => 'nullable',
'server.ip' => 'required',
'server.user' => 'required',
'server.port' => 'required',
2023-06-15 11:51:31 +00:00
'server.settings.is_reachable' => 'required',
2023-06-22 13:25:57 +00:00
'server.settings.is_part_of_swarm' => 'required',
2023-06-23 12:20:47 +00:00
'wildcard_domain' => 'nullable|url',
2023-04-25 08:47:13 +00:00
];
2023-06-16 10:35:40 +00: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-06-22 13:25:57 +00:00
public function mount()
{
2023-07-13 11:16:24 +00:00
$this->modalId = new Cuid2(7);
2023-06-22 13:25:57 +00:00
$this->wildcard_domain = $this->server->settings->wildcard_domain;
2023-07-07 19:07:42 +00:00
$this->cleanup_after_percentage = $this->server->settings->cleanup_after_percentage;
2023-06-22 13:25:57 +00:00
}
2023-05-02 13:20:45 +00:00
public function installDocker()
{
2023-06-23 07:58:15 +00:00
$activity = resolve(InstallDocker::class)($this->server, session('currentTeam'));
2023-05-04 07:11:11 +00:00
$this->emit('newMonitorActivity', $activity->id);
2023-05-02 13:20:45 +00:00
}
2023-05-14 12:43:49 +00:00
public function validateServer()
{
try {
2023-06-22 19:17:53 +00:00
$this->uptime = instant_remote_process(['uptime'], $this->server);
2023-06-15 11:51:31 +00:00
if ($this->uptime) {
$this->server->settings->is_reachable = true;
$this->server->settings->save();
} else {
2023-05-03 07:43:01 +00:00
$this->uptime = 'Server not reachable.';
throw new \Exception('Server not reachable.');
}
2023-05-24 13:25:08 +00:00
$this->dockerVersion = instant_remote_process(['docker version|head -2|grep -i version'], $this->server, false);
2023-05-03 07:43:01 +00:00
if (!$this->dockerVersion) {
$this->dockerVersion = 'Not installed.';
2023-06-15 11:28:16 +00:00
} else {
2023-06-15 11:51:31 +00:00
$this->server->settings->is_usable = true;
2023-06-15 11:28:16 +00:00
$this->server->settings->save();
$this->emit('proxyStatusUpdated');
2023-05-03 07:43:01 +00:00
}
} catch (\Exception $e) {
2023-06-22 19:17:53 +00:00
$this->server->settings->is_reachable = false;
$this->server->settings->is_usable = false;
$this->server->settings->save();
return general_error_handler(customErrorMessage: "Server is not reachable. Reason: {$e->getMessage()}", that: $this);
}
}
2023-05-03 08:25:44 +00:00
public function delete()
{
2023-06-15 13:15:27 +00:00
if (!$this->server->isEmpty()) {
$this->emit('error', 'Server has defined resources. Please delete them first.');
return;
}
2023-05-03 08:25:44 +00:00
$this->server->delete();
2023-06-19 07:44:39 +00:00
redirect()->route('server.all');
2023-05-03 08:25:44 +00:00
}
2023-04-25 08:47:13 +00: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 13:25:57 +00:00
$this->server->settings->wildcard_domain = $this->wildcard_domain;
2023-07-07 19:07:42 +00:00
$this->server->settings->cleanup_after_percentage = $this->cleanup_after_percentage;
2023-06-22 13:25:57 +00:00
$this->server->settings->save();
2023-04-25 08:47:13 +00:00
$this->server->save();
2023-06-22 08:04:39 +00:00
$this->emit('success', 'Server updated successfully.');
2023-04-25 08:47:13 +00:00
}
2023-07-13 11:16:24 +00:00
}