fix: proxy configuration + starter

This commit is contained in:
Andras Bacsai
2023-09-25 09:17:42 +02:00
parent 80a797aec8
commit 51c468ae0b
9 changed files with 49 additions and 35 deletions

View File

@@ -22,6 +22,7 @@ class StartProxy
if (!$configuration) { if (!$configuration) {
throw new \Exception("Configuration is not synced"); throw new \Exception("Configuration is not synced");
} }
SaveConfiguration::run($server, $configuration);
$docker_compose_yml_base64 = base64_encode($configuration); $docker_compose_yml_base64 = base64_encode($configuration);
$server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value; $server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save(); $server->save();
@@ -50,12 +51,15 @@ class StartProxy
"echo '####### Proxy installed successfully.'" "echo '####### Proxy installed successfully.'"
]); ]);
$commands = $commands->merge(connectProxyToNetworks($server)); $commands = $commands->merge(connectProxyToNetworks($server));
if (!$async) { if ($async) {
instant_remote_process($commands, $server);
return 'OK';
} else {
$activity = remote_process($commands, $server); $activity = remote_process($commands, $server);
return $activity; return $activity;
} else {
instant_remote_process($commands, $server);
$server->proxy->set('status', 'running');
$server->proxy->set('type', $proxyType);
$server->save();
return 'OK';
} }
} }
} }

View File

@@ -38,8 +38,8 @@ class Proxy extends Component
public function select_proxy($proxy_type) public function select_proxy($proxy_type)
{ {
$this->server->proxy->type = $proxy_type; $this->server->proxy->set('status', 'exited');
$this->server->proxy->status = 'exited'; $this->server->proxy->set('type', $proxy_type);
$this->server->save(); $this->server->save();
$this->selectedProxy = $this->server->proxy->type; $this->selectedProxy = $this->server->proxy->type;
$this->emit('proxyStatusUpdated'); $this->emit('proxyStatusUpdated');

View File

@@ -11,6 +11,8 @@ class Modal extends Component
public function proxyStatusUpdated() public function proxyStatusUpdated()
{ {
$this->server->proxy->set('status', 'running');
$this->server->save();
$this->emit('proxyStatusUpdated'); $this->emit('proxyStatusUpdated');
} }
} }

View File

@@ -77,11 +77,13 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted
$services = $this->server->services(); $services = $this->server->services();
$previews = $this->server->previews(); $previews = $this->server->previews();
$this->server->proxyType();
/// Check if proxy is running /// Check if proxy is running
$foundProxyContainer = $containers->filter(function ($value, $key) { $foundProxyContainer = $containers->filter(function ($value, $key) {
return data_get($value, 'Name') === '/coolify-proxy'; return data_get($value, 'Name') === '/coolify-proxy';
})->first(); })->first();
if (!$foundProxyContainer) { if (!$foundProxyContainer) {
ray('Proxy not found, starting it...');
if ($this->server->isProxyShouldRun()) { if ($this->server->isProxyShouldRun()) {
StartProxy::run($this->server, false); StartProxy::run($this->server, false);
$this->server->team->notify(new ContainerRestarted('coolify-proxy', $this->server)); $this->server->team->notify(new ContainerRestarted('coolify-proxy', $this->server));

View File

@@ -78,7 +78,8 @@ class Server extends BaseModel
return $this->hasOne(ServerSetting::class); return $this->hasOne(ServerSetting::class);
} }
public function proxyType() { public function proxyType()
{
$type = $this->proxy->get('type'); $type = $this->proxy->get('type');
if (is_null($type)) { if (is_null($type)) {
$this->proxy->type = ProxyTypes::TRAEFIK_V2->value; $this->proxy->type = ProxyTypes::TRAEFIK_V2->value;
@@ -115,11 +116,13 @@ class Server extends BaseModel
return $standaloneDocker->applications; return $standaloneDocker->applications;
})->flatten(); })->flatten();
} }
public function services() { public function services()
{
return $this->hasMany(Service::class); return $this->hasMany(Service::class);
} }
public function previews() { public function previews()
{
return $this->destinations()->map(function ($standaloneDocker) { return $this->destinations()->map(function ($standaloneDocker) {
return $standaloneDocker->applications->map(function ($application) { return $standaloneDocker->applications->map(function ($application) {
return $application->previews; return $application->previews;
@@ -161,6 +164,9 @@ class Server extends BaseModel
public function isProxyShouldRun() public function isProxyShouldRun()
{ {
$shouldRun = false; $shouldRun = false;
if ($this->proxyType() === ProxyTypes::NONE->value) {
return false;
}
foreach ($this->applications() as $application) { foreach ($this->applications() as $application) {
if (data_get($application, 'fqdn')) { if (data_get($application, 'fqdn')) {
$shouldRun = true; $shouldRun = true;
@@ -175,7 +181,8 @@ class Server extends BaseModel
} }
return $shouldRun; return $shouldRun;
} }
public function isFunctional() { public function isFunctional()
{
return $this->settings->is_reachable && $this->settings->is_usable; return $this->settings->is_reachable && $this->settings->is_usable;
} }
} }

View File

@@ -405,7 +405,7 @@ class Service extends BaseModel
} }
} }
} }
if ($this->server->proxyType() === ProxyTypes::TRAEFIK_V2->value) { // Add labels to the service
$labels = collect(data_get($service, 'labels', [])); $labels = collect(data_get($service, 'labels', []));
$labels = collect([]); $labels = collect([]);
$labels = $labels->merge(defaultLabels($this->id, $container_name, type: 'service')); $labels = $labels->merge(defaultLabels($this->id, $container_name, type: 'service'));
@@ -415,7 +415,6 @@ class Service extends BaseModel
} }
} }
data_set($service, 'labels', $labels->toArray()); data_set($service, 'labels', $labels->toArray());
}
data_forget($service, 'is_database'); data_forget($service, 'is_database');
data_set($service, 'restart', RESTART_MODE); data_set($service, 'restart', RESTART_MODE);
data_set($service, 'container_name', $container_name); data_set($service, 'container_name', $container_name);

