coolify/app/Livewire/Server/Proxy/Deploy.php

104 lines
2.9 KiB
PHP
Raw Normal View History

2023-06-02 15:15:12 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Server\Proxy;
2023-06-02 15:15:12 +02:00
use App\Actions\Proxy\CheckProxy;
2023-07-14 13:38:24 +02:00
use App\Actions\Proxy\StartProxy;
use App\Events\ProxyStatusChanged;
2023-06-02 15:15:12 +02:00
use App\Models\Server;
use Livewire\Component;
class Deploy extends Component
{
public Server $server;
public bool $traefikDashboardAvailable = false;
public ?string $currentRoute = null;
2023-10-13 14:35:02 +02:00
public ?string $serverIp = null;
public function getListeners()
{
$teamId = auth()->user()->currentTeam()->id;
return [
"echo-private:team.{$teamId},ProxyStatusChanged" => 'proxyStarted',
'proxyStatusUpdated',
'traefikDashboardAvailable',
'serverRefresh' => 'proxyStatusUpdated',
"checkProxy", "startProxy"
];
}
public function mount()
{
2023-10-13 14:35:02 +02:00
if ($this->server->id === 0) {
$this->serverIp = base_ip();
} else {
$this->serverIp = $this->server->ip;
}
$this->currentRoute = request()->route()->getName();
}
public function traefikDashboardAvailable(bool $data)
{
$this->traefikDashboardAvailable = $data;
}
public function proxyStarted()
2023-09-18 12:18:45 +02:00
{
CheckProxy::run($this->server, true);
$this->dispatch('success', 'Proxy started.');
2023-09-11 22:29:34 +02:00
}
public function proxyStatusUpdated()
2023-10-13 14:35:02 +02:00
{
$this->server->refresh();
2023-10-13 14:35:02 +02:00
}
2024-03-21 12:44:32 +01:00
public function restart()
{
try {
$this->stop();
$this->dispatch('checkProxy');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2023-10-13 14:35:02 +02:00
public function checkProxy()
{
try {
2023-10-17 19:00:23 +02:00
CheckProxy::run($this->server, true);
2023-12-07 19:06:32 +01:00
$this->dispatch('startProxyPolling');
$this->dispatch('proxyChecked');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2023-09-11 22:29:34 +02:00
public function startProxy()
2023-06-02 15:15:12 +02:00
{
2023-09-18 12:18:45 +02:00
try {
2023-09-18 12:29:50 +02:00
$activity = StartProxy::run($this->server);
$this->dispatch('activityMonitor', $activity->id, ProxyStatusChanged::class);
2023-09-18 12:18:45 +02:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-09-18 12:18:45 +02:00
}
2023-06-02 15:15:12 +02:00
}
2023-06-02 15:15:12 +02:00
public function stop()
{
2023-11-29 14:59:06 +01:00
try {
if ($this->server->isSwarm()) {
instant_remote_process([
"docker service rm coolify-proxy_traefik",
], $this->server);
$this->server->proxy->status = 'exited';
$this->server->save();
2023-12-07 19:06:32 +01:00
$this->dispatch('proxyStatusUpdated');
2023-11-29 14:59:06 +01:00
} else {
instant_remote_process([
"docker rm -f coolify-proxy",
], $this->server);
$this->server->proxy->status = 'exited';
$this->server->save();
2023-12-07 19:06:32 +01:00
$this->dispatch('proxyStatusUpdated');
2023-11-29 14:59:06 +01:00
}
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-06-02 15:15:12 +02:00
}
}