MORE PARSERS

This commit is contained in:
Andras Bacsai
2024-08-27 21:48:25 +02:00
parent d8d821e7a9
commit 954d82207d
4 changed files with 280 additions and 167 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Models;
use App\Enums\ApplicationDeploymentStatus; use App\Enums\ApplicationDeploymentStatus;
use App\Enums\ProxyTypes; use App\Enums\ProxyTypes;
use App\Jobs\ServerFilesFromServerJob; use App\Jobs\ServerFilesFromServerJob;
use App\Livewire\Project\Shared\Destination;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;

View File

@@ -253,7 +253,7 @@ function generateServiceSpecificFqdns(ServiceApplication|Application $resource)
return $payload; return $payload;
} }
function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains, bool $is_force_https_enabled = false, $onlyPort = null, ?Collection $serviceLabels = null, ?bool $is_gzip_enabled = true, ?bool $is_stripprefix_enabled = true, ?string $service_name = null, ?string $image = null, string $redirect_direction = 'both') function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains, bool $is_force_https_enabled = false, $onlyPort = null, ?Collection $serviceLabels = null, ?bool $is_gzip_enabled = true, ?bool $is_stripprefix_enabled = true, ?string $service_name = null, ?string $image = null, string $redirect_direction = 'both', ?string $predefinedPort = null)
{ {
$labels = collect([]); $labels = collect([]);
if ($serviceLabels) { if ($serviceLabels) {
@@ -272,6 +272,9 @@ function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains,
if (is_null($port) && ! is_null($onlyPort)) { if (is_null($port) && ! is_null($onlyPort)) {
$port = $onlyPort; $port = $onlyPort;
} }
if (is_null($port) && $predefinedPort) {
$port = $predefinedPort;
}
$labels->push("caddy_{$loop}={$schema}://{$host}"); $labels->push("caddy_{$loop}={$schema}://{$host}");
$labels->push("caddy_{$loop}.header=-Server"); $labels->push("caddy_{$loop}.header=-Server");
$labels->push("caddy_{$loop}.try_files={path} /index.html /index.php"); $labels->push("caddy_{$loop}.try_files={path} /index.html /index.php");

View File

@@ -852,7 +852,7 @@ function parseEnvVariable(Str|string $value)
'port' => $port, 'port' => $port,
]; ];
} }
function generateEnvValue(string $command, ?Service $service = null) function generateEnvValue(string $command, Service|Application|null $service = null)
{ {
switch ($command) { switch ($command) {
case 'PASSWORD': case 'PASSWORD':
@@ -927,7 +927,8 @@ function generateEnvValue(string $command, ?Service $service = null)
$generatedValue = $token->toString(); $generatedValue = $token->toString();
break; break;
default: default:
$generatedValue = Str::random(16); // $generatedValue = Str::random(16);
$generatedValue = null;
break; break;
} }
@@ -1341,7 +1342,6 @@ function parseServiceVolumes($serviceVolumes, $resource, $topLevelVolumes, $pull
$isDirectory = true; $isDirectory = true;
} }
} }
} }
if ($type?->value() === 'bind') { if ($type?->value() === 'bind') {
if ($source->value() === '/var/run/docker.sock') { if ($source->value() === '/var/run/docker.sock') {
@@ -2083,7 +2083,6 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
updateCompose($savedService); updateCompose($savedService);
return $service; return $service;
}); });
$envs_from_coolify = $resource->environment_variables()->get(); $envs_from_coolify = $resource->environment_variables()->get();
@@ -2863,7 +2862,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
return collect($finalServices); return collect($finalServices);
} }
} }
function newParser(Application|Service $resource, int $pull_request_id = 0) function newParser(Application|Service $resource, int $pull_request_id = 0, ?int $preview_id = null)
{ {
$isApplication = $resource instanceof Application; $isApplication = $resource instanceof Application;
$isService = $resource instanceof Service; $isService = $resource instanceof Service;
@@ -2936,6 +2935,7 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
$depends_on = collect(data_get($service, 'depends_on', [])); $depends_on = collect(data_get($service, 'depends_on', []));
$labels = collect(data_get($service, 'labels', [])); $labels = collect(data_get($service, 'labels', []));
$environment = collect(data_get($service, 'environment', [])); $environment = collect(data_get($service, 'environment', []));
$ports = collect(data_get($service, 'ports', []));
$buildArgs = collect(data_get($service, 'build.args', [])); $buildArgs = collect(data_get($service, 'build.args', []));
$environment = $environment->merge($buildArgs); $environment = $environment->merge($buildArgs);
$isDatabase = isDatabaseImage(data_get_str($service, 'image')); $isDatabase = isDatabaseImage(data_get_str($service, 'image'));
@@ -2947,6 +2947,7 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
pull_request_id: $pullRequestId pull_request_id: $pullRequestId
); );
$containerName = "$serviceName-$baseName"; $containerName = "$serviceName-$baseName";
$predefinedPort = data_get($resource, 'ports_exposes');
} elseif ($isService) { } elseif ($isService) {
$containerName = "$serviceName-{$resource->uuid}"; $containerName = "$serviceName-{$resource->uuid}";
@@ -2985,6 +2986,9 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
$savedService->save(); $savedService->save();
} }
} }
$originalResource = $isApplication ? $resource : $savedService;
if ($volumes->count() > 0) { if ($volumes->count() > 0) {
foreach ($volumes as $index => $volume) { foreach ($volumes as $index => $volume) {
$type = null; $type = null;
@@ -3042,19 +3046,20 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
$mainDirectory = str(base_configuration_dir() . '/applications/' . $uuid); $mainDirectory = str(base_configuration_dir() . '/applications/' . $uuid);
$source = replaceLocalSource($source, $mainDirectory); $source = replaceLocalSource($source, $mainDirectory);
LocalFileVolume::updateOrCreate( LocalFileVolume::updateOrCreate(
[ [
'mount_path' => $target, 'mount_path' => $target,
'resource_id' => $savedService->id, 'resource_id' => $originalResource->id,
'resource_type' => get_class($savedService), 'resource_type' => get_class($originalResource),
], ],
[ [
'fs_path' => $source, 'fs_path' => $source,
'mount_path' => $target, 'mount_path' => $target,
'content' => $content, 'content' => $content,
'is_directory' => $isDirectory, 'is_directory' => $isDirectory,
'resource_id' => $savedService->id, 'resource_id' => $originalResource->id,
'resource_type' => get_class($savedService), 'resource_type' => get_class($originalResource),
] ]
); );
$volume = "$source:$target"; $volume = "$source:$target";
@@ -3085,18 +3090,18 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
LocalPersistentVolume::updateOrCreate( LocalPersistentVolume::updateOrCreate(
[ [
'mount_path' => $target, 'mount_path' => $target,
'resource_id' => $savedService->id, 'resource_id' => $originalResource->id,
'resource_type' => get_class($savedService), 'resource_type' => get_class($originalResource),
], ],
[ [
'name' => $name, 'name' => $name,
'mount_path' => $target, 'mount_path' => $target,
'resource_id' => $savedService->id, 'resource_id' => $originalResource->id,
'resource_type' => get_class($savedService), 'resource_type' => get_class($originalResource),
] ]
); );
} }
dispatch(new ServerFilesFromServerJob($savedService)); dispatch(new ServerFilesFromServerJob($originalResource));
$volumesParsed->put($index, $volume); $volumesParsed->put($index, $volume);
} }
} }
@@ -3128,6 +3133,28 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
]); ]);
} }
} }
// Collect/create/update ports
$collectedPorts = collect([]);
if ($ports->count() > 0) {
foreach ($ports as $sport) {
if (is_string($sport) || is_numeric($sport)) {
$collectedPorts->push($sport);
}
if (is_array($sport)) {
$target = data_get($sport, 'target');
$published = data_get($sport, 'published');
$protocol = data_get($sport, 'protocol');
$collectedPorts->push("$target:$published/$protocol");
}
}
}
if ($isService) {
$originalResource->ports = $collectedPorts->implode(',');
$originalResource->save();
}
$networks_temp = collect(); $networks_temp = collect();
foreach ($networks as $key => $network) { foreach ($networks as $key => $network) {
@@ -3156,7 +3183,7 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
]); ]);
} }
} }
// convert environment variables a different format // convert environment variables to one format
$convertedServiceVariables = collect([]); $convertedServiceVariables = collect([]);
foreach ($environment as $variableName => $variable) { foreach ($environment as $variableName => $variable) {
if (is_numeric($variableName)) { if (is_numeric($variableName)) {
@@ -3184,17 +3211,16 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
return str($key)->startsWith('SERVICE_') || str($value)->startsWith('SERVICE_'); return str($key)->startsWith('SERVICE_') || str($value)->startsWith('SERVICE_');
}); });
ray($magicEnvironments); $normalEnvironments = $environment->diffKeys($magicEnvironments);
// TODO: go through all the magic environments and handle all kinds of cases, FQDN, URL, PASSWORD, USER, etc.
if ($magicEnvironments->count() > 0) { if ($magicEnvironments->count() > 0) {
foreach ($magicEnvironments as $key => $value) { foreach ($magicEnvironments as $key => $value) {
$key = str($key); $key = str($key);
$value = str($value); $value = str($value);
$command = $key->after('SERVICE_')->before('_'); $keyCommand = $key->after('SERVICE_')->before('_');
if ($command->value() === 'FQDN') { $valueCommand = $value->after('SERVICE_')->before('_');
if ($keyCommand->value() === 'FQDN') {
if ($isApplication) { if ($isApplication) {
$fqdn = generateFqdn($server, "{$resource->name}-{$uuid}"); $fqdn = generateFqdn($server, "{$resource->name}-{$uuid}");
} elseif ($isService) { } elseif ($isService) {
$fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}"); $fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}");
} }
@@ -3204,96 +3230,61 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
} else { } else {
$value = $fqdn; $value = $fqdn;
} }
} $environment->forget($key);
$resource->environment_variables()->where('key', $key)->where($nameOfId, $resource->id)->firstOrCreate([ if (!$isDatabase) {
'key' => $key,
$nameOfId => $resource->id,
], [
'value' => $value,
'is_build_time' => false,
'is_preview' => false,
]);
}
}
foreach ($environment as $key => $value) {
if (is_numeric($key)) {
if (is_array($value)) {
// - SESSION_SECRET: 123
// - SESSION_SECRET:
$key = str(collect($value)->keys()->first());
$value = str(collect($value)->values()->first());
} else {
$variable = str($value);
if ($variable->contains('=')) {
// - SESSION_SECRET=123
// - SESSION_SECRET=
$key = $variable->before('=');
$value = $variable->after('=');
} else {
// - SESSION_SECRET
$key = $variable;
$value = null;
}
}
} else {
// SESSION_SECRET: 123
// SESSION_SECRET:
$key = str($key);
$value = str($value);
}
// Auto generate FQDN and URL
// environment:
// - SERVICE_FQDN_UMAMI=/umami
// - FQDN=$SERVICE_FQDN_UMAMI
// - URL=$SERVICE_URL_UMAMI
// - TEST=${TEST:-initial}
// - HARDCODED=stuff
if ($value->startsWith('$')) {
$value = str(replaceVariables($value));
if ($value->startsWith('SERVICE_')) {
$command = $value->after('SERVICE_')->before('_');
if ($command->value() === 'FQDN') {
if ($magicEnvironments->has($value->value())) {
$found = $magicEnvironments->get($value->value());
if ($found) {
$found = $resource->environment_variables()->where('key', $value->value())->where($nameOfId, $resource->id)->first();
if ($found) {
$value = $found->value;
}
}
} else {
if ($isApplication) { if ($isApplication) {
$fqdn = generateFqdn($server, "{$resource->name}-{$uuid}"); $resource->fqdn = $value;
$resource->save();
} elseif ($isService) { } elseif ($isService) {
$fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}"); $savedService->fqdn = $value;
} $savedService->save();
if ($value && get_class($value) === 'Illuminate\Support\Stringable' && $value->startsWith('/')) {
$path = $value->value();
$value = "$fqdn$path";
} else {
$value = $fqdn;
} }
} }
} elseif ($command->value() === 'URL') { } elseif ($keyCommand->value() === 'URL') {
if ($magicEnvironments->has($value->value())) {
$found = $magicEnvironments->get($value->value());
if ($found) {
$found = $resource->environment_variables()->where('key', $value->value())->where($nameOfId, $resource->id)->first();
if ($found) {
$value = $found->value;
}
}
} else {
if ($isApplication) { if ($isApplication) {
$fqdn = generateFqdn($server, "{$resource->name}-{$uuid}"); $fqdn = generateFqdn($server, "{$resource->name}-{$uuid}");
} elseif ($isService) { } elseif ($isService) {
$fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}"); $fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}");
} }
$value = str($fqdn)->replace('http://', '')->replace('https://', '')->replace('www.', ''); $value = str($fqdn)->replace('http://', '')->replace('https://', '')->replace('www.', '');
} // remove it from environment
$environment->forget($key);
} else { } else {
$value = generateEnvValue($command, $resource); $generatedValue = generateEnvValue($valueCommand, $resource);
if ($generatedValue) {
$value = $generatedValue;
}
}
$resource->environment_variables()->where('key', $key)->where($nameOfId, $resource->id)->firstOrCreate([
'key' => $key,
$nameOfId => $resource->id,
], [
'value' => $value,
'is_build_time' => false,
'is_preview' => false,
]);
}
foreach ($normalEnvironments as $key => $value) {
$key = str($key);
$value = str($value);
if ($value->startsWith('$')) {
$value = str(replaceVariables(str($value)));
if ($value->contains(':-')) {
$key = $value->before(':');
$value = $value->after(':-');
} elseif ($value->contains('-')) {
$key = $value->before('-');
$value = $value->after('-');
} elseif ($value->contains(':?')) {
$key = $value->before(':');
$value = $value->after(':?');
} elseif ($value->contains('?')) {
$key = $value->before('?');
$value = $value->after('?');
} else {
$key = $value;
$value = null;
} }
$resource->environment_variables()->where('key', $key)->where($nameOfId, $resource->id)->firstOrCreate([ $resource->environment_variables()->where('key', $key)->where($nameOfId, $resource->id)->firstOrCreate([
'key' => $key, 'key' => $key,
@@ -3305,13 +3296,132 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
]); ]);
} }
} }
// Add COOLIFY_CONTAINER_NAME to environment
if ($resource->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {
$environment->put('COOLIFY_CONTAINER_NAME', $containerName);
} }
if ($isApplication) {
$domains = collect(json_decode($resource->docker_compose_domains)) ?? collect([]);
$fqdns = data_get($domains, "$serviceName.domain");
if ($fqdns) {
$fqdns = str($fqdns)->explode(',');
if ($isPullRequest) {
$preview = $resource->previews()->find($preview_id);
$docker_compose_domains = collect(json_decode(data_get($preview, 'docker_compose_domains')));
if ($docker_compose_domains->count() > 0) {
$found_fqdn = data_get($docker_compose_domains, "$serviceName.domain");
if ($found_fqdn) {
$fqdns = collect($found_fqdn);
} else {
$fqdns = collect([]);
}
} else {
$fqdns = $fqdns->map(function ($fqdn) use ($pullRequestId) {
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->id, $pullRequestId);
$url = Url::fromString($fqdn);
$template = $this->preview_url_template;
$host = $url->getHost();
$schema = $url->getScheme();
$random = new Cuid2;
$preview_fqdn = str_replace('{{random}}', $random, $template);
$preview_fqdn = str_replace('{{domain}}', $host, $preview_fqdn);
$preview_fqdn = str_replace('{{pr_id}}', $pullRequestId, $preview_fqdn);
$preview_fqdn = "$schema://$preview_fqdn";
$preview->fqdn = $preview_fqdn;
$preview->save();
return $preview_fqdn;
});
}
}
}
$defaultLabels = defaultLabels(
id: $resource->id,
name: $containerName,
pull_request_id: $pullRequestId,
type: 'application'
);
} elseif ($isService) {
if ($savedService->serviceType()) {
$fqdns = generateServiceSpecificFqdns($savedService);
} else {
$fqdns = collect(data_get($savedService, 'fqdns'))->filter();
}
$defaultLabels = defaultLabels($resource->id, $containerName, type: 'service', subType: $isDatabase ? 'database' : 'application', subId: $savedService->id);
}
$serviceLabels = $labels->merge($defaultLabels);
if (!$isDatabase && $fqdns->count() > 0) {
if ($isApplication) {
$shouldGenerateLabelsExactly = $resource->destination->server->settings->generate_exact_labels;
} else {
$shouldGenerateLabelsExactly = $resource->server->settings->generate_exact_labels;
}
if ($shouldGenerateLabelsExactly) {
switch ($server->proxyType()) {
case ProxyTypes::TRAEFIK->value:
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
uuid: $resource->uuid,
domains: $fqdns,
is_force_https_enabled: true,
serviceLabels: $serviceLabels,
is_gzip_enabled: $originalResource->isGzipEnabled(),
is_stripprefix_enabled: $originalResource->isStripprefixEnabled(),
service_name: $serviceName,
image: $image
));
break;
case ProxyTypes::CADDY->value:
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
network: $resource->destination->network,
uuid: $resource->uuid,
domains: $fqdns,
is_force_https_enabled: true,
serviceLabels: $serviceLabels,
is_gzip_enabled: $originalResource->isGzipEnabled(),
is_stripprefix_enabled: $originalResource->isStripprefixEnabled(),
service_name: $serviceName,
image: $image,
predefinedPort: $predefinedPort
));
break;
}
} else {
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
uuid: $resource->uuid,
domains: $fqdns,
is_force_https_enabled: true,
serviceLabels: $serviceLabels,
is_gzip_enabled: $originalResource->isGzipEnabled(),
is_stripprefix_enabled: $originalResource->isStripprefixEnabled(),
service_name: $serviceName,
image: $image
));
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
network: $resource->destination->network,
uuid: $resource->uuid,
domains: $fqdns,
is_force_https_enabled: true,
serviceLabels: $serviceLabels,
is_gzip_enabled: $originalResource->isGzipEnabled(),
is_stripprefix_enabled: $originalResource->isStripprefixEnabled(),
service_name: $serviceName,
image: $image,
predefinedPort: $predefinedPort
));
}
}
}
$payload = collect($service)->merge([ $payload = collect($service)->merge([
'restart' => $restart->value(), 'restart' => $restart->value(),
'container_name' => $containerName, 'container_name' => $containerName,
'volumes' => $volumesParsed, 'volumes' => $volumesParsed,
'networks' => $networks_temp, 'networks' => $networks_temp,
'labels' => $labels, 'labels' => $serviceLabels,
'environment' => $environment, 'environment' => $environment,
]); ]);

