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