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;
|
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;
|
2024-04-10 13:00:46 +00:00
|
|
|
use App\Models\StandaloneClickhouse;
|
|
|
|
|
use App\Models\StandaloneDragonfly;
|
|
|
|
|
use App\Models\StandaloneKeydb;
|
2023-12-01 09:34:30 +00:00
|
|
|
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
|
|
|
|
|
{
|
2025-12-29 17:52:35 +00:00
|
|
|
public const MAX_LOG_LINES = 50000;
|
|
|
|
|
|
2026-01-02 11:04:17 +00:00
|
|
|
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
|
|
|
|
2023-12-01 09:34:30 +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
|
|
|
|
2025-10-26 22:39:40 +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
|
|
|
|
2025-12-03 08:09:12 +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;
|
2023-12-01 09:34:30 +00:00
|
|
|
|
2025-12-04 09:57:58 +00:00
|
|
|
public bool $expandByDefault = false;
|
|
|
|
|
|
2025-12-04 14:37:14 +00:00
|
|
|
public bool $collapsible = true;
|
|
|
|
|
|
2023-12-01 09:34:30 +00:00
|
|
|
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;
|
2023-12-01 09:34:30 +00:00
|
|
|
} 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-12-01 09:34:30 +00:00
|
|
|
}
|
2024-10-28 13:56:13 +00:00
|
|
|
if ($this->resource?->getMorphClass() === \App\Models\Application::class) {
|
2024-05-30 10:28:29 +00:00
|
|
|
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-05-30 10:28:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-01 09:34:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
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();
|
2023-12-11 10:27:41 +00:00
|
|
|
}
|
2024-10-28 13:56:13 +00:00
|
|
|
if ($this->resource->getMorphClass() === \App\Models\Service::class) {
|
2023-12-11 10:27:41 +00:00
|
|
|
$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 {
|
2023-12-11 10:27:41 +00:00
|
|
|
$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-12-01 09:34:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-02 11:38:16 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-12-04 09:57:58 +00:00
|
|
|
public function toggleTimestamps()
|
|
|
|
|
{
|
2025-12-04 12:14:44 +00:00
|
|
|
$previousValue = $this->showTimeStamps;
|
2025-12-04 09:57:58 +00:00
|
|
|
$this->showTimeStamps = ! $this->showTimeStamps;
|
2025-12-04 12:14:44 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2025-12-04 09:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()) {
|
2024-03-18 07:53:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2025-12-05 13:19:31 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2025-12-29 17:52:35 +00:00
|
|
|
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
|
|
|
}
|
2025-12-18 07:46:39 +00:00
|
|
|
// Collect new logs into temporary variable first to prevent flickering
|
|
|
|
|
// (avoids clearing output before new data is ready)
|
2026-01-02 11:04:17 +00:00
|
|
|
// 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
|
|
|
});
|
2026-01-02 11:04:17 +00:00
|
|
|
$newOutputs = implode('', $logChunks);
|
2025-12-18 07:46:39 +00:00
|
|
|
|
2023-12-11 14:09:36 +00:00
|
|
|
if ($this->showTimeStamps) {
|
2025-12-18 07:46:39 +00:00
|
|
|
$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");
|
|
|
|
|
}
|
2025-12-18 07:46:39 +00:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2025-12-16 02:43:18 +00:00
|
|
|
public function copyLogs(): string
|
|
|
|
|
{
|
|
|
|
|
return sanitizeLogsForExport($this->outputs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 17:59:23 +00:00
|
|
|
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);
|
|
|
|
|
|
2026-01-02 11:04:17 +00:00
|
|
|
// 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;
|
2025-12-29 17:59:23 +00:00
|
|
|
});
|
|
|
|
|
|
2026-01-02 11:04:17 +00:00
|
|
|
$allLogs = implode('', $logChunks);
|
|
|
|
|
|
|
|
|
|
if ($truncated) {
|
|
|
|
|
$allLogs .= "\n\n[... Output truncated at 50MB limit ...]";
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 17:59:23 +00:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|