Skip to content

Commit f71fb72

Browse files
committed
fix: terminal
1 parent 35f23cf commit f71fb72

14 files changed

+118
-59
lines changed

app/Livewire/Project/Shared/ExecuteContainerCommand.php

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ExecuteContainerCommand extends Component
1313
{
14-
public string $container;
14+
public $container;
1515

1616
public Collection $containers;
1717

@@ -57,24 +57,13 @@ public function mount()
5757
if ($this->resource->destination->server->isFunctional()) {
5858
$this->servers = $this->servers->push($this->resource->destination->server);
5959
}
60-
$this->container = $this->resource->uuid;
61-
$this->containers->push($this->container);
6260
} elseif (data_get($this->parameters, 'service_uuid')) {
6361
$this->type = 'service';
6462
$this->resource = Service::where('uuid', $this->parameters['service_uuid'])->firstOrFail();
65-
$this->resource->applications()->get()->each(function ($application) {
66-
$this->containers->push(data_get($application, 'name').'-'.data_get($this->resource, 'uuid'));
67-
});
68-
$this->resource->databases()->get()->each(function ($database) {
69-
$this->containers->push(data_get($database, 'name').'-'.data_get($this->resource, 'uuid'));
70-
});
7163
if ($this->resource->server->isFunctional()) {
7264
$this->servers = $this->servers->push($this->resource->server);
7365
}
7466
}
75-
if ($this->containers->count() > 0) {
76-
$this->container = $this->containers->first();
77-
}
7867
}
7968

8069
public function loadContainers()
@@ -97,37 +86,59 @@ public function loadContainers()
9786
];
9887
$this->containers = $this->containers->push($payload);
9988
}
100-
}
101-
}
102-
if ($this->containers->count() > 0) {
103-
if (data_get($this->parameters, 'application_uuid')) {
104-
$this->container = data_get($this->containers->first(), 'container.Names');
10589
} elseif (data_get($this->parameters, 'database_uuid')) {
106-
$this->container = $this->containers->first();
90+
if ($this->resource->isRunning()) {
91+
$this->containers = $this->containers->push([
92+
'server' => $server,
93+
'container' => [
94+
'Names' => $this->resource->uuid,
95+
],
96+
]);
97+
}
10798
} elseif (data_get($this->parameters, 'service_uuid')) {
108-
$this->container = $this->containers->first();
109-
}
110-
if ($this->containers->count() === 1) {
111-
$this->dispatch('connectToContainer');
99+
$this->resource->applications()->get()->each(function ($application) {
100+
ray($application);
101+
if ($application->isRunning()) {
102+
$this->containers->push([
103+
'server' => $this->resource->server,
104+
'container' => [
105+
'Names' => data_get($application, 'name').'-'.data_get($this->resource, 'uuid'),
106+
],
107+
]);
108+
}
109+
});
110+
$this->resource->databases()->get()->each(function ($database) {
111+
if ($database->isRunning()) {
112+
$this->containers->push([
113+
'server' => $this->resource->server,
114+
'container' => [
115+
'Names' => data_get($database, 'name').'-'.data_get($this->resource, 'uuid'),
116+
],
117+
]);
118+
}
119+
});
112120
}
121+
122+
}
123+
if ($this->containers->count() > 0) {
124+
$this->container = $this->containers->first();
125+
}
126+
if ($this->containers->count() === 1) {
127+
$this->dispatch('connectToContainer');
113128
}
114129
}
115130

