MORE PARSERS
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use App\Enums\ApplicationDeploymentStatus;
|
||||
use App\Enums\ProxyTypes;
|
||||
use App\Jobs\ServerFilesFromServerJob;
|
||||
use App\Livewire\Project\Shared\Destination;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
@@ -253,7 +253,7 @@ function generateServiceSpecificFqdns(ServiceApplication|Application $resource)
|
||||
|
||||
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([]);
|
||||
if ($serviceLabels) {
|
||||
@@ -272,6 +272,9 @@ function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains,
|
||||
if (is_null($port) && ! is_null($onlyPort)) {
|
||||
$port = $onlyPort;
|
||||
}
|
||||
if (is_null($port) && $predefinedPort) {
|
||||
$port = $predefinedPort;
|
||||
}
|
||||
$labels->push("caddy_{$loop}={$schema}://{$host}");
|
||||
$labels->push("caddy_{$loop}.header=-Server");
|
||||
$labels->push("caddy_{$loop}.try_files={path} /index.html /index.php");
|
||||
|
@@ -852,7 +852,7 @@ function parseEnvVariable(Str|string $value)
|
||||
'port' => $port,
|
||||
];
|
||||
}
|
||||
function generateEnvValue(string $command, ?Service $service = null)
|
||||
function generateEnvValue(string $command, Service|Application|null $service = null)
|
||||
{
|
||||
switch ($command) {
|
||||
case 'PASSWORD':
|
||||
@@ -927,7 +927,8 @@ function generateEnvValue(string $command, ?Service $service = null)
|
||||
$generatedValue = $token->toString();
|
||||
break;
|
||||
default:
|
||||
$generatedValue = Str::random(16);
|
||||
// $generatedValue = Str::random(16);
|
||||
$generatedValue = null;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1341,7 +1342,6 @@ function parseServiceVolumes($serviceVolumes, $resource, $topLevelVolumes, $pull
|
||||
$isDirectory = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if ($type?->value() === 'bind') {
|
||||
if ($source->value() === '/var/run/docker.sock') {
|
||||
@@ -2083,7 +2083,6 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
updateCompose($savedService);
|
||||
|
||||
return $service;
|
||||
|
||||
});
|
||||
|
||||
$envs_from_coolify = $resource->environment_variables()->get();
|
||||
@@ -2863,7 +2862,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
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;
|
||||
$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', []));
|
||||
$labels = collect(data_get($service, 'labels', []));
|
||||
$environment = collect(data_get($service, 'environment', []));
|
||||
$ports = collect(data_get($service, 'ports', []));
|
||||
$buildArgs = collect(data_get($service, 'build.args', []));
|
||||
$environment = $environment->merge($buildArgs);
|
||||
$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
|
||||
);
|
||||
$containerName = "$serviceName-$baseName";
|
||||
$predefinedPort = data_get($resource, 'ports_exposes');
|
||||
} elseif ($isService) {
|
||||
$containerName = "$serviceName-{$resource->uuid}";
|
||||
|
||||
@@ -2985,6 +2986,9 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
|
||||
$savedService->save();
|
||||
}
|
||||
}
|
||||
|
||||
$originalResource = $isApplication ? $resource : $savedService;
|
||||
|
||||
if ($volumes->count() > 0) {
|
||||
foreach ($volumes as $index => $volume) {
|
||||
$type = null;
|
||||
@@ -3042,19 +3046,20 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
|
||||
$mainDirectory = str(base_configuration_dir() . '/applications/' . $uuid);
|
||||
$source = replaceLocalSource($source, $mainDirectory);
|
||||
|
||||
|
||||
LocalFileVolume::updateOrCreate(
|
||||
[
|
||||
'mount_path' => $target,
|
||||
'resource_id' => $savedService->id,
|
||||
'resource_type' => get_class($savedService),
|
||||
'resource_id' => $originalResource->id,
|
||||
'resource_type' => get_class($originalResource),
|
||||
],
|
||||
[
|
||||
'fs_path' => $source,
|
||||
'mount_path' => $target,
|
||||
'content' => $content,
|
||||
'is_directory' => $isDirectory,
|
||||
'resource_id' => $savedService->id,
|
||||
'resource_type' => get_class($savedService),
|
||||
'resource_id' => $originalResource->id,
|
||||
'resource_type' => get_class($originalResource),
|
||||
]
|
||||
);
|
||||
$volume = "$source:$target";
|
||||
@@ -3085,18 +3090,18 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
|
||||
LocalPersistentVolume::updateOrCreate(
|
||||
[
|
||||
'mount_path' => $target,
|
||||
'resource_id' => $savedService->id,
|
||||
'resource_type' => get_class($savedService),
|
||||
'resource_id' => $originalResource->id,
|
||||
'resource_type' => get_class($originalResource),
|
||||
],
|
||||
[
|
||||
'name' => $name,
|
||||
'mount_path' => $target,
|
||||
'resource_id' => $savedService->id,
|
||||
'resource_type' => get_class($savedService),
|
||||
'resource_id' => $originalResource->id,
|
||||
'resource_type' => get_class($originalResource),
|
||||
]
|
||||
);
|
||||
}
|
||||
dispatch(new ServerFilesFromServerJob($savedService));
|
||||
dispatch(new ServerFilesFromServerJob($originalResource));
|
||||
$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();
|
||||
|
||||
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([]);
|
||||
foreach ($environment as $variableName => $variable) {
|
||||
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_');
|
||||
});
|
||||
ray($magicEnvironments);
|
||||
// TODO: go through all the magic environments and handle all kinds of cases, FQDN, URL, PASSWORD, USER, etc.
|
||||
$normalEnvironments = $environment->diffKeys($magicEnvironments);
|
||||
if ($magicEnvironments->count() > 0) {
|
||||
foreach ($magicEnvironments as $key => $value) {
|
||||
$key = str($key);
|
||||
$value = str($value);
|
||||
$command = $key->after('SERVICE_')->before('_');
|
||||
if ($command->value() === 'FQDN') {
|
||||
$keyCommand = $key->after('SERVICE_')->before('_');
|
||||
$valueCommand = $value->after('SERVICE_')->before('_');
|
||||
if ($keyCommand->value() === 'FQDN') {
|
||||
if ($isApplication) {
|
||||
$fqdn = generateFqdn($server, "{$resource->name}-{$uuid}");
|
||||
|
||||
} elseif ($isService) {
|
||||
$fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}");
|
||||
}
|
||||
@@ -3204,96 +3230,61 @@ function newParser(Application|Service $resource, int $pull_request_id = 0)
|
||||
} else {
|
||||
$value = $fqdn;
|
||||
}
|
||||
}
|
||||
$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 ($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 {
|
||||
$environment->forget($key);
|
||||
if (!$isDatabase) {
|
||||
if ($isApplication) {
|
||||
$fqdn = generateFqdn($server, "{$resource->name}-{$uuid}");
|
||||
|
||||
$resource->fqdn = $value;
|
||||
$resource->save();
|
||||
} elseif ($isService) {
|
||||
$fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}");
|
||||
}
|
||||
if ($value && get_class($value) === 'Illuminate\Support\Stringable' && $value->startsWith('/')) {
|
||||
$path = $value->value();
|
||||
$value = "$fqdn$path";
|
||||
} else {
|
||||
$value = $fqdn;
|
||||
$savedService->fqdn = $value;
|
||||
$savedService->save();
|
||||
}
|
||||
}
|
||||
} elseif ($command->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 {
|
||||
} elseif ($keyCommand->value() === 'URL') {
|
||||
if ($isApplication) {
|
||||
$fqdn = generateFqdn($server, "{$resource->name}-{$uuid}");
|
||||
} elseif ($isService) {
|
||||
$fqdn = generateFqdn($server, "{$savedService->name}-{$uuid}");
|
||||
}
|
||||
$value = str($fqdn)->replace('http://', '')->replace('https://', '')->replace('www.', '');
|
||||
}
|
||||
// remove it from environment
|
||||
$environment->forget($key);
|
||||
} 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([
|
||||
'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([
|
||||
'restart' => $restart->value(),
|
||||
'container_name' => $containerName,
|
||||
'volumes' => $volumesParsed,
|
||||
'networks' => $networks_temp,
|
||||
'labels' => $labels,
|
||||
'labels' => $serviceLabels,
|
||||
'environment' => $environment,
|
||||
]);
|
||||
|
||||
|
@@ -70,6 +70,11 @@ beforeEach(function () {
|
||||
|
||||
$this->application = Application::create([
|
||||
'name' => 'Application for tests',
|
||||
'docker_compose_domains' => json_encode([
|
||||
'app' => [
|
||||
'domain' => 'http://bcoowoookw0co4cok4sgc4k8.127.0.0.1.sslip.io',
|
||||
]
|
||||
]),
|
||||
'uuid' => 'bcoowoookw0co4cok4sgc4k8',
|
||||
'repository_project_id' => 603035348,
|
||||
'git_repository' => 'coollabsio/coolify-examples',
|
||||
@@ -86,6 +91,7 @@ beforeEach(function () {
|
||||
'source_type' => GithubApp::class,
|
||||
]);
|
||||
|
||||
|
||||
$this->serviceComposeFile = [
|
||||
'services' => [
|
||||
'activepieces' => [
|
||||
@@ -356,16 +362,9 @@ afterEach(function () {
|
||||
|
||||
test('ServiceComposeParseNew', function () {
|
||||
ray()->clearAll();
|
||||
$output = $this->service->newParser();
|
||||
// ray('New parser');
|
||||
// ray(data_get($output, 'services.activepieces.environment')->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());
|
||||
// }
|
||||
$output = newParser($this->application);
|
||||
ray('New parser');
|
||||
ray($output->toArray());
|
||||
expect($output)->toBeInstanceOf(Collection::class);
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user