feat: fully functional terminal for command center
This commit is contained in:
@@ -2,42 +2,67 @@
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Actions\Server\RunCommand as ServerRunCommand;
|
||||
use App\Models\Server;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class RunCommand extends Component
|
||||
{
|
||||
public string $command;
|
||||
|
||||
public $server;
|
||||
public $selected_uuid;
|
||||
|
||||
public $servers = [];
|
||||
|
||||
protected $rules = [
|
||||
'server' => 'required',
|
||||
'command' => 'required',
|
||||
];
|
||||
|
||||
protected $validationAttributes = [
|
||||
'server' => 'server',
|
||||
'command' => 'command',
|
||||
];
|
||||
public $containers = [];
|
||||
|
||||
public function mount($servers)
|
||||
{
|
||||
$this->servers = $servers;
|
||||
$this->server = $servers[0]->uuid;
|
||||
$this->selected_uuid = $servers[0]->uuid;
|
||||
$this->containers = $this->getAllActiveContainers();
|
||||
}
|
||||
|
||||
public function runCommand()
|
||||
private function getAllActiveContainers()
|
||||
{
|
||||
$this->validate();
|
||||
try {
|
||||
$activity = ServerRunCommand::run(server: Server::where('uuid', $this->server)->first(), command: $this->command);
|
||||
$this->dispatch('activityMonitor', $activity->id);
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
return Server::all()->flatMap(function ($server) {
|
||||
if (! $server->isFunctional()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $server->definedResources()
|
||||
->filter(fn ($resource) => str_starts_with($resource->status, 'running:'))
|
||||
->map(function ($resource) use ($server) {
|
||||
$container_name = $resource->uuid;
|
||||
|
||||
if (class_basename($resource) === 'Application' || class_basename($resource) === 'Service') {
|
||||
if ($server->isSwarm()) {
|
||||
$container_name = $resource->uuid.'_'.$resource->uuid;
|
||||
} else {
|
||||
$current_containers = getCurrentApplicationContainerStatus($server, $resource->id, includePullrequests: true);
|
||||
$container_name = data_get($current_containers->first(), 'Names');
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $resource->name,
|
||||
'connection_name' => $container_name,
|
||||
'uuid' => $resource->uuid,
|
||||
'status' => $resource->status,
|
||||
'server' => $server,
|
||||
'server_uuid' => $server->uuid,
|
||||
];
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[On('connectToContainer')]
|
||||
public function connectToContainer()
|
||||
{
|
||||
$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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user