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

273 lines
9.9 KiB
PHP
Raw Permalink 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
2024-09-23 17:51:31 +00:00
use App\Helpers\SshMultiplexingHelper;
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 const MAX_LOG_LINES = 50000;
public const MAX_DOWNLOAD_SIZE_BYTES = 50 * 1024 * 1024; // 50MB
2023-10-02 11:38:16 +00:00
public string $outputs = '';
2024-06-10 20:43:34 +00:00
2023-10-02 11:38:16 +00:00
public string $errors = '';
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|null $resource = null;
2024-06-10 20:43:34 +00:00
public ServiceApplication|ServiceDatabase|null $servicesubtype = null;
2024-06-10 20:43:34 +00:00
2023-10-02 11:38:16 +00:00
public Server $server;
2024-06-10 20:43:34 +00:00
2023-10-02 11:38:16 +00:00
public ?string $container = null;
2024-06-10 20:43:34 +00:00
public ?string $displayName = null;
2024-02-27 08:01:19 +00:00
public ?string $pull_request = null;
2024-06-10 20:43:34 +00:00
2023-10-02 11:38:16 +00:00
public ?bool $streamLogs = false;
2024-06-10 20:43:34 +00:00
public ?bool $showTimeStamps = true;
2024-06-10 20:43:34 +00:00
2024-10-25 15:38:14 +00:00
public ?int $numberOfLines = 100;
public bool $expandByDefault = false;
public bool $collapsible = true;
public function mount()
{
2024-06-10 20:43:34 +00:00
if (! is_null($this->resource)) {
2024-10-28 13:56:13 +00:00
if ($this->resource->getMorphClass() === \App\Models\Application::class) {
2023-12-07 21:56:55 +00:00
$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;
}
}
2024-10-28 13:56:13 +00:00
if ($this->resource?->getMorphClass() === \App\Models\Application::class) {
if (str($this->container)->contains('-pr-')) {
2024-06-10 20:43:34 +00:00
$this->pull_request = 'Pull Request: '.str($this->container)->afterLast('-pr-')->beforeLast('_')->value();
}
}
}
}
2024-06-10 20:43:34 +00:00
2023-10-02 11:38:16 +00:00
public function instantSave()
{
2024-06-10 20:43:34 +00:00
if (! is_null($this->resource)) {
2024-10-28 13:56:13 +00:00
if ($this->resource->getMorphClass() === \App\Models\Application::class) {
2023-12-07 21:56:55 +00:00
$this->resource->settings->is_include_timestamps = $this->showTimeStamps;
$this->resource->settings->save();
}
2024-10-28 13:56:13 +00:00
if ($this->resource->getMorphClass() === \App\Models\Service::class) {
$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
}
2024-06-10 20:43:34 +00:00
public function toggleTimestamps()
{
$previousValue = $this->showTimeStamps;
$this->showTimeStamps = ! $this->showTimeStamps;
try {
$this->instantSave();
$this->getLogs(true);
} catch (\Throwable $e) {
// Revert the flag to its previous value on failure
$this->showTimeStamps = $previousValue;
return handleError($e, $this);
}
}
public function toggleStreamLogs()
{
$this->streamLogs = ! $this->streamLogs;
}
2023-10-02 11:38:16 +00:00
public function getLogs($refresh = false)
{
2024-06-10 20:43:34 +00:00
if (! $this->server->isFunctional()) {
return;
}
if (! $refresh && ! $this->expandByDefault && ($this->resource?->getMorphClass() === \App\Models\Service::class || str($this->container)->contains('-pr-'))) {
2024-06-10 20:43:34 +00:00
return;
}
2024-10-25 15:38:14 +00:00
if ($this->numberOfLines <= 0 || is_null($this->numberOfLines)) {
2024-03-17 17:27:01 +00:00
$this->numberOfLines = 1000;
}
if ($this->numberOfLines > self::MAX_LOG_LINES) {
$this->numberOfLines = self::MAX_LOG_LINES;
}
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()) {
2024-05-02 09:06:12 +00:00
$command = "docker service logs -n {$this->numberOfLines} -t {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
2024-09-17 10:26:11 +00:00
$sshCommand = SshMultiplexingHelper::generateSshCommand($this->server, $command);
2023-12-20 13:11:50 +00:00
} else {
2024-05-02 09:06:12 +00:00
$command = "docker logs -n {$this->numberOfLines} -t {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
2024-09-17 10:26:11 +00:00
$sshCommand = SshMultiplexingHelper::generateSshCommand($this->server, $command);
2023-12-20 13:11:50 +00:00
}
2023-12-11 14:09:36 +00:00
} else {
2023-12-20 13:11:50 +00:00
if ($this->server->isSwarm()) {
2024-05-02 09:06:12 +00:00
$command = "docker service logs -n {$this->numberOfLines} {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
2024-09-17 10:26:11 +00:00
$sshCommand = SshMultiplexingHelper::generateSshCommand($this->server, $command);
2023-12-20 13:11:50 +00:00
} else {
2024-05-02 09:06:12 +00:00
$command = "docker logs -n {$this->numberOfLines} {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
2024-09-17 10:26:11 +00:00
$sshCommand = SshMultiplexingHelper::generateSshCommand($this->server, $command);
2023-12-20 13:11:50 +00:00
}
2023-12-11 14:09:36 +00:00
}
// Collect new logs into temporary variable first to prevent flickering
// (avoids clearing output before new data is ready)
// Use array accumulation + implode for O(n) instead of O(n²) string concatenation
$logChunks = [];
Process::timeout(config('constants.ssh.command_timeout'))->run($sshCommand, function (string $type, string $output) use (&$logChunks) {
$logChunks[] = removeAnsiColors($output);
2023-12-11 14:09:36 +00:00
});
$newOutputs = implode('', $logChunks);
2023-12-11 14:09:36 +00:00
if ($this->showTimeStamps) {
$newOutputs = str($newOutputs)->split('/\n/')->sort(function ($a, $b) {
2023-12-11 14:09:36 +00:00
$a = explode(' ', $a);
$b = explode(' ', $b);
2024-06-10 20:43:34 +00:00
2023-12-11 14:09:36 +00:00
return $a[0] <=> $b[0];
})->join("\n");
}
// Only update outputs after new data is ready (atomic update prevents flicker)
$this->outputs = $newOutputs;
2023-12-11 14:09:36 +00:00
}
2023-10-02 11:38:16 +00:00
}
2024-06-10 20:43:34 +00:00
public function copyLogs(): string
{
return sanitizeLogsForExport($this->outputs);
}
public function downloadAllLogs(): string
{
if (! $this->server->isFunctional() || ! $this->container) {
return '';
}
if ($this->showTimeStamps) {
if ($this->server->isSwarm()) {
$command = "docker service logs -t {$this->container}";
} else {
$command = "docker logs -t {$this->container}";
}
} else {
if ($this->server->isSwarm()) {
$command = "docker service logs {$this->container}";
} else {
$command = "docker logs {$this->container}";
}
}
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
$sshCommand = SshMultiplexingHelper::generateSshCommand($this->server, $command);
// Use array accumulation + implode for O(n) instead of O(n²) string concatenation
// Enforce 50MB size limit to prevent memory exhaustion from large logs
$logChunks = [];
$accumulatedBytes = 0;
$truncated = false;
Process::timeout(config('constants.ssh.command_timeout'))->run($sshCommand, function (string $type, string $output) use (&$logChunks, &$accumulatedBytes, &$truncated) {
if ($truncated) {
return;
}
$output = removeAnsiColors($output);
$outputBytes = strlen($output);
if ($accumulatedBytes + $outputBytes > self::MAX_DOWNLOAD_SIZE_BYTES) {
$remaining = self::MAX_DOWNLOAD_SIZE_BYTES - $accumulatedBytes;
if ($remaining > 0) {
$logChunks[] = substr($output, 0, $remaining);
}
$truncated = true;
return;
}
$logChunks[] = $output;
$accumulatedBytes += $outputBytes;
});
$allLogs = implode('', $logChunks);
if ($truncated) {
$allLogs .= "\n\n[... Output truncated at 50MB limit ...]";
}
if ($this->showTimeStamps) {
$allLogs = str($allLogs)->split('/\n/')->sort(function ($a, $b) {
$a = explode(' ', $a);
$b = explode(' ', $b);
return $a[0] <=> $b[0];
})->join("\n");
}
return sanitizeLogsForExport($allLogs);
}
2023-10-02 11:38:16 +00:00
public function render()
{
return view('livewire.project.shared.get-logs');
}
}