View File

@@ -70,6 +70,11 @@ beforeEach(function () {
$this->application = Application::create([ $this->application = Application::create([
'name' => 'Application for tests', 'name' => 'Application for tests',
'docker_compose_domains' => json_encode([
'app' => [
'domain' => 'http://bcoowoookw0co4cok4sgc4k8.127.0.0.1.sslip.io',
]
]),
'uuid' => 'bcoowoookw0co4cok4sgc4k8', 'uuid' => 'bcoowoookw0co4cok4sgc4k8',
'repository_project_id' => 603035348, 'repository_project_id' => 603035348,
'git_repository' => 'coollabsio/coolify-examples', 'git_repository' => 'coollabsio/coolify-examples',
@@ -86,6 +91,7 @@ beforeEach(function () {
'source_type' => GithubApp::class, 'source_type' => GithubApp::class,
]); ]);
$this->serviceComposeFile = [ $this->serviceComposeFile = [
'services' => [ 'services' => [
'activepieces' => [ 'activepieces' => [
@@ -356,16 +362,9 @@ afterEach(function () {
test('ServiceComposeParseNew', function () { test('ServiceComposeParseNew', function () {
ray()->clearAll(); ray()->clearAll();
$output = $this->service->newParser(); $output = newParser($this->application);
// ray('New parser'); ray('New parser');
// ray(data_get($output, 'services.activepieces.environment')->toArray()); ray($output->toArray());
ray($this->service->environment_variables->pluck('value', 'key')->toArray());
// foreach ($this->service->applications as $application) {
// ray($application->persistentStorages->pluck('mount_path', 'name')->toArray());
// }
// foreach ($this->service->databases as $database) {
// ray($database->persistentStorages->pluck('mount_path', 'name')->toArray());
// }
expect($output)->toBeInstanceOf(Collection::class); expect($output)->toBeInstanceOf(Collection::class);
}); });