lasthourcloud/app/Livewire/Project/Shared/GetLogs.php

125 lines
5.0 KiB
PHP
Raw Normal View History

2023-10-02 11:38:16 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Shared;
2023-10-02 11:38:16 +00:00
use App\Models\Application;
2023-10-02 11:38:16 +00:00
use App\Models\Server;
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
2024-04-10 13:00:46 +00:00
use App\Models\StandaloneClickhouse;
use App\Models\StandaloneDragonfly;
use App\Models\StandaloneKeydb;
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 = '';
2024-04-10 13:00:46 +00:00
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|null $resource = null;
public ServiceApplication|ServiceDatabase|null $servicesubtype = null;
2023-10-02 11:38:16 +00:00
public Server $server;
public ?string $container = null;
2024-02-27 08:01:19 +00:00
public ?string $pull_request = null;
2023-10-02 11:38:16 +00:00
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;
public function mount()
{
2023-12-07 21:56:55 +00:00
if (!is_null($this->resource)) {
if ($this->resource->getMorphClass() === 'App\Models\Application') {
$this->showTimeStamps = $this->resource->settings->is_include_timestamps;
} else {
2023-12-07 21:56:55 +00:00
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)
{
$this->outputs .= removeAnsiColors($output);
2023-10-02 11:38:16 +00:00
}
public function instantSave()
{
2023-12-07 21:56:55 +00:00
if (!is_null($this->resource)) {
if ($this->resource->getMorphClass() === 'App\Models\Application') {
$this->resource->settings->is_include_timestamps = $this->showTimeStamps;
$this->resource->settings->save();
}
if ($this->resource->getMorphClass() === 'App\Models\Service') {
$serviceName = str($this->container)->beforeLast('-')->value();
$subType = $this->resource->applications()->where('name', $serviceName)->first();
if ($subType) {
$subType->is_include_timestamps = $this->showTimeStamps;
$subType->save();
2023-12-07 21:56:55 +00:00
} else {
$subType = $this->resource->databases()->where('name', $serviceName)->first();
if ($subType) {
$subType->is_include_timestamps = $this->showTimeStamps;
$subType->save();
}
2023-12-07 21:56:55 +00:00
}
}
}
2023-10-02 11:38:16 +00:00
}
public function getLogs($refresh = false)
{
if (!$this->server->isFunctional()) {
return;
}
if ($this->resource?->getMorphClass() === 'App\Models\Application') {
if (str($this->container)->contains('-pr-')) {
$this->pull_request = "Pull Request: " . str($this->container)->afterLast('-pr-')->beforeLast('_')->value();
} else {
$this->pull_request = 'branch';
}
}
if (!$refresh && ($this->resource?->getMorphClass() === 'App\Models\Service' || str($this->container)->contains('-pr-'))) return;
2024-03-17 17:27:01 +00:00
if (!$this->numberOfLines) {
$this->numberOfLines = 1000;
}
2023-12-11 14:09:36 +00:00
if ($this->container) {
if ($this->showTimeStamps) {
2023-12-20 13:11:50 +00:00
if ($this->server->isSwarm()) {
$sshCommand = generateSshCommand($this->server, "docker service logs -n {$this->numberOfLines} -t {$this->container}");
} else {
$sshCommand = generateSshCommand($this->server, "docker logs -n {$this->numberOfLines} -t {$this->container}");
}
2023-12-11 14:09:36 +00:00
} else {
2023-12-20 13:11:50 +00:00
if ($this->server->isSwarm()) {
$sshCommand = generateSshCommand($this->server, "docker service logs -n {$this->numberOfLines} {$this->container}");
} else {
$sshCommand = generateSshCommand($this->server, "docker logs -n {$this->numberOfLines} {$this->container}");
}
2023-12-11 14:09:36 +00:00
}
if ($refresh) {
$this->outputs = '';
}
2023-12-11 14:09:36 +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');
}
}