116131
#[On('connectToContainer')]
117132
public function connectToContainer()
118133
{
119134
try {
120-
if (data_get($this->parameters, 'application_uuid')) {
121-
$container = $this->containers->where('container.Names', $this->container)->first();
122-
$container_name = data_get($container, 'container.Names');
123-
if (is_null($container)) {
124-
throw new \RuntimeException('Container not found.');
125-
}
126-
$server = data_get($container, 'server');
127-
} else {
128-
$container_name = $this->container;
129-
$server = $this->servers->first();
135+
$container_name = data_get($this->container, 'container.Names');
136+
ray($this->container);
137+
if (is_null($container_name)) {
138+
throw new \RuntimeException('Container not found.');
130139
}
140+
$server = data_get($this->container, 'server');
141+
131142
if ($server->isForceDisabled()) {
132143
throw new \RuntimeException('Server is disabled.');
133144
}

app/Livewire/Project/Shared/Terminal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function sendTerminalCommand($isContainer, $identifier, $serverUuid)
2424
if ($isContainer) {
2525
$status = getContainerStatus($server, $identifier);
2626
if ($status !== 'running') {
27-
return handleError(new \Exception('Container is not running'), $this);
27+
return;
2828
}
2929
$command = generateSshCommand($server, "docker exec -it {$identifier} sh -c 'if [ -f ~/.profile ]; then . ~/.profile; fi; if [ -n \"\$SHELL\" ]; then exec \$SHELL; else sh; fi'");
3030
} else {

app/Models/ServiceApplication.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public static function ownedByCurrentTeamAPI(int $teamId)
3232
return ServiceApplication::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');
3333
}
3434

35+
public function isRunning()
36+
{
37+
return str($this->status)->contains('running');
38+
}
39+
40+
public function isExited()
41+
{
42+
return str($this->status)->contains('exited');
43+
}
44+
3545
public function isLogDrainEnabled()
3646
{
3747
return data_get($this, 'is_log_drain_enabled', false);

app/Models/ServiceDatabase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public function restart()
2525
remote_process(["docker restart {$container_id}"], $this->service->server);
2626
}
2727

28+
public function isRunning()
29+
{
30+
return str($this->status)->contains('running');
31+
}
32+
33+
public function isExited()
34+
{
35+
return str($this->status)->contains('exited');
36+
}
37+
2838
public function isLogDrainEnabled()
2939
{
3040
return data_get($this, 'is_log_drain_enabled', false);

app/Models/StandaloneClickhouse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function isConfigurationChanged(bool $save = false)
7575
}
7676
}
7777

78+
public function isRunning()
79+
{
80+
return (bool) str($this->status)->contains('running');
81+
}
82+
7883
public function isExited()
7984
{
8085
return (bool) str($this->status)->startsWith('exited');

app/Models/StandaloneDragonfly.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function isConfigurationChanged(bool $save = false)
7575
}
7676
}
7777

78+
public function isRunning()
79+
{
80+
return (bool) str($this->status)->contains('running');
81+
}
82+
7883
public function isExited()
7984
{
8085
return (bool) str($this->status)->startsWith('exited');

app/Models/StandaloneKeydb.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function isConfigurationChanged(bool $save = false)
7575
}
7676
}
7777

78+
public function isRunning()
79+
{
80+
return (bool) str($this->status)->contains('running');
81+
}
82+
7883
public function isExited()
7984
{
8085
return (bool) str($this->status)->startsWith('exited');

app/Models/StandaloneMariadb.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function isConfigurationChanged(bool $save = false)
7575
}
7676
}
7777

78+
public function isRunning()
79+
{
80+
return (bool) str($this->status)->contains('running');
81+
}
82+
7883
public function isExited()
7984
{
8085
return (bool) str($this->status)->startsWith('exited');

app/Models/StandaloneMongodb.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public function isConfigurationChanged(bool $save = false)
7979
}
8080
}
8181

82+
public function isRunning()
83+
{
84+
return (bool) str($this->status)->contains('running');
85+
}
86+
8287
public function isExited()
8388
{
8489
return (bool) str($this->status)->startsWith('exited');

app/Models/StandaloneMysql.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public function isConfigurationChanged(bool $save = false)
7676
}
7777
}
7878

79+
public function isRunning()
80+
{
81+
return (bool) str($this->status)->contains('running');
82+
}
83+
7984
public function isExited()
8085
{
8186
return (bool) str($this->status)->startsWith('exited');

0 commit comments

Comments
 (0)