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

159 lines
6.0 KiB
PHP
Raw Normal View History

2023-10-02 13:38:16 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Shared;
2023-10-02 13:38:16 +02:00
use App\Models\Application;
2023-10-02 13:38:16 +02:00
use App\Models\Server;
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
2024-04-10 15:00:46 +02: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 13:38:16 +02:00
use Illuminate\Support\Facades\Process;
use Livewire\Component;
class GetLogs extends Component
{
public string $outputs = '';
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public string $errors = '';
2024-06-10 22:43:34 +02:00
2024-04-10 15:00:46 +02:00
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|null $resource = null;
2024-06-10 22:43:34 +02:00
public ServiceApplication|ServiceDatabase|null $servicesubtype = null;
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public Server $server;
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public ?string $container = null;
2024-06-10 22:43:34 +02:00
2024-02-27 09:01:19 +01:00
public ?string $pull_request = null;
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public ?bool $streamLogs = false;
2024-06-10 22:43:34 +02:00
2023-10-19 13:32:03 +02:00
public ?bool $showTimeStamps = true;
2024-06-10 22:43:34 +02:00
2023-10-02 14:01:54 +02:00
public int $numberOfLines = 100;
public function mount()
{
2024-06-10 22:43:34 +02:00
if (! is_null($this->resource)) {
2023-12-07 22:56:55 +01:00
if ($this->resource->getMorphClass() === 'App\Models\Application') {
$this->showTimeStamps = $this->resource->settings->is_include_timestamps;
} else {
2023-12-07 22:56:55 +01:00
if ($this->servicesubtype) {
$this->showTimeStamps = $this->servicesubtype->is_include_timestamps;
} else {
$this->showTimeStamps = $this->resource->is_include_timestamps;
}
}
if ($this->resource?->getMorphClass() === 'App\Models\Application') {
if (str($this->container)->contains('-pr-')) {
2024-06-10 22:43:34 +02:00
$this->pull_request = 'Pull Request: '.str($this->container)->afterLast('-pr-')->beforeLast('_')->value();
}
}
}
}
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public function doSomethingWithThisChunkOfOutput($output)
{
$this->outputs .= removeAnsiColors($output);
2023-10-02 13:38:16 +02:00
}
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public function instantSave()
{
2024-06-10 22:43:34 +02:00
if (! is_null($this->resource)) {
2023-12-07 22:56:55 +01:00
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 22:56:55 +01:00
} else {
$subType = $this->resource->databases()->where('name', $serviceName)->first();
if ($subType) {
$subType->is_include_timestamps = $this->showTimeStamps;
$subType->save();
}
2023-12-07 22:56:55 +01:00
}
}
}
2023-10-02 13:38:16 +02:00
}
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public function getLogs($refresh = false)
{
2024-06-10 22:43:34 +02:00
if (! $this->server->isFunctional()) {
return;
}
2024-06-10 22:43:34 +02:00
if (! $refresh && ($this->resource?->getMorphClass() === 'App\Models\Service' || str($this->container)->contains('-pr-'))) {
return;
}
if (! $this->numberOfLines) {
2024-03-17 18:27:01 +01:00
$this->numberOfLines = 1000;
}
2023-12-11 15:09:36 +01:00
if ($this->container) {
if ($this->showTimeStamps) {
2023-12-20 14:11:50 +01:00
if ($this->server->isSwarm()) {
2024-05-02 11:06:12 +02:00
$command = "docker service logs -n {$this->numberOfLines} -t {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
$sshCommand = generateSshCommand($this->server, $command);
2023-12-20 14:11:50 +01:00
} else {
2024-05-02 11:06:12 +02:00
$command = "docker logs -n {$this->numberOfLines} -t {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
$sshCommand = generateSshCommand($this->server, $command);
2023-12-20 14:11:50 +01:00
}
2023-12-11 15:09:36 +01:00
} else {
2023-12-20 14:11:50 +01:00
if ($this->server->isSwarm()) {
2024-05-02 11:06:12 +02:00
$command = "docker service logs -n {$this->numberOfLines} {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
$sshCommand = generateSshCommand($this->server, $command);
2023-12-20 14:11:50 +01:00
} else {
2024-05-02 11:06:12 +02:00
$command = "docker logs -n {$this->numberOfLines} {$this->container}";
if ($this->server->isNonRoot()) {
$command = parseCommandsByLineForSudo(collect($command), $this->server);
$command = $command[0];
}
$sshCommand = generateSshCommand($this->server, $command);
2023-12-20 14:11:50 +01:00
}
2023-12-11 15:09:36 +01:00
}
if ($refresh) {
$this->outputs = '';
}
2023-12-11 15:09:36 +01: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);
2024-06-10 22:43:34 +02:00
2023-12-11 15:09:36 +01:00
return $a[0] <=> $b[0];
})->join("\n");
}
}
2023-10-02 13:38:16 +02:00
}
2024-06-10 22:43:34 +02:00
2023-10-02 13:38:16 +02:00
public function render()
{
return view('livewire.project.shared.get-logs');
}
}