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;
|
|
|
|
|
|
|
|
class Form extends Component
|
|
|
|
{
|
|
|
|
public $server_id;
|
|
|
|
public Server $server;
|
2023-04-26 13:38:50 +00:00
|
|
|
public $uptime;
|
2023-05-02 17:25:47 +00:00
|
|
|
public $dockerVersion;
|
|
|
|
public $dockerComposeVersion;
|
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-05-16 09:02:51 +00:00
|
|
|
'server.settings.is_validated' => 'required',
|
|
|
|
'server.settings.is_part_of_swarm' => 'required'
|
2023-04-25 08:47:13 +00:00
|
|
|
];
|
|
|
|
public function mount()
|
|
|
|
{
|
2023-05-08 07:16:50 +00:00
|
|
|
$this->server = Server::find($this->server_id)->load(['settings']);
|
2023-04-25 08:47:13 +00:00
|
|
|
}
|
2023-05-02 13:20:45 +00:00
|
|
|
public function installDocker()
|
|
|
|
{
|
2023-05-04 07:11:11 +00:00
|
|
|
$activity = resolve(InstallDocker::class)($this->server);
|
|
|
|
|
|
|
|
$this->emit('newMonitorActivity', $activity->id);
|
2023-05-02 13:20:45 +00:00
|
|
|
}
|
2023-05-14 12:43:49 +00:00
|
|
|
public function validateServer()
|
2023-04-26 13:38:50 +00:00
|
|
|
{
|
2023-05-03 07:38:53 +00:00
|
|
|
try {
|
2023-05-24 13:25:08 +00:00
|
|
|
$this->uptime = instant_remote_process(['uptime'], $this->server, false);
|
2023-05-03 07:43:01 +00:00
|
|
|
if (!$this->uptime) {
|
|
|
|
$this->uptime = 'Server not reachable.';
|
|
|
|
throw new \Exception('Server not reachable.');
|
2023-05-08 07:16:50 +00:00
|
|
|
} else {
|
|
|
|
if (!$this->server->settings->is_validated) {
|
|
|
|
$this->server->settings->is_validated = true;
|
|
|
|
$this->server->settings->save();
|
2023-05-15 19:14:45 +00:00
|
|
|
$this->emit('serverValidated');
|
2023-05-08 07:16:50 +00:00
|
|
|
}
|
2023-05-03 07:43:01 +00:00
|
|
|
}
|
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-05-24 13:25:08 +00:00
|
|
|
$this->dockerComposeVersion = instant_remote_process(['docker compose version|head -2|grep -i version'], $this->server, false);
|
2023-05-03 07:43:01 +00:00
|
|
|
if (!$this->dockerComposeVersion) {
|
|
|
|
$this->dockerComposeVersion = 'Not installed.';
|
|
|
|
}
|
2023-05-03 07:38:53 +00:00
|
|
|
} catch (\Exception $e) {
|
2023-05-24 12:26:50 +00:00
|
|
|
return general_error_handler($e, $this);
|
2023-05-03 07:38:53 +00:00
|
|
|
}
|
2023-04-26 13:38:50 +00:00
|
|
|
}
|
2023-05-03 08:25:44 +00:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
$this->server->delete();
|
|
|
|
redirect()->route('dashboard');
|
|
|
|
}
|
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;
|
|
|
|
// }
|
|
|
|
$this->server->save();
|
|
|
|
}
|
|
|
|
}
|