coolify/app/Livewire/RunCommand.php

71 lines
2 KiB
PHP
Raw Normal View History

2023-03-20 12:04:22 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire;
2023-03-20 12:04:22 +00:00
2024-09-13 10:21:02 +00:00
use Livewire\Attributes\On;
2023-03-20 12:04:22 +00:00
use Livewire\Component;
class RunCommand extends Component
{
public $selected_uuid = 'default';
2024-06-10 20:43:34 +00:00
public $servers = [];
2024-09-13 10:21:02 +00:00
public $containers = [];
2023-05-12 13:39:07 +00:00
public function mount($servers)
{
if (! auth()->user()->isAdmin()) {
abort(403);
}
2023-05-12 13:39:07 +00:00
$this->servers = $servers;
2024-09-13 10:21:02 +00:00
$this->containers = $this->getAllActiveContainers();
}
2023-03-20 12:04:22 +00:00
2024-09-13 10:21:02 +00:00
private function getAllActiveContainers()
2023-03-20 12:04:22 +00:00
{
2024-09-13 10:21:02 +00:00
return collect($this->servers)->flatMap(function ($server) {
if (! $server->isFunctional()) {
return [];
}
return $server->loadAllContainers()->map(function ($container) use ($server) {
$state = data_get_str($container, 'State')->lower();
if ($state->contains('running')) {
2024-09-13 10:21:02 +00:00
return [
'name' => data_get($container, 'Names'),
'connection_name' => data_get($container, 'Names'),
'uuid' => data_get($container, 'Names'),
'status' => data_get_str($container, 'State')->lower(),
2024-09-13 10:21:02 +00:00
'server' => $server,
'server_uuid' => $server->uuid,
];
}
return null;
})->filter();
2024-09-13 10:21:02 +00:00
});
}
public function updatedSelectedUuid()
{
$this->connectToContainer();
}
2024-09-13 10:21:02 +00:00
#[On('connectToContainer')]
public function connectToContainer()
{
if ($this->selected_uuid === 'default') {
$this->dispatch('error', 'Please select a server or a container.');
return;
}
2024-09-13 10:21:02 +00:00
$container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid);
$this->dispatch('send-terminal-command',
isset($container),
$container['connection_name'] ?? $this->selected_uuid,
$container['server_uuid'] ?? $this->selected_uuid
);
2023-03-20 12:04:22 +00:00
}
}