fix: traefik labelling in case of several http and https domain added
This commit is contained in:
		@@ -69,7 +69,6 @@ class ProjectController extends Controller
 | 
				
			|||||||
        if ($type->startsWith('one-click-service-') && !is_null( (int)$server_id)) {
 | 
					        if ($type->startsWith('one-click-service-') && !is_null( (int)$server_id)) {
 | 
				
			||||||
            $oneClickServiceName = $type->after('one-click-service-')->value();
 | 
					            $oneClickServiceName = $type->after('one-click-service-')->value();
 | 
				
			||||||
            $oneClickService = data_get($services, "$oneClickServiceName.compose");
 | 
					            $oneClickService = data_get($services, "$oneClickServiceName.compose");
 | 
				
			||||||
            ray($oneClickServiceName);
 | 
					 | 
				
			||||||
            $oneClickDotEnvs = data_get($services, "$oneClickServiceName.envs", null);
 | 
					            $oneClickDotEnvs = data_get($services, "$oneClickServiceName.envs", null);
 | 
				
			||||||
            if ($oneClickDotEnvs) {
 | 
					            if ($oneClickDotEnvs) {
 | 
				
			||||||
                $oneClickDotEnvs = Str::of(base64_decode($oneClickDotEnvs))->split('/\r\n|\r|\n/');
 | 
					                $oneClickDotEnvs = Str::of(base64_decode($oneClickDotEnvs))->split('/\r\n|\r|\n/');
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -233,7 +233,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
 | 
				
			|||||||
        );
 | 
					        );
 | 
				
			||||||
        $this->build_image_name = Str::lower("{$this->application->git_repository}:build");
 | 
					        $this->build_image_name = Str::lower("{$this->application->git_repository}:build");
 | 
				
			||||||
        $this->production_image_name = Str::lower("{$this->application->uuid}:latest");
 | 
					        $this->production_image_name = Str::lower("{$this->application->uuid}:latest");
 | 
				
			||||||
        ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green();
 | 
					        // ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green();
 | 
				
			||||||
        $this->generate_compose_file();
 | 
					        $this->generate_compose_file();
 | 
				
			||||||
        $this->generate_build_env_variables();
 | 
					        $this->generate_build_env_variables();
 | 
				
			||||||
        $this->add_build_env_variables_to_dockerfile();
 | 
					        $this->add_build_env_variables_to_dockerfile();
 | 
				
			||||||
@@ -258,7 +258,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $this->build_image_name = Str::lower("{$this->application->git_repository}:{$tag}-build");
 | 
					        $this->build_image_name = Str::lower("{$this->application->git_repository}:{$tag}-build");
 | 
				
			||||||
        $this->production_image_name = Str::lower("{$this->application->uuid}:{$tag}");
 | 
					        $this->production_image_name = Str::lower("{$this->application->uuid}:{$tag}");
 | 
				
			||||||
        ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green();
 | 
					        // ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!$this->force_rebuild) {
 | 
					        if (!$this->force_rebuild) {
 | 
				
			||||||
            $this->execute_remote_command([
 | 
					            $this->execute_remote_command([
 | 
				
			||||||
@@ -307,7 +307,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
 | 
				
			|||||||
            $this->newVersionIsHealthy = true;
 | 
					            $this->newVersionIsHealthy = true;
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        ray('New container name: ', $this->container_name);
 | 
					        // ray('New container name: ', $this->container_name);
 | 
				
			||||||
        if ($this->container_name) {
 | 
					        if ($this->container_name) {
 | 
				
			||||||
            $counter = 0;
 | 
					            $counter = 0;
 | 
				
			||||||
            $this->execute_remote_command(
 | 
					            $this->execute_remote_command(
 | 
				
			||||||
@@ -354,7 +354,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->build_image_name = Str::lower("{$this->application->uuid}:pr-{$this->pull_request_id}-build");
 | 
					        $this->build_image_name = Str::lower("{$this->application->uuid}:pr-{$this->pull_request_id}-build");
 | 
				
			||||||
        $this->production_image_name = Str::lower("{$this->application->uuid}:pr-{$this->pull_request_id}");
 | 
					        $this->production_image_name = Str::lower("{$this->application->uuid}:pr-{$this->pull_request_id}");
 | 
				
			||||||
        ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green();
 | 
					        // ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green();
 | 
				
			||||||
        $this->execute_remote_command([
 | 
					        $this->execute_remote_command([
 | 
				
			||||||
            "echo 'Starting pull request (#{$this->pull_request_id}) deployment of {$this->application->git_repository}:{$this->application->git_branch}.'",
 | 
					            "echo 'Starting pull request (#{$this->pull_request_id}) deployment of {$this->application->git_repository}:{$this->application->git_branch}.'",
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
@@ -628,14 +628,14 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
 | 
				
			|||||||
    private function generate_environment_variables($ports)
 | 
					    private function generate_environment_variables($ports)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $environment_variables = collect();
 | 
					        $environment_variables = collect();
 | 
				
			||||||
        ray('Generate Environment Variables')->green();
 | 
					        // ray('Generate Environment Variables')->green();
 | 
				
			||||||
        if ($this->pull_request_id === 0) {
 | 
					        if ($this->pull_request_id === 0) {
 | 
				
			||||||
            ray($this->application->runtime_environment_variables)->green();
 | 
					            // ray($this->application->runtime_environment_variables)->green();
 | 
				
			||||||
            foreach ($this->application->runtime_environment_variables as $env) {
 | 
					            foreach ($this->application->runtime_environment_variables as $env) {
 | 
				
			||||||
                $environment_variables->push("$env->key=$env->value");
 | 
					                $environment_variables->push("$env->key=$env->value");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            ray($this->application->runtime_environment_variables_preview)->green();
 | 
					            // ray($this->application->runtime_environment_variables_preview)->green();
 | 
				
			||||||
            foreach ($this->application->runtime_environment_variables_preview as $env) {
 | 
					            foreach ($this->application->runtime_environment_variables_preview as $env) {
 | 
				
			||||||
                $environment_variables->push("$env->key=$env->value");
 | 
					                $environment_variables->push("$env->key=$env->value");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -540,7 +540,7 @@ class Service extends BaseModel
 | 
				
			|||||||
                $serviceLabels = $serviceLabels->merge($defaultLabels);
 | 
					                $serviceLabels = $serviceLabels->merge($defaultLabels);
 | 
				
			||||||
                if (!$isDatabase && $fqdns->count() > 0) {
 | 
					                if (!$isDatabase && $fqdns->count() > 0) {
 | 
				
			||||||
                    if ($fqdns) {
 | 
					                    if ($fqdns) {
 | 
				
			||||||
                        $serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik($fqdns, $containerName, true));
 | 
					                        $serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik($fqdns, true));
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                data_set($service, 'labels', $serviceLabels->toArray());
 | 
					                data_set($service, 'labels', $serviceLabels->toArray());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,12 +1,12 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use App\Enums\ProxyTypes;
 | 
					 | 
				
			||||||
use App\Models\Application;
 | 
					use App\Models\Application;
 | 
				
			||||||
use App\Models\ApplicationPreview;
 | 
					use App\Models\ApplicationPreview;
 | 
				
			||||||
use App\Models\Server;
 | 
					use App\Models\Server;
 | 
				
			||||||
use Illuminate\Support\Collection;
 | 
					use Illuminate\Support\Collection;
 | 
				
			||||||
use Illuminate\Support\Str;
 | 
					use Illuminate\Support\Str;
 | 
				
			||||||
use Spatie\Url\Url;
 | 
					use Spatie\Url\Url;
 | 
				
			||||||
 | 
					use Visus\Cuid2\Cuid2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getCurrentApplicationContainerStatus(Server $server, int $id): Collection
 | 
					function getCurrentApplicationContainerStatus(Server $server, int $id): Collection
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -147,23 +147,21 @@ function defaultLabels($id, $name, $pull_request_id = 0, string $type = 'applica
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    return $labels;
 | 
					    return $labels;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
function fqdnLabelsForTraefik(Collection $domains, $container_name, $is_force_https_enabled)
 | 
					function fqdnLabelsForTraefik(Collection $domains, bool $is_force_https_enabled)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    $labels = collect([]);
 | 
					    $labels = collect([]);
 | 
				
			||||||
    $labels->push('traefik.enable=true');
 | 
					    $labels->push('traefik.enable=true');
 | 
				
			||||||
    foreach ($domains as $domain) {
 | 
					    foreach ($domains as $domain) {
 | 
				
			||||||
 | 
					        $uuid = (string)new Cuid2(7);
 | 
				
			||||||
        $url = Url::fromString($domain);
 | 
					        $url = Url::fromString($domain);
 | 
				
			||||||
        $host = $url->getHost();
 | 
					        $host = $url->getHost();
 | 
				
			||||||
        $path = $url->getPath();
 | 
					        $path = $url->getPath();
 | 
				
			||||||
        $schema = $url->getScheme();
 | 
					        $schema = $url->getScheme();
 | 
				
			||||||
        $port = $url->getPort();
 | 
					        $port = $url->getPort();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $http_label = "{$container_name}-http";
 | 
					        $http_label = "{$uuid}-http";
 | 
				
			||||||
        $https_label = "{$container_name}-https";
 | 
					        $https_label = "{$uuid}-https";
 | 
				
			||||||
        if ($port) {
 | 
					
 | 
				
			||||||
            $http_label = "{$http_label}-{$port}";
 | 
					 | 
				
			||||||
            $https_label = "{$https_label}-{$port}";
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if ($schema === 'https') {
 | 
					        if ($schema === 'https') {
 | 
				
			||||||
            // Set labels for https
 | 
					            // Set labels for https
 | 
				
			||||||
            $labels->push("traefik.http.routers.{$https_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)");
 | 
					            $labels->push("traefik.http.routers.{$https_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)");
 | 
				
			||||||
@@ -223,7 +221,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
 | 
				
			|||||||
            $domains = Str::of(data_get($application, 'fqdn'))->explode(',');
 | 
					            $domains = Str::of(data_get($application, 'fqdn'))->explode(',');
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // Add Traefik labels no matter which proxy is selected
 | 
					        // Add Traefik labels no matter which proxy is selected
 | 
				
			||||||
        $labels = $labels->merge(fqdnLabelsForTraefik($domains, $container_name, $application->settings->is_force_https_enabled));
 | 
					        $labels = $labels->merge(fqdnLabelsForTraefik($domains, $application->settings->is_force_https_enabled));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return $labels->all();
 | 
					    return $labels->all();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user