2023-10-02 11:38:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Project\Shared;
|
|
|
|
|
2023-12-01 09:34:30 +00:00
|
|
|
use App\Models\Application;
|
2023-10-02 11:38:16 +00:00
|
|
|
use App\Models\Server;
|
2023-12-01 09:34:30 +00:00
|
|
|
use App\Models\Service;
|
|
|
|
use App\Models\ServiceApplication;
|
|
|
|
use App\Models\ServiceDatabase;
|
|
|
|
use App\Models\StandaloneMariadb;
|
|
|
|
use App\Models\StandaloneMongodb;
|
|
|
|
use App\Models\StandaloneMysql;
|
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
use App\Models\StandaloneRedis;
|
2023-10-02 11:38:16 +00:00
|
|
|
use Illuminate\Support\Facades\Process;
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class GetLogs extends Component
|
|
|
|
{
|
|
|
|
public string $outputs = '';
|
|
|
|
public string $errors = '';
|
2023-12-01 09:34:30 +00:00
|
|
|
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb $resource;
|
|
|
|
public ServiceApplication|ServiceDatabase|null $servicesubtype = null;
|
2023-10-02 11:38:16 +00:00
|
|
|
public Server $server;
|
|
|
|
public ?string $container = null;
|
|
|
|
public ?bool $streamLogs = false;
|
2023-10-19 11:32:03 +00:00
|
|
|
public ?bool $showTimeStamps = true;
|
2023-10-02 12:01:54 +00:00
|
|
|
public int $numberOfLines = 100;
|
2023-12-01 09:34:30 +00:00
|
|
|
|
|
|
|
public function mount()
|
|
|
|
{
|
|
|
|
if ($this->resource->getMorphClass() === 'App\Models\Application') {
|
|
|
|
$this->showTimeStamps = $this->resource->settings->is_include_timestamps;
|
|
|
|
} else {
|
|
|
|
if ($this->servicesubtype) {
|
|
|
|
$this->showTimeStamps = $this->servicesubtype->is_include_timestamps;
|
|
|
|
} else {
|
|
|
|
$this->showTimeStamps = $this->resource->is_include_timestamps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-02 11:38:16 +00:00
|
|
|
public function doSomethingWithThisChunkOfOutput($output)
|
|
|
|
{
|
2023-11-07 13:40:58 +00:00
|
|
|
$this->outputs .= removeAnsiColors($output);
|
2023-10-02 11:38:16 +00:00
|
|
|
}
|
|
|
|
public function instantSave()
|
|
|
|
{
|
2023-12-01 09:34:30 +00:00
|
|
|
if ($this->resource->getMorphClass() === 'App\Models\Application') {
|
|
|
|
$this->resource->settings->is_include_timestamps = $this->showTimeStamps;
|
|
|
|
$this->resource->settings->save();
|
|
|
|
} else {
|
|
|
|
if ($this->servicesubtype) {
|
|
|
|
$this->servicesubtype->is_include_timestamps = $this->showTimeStamps;
|
|
|
|
$this->servicesubtype->save();
|
|
|
|
} else {
|
|
|
|
$this->resource->is_include_timestamps = $this->showTimeStamps;
|
|
|
|
$this->resource->save();
|
|
|
|
}
|
|
|
|
}
|
2023-10-02 11:38:16 +00:00
|
|
|
}
|
|
|
|
public function getLogs($refresh = false)
|
|
|
|
{
|
|
|
|
if ($this->container) {
|
2023-10-19 11:32:03 +00:00
|
|
|
if ($this->showTimeStamps) {
|
|
|
|
$sshCommand = generateSshCommand($this->server, "docker logs -n {$this->numberOfLines} -t {$this->container}");
|
|
|
|
} else {
|
|
|
|
$sshCommand = generateSshCommand($this->server, "docker logs -n {$this->numberOfLines} {$this->container}");
|
|
|
|
}
|
2023-10-02 11:38:16 +00:00
|
|
|
if ($refresh) {
|
|
|
|
$this->outputs = '';
|
|
|
|
}
|
2023-11-06 12:53:05 +00:00
|
|
|
Process::run($sshCommand, function (string $type, string $output) {
|
|
|
|
$this->doSomethingWithThisChunkOfOutput($output);
|
|
|
|
});
|
|
|
|
if ($this->showTimeStamps) {
|
|
|
|
$this->outputs = str($this->outputs)->split('/\n/')->sort(function ($a, $b) {
|
|
|
|
$a = explode(' ', $a);
|
|
|
|
$b = explode(' ', $b);
|
|
|
|
return $a[0] <=> $b[0];
|
|
|
|
})->join("\n");
|
|
|
|
}
|
2023-10-02 11:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.project.shared.get-logs');
|
|
|
|
}
|
|
|
|
}
|