lasthourcloud/app/Http/Livewire/Server/Form.php
Andras Bacsai ac976d0f3a fixes
2023-05-04 09:11:11 +02:00

75 lines
2.2 KiB
PHP

<?php
namespace App\Http\Livewire\Server;
use App\Actions\Server\InstallDocker;
use App\Models\Server;
use Livewire\Component;
class Form extends Component
{
public $server_id;
public Server $server;
public $uptime;
public $dockerVersion;
public $dockerComposeVersion;
protected $rules = [
'server.name' => 'required|min:6',
'server.description' => 'nullable',
'server.ip' => 'required',
'server.user' => 'required',
'server.port' => 'required',
];
public function mount()
{
$this->server = Server::find($this->server_id);
}
public function installDocker()
{
$activity = resolve(InstallDocker::class)($this->server);
$this->emit('newMonitorActivity', $activity->id);
}
public function checkServer()
{
try {
$this->uptime = instantRemoteProcess(['uptime'], $this->server, false);
if (!$this->uptime) {
$this->uptime = 'Server not reachable.';
throw new \Exception('Server not reachable.');
}
$this->dockerVersion = instantRemoteProcess(['docker version|head -2|grep -i version'], $this->server, false);
if (!$this->dockerVersion) {
$this->dockerVersion = 'Not installed.';
}
$this->dockerComposeVersion = instantRemoteProcess(['docker compose version|head -2|grep -i version'], $this->server, false);
if (!$this->dockerComposeVersion) {
$this->dockerComposeVersion = 'Not installed.';
}
} catch (\Exception $e) {
}
}
public function delete()
{
$this->server->delete();
redirect()->route('dashboard');
}
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();
}
}