View File

@@ -1,5 +1,6 @@
<?php <?php
use App\Actions\Proxy\SaveConfiguration;
use App\Models\Server; use App\Models\Server;
use App\Models\StandalonePostgresql; use App\Models\StandalonePostgresql;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
@@ -10,7 +11,8 @@ function get_proxy_path()
$proxy_path = "$base_path/proxy"; $proxy_path = "$base_path/proxy";
return $proxy_path; return $proxy_path;
} }
function connectProxyToNetworks(Server $server) { function connectProxyToNetworks(Server $server)
{
$networks = collect($server->standaloneDockers)->map(function ($docker) { $networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network']; return $docker['network'];
})->unique(); })->unique();
@@ -20,7 +22,7 @@ function connectProxyToNetworks(Server $server) {
$commands = $networks->map(function ($network) { $commands = $networks->map(function ($network) {
return [ return [
"echo '####### Connecting coolify-proxy to $network network...'", "echo '####### Connecting coolify-proxy to $network network...'",
"docker network ls --format '{{.Name}}' | grep '^$network$' || docker network create --attachable $network >/dev/null", "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null || docker network create --attachable $network >/dev/null",
"docker network connect $network coolify-proxy >/dev/null 2>&1 || true", "docker network connect $network coolify-proxy >/dev/null 2>&1 || true",
]; ];
}); });
@@ -101,7 +103,9 @@ function generate_default_proxy_configuration(Server $server)
if (isDev()) { if (isDev()) {
$config['services']['traefik']['command'][] = "--log.level=debug"; $config['services']['traefik']['command'][] = "--log.level=debug";
} }
return Yaml::dump($config, 4, 2); $config = Yaml::dump($config, 4, 2);
SaveConfiguration::run($server, $config);
return $config;
} }
function setup_default_redirect_404(string|null $redirect_url, Server $server) function setup_default_redirect_404(string|null $redirect_url, Server $server)

View File

@@ -19,8 +19,8 @@
configurations. configurations.
</div> </div>
@endif @endif
<x-forms.input placeholder="https://coolify.io" id="redirect_url" label="Default Redirect 404" <x-forms.input placeholder="https://app.coolify.io" id="redirect_url" label="Default Redirect 404"
helper="All urls that has no service available will be redirected to this domain.<span class='text-helper'>You can set to your main marketing page or your social media link.</span>" /> helper="All urls that has no service available will be redirected to this domain." />
<div wire:loading wire:target="loadProxyConfiguration" class="pt-4"> <div wire:loading wire:target="loadProxyConfiguration" class="pt-4">
<x-loading text="Loading proxy configuration..." /> <x-loading text="Loading proxy configuration..." />
</div> </div>
@@ -39,17 +39,13 @@
@elseif($selectedProxy === 'NONE') @elseif($selectedProxy === 'NONE')
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h2>Proxy</h2> <h2>Proxy</h2>
@if ($server->proxy->status === 'exited')
<x-forms.button wire:click.prevent="change_proxy">Switch Proxy</x-forms.button> <x-forms.button wire:click.prevent="change_proxy">Switch Proxy</x-forms.button>
@endif
</div> </div>
<div class="pt-3 pb-4">None</div> <div class="pt-3 pb-4">None</div>
@else @else
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h2>Proxy</h2> <h2>Proxy</h2>
@if ($server->proxy->status === 'exited')
<x-forms.button wire:click.prevent="change_proxy">Switch Proxy</x-forms.button> <x-forms.button wire:click.prevent="change_proxy">Switch Proxy</x-forms.button>
@endif
</div> </div>
@endif @endif
@else @else

View File

@@ -8,7 +8,7 @@
</x-slot:modalBody> </x-slot:modalBody>
</x-modal> </x-modal>
@if ($server->isFunctional() && data_get($server, 'proxy.type') !== 'NONE') @if ($server->isFunctional() && data_get($server, 'proxy.type') !== 'NONE')
@if (data_get($server, 'proxy.status') !== 'exited') @if (data_get($server, 'proxy.status') === 'running')
<div class="flex gap-4"> <div class="flex gap-4">
@if ($currentRoute === 'server.proxy' && $traefikDashboardAvailable) @if ($currentRoute === 'server.proxy' && $traefikDashboardAvailable)
<button> <button>