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

114 lines
4.8 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;
use App\Models\Server;
use App\Models\Service;
2023-10-24 12:31:28 +00:00
use App\Models\StandaloneMariadb;
2023-10-19 11:32:03 +00:00
use App\Models\StandaloneMongodb;
2023-10-24 12:31:28 +00:00
use App\Models\StandaloneMysql;
2023-10-02 11:38:16 +00:00
use App\Models\StandalonePostgresql;
2023-10-12 15:18:33 +00:00
use App\Models\StandaloneRedis;
use Illuminate\Support\Collection;
2023-10-02 11:38:16 +00:00
use Livewire\Component;
class Logs extends Component
{
public ?string $type = null;
2023-10-24 12:31:28 +00:00
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb $resource;
public Collection $servers;
public Collection $containers;
public $container = [];
2023-10-02 11:38:16 +00:00
public $parameters;
public $query;
public $status;
public $serviceSubType;
2023-10-02 11:38:16 +00:00
public function loadContainers($server_id)
{
try {
$server = $this->servers->firstWhere('id', $server_id);
if ($server->isSwarm()) {
$containers = collect([
[
'Names' => $this->resource->uuid . '_' . $this->resource->uuid,
]
]);
} else {
$containers = getCurrentApplicationContainerStatus($server, $this->resource->id, includePullrequests: true);
}
$server->containers = $containers;
} catch (\Exception $e) {
return handleError($e, $this);
}
}
2023-10-02 11:38:16 +00:00
public function mount()
{
2024-03-05 08:19:15 +00:00
try {
$this->containers = collect();
$this->servers = collect();
$this->parameters = get_route_parameters();
$this->query = request()->query();
if (data_get($this->parameters, 'application_uuid')) {
$this->type = 'application';
$this->resource = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail();
$this->status = $this->resource->status;
if ($this->resource->destination->server->isFunctional()) {
$this->servers = $this->servers->push($this->resource->destination->server);
2024-02-27 08:01:19 +00:00
}
2024-03-05 08:19:15 +00:00
foreach ($this->resource->additional_servers as $server) {
if ($server->isFunctional()) {
$this->servers = $this->servers->push($server);
}
}
} else if (data_get($this->parameters, 'database_uuid')) {
$this->type = 'database';
$resource = StandalonePostgresql::where('uuid', $this->parameters['database_uuid'])->first();
2023-10-12 15:18:33 +00:00
if (is_null($resource)) {
2024-03-05 08:19:15 +00:00
$resource = StandaloneRedis::where('uuid', $this->parameters['database_uuid'])->first();
2023-10-19 11:32:03 +00:00
if (is_null($resource)) {
2024-03-05 08:19:15 +00:00
$resource = StandaloneMongodb::where('uuid', $this->parameters['database_uuid'])->first();
2023-10-24 12:31:28 +00:00
if (is_null($resource)) {
2024-03-05 08:19:15 +00:00
$resource = StandaloneMysql::where('uuid', $this->parameters['database_uuid'])->first();
2023-10-24 12:31:28 +00:00
if (is_null($resource)) {
2024-03-05 08:19:15 +00:00
$resource = StandaloneMariadb::where('uuid', $this->parameters['database_uuid'])->first();
if (is_null($resource)) {
abort(404);
}
2023-10-24 12:31:28 +00:00
}
}
2023-10-19 11:32:03 +00:00
}
2023-10-12 15:18:33 +00:00
}
2024-03-05 08:19:15 +00:00
$this->resource = $resource;
$this->status = $this->resource->status;
if ($this->resource->destination->server->isFunctional()) {
$this->servers = $this->servers->push($this->resource->destination->server);
}
$this->container = $this->resource->uuid;
$this->containers->push($this->container);
} else if (data_get($this->parameters, 'service_uuid')) {
$this->type = 'service';
$this->resource = Service::where('uuid', $this->parameters['service_uuid'])->firstOrFail();
$this->resource->applications()->get()->each(function ($application) {
$this->containers->push(data_get($application, 'name') . '-' . data_get($this->resource, 'uuid'));
});
$this->resource->databases()->get()->each(function ($database) {
$this->containers->push(data_get($database, 'name') . '-' . data_get($this->resource, 'uuid'));
});
2024-03-05 08:19:15 +00:00
if ($this->resource->server->isFunctional()) {
$this->servers = $this->servers->push($this->resource->server);
}
}
2024-03-05 08:19:15 +00:00
} catch (\Exception $e) {
return handleError($e, $this);
2023-10-02 11:38:16 +00:00
}
}
public function render()
{
return view('livewire.project.shared.logs');
}
}